github.com/google/osv-scalibr@v0.4.1/veles/secrets/grokxaiapikey/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  // Copyright 2025 Google LLC
    16  //
    17  // Licensed under the Apache License, Version 2.0 (the "License");
    18  // you may not use this file except in compliance with the License.
    19  // You may obtain a copy of the License at
    20  //
    21  //      http://www.apache.org/licenses/LICENSE-2.0
    22  //
    23  // Unless required by applicable law or agreed to in writing,
    24  // distributed under the License is distributed on an "AS IS" BASIS,
    25  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    26  // See the License for the specific language governing permissions and
    27  // limitations under the License.
    28  
    29  package grokxaiapikey
    30  
    31  import (
    32  	"regexp"
    33  
    34  	"github.com/google/osv-scalibr/veles"
    35  	"github.com/google/osv-scalibr/veles/secrets/common/simpletoken"
    36  )
    37  
    38  var (
    39  	// Ensure constructors satisfy the interface at compile time.
    40  	_ veles.Detector = NewAPIKeyDetector()
    41  	_ veles.Detector = NewManagementKeyDetector()
    42  )
    43  
    44  // API key: prefix `xai-` followed by 80 alphanumeric characters.
    45  const apiKeyMaxLen = 84
    46  
    47  var apiKeyRe = regexp.MustCompile(`xai-[A-Za-z0-9]{80}`)
    48  
    49  // Management key: prefix `xai-token-` followed by 80 alphanumeric characters.
    50  const mgmtKeyMaxLen = 90
    51  
    52  var mgmtKeyRe = regexp.MustCompile(`xai-token-[A-Za-z0-9]{80}`)
    53  
    54  // NewAPIKeyDetector returns a detector for standard xAI API keys (xai-...).
    55  func NewAPIKeyDetector() veles.Detector {
    56  	return simpletoken.Detector{
    57  		MaxLen: apiKeyMaxLen,
    58  		Re:     apiKeyRe,
    59  		FromMatch: func(b []byte) (veles.Secret, bool) {
    60  			return GrokXAIAPIKey{Key: string(b)}, true
    61  		},
    62  	}
    63  }
    64  
    65  // NewManagementKeyDetector returns a detector for xAI management keys (xai-token-...).
    66  func NewManagementKeyDetector() veles.Detector {
    67  	return simpletoken.Detector{
    68  		MaxLen: mgmtKeyMaxLen,
    69  		Re:     mgmtKeyRe,
    70  		FromMatch: func(b []byte) (veles.Secret, bool) {
    71  			return GrokXAIManagementKey{Key: string(b)}, true
    72  		},
    73  	}
    74  }