-
Notifications
You must be signed in to change notification settings - Fork 94
Veles custom extractor for aws access key #1568
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
Closed
alessandro-Doyensec
wants to merge
15
commits into
google:main
from
doyensec:veles-custom-extractor-for-aws-access-key
Closed
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2343e03
add: specific filesystem converter
alessandro-Doyensec 5029446
add: aws credentials extractor
alessandro-Doyensec a21270b
add: docs
alessandro-Doyensec 1306cce
edit: register awscredential extractor
alessandro-Doyensec 53d6e74
edit: extend the convert library to support extractors which implemen…
alessandro-Doyensec 770b4bf
edit: update docs/supported_inventory_types.md
alessandro-Doyensec 59e5bb0
edit: restore initMapFromVelesPlugins
alessandro-Doyensec bb8c33c
refactor: better placeholder naming
alessandro-Doyensec e13727f
fix: remove unused function
alessandro-Doyensec 0567d56
add: type assertion for detectorWithRequire
alessandro-Doyensec 50e75e6
edit: handle extractor keepers
alessandro-Doyensec 04f380a
fix: test want
alessandro-Doyensec a664e25
add: docs to extractorKeeper
alessandro-Doyensec ae71ff2
Merge branch 'main' into veles-custom-extractor-for-aws-access-key
erikvarga 462f138
add: cmpopts.SortSlices to cmp.Diff in TestScan
alessandro-Doyensec 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Package awsaccesskey extends the veles awsaccesskey.Detector to search inside the `~/.aws/credentials` file | ||
| package awsaccesskey | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/google/osv-scalibr/extractor/filesystem" | ||
| "github.com/google/osv-scalibr/veles/secrets/awsaccesskey" | ||
|
|
||
| "github.com/google/osv-scalibr/extractor/filesystem/secrets/convert" | ||
| ) | ||
|
|
||
| const ( | ||
| // Name is the name of the extractor | ||
| Name = "secrets/awsaccesskey" | ||
| // Version is the version of the extractor | ||
| Version = 0 | ||
| ) | ||
|
|
||
| // New returns a filesystem.Extractor which extracts AWS Access Keys using the awsaccesskey.Detector | ||
| func New() filesystem.Extractor { | ||
| return convert.FromVelesDetectorWithRequire( | ||
| awsaccesskey.NewDetector(), Name, Version, FileRequired, | ||
| ) | ||
| } | ||
|
|
||
| // FileRequired returns true if a file contains aws credentials. | ||
| func FileRequired(api filesystem.FileAPI) bool { | ||
| path := filepath.ToSlash(api.Path()) | ||
| return strings.HasSuffix(path, ".aws/credentials") | ||
| } |
119 changes: 119 additions & 0 deletions
119
extractor/filesystem/secrets/awsaccesskey/awsaccesskey_test.go
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,119 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package awsaccesskey_test | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "github.com/google/osv-scalibr/extractor/filesystem/secrets/awsaccesskey" | ||
| "github.com/google/osv-scalibr/extractor/filesystem/simplefileapi" | ||
| "github.com/google/osv-scalibr/inventory" | ||
| "github.com/google/osv-scalibr/testing/extracttest" | ||
| awsaccesskeydetector "github.com/google/osv-scalibr/veles/secrets/awsaccesskey" | ||
| ) | ||
|
|
||
| func TestExtractor_FileRequired(t *testing.T) { | ||
| tests := []struct { | ||
| inputPath string | ||
| want bool | ||
| isWindows bool | ||
| }{ | ||
| {inputPath: "", want: false}, | ||
|
|
||
| // linux | ||
| {inputPath: `/Users/example-user/.aws/credentials`, want: true}, | ||
| {inputPath: `/Users/example-user/bad/path`, want: false}, | ||
|
|
||
| // windows | ||
| {inputPath: `C:\Users\USERNAME\.aws\credentials`, isWindows: true, want: true}, | ||
| {inputPath: `C:\Users\USERNAME\another\bad\path`, isWindows: true, want: false}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.inputPath, func(t *testing.T) { | ||
| if tt.isWindows && runtime.GOOS != "windows" { | ||
| t.Skipf("Skipping test %q for %q", t.Name(), runtime.GOOS) | ||
| } | ||
| e := awsaccesskey.New() | ||
| got := e.FileRequired(simplefileapi.New(tt.inputPath, nil)) | ||
| if got != tt.want { | ||
| t.Errorf("FileRequired(%s) got = %v, want %v", tt.inputPath, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestExtractor_Extract(t *testing.T) { | ||
| tests := []*struct { | ||
| Name string | ||
| Path string | ||
| WantSecrets []*inventory.Secret | ||
| WantErr error | ||
| }{ | ||
| { | ||
| Name: "empty", | ||
| Path: "empty", | ||
| WantSecrets: nil, | ||
| }, | ||
| { | ||
| Name: "aws_credentials", | ||
| Path: "aws_credentials", | ||
| WantSecrets: []*inventory.Secret{ | ||
| { | ||
| Secret: awsaccesskeydetector.Credentials{ | ||
| AccessID: "AIKA1984R439T439HTH4", | ||
| Secret: "32r923jr023rk320rk2a3rkB34tj340r32Ckt433", | ||
| }, | ||
| Location: "aws_credentials", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| Name: "random_content", | ||
| Path: "random_content", | ||
| WantSecrets: nil, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.Name, func(t *testing.T) { | ||
| extr := awsaccesskey.New() | ||
|
|
||
| inputCfg := extracttest.ScanInputMockConfig{ | ||
| Path: tt.Path, | ||
| FakeScanRoot: "testdata", | ||
| } | ||
|
|
||
| scanInput := extracttest.GenerateScanInputMock(t, inputCfg) | ||
| defer extracttest.CloseTestScanInput(t, scanInput) | ||
|
|
||
| got, err := extr.Extract(t.Context(), &scanInput) | ||
|
|
||
| if diff := cmp.Diff(tt.WantErr, err, cmpopts.EquateErrors()); diff != "" { | ||
| t.Errorf("%s.Extract(%q) error diff (-want +got):\n%s", extr.Name(), tt.Path, diff) | ||
| return | ||
| } | ||
|
|
||
| wantInv := inventory.Inventory{Secrets: tt.WantSecrets} | ||
| opts := []cmp.Option{cmpopts.SortSlices(extracttest.PackageCmpLess), cmpopts.EquateEmpty()} | ||
| if diff := cmp.Diff(wantInv, got, opts...); diff != "" { | ||
| t.Errorf("%s.Extract(%q) diff (-want +got):\n%s", extr.Name(), tt.Path, diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
extractor/filesystem/secrets/awsaccesskey/testdata/aws_credentials
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,3 @@ | ||
| [default] | ||
| aws_access_key_id = AIKA1984R439T439HTH4 | ||
| aws_secret_access_key = 32r923jr023rk320rk2a3rkB34tj340r32Ckt433 |
Empty file.
10 changes: 10 additions & 0 deletions
10
extractor/filesystem/secrets/awsaccesskey/testdata/random_content
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,10 @@ | ||
| localhost:5432:mydb:myuser:mypassword | ||
| hostname:1234:testdb:testuser:testpass123 | ||
| hostname:1234:testdb:testuser:passw*ord | ||
| # space inside one group (except password) | ||
| hostname:1234:testdb:testuser:passw ord | ||
| hostname:1234:db name:testuser:password | ||
| # this is a comment and should be ignored | ||
| *:*:db:admin:supersecret | ||
| # valid with escaped : | ||
| prod.example.com:5432:db:admin:pass\:word |
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,100 @@ | ||
| package convert | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/google/osv-scalibr/extractor/filesystem" | ||
| "github.com/google/osv-scalibr/inventory" | ||
| "github.com/google/osv-scalibr/plugin" | ||
| "github.com/google/osv-scalibr/veles" | ||
| ) | ||
|
|
||
| // FromVelesDetectorWithRequire works similar to FromVelesDetector but allows specifying additional files to look at on top of the default ones. | ||
| func FromVelesDetectorWithRequire(velesDetector veles.Detector, name string, version int, fileRequired func(filesystem.FileAPI) bool) filesystem.Extractor { | ||
| return &detectorWithRequire{ | ||
| velesDetector: velesDetector, | ||
| name: name, | ||
| version: version, | ||
| fileRequired: fileRequired, | ||
| } | ||
| } | ||
|
|
||
| // extractorKeeper signals that a Detector also functions as a standalone filesystem.Extractor. | ||
| type extractorKeeper interface { | ||
| KeepExtractor() bool | ||
| } | ||
|
|
||
| // Assert that detectorWithRequire implements the required interfaces. | ||
| var _ veles.Detector = &detectorWithRequire{} | ||
| var _ filesystem.Extractor = &detectorWithRequire{} | ||
| var _ extractorKeeper = &detectorWithRequire{} | ||
|
|
||
| // detectorWithRequire is a wrapper around the veles.Detector interface that | ||
| // implements the additional functions of the filesystem Extractor interface. | ||
| type detectorWithRequire struct { | ||
| velesDetector veles.Detector | ||
| name string | ||
| version int | ||
| fileRequired func(filesystem.FileAPI) bool | ||
| e *veles.DetectionEngine | ||
| } | ||
|
|
||
| // KeepExtractor signals that this detector must also be registered as a standalone | ||
| // filesystem.Extractor to handle the additional files specified in the fileRequired callback. | ||
| func (d *detectorWithRequire) KeepExtractor() bool { return true } | ||
|
|
||
| // MaxSecretLen returns the maximum length a secret from this Detector can have. | ||
| func (d *detectorWithRequire) MaxSecretLen() uint32 { | ||
| return d.velesDetector.MaxSecretLen() | ||
| } | ||
|
|
||
| // Detect finds candidate secrets in the data and returns them alongside their | ||
| // starting positions. | ||
| func (d *detectorWithRequire) Detect(data []byte) ([]veles.Secret, []int) { | ||
| return d.velesDetector.Detect(data) | ||
| } | ||
|
|
||
| // Name of the secret extractor. | ||
| func (d *detectorWithRequire) Name() string { | ||
| return d.name | ||
| } | ||
|
|
||
| // Version of the secret extractor. | ||
| func (d *detectorWithRequire) Version() int { | ||
| return d.version | ||
| } | ||
|
|
||
| // Requirements of the secret extractor. | ||
| func (d *detectorWithRequire) Requirements() *plugin.Capabilities { | ||
| // Veles plugins don't have any special requirements. | ||
| return &plugin.Capabilities{} | ||
| } | ||
|
|
||
| // FileRequired returns the provided file required callback. | ||
| func (d *detectorWithRequire) FileRequired(api filesystem.FileAPI) bool { | ||
| return d.fileRequired(api) | ||
| } | ||
|
|
||
| // Extract extracts secret from the filesystem using the provided detector. | ||
| func (d *detectorWithRequire) Extract(ctx context.Context, input *filesystem.ScanInput) (inventory.Inventory, error) { | ||
| if d.e == nil { | ||
| var err error | ||
| d.e, err = veles.NewDetectionEngine([]veles.Detector{d.velesDetector}) | ||
| if err != nil { | ||
| return inventory.Inventory{}, err | ||
| } | ||
| } | ||
| secrets, err := d.e.Detect(ctx, input.Reader) | ||
| if err != nil { | ||
| return inventory.Inventory{}, fmt.Errorf("unable to scan for secrets: %w", err) | ||
| } | ||
| i := inventory.Inventory{} | ||
| for _, s := range secrets { | ||
| i.Secrets = append(i.Secrets, &inventory.Secret{ | ||
| Secret: s, | ||
| Location: input.Path, | ||
| }) | ||
| } | ||
| return i, 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
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.