| @@ -1,10 +1,12 @@ |
| 1 | package sumologickey | 1 | package sumologickey |
| 2 | | 2 | |
| 3 | import ( | 3 | import ( |
| | 4 | +added: "cmp" |
| 4 | "context" | 5 | "context" |
| 5 | "fmt" | 6 | "fmt" |
| 6 | "io" | 7 | "io" |
| 7 | "net/http" | 8 | "net/http" |
| | 9 | +added: "net/url" |
| 8 | "strings" | 10 | "strings" |
| 9 | | 11 | |
| 10 | "github.com/trufflesecurity/trufflehog/v3/pkg/common" | 12 | "github.com/trufflesecurity/trufflehog/v3/pkg/common" |
| @@ -44,8 +46,10 @@ func (s Scanner) Keywords() []string { |
| 44 | return []string{"sumo", "accessId", "accessKey"} | 46 | return []string{"sumo", "accessId", "accessKey"} |
| 45 | } | 47 | } |
| 46 | | 48 | |
| 47 | −removed: // Default US API endpoint. | 49 | +added: // CloudEndpoint is the US API endpoint. Endpoints are full URLs so that |
| 48 | −removed: func (Scanner) CloudEndpoint() string { return "api.sumologic.com" } | 50 | +added: // configured verifier endpoints, which may carry a path prefix, are used as |
| | 51 | +added: // given. |
| | 52 | +added: func (Scanner) CloudEndpoint() string { return "https://api.sumologic.com" } |
| 49 | | 53 | |
| 50 | // FromData will find and optionally verify SumoLogicKey secrets in a given set of bytes. | 54 | // FromData will find and optionally verify SumoLogicKey secrets in a given set of bytes. |
| 51 | func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | 55 | func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { |
| @@ -59,50 +63,67 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result |
| 59 | for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) { | 63 | for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) { |
| 60 | keyMatches[match[1]] = struct{}{} | 64 | keyMatches[match[1]] = struct{}{} |
| 61 | } | 65 | } |
| 62 | −removed: endpointMatches := make(map[string]struct{}) | 66 | +added: hostMatches := make(map[string]struct{}) |
| 63 | for _, match := range urlPat.FindAllStringSubmatch(dataStr, -1) { | 67 | for _, match := range urlPat.FindAllStringSubmatch(dataStr, -1) { |
| 64 | −removed: endpointMatches[match[0]] = struct{}{} | 68 | +added: hostMatches[match[0]] = struct{}{} |
| 65 | } | 69 | } |
| 66 | −removed: if len(endpointMatches) == 0 { | 70 | +added: foundEndpoints := make([]string, 0, len(hostMatches)) |
| 67 | −removed: endpointMatches[s.CloudEndpoint()] = struct{}{} | 71 | +added: for host := range hostMatches { |
| | 72 | +added: foundEndpoints = append(foundEndpoints, "https://"+host) |
| 68 | } | 73 | } |
| 69 | | 74 | |
| | 75 | +added: // RawV2 identifies the secret, so it is built only from what the data |
| | 76 | +added: // says and never from the verification outcome. Otherwise the same key |
| | 77 | +added: // gets a different identity when it flips between verified and |
| | 78 | +added: // unverified (for example when it is revoked), and consumers that dedupe |
| | 79 | +added: // on RawV2 record it as a new secret. The access ID and regional host are |
| | 80 | +added: // included only when the data names exactly one of each. The host is kept |
| | 81 | +added: // bare (no scheme) because existing identities were recorded that way. |
| | 82 | +added: rawV2Id := soleKey(idMatches) |
| | 83 | +added: rawV2URL := soleKey(hostMatches) |
| | 84 | +added: |
| 70 | for accessKey := range keyMatches { | 85 | for accessKey := range keyMatches { |
| 71 | var ( | 86 | var ( |
| 72 | −removed: r *detectors.Result | 87 | +added: verified bool |
| 73 | −removed: accessId string | 88 | +added: verifiedId string |
| 74 | −removed: apiEndpoint string | 89 | +added: verifiedEndpoint string |
| | 90 | +added: lastErr error |
| 75 | ) | 91 | ) |
| 76 | | 92 | |
| 77 | −removed: for id := range idMatches { | 93 | +added: if verify { |
| 78 | −removed: accessId = id | 94 | +added: client := s.VerificationClient(cmp.Or(s.client, defaultClient)) |
| 79 | −removed: | 95 | +added: |
| 80 | −removed: for e := range endpointMatches { | 96 | +added: // Endpoints applies the engine's endpoint configuration: |
| 81 | −removed: apiEndpoint = e | 97 | +added: // configured verifier endpoints, the US cloud endpoint, and |
| 82 | −removed: | 98 | +added: // regional hosts found in the data, each only when enabled. |
| 83 | −removed: if verify { | 99 | +added: // |
| 84 | −removed: client := s.client | 100 | +added: // Only non-nil errors overwrite lastErr so that a clean 401 from |
| 85 | −removed: if client == nil { | 101 | +added: // the wrong region or access ID doesn't erase a failure (500, |
| 86 | −removed: client = defaultClient | 102 | +added: // timeout) from a pair that might be authoritative. |
| | 103 | +added: endpoints := s.Endpoints(foundEndpoints...) |
| | 104 | +added: verification: |
| | 105 | +added: for id := range idMatches { |
| | 106 | +added: for _, baseURL := range endpoints { |
| | 107 | +added: isVerified, vErr := verifyMatch(ctx, client, baseURL, id, accessKey) |
| | 108 | +added: if vErr != nil { |
| | 109 | +added: lastErr = vErr |
| 87 | } | 110 | } |
| 88 | −removed: | | |
| 89 | −removed: isVerified, verificationErr := verifyMatch(ctx, client, apiEndpoint, accessId, accessKey) | | |
| 90 | if isVerified { | 111 | if isVerified { |
| 91 | −removed: r = createResult(accessId, accessKey, apiEndpoint, isVerified, verificationErr) | 112 | +added: verified, verifiedId, verifiedEndpoint, lastErr = true, id, baseURL, nil |
| | 113 | +added: break verification |
| 92 | } | 114 | } |
| 93 | } | 115 | } |
| 94 | } | 116 | } |
| 95 | } | 117 | } |
| 96 | | 118 | |
| 97 | −removed: if r == nil { | 119 | +added: r := createResult(rawV2Id, accessKey, rawV2URL, verified, lastErr) |
| 98 | −removed: // Only include the accessId if we're confident which one it is. | 120 | +added: // Which access ID and endpoint a key verified with are reported here |
| 99 | −removed: if len(idMatches) != 1 { | 121 | +added: // rather than in RawV2, so they don't affect the secret's identity. |
| 100 | −removed: accessId = "" | 122 | +added: // RawV2 omits the access ID when the data names several, and keys |
| 101 | −removed: } | 123 | +added: // are managed by access ID, so triage needs it from here. |
| 102 | −removed: if len(endpointMatches) != 1 || apiEndpoint == s.CloudEndpoint() { | 124 | +added: if verified { |
| 103 | −removed: apiEndpoint = "" | 125 | +added: r.ExtraData["access_id"] = verifiedId |
| 104 | −removed: } | 126 | +added: r.ExtraData["endpoint"] = verifiedEndpoint |
| 105 | −removed: r = createResult(accessId, accessKey, apiEndpoint, false, nil) | | |
| 106 | } | 127 | } |
| 107 | results = append(results, *r) | 128 | results = append(results, *r) |
| 108 | } | 129 | } |
| @@ -110,8 +131,23 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result |
| 110 | return results, nil | 131 | return results, nil |
| 111 | } | 132 | } |
| 112 | | 133 | |
| 113 | −removed: func verifyMatch(ctx context.Context, client *http.Client, endpoint string, id string, key string) (bool, error) { | 134 | +added: // soleKey returns the only key in m, or "" when m has zero or several keys. |
| 114 | −removed: req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://%s/api/v1/users", endpoint), nil) | 135 | +added: func soleKey(m map[string]struct{}) string { |
| | 136 | +added: if len(m) != 1 { |
| | 137 | +added: return "" |
| | 138 | +added: } |
| | 139 | +added: for k := range m { |
| | 140 | +added: return k |
| | 141 | +added: } |
| | 142 | +added: return "" |
| | 143 | +added: } |
| | 144 | +added: |
| | 145 | +added: func verifyMatch(ctx context.Context, client *http.Client, baseURL string, id string, key string) (bool, error) { |
| | 146 | +added: endpoint, err := url.JoinPath(baseURL, "/api/v1/users") |
| | 147 | +added: if err != nil { |
| | 148 | +added: return false, err |
| | 149 | +added: } |
| | 150 | +added: req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) |
| 115 | if err != nil { | 151 | if err != nil { |
| 116 | return false, err | 152 | return false, err |
| 117 | } | 153 | } |