-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat(detectors): add JumpCloud API Key v2 detector (jca_ prefix) #4975
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
Open
mangod12
wants to merge
2
commits into
trufflesecurity:main
Choose a base branch
from
mangod12:feat/jumpcloud-v2-detector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
File renamed without changes.
File renamed without changes.
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,105 @@ | ||
| package jumpcloud | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| regexp "github.com/wasilibs/go-re2" | ||
|
|
||
| "github.com/trufflesecurity/trufflehog/v3/pkg/common" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb" | ||
| ) | ||
|
|
||
| type Scanner struct { | ||
| client *http.Client | ||
| } | ||
|
|
||
| func (Scanner) Version() int { return 2 } | ||
|
|
||
| // Ensure the Scanner satisfies the interface at compile time. | ||
| var _ detectors.Detector = (*Scanner)(nil) | ||
| var _ detectors.Versioner = (*Scanner)(nil) | ||
|
|
||
| var ( | ||
| defaultClient = common.SaneHttpClient() | ||
|
|
||
| // jca_ prefix followed by 36 alphanumeric characters (40 total). | ||
| keyPat = regexp.MustCompile(`\b(jca_[a-zA-Z0-9]{36})\b`) | ||
| ) | ||
|
|
||
| // Keywords are used for efficiently pre-filtering chunks. | ||
| // The jca_ prefix is self-identifying, no context keyword needed. | ||
| func (s Scanner) Keywords() []string { | ||
| return []string{"jca_"} | ||
| } | ||
|
|
||
| // FromData will find and optionally verify JumpCloud v2 API keys in a given set of bytes. | ||
| func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | ||
| dataStr := string(data) | ||
|
|
||
| uniqueMatches := make(map[string]struct{}) | ||
| for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) { | ||
| uniqueMatches[match[1]] = struct{}{} | ||
| } | ||
|
|
||
| for match := range uniqueMatches { | ||
| s1 := detectors.Result{ | ||
| DetectorType: detector_typepb.DetectorType_Jumpcloud, | ||
| Raw: []byte(match), | ||
| ExtraData: map[string]string{"version": "2"}, | ||
| SecretParts: map[string]string{"key": match}, | ||
| } | ||
|
|
||
| if verify { | ||
| client := s.client | ||
| if client == nil { | ||
| client = defaultClient | ||
| } | ||
|
|
||
| isVerified, verificationErr := verifyMatch(ctx, client, match) | ||
| s1.Verified = isVerified | ||
| s1.SetVerificationError(verificationErr, match) | ||
| } | ||
|
|
||
| results = append(results, s1) | ||
| } | ||
|
|
||
| return results, nil | ||
| } | ||
|
|
||
| func verifyMatch(ctx context.Context, client *http.Client, token string) (bool, error) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://console.jumpcloud.com/api/v2/systemgroups", nil) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| req.Header.Set("x-api-key", token) | ||
| res, err := client.Do(req) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| defer func() { | ||
| _, _ = io.Copy(io.Discard, res.Body) | ||
| _ = res.Body.Close() | ||
| }() | ||
|
|
||
| switch res.StatusCode { | ||
| case http.StatusOK: | ||
| return true, nil | ||
| case http.StatusBadRequest, http.StatusUnauthorized, http.StatusForbidden: | ||
| return false, nil | ||
| default: | ||
| return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | ||
| } | ||
| } | ||
|
|
||
| func (s Scanner) Type() detector_typepb.DetectorType { | ||
| return detector_typepb.DetectorType_Jumpcloud | ||
| } | ||
|
|
||
| func (s Scanner) Description() string { | ||
| return "JumpCloud is a cloud-based directory service platform that offers user and device management, single sign-on, and other IAM features. JumpCloud v2 API keys use a jca_ prefix format and can be used to access and manage these services." | ||
| } |
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 @@ | ||
| package jumpcloud | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick" | ||
| ) | ||
|
|
||
| func TestJumpCloudV2_Pattern(t *testing.T) { | ||
| d := Scanner{} | ||
| ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d}) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| input string | ||
| want []string | ||
| }{ | ||
| { | ||
| name: "valid pattern - env var", | ||
| input: ` | ||
| # JumpCloud API configuration | ||
| export JUMPCLOUD_API_KEY="jca_aBcDeFgHiJkLmNoPqRsTuVwXyZ1234567890" | ||
| `, | ||
| want: []string{"jca_aBcDeFgHiJkLmNoPqRsTuVwXyZ1234567890"}, | ||
| }, | ||
| { | ||
| name: "valid pattern - config file", | ||
| input: ` | ||
| api_key: jca_r7m2Xk9pL4nQ8vB3wF6yH1jD5sA0tC2eG4iK7oU | ||
| server: https://console.jumpcloud.com | ||
| `, | ||
| want: []string{"jca_r7m2Xk9pL4nQ8vB3wF6yH1jD5sA0tC2eG4iK7oU"}, | ||
| }, | ||
| { | ||
| name: "valid pattern - code usage", | ||
| input: ` | ||
| func main() { | ||
| req, _ := http.NewRequest("GET", "https://console.jumpcloud.com/api/v2/systemgroups", nil) | ||
| req.Header.Set("x-api-key", "jca_Tm4nQ8vB3wF6yH1jD5sA0tC2eG4iK7oUp9xL") | ||
| client := &http.Client{} | ||
| resp, _ := client.Do(req) | ||
| defer func() { _ = resp.Body.Close() }() | ||
| } | ||
| `, | ||
| want: []string{"jca_Tm4nQ8vB3wF6yH1jD5sA0tC2eG4iK7oUp9xL"}, | ||
| }, | ||
| { | ||
| name: "valid pattern - deduplicates", | ||
| input: ` | ||
| primary = "jca_aBcDeFgHiJkLmNoPqRsTuVwXyZ1234567890" | ||
| backup = "jca_aBcDeFgHiJkLmNoPqRsTuVwXyZ1234567890" | ||
| `, | ||
| want: []string{"jca_aBcDeFgHiJkLmNoPqRsTuVwXyZ1234567890"}, | ||
| }, | ||
| { | ||
| name: "invalid pattern - too short", | ||
| input: `jca_aBcDeFgHiJkLmNoPqRsTuVwXyZ12345`, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "invalid pattern - too long", | ||
| input: `jca_aBcDeFgHiJkLmNoPqRsTuVwXyZ12345678901`, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "invalid pattern - special characters", | ||
| input: `jca_aBcDeFgHi-kLmNoPqRsTuVwXyZ123456789!`, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "invalid pattern - no jca_ prefix", | ||
| input: `JUMPCLOUD_API_KEY=aBcDeFgHiJkLmNoPqRsTuVwXyZ1234567890abcd`, | ||
| want: nil, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input)) | ||
| if len(matchedDetectors) == 0 { | ||
| if len(test.want) > 0 { | ||
| t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| results, err := d.FromData(context.Background(), false, []byte(test.input)) | ||
| require.NoError(t, err) | ||
|
|
||
| if len(results) != len(test.want) { | ||
| t.Errorf("expected %d results, got %d", len(test.want), len(results)) | ||
| return | ||
| } | ||
|
|
||
| actual := make(map[string]struct{}, len(results)) | ||
| for _, r := range results { | ||
| if len(r.RawV2) > 0 { | ||
| actual[string(r.RawV2)] = struct{}{} | ||
| } else { | ||
| actual[string(r.Raw)] = struct{}{} | ||
| } | ||
| } | ||
|
|
||
| expected := make(map[string]struct{}, len(test.want)) | ||
| for _, v := range test.want { | ||
| expected[v] = struct{}{} | ||
| } | ||
|
|
||
| if diff := cmp.Diff(expected, actual); diff != "" { | ||
| t.Errorf("%s diff: (-want +got)\n%s", test.name, diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.