Repository navigation
add auto-discovery mode #796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package runner | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/projectdiscovery/cloudlist/pkg/inventory" | ||
| "github.com/projectdiscovery/cloudlist/pkg/schema" | ||
| "github.com/projectdiscovery/gologger" | ||
| ) | ||
|
|
||
| // autoDiscoveryProviders resolve credentials without config through their SDK | ||
| // default chains: environment variables, CLI logins, instance metadata and | ||
| // in-cluster service accounts. | ||
| var autoDiscoveryProviders = []string{"aws", "gcp", "azure", "kubernetes"} | ||
|
|
||
| func autoDiscoveryConfig() schema.Options { | ||
| config := make(schema.Options, 0, len(autoDiscoveryProviders)) | ||
| for _, provider := range autoDiscoveryProviders { | ||
| config = append(config, schema.OptionBlock{"provider": provider}) | ||
| } | ||
| return config | ||
| } | ||
|
|
||
| func (r *Runner) newInventory(config schema.Options) (*inventory.Inventory, error) { | ||
| if !r.options.AutoDiscovery { | ||
| return inventory.New(config) | ||
| } | ||
| // A provider fails to initialize when the environment has no credentials | ||
| // for it, which is expected here, so skip it instead of aborting. | ||
| discovered := &inventory.Inventory{} | ||
| for _, block := range config { | ||
| inv, err := inventory.New(schema.Options{block}) | ||
| if err != nil { | ||
| gologger.Verbose().Msgf("Skipping %s: %s", block["provider"], err) | ||
| continue | ||
| } | ||
| discovered.Providers = append(discovered.Providers, inv.Providers...) | ||
| } | ||
| if len(discovered.Providers) == 0 { | ||
| return nil, errors.New("no provider credentials found in the environment") | ||
| } | ||
| return discovered, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package runner | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/projectdiscovery/cloudlist/pkg/schema" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestNewInventorySkipsProvidersWithoutCredentials(t *testing.T) { | ||
| kubeconfig := filepath.Join(t.TempDir(), "config") | ||
| require.NoError(t, os.WriteFile(kubeconfig, []byte(`apiVersion: v1 | ||
| kind: Config | ||
| clusters: | ||
| - name: test | ||
| cluster: | ||
| server: https://k8s.example.test:6443 | ||
| contexts: | ||
| - name: test | ||
| context: | ||
| cluster: test | ||
| current-context: test | ||
| `), 0o600)) | ||
| r := &Runner{options: &Options{AutoDiscovery: true}} | ||
|
|
||
| inv, err := r.newInventory(schema.Options{ | ||
| {"provider": "kubernetes", "kubeconfig_file": filepath.Join(t.TempDir(), "missing")}, | ||
| {"provider": "kubernetes", "kubeconfig_file": kubeconfig}, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Len(t, inv.Providers, 1) | ||
|
|
||
| _, err = r.newInventory(schema.Options{ | ||
| {"provider": "kubernetes", "kubeconfig_file": filepath.Join(t.TempDir(), "missing")}, | ||
| }) | ||
| require.EqualError(t, err, "no provider credentials found in the environment") | ||
| } | ||
|
|
||
| func TestNewInventoryFailsFastWithoutAutoDiscovery(t *testing.T) { | ||
| r := &Runner{options: &Options{}} | ||
|
|
||
| _, err := r.newInventory(schema.Options{ | ||
| {"provider": "kubernetes", "kubeconfig_file": filepath.Join(t.TempDir(), "missing")}, | ||
| }) | ||
| require.Error(t, err) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package k8s | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/projectdiscovery/cloudlist/pkg/schema" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const testKubeconfig = `apiVersion: v1 | ||
| kind: Config | ||
| clusters: | ||
| - name: test | ||
| cluster: | ||
| server: https://k8s.example.test:6443 | ||
| users: | ||
| - name: test | ||
| user: | ||
| token: test | ||
| contexts: | ||
| - name: test | ||
| context: | ||
| cluster: test | ||
| user: test | ||
| current-context: test | ||
| ` | ||
|
|
||
| func isolateKubeconfig(t *testing.T, kubeconfig string) { | ||
| t.Helper() | ||
| t.Setenv("HOME", t.TempDir()) | ||
| t.Setenv("KUBERNETES_SERVICE_HOST", "") | ||
| t.Setenv("KUBECONFIG", kubeconfig) | ||
| } | ||
|
|
||
| func TestNewWithoutKubeconfigUsesEnvironment(t *testing.T) { | ||
| path := filepath.Join(t.TempDir(), "config") | ||
| require.NoError(t, os.WriteFile(path, []byte(testKubeconfig), 0o600)) | ||
| isolateKubeconfig(t, path) | ||
|
|
||
| p, err := New(schema.OptionBlock{}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "k8s.example.test:6443", p.clientSet.CoreV1().RESTClient().Get().URL().Host) | ||
| } | ||
|
|
||
| func TestNewWithoutKubeconfigFailsWhenNoneFound(t *testing.T) { | ||
| isolateKubeconfig(t, filepath.Join(t.TempDir(), "missing")) | ||
|
|
||
| _, err := New(schema.OptionBlock{}) | ||
| require.Error(t, err) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.