github.com/opentofu/opentofu@v1.7.1/internal/encryption/method/addr.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package method 7 8 import ( 9 "fmt" 10 "regexp" 11 12 "github.com/hashicorp/hcl/v2" 13 ) 14 15 // TODO is there a generalized way to regexp-check names? 16 var addrRe = regexp.MustCompile(`^method\.([a-zA-Z_0-9-]+)\.([a-zA-Z_0-9-]+)$`) 17 var nameRe = regexp.MustCompile("^([a-zA-Z_0-9-]+)$") 18 var idRe = regexp.MustCompile("^([a-zA-Z_0-9-]+)$") 19 20 // Addr is a type-alias for method address strings that identify a specific encryption method configuration. 21 // The Addr is an opaque value. Do not perform string manipulation on it outside the functions supplied by the 22 // method package. 23 type Addr string 24 25 // Validate validates the Addr for formal naming conformance, but does not check if the referenced method actually 26 // exists in the configuration. 27 func (a Addr) Validate() hcl.Diagnostics { 28 if !addrRe.MatchString(string(a)) { 29 return hcl.Diagnostics{ 30 &hcl.Diagnostic{ 31 Severity: hcl.DiagError, 32 Summary: "Invalid encryption method address", 33 Detail: fmt.Sprintf( 34 "The supplied encryption method address does not match the required form of %s", 35 addrRe.String(), 36 ), 37 }, 38 } 39 } 40 return nil 41 } 42 43 // NewAddr creates a new Addr type from the provider and name supplied. The Addr is a type-alias for encryption method 44 // address strings that identify a specific encryption method configuration. You should treat the value as opaque and 45 // not perform string manipulation on it outside the functions supplied by the method package. 46 func NewAddr(method string, name string) (addr Addr, err hcl.Diagnostics) { 47 if !nameRe.MatchString(method) { 48 err = err.Append( 49 &hcl.Diagnostic{ 50 Severity: hcl.DiagError, 51 Summary: "The provided encryption method type is invalid", 52 Detail: fmt.Sprintf( 53 "The supplied encryption method type (%s) does not match the required form of %s.", 54 method, 55 nameRe.String(), 56 ), 57 }, 58 ) 59 } 60 if !nameRe.MatchString(name) { 61 err = err.Append( 62 &hcl.Diagnostic{ 63 Severity: hcl.DiagError, 64 Summary: "The provided encryption method name is invalid", 65 Detail: fmt.Sprintf( 66 "The supplied encryption method name (%s) does not match the required form of %s.", 67 name, 68 nameRe.String(), 69 ), 70 }, 71 ) 72 } 73 return Addr(fmt.Sprintf("method.%s.%s", method, name)), err 74 }