Skip to content

Commit

Permalink
Add owner to github tokens (#3558)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin-decker authored Nov 4, 2024
1 parent fa871b0 commit 5ca4a17
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
1 change: 1 addition & 0 deletions pkg/analyzer/analyzers/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func secretInfoToAnalyzerResult(info *common.SecretInfo) *analyzers.AnalyzerResu
}
result := &analyzers.AnalyzerResult{
Metadata: map[string]any{
"owner": info.Metadata.User.Login,
"type": info.Metadata.Type,
"expiration": info.Metadata.Expiration,
},
Expand Down
24 changes: 9 additions & 15 deletions pkg/analyzer/analyzers/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
Expand Down Expand Up @@ -229,6 +230,7 @@ func TestAnalyzer_Analyze(t *testing.T) {
],
"UnboundedResources": null,
"Metadata": {
"owner": "sirdetectsalot",
"expiration": "2025-08-05T00:00:00-07:00",
"type": "Fine-Grained GitHub Personal Access Token"
}
Expand Down Expand Up @@ -276,6 +278,7 @@ func TestAnalyzer_Analyze(t *testing.T) {
],
"UnboundedResources": null,
"Metadata": {
"owner": "truffle-sandbox",
"expiration": "0001-01-01T00:00:00Z",
"type": "Classic GitHub Personal Access Token"
}
Expand All @@ -293,7 +296,7 @@ func TestAnalyzer_Analyze(t *testing.T) {
}

// Marshal the actual result to JSON
gotJSON, err := json.Marshal(got)
gotJSON, err := json.MarshalIndent(got, "", " ")
if err != nil {
t.Fatalf("could not marshal got to JSON: %s", err)
}
Expand All @@ -304,25 +307,16 @@ func TestAnalyzer_Analyze(t *testing.T) {
t.Fatalf("could not unmarshal want JSON string: %s", err)
}

// Marshal the expected result to JSON (to normalize)
wantJSON, err := json.Marshal(wantObj)
// Marshal the expected result to JSON with indentation
wantJSON, err := json.MarshalIndent(wantObj, "", " ")
if err != nil {
t.Fatalf("could not marshal want to JSON: %s", err)
}

// Compare the JSON strings
// Compare the JSON strings and show diff if they don't match
if string(gotJSON) != string(wantJSON) {
// Pretty-print both JSON strings for easier comparison
var gotIndented, wantIndented []byte
gotIndented, err = json.MarshalIndent(got, "", " ")
if err != nil {
t.Fatalf("could not marshal got to indented JSON: %s", err)
}
wantIndented, err = json.MarshalIndent(wantObj, "", " ")
if err != nil {
t.Fatalf("could not marshal want to indented JSON: %s", err)
}
t.Errorf("Analyzer.Analyze() = %s, want %s", gotIndented, wantIndented)
diff := cmp.Diff(string(wantJSON), string(gotJSON))
t.Errorf("Analyzer.Analyze() mismatch (-want +got):\n%s", diff)
}
})
}
Expand Down

0 comments on commit 5ca4a17

Please sign in to comment.