-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[analyze] Add Analyzer interface for Gitlab #3232
Merged
abmussani
merged 14 commits into
trufflesecurity:main
from
abmussani:impl-data-model-gitlab
Oct 30, 2024
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8248d52
implement analyzer interface for gitlab
abasit-folio3 54b79c7
generated permissions, added unit test for gitlab analyzer
abasit-folio3 2e8c534
revert deletion of scopes.go
abasit-folio3 8578acf
appending domain in resource names
abmussani 051aadf
[chore]
abmussani 6211ab6
updated the test for gitlab analyzer
abmussani f4ed0d8
remove unnecessary metadata field and fix github -> gitlab
abmussani 1e8b1d5
Merge branch 'main' into impl-data-model-gitlab
abmussani 735dc9d
extract user id from access token json, make user as resource
abmussani 5905729
link analyzer with gitlab v2 detector
abmussani 3860259
Merge branch 'main' into impl-data-model-gitlab
abmussani 81eece6
fixed code breaking changes due to analyzer protobuf removal.
abmussani 58bba02
Merge branch 'main' into impl-data-model-gitlab
abmussani 58fb84f
Merge branch 'main' into impl-data-model-gitlab
abmussani 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 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 @@ | ||
{"AnalyzerType":5,"Bindings":[{"Resource":{"Name":"test-project-token","FullyQualifiedName":"github.com/token/10470457","Type":"access_token","Metadata":{"created_at":"2024-08-15T06:33:00.337Z","expires_at":"2025-08-15","last_used_at":"2024-09-09T16:41:06.941Z","revoked":false},"Parent":null},"Permission":{"Value":"read_api","Parent":null}},{"Resource":{"Name":"test-project-token","FullyQualifiedName":"github.com/token/10470457","Type":"access_token","Metadata":{"created_at":"2024-08-15T06:33:00.337Z","expires_at":"2025-08-15","last_used_at":"2024-09-09T16:41:06.941Z","revoked":false},"Parent":null},"Permission":{"Value":"read_repository","Parent":null}},{"Resource":{"Name":"truffletester / trufflehog","FullyQualifiedName":"github.com/project/60871295","Type":"project","Metadata":null,"Parent":null},"Permission":{"Value":"Developer","Parent":null}}],"UnboundedResources":null,"Metadata":{"enterprise":true,"version":"17.4.0-pre"}} |
This file contains 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 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,78 @@ | ||
package gitlab | ||
|
||
import ( | ||
_ "embed" | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/common" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/context" | ||
) | ||
|
||
//go:embed expected_output.json | ||
var expectedOutput []byte | ||
|
||
func TestAnalyzer_Analyze(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors4") | ||
if err != nil { | ||
t.Fatalf("could not get test secrets from GCP: %s", err) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
key string | ||
want string // JSON string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid gitlab access token", | ||
key: testSecrets.MustGetField("GITLABV2"), | ||
want: string(expectedOutput), | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
a := Analyzer{} | ||
got, err := a.Analyze(ctx, map[string]string{"key": tt.key}) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Analyzer.Analyze() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
// Marshal the actual result to JSON | ||
gotJSON, err := json.Marshal(got) | ||
if err != nil { | ||
t.Fatalf("could not marshal got to JSON: %s", err) | ||
} | ||
|
||
// Parse the expected JSON string | ||
var wantObj analyzers.AnalyzerResult | ||
if err := json.Unmarshal([]byte(tt.want), &wantObj); err != nil { | ||
t.Fatalf("could not unmarshal want JSON string: %s", err) | ||
} | ||
|
||
// Marshal the expected result to JSON (to normalize) | ||
wantJSON, err := json.Marshal(wantObj) | ||
if err != nil { | ||
t.Fatalf("could not marshal want to JSON: %s", err) | ||
} | ||
|
||
// Compare the JSON strings | ||
if string(gotJSON) != string(wantJSON) { | ||
// Pretty-print both JSON strings for easier comparison | ||
var gotIndented []byte | ||
gotIndented, err = json.MarshalIndent(got, "", " ") | ||
if err != nil { | ||
t.Fatalf("could not marshal got to indented JSON: %s", err) | ||
} | ||
t.Errorf("Analyzer.Analyze() = \n%s", gotIndented) | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,15 @@ | ||
permissions: | ||
- api | ||
- read_user | ||
- read_api | ||
- read_repository | ||
- write_repository | ||
- read_registry | ||
- write_registry | ||
- sudo | ||
- admin_mode | ||
- create_runner | ||
- manage_runner | ||
- ai_features | ||
- k8s_proxy | ||
- read_service_ping |
This file contains 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 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure
token
should be a resource here? Does the credential we're analyzing grant permission to perform actions on the token?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, The permission determine what action can be perform using that token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Permissions operate on a resource though, so in this case the permissions would describe operations that could be performed on the token itself, which seems wrong to me..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mcastorina I had gone through the login. PAT token does belongs to user and we do get userID in response. So, resource can be a User with those permissions. What do you think ?