-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[analyze] Add Analyzer for Mailgun (#3206)
* implement analyzer interface, add unit test and link with detector for mailgun * [chore] moved expected output of test in json file to neat the code. corrected variable name for test in detector bucket * append domain id in fully qualified name of domain resources * [Fixes] domains will be added as resource in bindings and permissions. updated the test. --------- Co-authored-by: Abdul Basit <[email protected]>
- Loading branch information
1 parent
b0318a9
commit 57e5812
Showing
7 changed files
with
245 additions
and
4 deletions.
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,25 @@ | ||
{ | ||
"AnalyzerType": 8, | ||
"Bindings": [ | ||
{ | ||
"Resource": { | ||
"Name": "sandbox19e49763d44e498e850589ea7d54bd82.mailgun.org", | ||
"FullyQualifiedName": "mailgun/6478cb31d026c112819856cd/sandbox19e49763d44e498e850589ea7d54bd82.mailgun.org", | ||
"Type": "domain", | ||
"Metadata": { | ||
"created_at": "Thu, 01 Jun 2023 16:45:37 GMT", | ||
"is_disabled": false, | ||
"state": "active", | ||
"type": "sandbox" | ||
}, | ||
"Parent": null | ||
}, | ||
"Permission": { | ||
"Value": "full_access", | ||
"Parent": null | ||
} | ||
} | ||
], | ||
"UnboundedResources": null, | ||
"Metadata": null | ||
} |
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,100 @@ | ||
package mailgun | ||
|
||
import ( | ||
_ "embed" | ||
"encoding/json" | ||
"sort" | ||
"testing" | ||
"time" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/config" | ||
"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", "detectors5") | ||
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 Mailgun key", | ||
key: testSecrets.MustGetField("NEW_MAILGUN_TOKEN_ACTIVE"), | ||
want: string(expectedOutput), | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
a := Analyzer{Cfg: &config.Config{}} | ||
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 | ||
} | ||
|
||
// bindings need to be in the same order to be comparable | ||
sortBindings(got.Bindings) | ||
|
||
// 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) | ||
} | ||
|
||
// bindings need to be in the same order to be comparable | ||
sortBindings(wantObj.Bindings) | ||
|
||
// 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, 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) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Helper function to sort bindings | ||
func sortBindings(bindings []analyzers.Binding) { | ||
sort.SliceStable(bindings, func(i, j int) bool { | ||
if bindings[i].Resource.Name == bindings[j].Resource.Name { | ||
return bindings[i].Permission.Value < bindings[j].Permission.Value | ||
} | ||
return bindings[i].Resource.Name < bindings[j].Resource.Name | ||
}) | ||
} |
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,4 @@ | ||
permissions: | ||
- read | ||
- write | ||
- full_access |
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