github.com/google/osv-scalibr@v0.4.1/veles/secrets/awsaccesskey/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 awsaccesskey
    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  	accessIDPattern = regexp.MustCompile(`\b(AIKA[A-Z0-9]{16}|ASIA[A-Z0-9]{16})\b`)
    26  	secretPattern   = regexp.MustCompile(`\b[A-Za-z0-9+/]{40}\b`)
    27  )
    28  
    29  const (
    30  	maxAccessIDLen = 124
    31  	maxSecretLen   = 40
    32  	// maxDistance is the maximum distance between AccessID and secrets to be considered for pairing.
    33  	// 10 KiB is a good upper bound as we don't expect files containing credentials to be larger than this.
    34  	maxDistance = 10 * 1 << 10 // 10 KiB
    35  )
    36  
    37  // NewDetector returns a new Veles Detector that finds Google Cloud Storage HMAC keys
    38  func NewDetector() veles.Detector {
    39  	return &pair.Detector{
    40  		MaxElementLen: max(maxAccessIDLen, maxSecretLen), MaxDistance: uint32(maxDistance),
    41  		FindA: pair.FindAllMatches(accessIDPattern),
    42  		FindB: pair.FindAllMatches(secretPattern),
    43  		FromPair: func(p pair.Pair) (veles.Secret, bool) {
    44  			return Credentials{AccessID: string(p.A.Value), Secret: string(p.B.Value)}, true
    45  		},
    46  	}
    47  }