github.com/google/osv-scalibr@v0.4.1/veles/secrets/gcshmackey/detector.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gcshmackey
    16  
    17  import (
    18  	"regexp"
    19  
    20  	"github.com/google/osv-scalibr/veles"
    21  	"github.com/google/osv-scalibr/veles/secrets/common/pair"
    22  )
    23  
    24  var (
    25  	// ref: https://cloud.google.com/storage/docs/authentication/hmackeys#overview
    26  	accessIDPattern = regexp.MustCompile(`GOOG(?:[A-Z0-9]{57}|[A-Z0-9]{20})`)
    27  	secretPattern   = regexp.MustCompile(`[A-Za-z0-9+/]{40}`)
    28  )
    29  
    30  const (
    31  	maxAccessIDLen = 61
    32  	maxSecretLen   = 40
    33  	// maxDistance is the maximum distance between AccessID and secrets to be considered for pairing.
    34  	// 10 KiB is a good upper bound as we don't expect files containing credentials to be larger than this.
    35  	maxDistance = 10 * 1 << 10 // 10 KiB
    36  )
    37  
    38  // NewDetector returns a new Veles Detector that finds Google Cloud Storage HMAC keys
    39  func NewDetector() veles.Detector {
    40  	return &pair.Detector{
    41  		MaxElementLen: max(maxAccessIDLen, maxSecretLen), MaxDistance: uint32(maxDistance),
    42  		FindA: pair.FindAllMatches(accessIDPattern),
    43  		FindB: pair.FindAllMatches(secretPattern),
    44  		FromPair: func(p pair.Pair) (veles.Secret, bool) {
    45  			return HMACKey{AccessID: string(p.A.Value), Secret: string(p.B.Value)}, true
    46  		},
    47  	}
    48  }