Skip to content

Find, verify, and analyze leaked credentials

[chore] - Remove monolithic config struct (#1091)

ahravc5c8d10d28d1
diff --git a/main.go b/main.go
index d23548230..93e59743c 100644
--- a/main.go
+++ b/main.go
@@ -235,15 +235,14 @@ func run(state overseer.State) {
defer os.RemoveAll(repoPath)
}
- g := func(c *sources.Config) {
- c.RepoPath = repoPath
- c.HeadRef = *gitScanBranch
- c.BaseRef = *gitScanSinceCommit
- c.MaxDepth = *gitScanMaxDepth
- c.Filter = filter
+ cfg := sources.GitConfig{
+ RepoPath: repoPath,
+ HeadRef: *gitScanBranch,
+ BaseRef: *gitScanSinceCommit,
+ MaxDepth: *gitScanMaxDepth,
+ Filter: filter,
}
-
- if err = e.ScanGit(ctx, sources.NewConfig(g)); err != nil {
+ if err = e.ScanGit(ctx, cfg); err != nil {
logrus.WithError(err).Fatal("Failed to scan Git.")
}
case githubScan.FullCommand():
@@ -251,19 +250,18 @@ func run(state overseer.State) {
logrus.Fatal("You must specify at least one organization or repository.")
}
- github := func(c *sources.Config) {
- c.Endpoint = *githubScanEndpoint
- c.Repos = *githubScanRepos
- c.Orgs = *githubScanOrgs
- c.Token = *githubScanToken
- c.IncludeForks = *githubIncludeForks
- c.IncludeMembers = *githubIncludeMembers
- c.Concurrency = *concurrency
- c.ExcludeRepos = *githubExcludeRepos
- c.IncludeRepos = *githubIncludeRepos
+ cfg := sources.GithubConfig{
+ Endpoint: *githubScanEndpoint,
+ Token: *githubScanToken,
+ IncludeForks: *githubIncludeForks,
+ IncludeMembers: *githubIncludeMembers,
+ Concurrency: *concurrency,
+ ExcludeRepos: *githubExcludeRepos,
+ IncludeRepos: *githubIncludeRepos,
+ Repos: *githubScanRepos,
+ Orgs: *githubScanOrgs,
}
-
- if err := e.ScanGitHub(ctx, sources.NewConfig(github)); err != nil {
+ if err := e.ScanGitHub(ctx, cfg); err != nil {
logrus.WithError(err).Fatal("Failed to scan Github.")
}
case gitlabScan.FullCommand():
@@ -272,14 +270,13 @@ func run(state overseer.State) {
logrus.WithError(err).Fatal("could not create filter")
}
- gitlab := func(c *sources.Config) {
- c.Endpoint = *gitlabScanEndpoint
- c.Token = *gitlabScanToken
- c.Repos = *gitlabScanRepos
- c.Filter = filter
+ cfg := sources.GitlabConfig{
+ Endpoint: *gitlabScanEndpoint,
+ Token: *gitlabScanToken,
+ Repos: *gitlabScanRepos,
+ Filter: filter,
}
-
- if err := e.ScanGitLab(ctx, sources.NewConfig(gitlab)); err != nil {
+ if err := e.ScanGitLab(ctx, cfg); err != nil {
logrus.WithError(err).Fatal("Failed to scan GitLab.")
}
case filesystemScan.FullCommand():
@@ -288,35 +285,33 @@ func run(state overseer.State) {
logrus.WithError(err).Fatal("could not create filter")
}
- fs := func(c *sources.Config) {
- c.Directories = *filesystemDirectories
- c.Filter = filter
+ cfg := sources.FilesystemConfig{
+ Directories: *filesystemDirectories,
+ Filter: filter,
}
-
- if err = e.ScanFileSystem(ctx, sources.NewConfig(fs)); err != nil {
+ if err = e.ScanFileSystem(ctx, cfg); err != nil {
logrus.WithError(err).Fatal("Failed to scan filesystem")
}
case s3Scan.FullCommand():
- s3 := func(c *sources.Config) {
- c.Key = *s3ScanKey
- c.Secret = *s3ScanSecret
- c.Buckets = *s3ScanBuckets
+ cfg := sources.S3Config{
+ Key: *s3ScanKey,
+ Secret: *s3ScanSecret,
+ Buckets: *s3ScanBuckets,
+ CloudCred: *s3ScanCloudEnv,
}
-
- if err := e.ScanS3(ctx, sources.NewConfig(s3)); err != nil {
+ if err := e.ScanS3(ctx, cfg); err != nil {
logrus.WithError(err).Fatal("Failed to scan S3.")
}
case syslogScan.FullCommand():
- syslog := func(c *sources.Config) {
- c.Address = *syslogAddress
- c.Protocol = *syslogProtocol
- c.CertPath = *syslogTLSCert
- c.KeyPath = *syslogTLSKey
- c.Format = *syslogFormat
- c.Concurrency = *concurrency
+ cfg := sources.SyslogConfig{
+ Address: *syslogAddress,
+ Format: *syslogFormat,
+ Protocol: *syslogProtocol,
+ CertPath: *syslogTLSCert,
+ KeyPath: *syslogTLSKey,
+ Concurrency: *concurrency,
}
-
- if err := e.ScanSyslog(ctx, sources.NewConfig(syslog)); err != nil {
+ if err := e.ScanSyslog(ctx, cfg); err != nil {
logrus.WithError(err).Fatal("Failed to scan syslog.")
}
case circleCiScan.FullCommand():
diff --git a/pkg/engine/filesystem.go b/pkg/engine/filesystem.go
index 61527bf8b..ed52d214f 100644
--- a/pkg/engine/filesystem.go
+++ b/pkg/engine/filesystem.go
@@ -15,7 +15,7 @@ import (
)
// ScanFileSystem scans a given file system.
-func (e *Engine) ScanFileSystem(ctx context.Context, c sources.Config) error {
+func (e *Engine) ScanFileSystem(ctx context.Context, c sources.FilesystemConfig) error {
connection := &sourcespb.Filesystem{
Directories: c.Directories,
}
diff --git a/pkg/engine/git.go b/pkg/engine/git.go
index edc3b02d9..ac4db8527 100644
--- a/pkg/engine/git.go
+++ b/pkg/engine/git.go
@@ -15,7 +15,7 @@ import (
)
// ScanGit scans any git source.
-func (e *Engine) ScanGit(ctx context.Context, c sources.Config) error {
+func (e *Engine) ScanGit(ctx context.Context, c sources.GitConfig) error {
logOptions := &gogit.LogOptions{}
opts := []git.ScanOption{
git.ScanOptionFilter(c.Filter),
diff --git a/pkg/engine/git_test.go b/pkg/engine/git_test.go
index 3acabf4ca..9b099429d 100644
--- a/pkg/engine/git_test.go
+++ b/pkg/engine/git_test.go
@@ -58,7 +58,7 @@ func TestGitEngine(t *testing.T) {
WithDecoders(decoders.DefaultDecoders()...),
WithDetectors(false, DefaultDetectors()...),
)
- cfg := sources.Config{
+ cfg := sources.GitConfig{
RepoPath: path,
HeadRef: tTest.branch,
BaseRef: tTest.base,
@@ -116,7 +116,7 @@ func BenchmarkGitEngine(b *testing.B) {
for i := 0; i < b.N; i++ {
// TODO: this is measuring the time it takes to initialize the source
// and not to do the full scan
- cfg := sources.Config{
+ cfg := sources.GitConfig{
RepoPath: path,
Filter: common.FilterEmpty(),
}
diff --git a/pkg/engine/github.go b/pkg/engine/github.go
index 927415df6..a53b0ca30 100644
--- a/pkg/engine/github.go
+++ b/pkg/engine/github.go
@@ -12,7 +12,7 @@ import (
)
// ScanGitHub scans Github with the provided options.
-func (e *Engine) ScanGitHub(ctx context.Context, c sources.Config) error {
+func (e *Engine) ScanGitHub(ctx context.Context, c sources.GithubConfig) error {
source := github.Source{}
connection := sourcespb.GitHub{
diff --git a/pkg/engine/gitlab.go b/pkg/engine/gitlab.go
index c0257c8a5..f7fc95994 100644
--- a/pkg/engine/gitlab.go
+++ b/pkg/engine/gitlab.go
@@ -18,7 +18,7 @@ import (
)
// ScanGitLab scans GitLab with the provided configuration.
-func (e *Engine) ScanGitLab(ctx context.Context, c sources.Config) error {
+func (e *Engine) ScanGitLab(ctx context.Context, c sources.GitlabConfig) error {
logOptions := &gogit.LogOptions{}
opts := []git.ScanOption{
git.ScanOptionFilter(c.Filter),
diff --git a/pkg/engine/s3.go b/pkg/engine/s3.go
index 5ba708769..af6643770 100644
--- a/pkg/engine/s3.go
+++ b/pkg/engine/s3.go
@@ -17,7 +17,7 @@ import (
)
// ScanS3 scans S3 buckets.
-func (e *Engine) ScanS3(ctx context.Context, c sources.Config) error {
+func (e *Engine) ScanS3(ctx context.Context, c sources.S3Config) error {
connection := &sourcespb.S3{
Credential: &sourcespb.S3_Unauthenticated{},
}
diff --git a/pkg/engine/syslog.go b/pkg/engine/syslog.go
index d12ba4705..d09ea7731 100644
--- a/pkg/engine/syslog.go
+++ b/pkg/engine/syslog.go
@@ -15,7 +15,7 @@ import (
)
// ScanSyslog is a source that scans syslog files.
-func (e *Engine) ScanSyslog(ctx context.Context, c sources.Config) error {
+func (e *Engine) ScanSyslog(ctx context.Context, c sources.SyslogConfig) error {
connection := &sourcespb.Syslog{
Protocol: c.Protocol,
ListenAddress: c.Address,
diff --git a/pkg/sources/sources.go b/pkg/sources/sources.go
index e3be1f2d5..c51672a89 100644
--- a/pkg/sources/sources.go
+++ b/pkg/sources/sources.go
@@ -44,68 +44,89 @@ type Source interface {
GetProgress() *Progress
}
-// Config defines the optional configuration for a source.
-type Config struct {
- // Endpoint is the endpoint of the source.
- Endpoint,
- // Repo is the repository to scan.
- Repo,
- // Token is the token to use to authenticate with the source.
- Token,
- // Key is any key to use to authenticate with the source. (ex: S3)
- Key,
- // Secret is any secret to use to authenticate with the source. (ex: S3)
- Secret,
- // Address used to connect to the source. (ex: syslog)
- Address,
- // Protocol used to connect to the source.
- Protocol,
- // CertPath is the path to the certificate to use to connect to the source.
- CertPath,
- // KeyPath is the path to the key to use to connect to the source.
- KeyPath,
- // Format is the format used to connect to the source.
- Format,
+// GitConfig defines the optional configuration for a git source.
+type GitConfig struct {
// RepoPath is the path to the repository to scan.
RepoPath,
// HeadRef is the head reference to use to scan from.
HeadRef,
// BaseRef is the base reference to use to scan from.
BaseRef string
- // Concurrency is the number of concurrent workers to use to scan the source.
- Concurrency,
// MaxDepth is the maximum depth to scan the source.
MaxDepth int
+ // Filter is the filter to use to scan the source.
+ Filter *common.Filter
+}
+
+// GithubConfig defines the optional configuration for a github source.
+type GithubConfig struct {
+ // Endpoint is the endpoint of the source.
+ Endpoint,
+ // Token is the token to use to authenticate with the source.
+ Token string
// IncludeForks indicates whether to include forks in the scan.
IncludeForks,
// IncludeMembers indicates whether to include members in the scan.
- IncludeMembers,
- // CloudCred determines whether to use cloud credentials.
- // This can NOT be used with a secret.
- CloudCred bool
+ IncludeMembers bool
+ // Concurrency is the number of concurrent workers to use to scan the source.
+ Concurrency int
// Repos is the list of repositories to scan.
Repos,
// Orgs is the list of organizations to scan.
Orgs,
- // Buckets is the list of buckets to scan.
- Buckets,
// ExcludeRepos is a list of repositories to exclude from the scan.
ExcludeRepos,
// IncludeRepos is a list of repositories to include in the scan.
- IncludeRepos,
+ IncludeRepos []string
+}
+
+// GitlabConfig defines the optional configuration for a gitlab source.
+type GitlabConfig struct {
+ // Endpoint is the endpoint of the source.
+ Endpoint,
+ // Token is the token to use to authenticate with the source.
+ Token string
+ // Repos is the list of repositories to scan.
+ Repos []string
+ // Filter is the filter to use to scan the source.
+ Filter *common.Filter
+}
+
+// FilesystemConfig defines the optional configuration for a filesystem source.
+type FilesystemConfig struct {
// Directories is the list of directories to scan.
Directories []string
// Filter is the filter to use to scan the source.
Filter *common.Filter
}
-// NewConfig returns a new Config with optional values.
-func NewConfig(opts ...func(*Config)) Config {
- c := &Config{}
- for _, opt := range opts {
- opt(c)
- }
- return *c
+// S3Config defines the optional configuration for an S3 source.
+type S3Config struct {
+ // CloudCred determines whether to use cloud credentials.
+ // This can NOT be used with a secret.
+ CloudCred bool
+ // Key is any key to use to authenticate with the source.
+ Key,
+ // Secret is any secret to use to authenticate with the source.
+ Secret string
+ // Buckets is the list of buckets to scan.
+ Buckets []string
+}
+
+// SyslogConfig defines the optional configuration for a syslog source.
+type SyslogConfig struct {
+ // Address used to connect to the source.
+ Address,
+ // Protocol used to connect to the source.
+ Protocol,
+ // CertPath is the path to the certificate to use to connect to the source.
+ CertPath,
+ // Format is the format used to connect to the source.
+ Format,
+ // KeyPath is the path to the key to use to connect to the source.
+ KeyPath string
+ // Concurrency is the number of concurrent workers to use to scan the source.
+ Concurrency int
}
// Progress is used to update job completion progress across sources.