github.com/opentofu/opentofu@v1.7.1/internal/encryption/keyprovider/static/provider.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 static contains a key provider that emits a static key. 7 package static 8 9 import ( 10 "fmt" 11 12 "github.com/opentofu/opentofu/internal/encryption/keyprovider" 13 ) 14 15 type staticKeyProvider struct { 16 key []byte 17 } 18 19 const magic = "Hello world!" 20 21 func (p staticKeyProvider) Provide(meta keyprovider.KeyMeta) (keyprovider.Output, keyprovider.KeyMeta, error) { 22 // Note: this is a demonstration how you can handle metadata. Using a magic string does not make any sense, 23 // but it illustrates well how you can store and retrieve metadata. We wish we could use generics to 24 // save you the trouble of doing a type assertion, but Go does not have sufficiently advanced enough generics 25 // to do that. 26 if meta == nil { 27 return keyprovider.Output{}, nil, &keyprovider.ErrInvalidMetadata{ 28 Message: "bug: nil provided as metadata", 29 } 30 } 31 typedMeta, ok := meta.(*Metadata) 32 if !ok { 33 return keyprovider.Output{}, nil, &keyprovider.ErrInvalidMetadata{ 34 Message: fmt.Sprintf("bug: invalid metadata type received: %T", meta), 35 } 36 } 37 // Note: the Magic may be empty if OpenTofu isn't decrypting anything, make sure to account for that possibility. 38 var decryptionKey []byte 39 if typedMeta.Magic != "" { 40 decryptionKey = p.key 41 if typedMeta.Magic != magic { 42 return keyprovider.Output{}, nil, &keyprovider.ErrInvalidMetadata{ 43 Message: fmt.Sprintf("corrupted data received, no or invalid magic string: %s", typedMeta.Magic), 44 } 45 } 46 } 47 48 return keyprovider.Output{ 49 EncryptionKey: p.key, 50 DecryptionKey: decryptionKey, 51 }, &Metadata{Magic: magic}, nil 52 }