1 // Copyright 2022 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 testvariable 16 17 import ( 18 "crypto/rand" 19 "encoding/base64" 20 "fmt" 21 "log" 22 "regexp" 23 "strings" 24 25 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/randomid" 26 ) 27 28 func NewUniqueId() string { 29 var id string 30 for i := 0; i < 3; i++ { 31 // generate a globally unique ID 32 id = randomid.New().String() 33 if isValidId(id) { 34 return id 35 } 36 } 37 panic(fmt.Sprintf("unable to generate a valid id, last attempt resulted in value: %v", id)) 38 } 39 40 func isValidId(id string) bool { 41 // ssl and google are reserved words and cannot be used in project ids, a valid lookbehind regex would be 42 // "((?!ssl|google).*)", but golang does not support lookbehinds. For that reason, just throw out any id 43 // that contains these reserved words 44 return !strings.Contains(id, "google") && !strings.Contains(id, "ssl") 45 } 46 47 // RandomIdGenerator generates a randomized string of a given length and 48 // contains only the characters defined in the given regex 49 // the IDs returned by this are NOT globally unique so this should only be used in locations where there cannot be 50 // collisions across tests 51 func RandomIdGenerator(reg *regexp.Regexp, length uint) string { 52 var stringBuffer strings.Builder 53 54 // Keep generating and appending randomized characters until the length is hit 55 for uint(len(stringBuffer.String())) < length { 56 b := make([]byte, length*5) // Add more to account for filtering out unwanted chars 57 _, err := rand.Read(b) 58 if err != nil { 59 log.Fatalf("Could not read random bytes: %v", err) 60 } 61 62 s := base64.URLEncoding.EncodeToString(b) 63 allowedChars := reg.FindAllString(s, -1) 64 stringBuffer.WriteString(strings.Join(allowedChars, "")) 65 } 66 return stringBuffer.String()[0:length] 67 } 68