What this change means
Atlas added kong to its detector catalog in this update.
| @@ -0,0 +1,109 @@ | ||
| 1 | added: package kong | |
| 2 | added: | |
| 3 | added: import ( | |
| 4 | added: "context" | |
| 5 | added: "fmt" | |
| 6 | added: "io" | |
| 7 | added: "net/http" | |
| 8 | added: | |
| 9 | added: regexp "github.com/wasilibs/go-re2" | |
| 10 | added: | |
| 11 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/common" | |
| 12 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | |
| 13 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb" | |
| 14 | added: ) | |
| 15 | added: | |
| 16 | added: type Scanner struct { | |
| 17 | added: client *http.Client | |
| 18 | added: } | |
| 19 | added: | |
| 20 | added: // Ensure the Scanner satisfies the interface at compile time. | |
| 21 | added: var _ detectors.Detector = (*Scanner)(nil) | |
| 22 | added: | |
| 23 | added: const verifyURL = "https://global.api.konghq.com/v3/organizations/me" | |
| 24 | added: | |
| 25 | added: var ( | |
| 26 | added: defaultClient = common.SaneHttpClient() | |
| 27 | added: keyPat = regexp.MustCompile(`\b((?:kpat_|spat_)[a-zA-Z0-9]{49})\b`) | |
| 28 | added: ) | |
| 29 | added: | |
| 30 | added: // Keywords are used for efficiently pre-filtering chunks. | |
| 31 | added: // Use identifiers in the secret preferably, or the provider name. | |
| 32 | added: func (s Scanner) Keywords() []string { | |
| 33 | added: return []string{"kpat_", "spat_"} | |
| 34 | added: } | |
| 35 | added: | |
| 36 | added: // FromData will find and optionally verify Kong secrets in a given set of bytes. | |
| 37 | added: func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | |
| 38 | added: dataStr := string(data) | |
| 39 | added: | |
| 40 | added: uniqueMatches := make(map[string]struct{}) | |
| 41 | added: for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) { | |
| 42 | added: uniqueMatches[match[1]] = struct{}{} | |
| 43 | added: } | |
| 44 | added: | |
| 45 | added: for match := range uniqueMatches { | |
| 46 | added: s1 := detectors.Result{ | |
| 47 | added: DetectorType: detector_typepb.DetectorType_KongKonnect, | |
| 48 | added: Raw: []byte(match), | |
| 49 | added: SecretParts: map[string]string{"key": match}, | |
| 50 | added: } | |
| 51 | added: | |
| 52 | added: if verify { | |
| 53 | added: client := s.client | |
| 54 | added: if client == nil { | |
| 55 | added: client = defaultClient | |
| 56 | added: } | |
| 57 | added: | |
| 58 | added: isVerified, extraData, verificationErr := verifyMatch(ctx, client, match) | |
| 59 | added: s1.Verified = isVerified | |
| 60 | added: s1.ExtraData = extraData | |
| 61 | added: s1.SetVerificationError(verificationErr, match) | |
| 62 | added: } | |
| 63 | added: | |
| 64 | added: results = append(results, s1) | |
| 65 | added: } | |
| 66 | added: | |
| 67 | added: return | |
| 68 | added: } | |
| 69 | added: | |
| 70 | added: func verifyMatch(ctx context.Context, client *http.Client, token string) (bool, map[string]string, error) { | |
| 71 | added: req, err := http.NewRequestWithContext(ctx, http.MethodGet, verifyURL, nil) | |
| 72 | added: if err != nil { | |
| 73 | added: return false, nil, err | |
| 74 | added: } | |
| 75 | added: | |
| 76 | added: req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | |
| 77 | added: | |
| 78 | added: res, err := client.Do(req) | |
| 79 | added: if err != nil { | |
| 80 | added: return false, nil, err | |
| 81 | added: } | |
| 82 | added: defer func() { | |
| 83 | added: _, _ = io.Copy(io.Discard, res.Body) | |
| 84 | added: _ = res.Body.Close() | |
| 85 | added: }() | |
| 86 | added: | |
| 87 | added: extraData := map[string]string{ | |
| 88 | added: "rotation_guide": "https://developer.konghq.com/konnect-api/#konnect-api-authentication", | |
| 89 | added: } | |
| 90 | added: | |
| 91 | added: switch res.StatusCode { | |
| 92 | added: case http.StatusOK: | |
| 93 | added: // If the endpoint returns useful information, we can return it as a map. | |
| 94 | added: return true, extraData, nil | |
| 95 | added: case http.StatusUnauthorized: | |
| 96 | added: // The secret is determinately not verified (nothing to do) | |
| 97 | added: return false, nil, nil | |
| 98 | added: default: | |
| 99 | added: return false, nil, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | |
| 100 | added: } | |
| 101 | added: } | |
| 102 | added: | |
| 103 | added: func (s Scanner) Type() detector_typepb.DetectorType { | |
| 104 | added: return detector_typepb.DetectorType_KongKonnect | |
| 105 | added: } | |
| 106 | added: | |
| 107 | added: func (s Scanner) Description() string { | |
| 108 | added: return "Kong Konnect is a API and AI management platform, and Konnect tokens (like personal or system access tokens) are used to securely authenticate developers, scripts, and CI/CD pipelines to manage and automate gateway configurations programmatically" | |
| 109 | added: } | |