What this change means
Atlas added lambdacloud to its detector catalog in this update.
| @@ -0,0 +1,126 @@ | ||
| 1 | added: package lambdacloud | |
| 2 | added: | |
| 3 | added: import ( | |
| 4 | added: "context" | |
| 5 | added: "encoding/json" | |
| 6 | added: "fmt" | |
| 7 | added: "io" | |
| 8 | added: "net/http" | |
| 9 | added: | |
| 10 | added: regexp "github.com/wasilibs/go-re2" | |
| 11 | added: | |
| 12 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/common" | |
| 13 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | |
| 14 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb" | |
| 15 | added: ) | |
| 16 | added: | |
| 17 | added: const verifyURL = "https://cloud.lambda.ai/api/v1/identity" | |
| 18 | added: | |
| 19 | added: type Scanner struct { | |
| 20 | added: client *http.Client | |
| 21 | added: } | |
| 22 | added: | |
| 23 | added: var _ detectors.Detector = (*Scanner)(nil) | |
| 24 | added: | |
| 25 | added: var ( | |
| 26 | added: defaultClient = common.SaneHttpClient() | |
| 27 | added: | |
| 28 | added: keyPat = regexp.MustCompile(`\b(secret_[a-z0-9][a-z0-9-]{0,63}_[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}\.[A-Za-z0-9]{32})(?:[^A-Za-z0-9]|$)`) | |
| 29 | added: ) | |
| 30 | added: | |
| 31 | added: func (s Scanner) Keywords() []string { | |
| 32 | added: return []string{"secret_"} | |
| 33 | added: } | |
| 34 | added: | |
| 35 | added: func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) ([]detectors.Result, error) { | |
| 36 | added: var results []detectors.Result | |
| 37 | added: seen := make(map[string]struct{}) | |
| 38 | added: for _, match := range keyPat.FindAllSubmatchIndex(data, -1) { | |
| 39 | added: token := string(data[match[2]:match[3]]) | |
| 40 | added: if _, ok := seen[token]; ok { | |
| 41 | added: continue | |
| 42 | added: } | |
| 43 | added: seen[token] = struct{}{} | |
| 44 | added: | |
| 45 | added: result := detectors.Result{ | |
| 46 | added: DetectorType: s.Type(), | |
| 47 | added: Raw: []byte(token), | |
| 48 | added: SecretParts: map[string]string{"key": token}, | |
| 49 | added: } | |
| 50 | added: | |
| 51 | added: if verify { | |
| 52 | added: client := s.client | |
| 53 | added: if client == nil { | |
| 54 | added: client = defaultClient | |
| 55 | added: } | |
| 56 | added: verified, extraData, verificationErr := verifyToken(ctx, client, token) | |
| 57 | added: result.Verified = verified | |
| 58 | added: result.ExtraData = extraData | |
| 59 | added: result.SetVerificationError(verificationErr, token) | |
| 60 | added: } | |
| 61 | added: results = append(results, result) | |
| 62 | added: } | |
| 63 | added: return results, nil | |
| 64 | added: } | |
| 65 | added: | |
| 66 | added: func verifyToken(ctx context.Context, client *http.Client, token string) (bool, map[string]string, error) { | |
| 67 | added: req, err := http.NewRequestWithContext(ctx, http.MethodGet, verifyURL, nil) | |
| 68 | added: if err != nil { | |
| 69 | added: return false, nil, err | |
| 70 | added: } | |
| 71 | added: req.Header.Set("Authorization", "Bearer "+token) | |
| 72 | added: req.Header.Set("Accept", "application/json") | |
| 73 | added: | |
| 74 | added: res, err := client.Do(req) | |
| 75 | added: if err != nil { | |
| 76 | added: return false, nil, err | |
| 77 | added: } | |
| 78 | added: defer func() { | |
| 79 | added: _, _ = io.Copy(io.Discard, res.Body) | |
| 80 | added: _ = res.Body.Close() | |
| 81 | added: }() | |
| 82 | added: | |
| 83 | added: switch res.StatusCode { | |
| 84 | added: case http.StatusOK: | |
| 85 | added: var response struct { | |
| 86 | added: Data identityResponse `json:"data"` | |
| 87 | added: } | |
| 88 | added: if err := json.NewDecoder(res.Body).Decode(&response); err != nil { | |
| 89 | added: return false, nil, err | |
| 90 | added: } | |
| 91 | added: if response.Data.ID == "" || response.Data.AccountID == "" { | |
| 92 | added: return false, nil, fmt.Errorf("unexpected Lambda Cloud identity response") | |
| 93 | added: } | |
| 94 | added: | |
| 95 | added: extraData := map[string]string{ | |
| 96 | added: "identity_id": response.Data.ID, | |
| 97 | added: "account_id": response.Data.AccountID, | |
| 98 | added: } | |
| 99 | added: if response.Data.IdentityType != "" { | |
| 100 | added: extraData["identity_type"] = response.Data.IdentityType | |
| 101 | added: } | |
| 102 | added: if response.Data.Email != "" { | |
| 103 | added: extraData["email"] = response.Data.Email | |
| 104 | added: } | |
| 105 | added: return true, extraData, nil | |
| 106 | added: case http.StatusUnauthorized: | |
| 107 | added: return false, nil, nil | |
| 108 | added: default: | |
| 109 | added: return false, nil, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | |
| 110 | added: } | |
| 111 | added: } | |
| 112 | added: | |
| 113 | added: func (s Scanner) Type() detector_typepb.DetectorType { | |
| 114 | added: return detector_typepb.DetectorType_LambdaCloud | |
| 115 | added: } | |
| 116 | added: | |
| 117 | added: func (s Scanner) Description() string { | |
| 118 | added: return "Lambda Cloud provides GPU computing infrastructure. Lambda Cloud API keys can be used to manage instances, filesystems, SSH keys, and other cloud resources." | |
| 119 | added: } | |
| 120 | added: | |
| 121 | added: type identityResponse struct { | |
| 122 | added: ID string `json:"id"` | |
| 123 | added: AccountID string `json:"account_id"` | |
| 124 | added: IdentityType string `json:"identity_type"` | |
| 125 | added: Email string `json:"email"` | |
| 126 | added: } | |