github.com/opentofu/opentofu@v1.7.1/internal/encryption/keyprovider/compliancetest/configuration.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 compliancetest 7 8 import ( 9 "github.com/opentofu/opentofu/internal/encryption/keyprovider" 10 ) 11 12 type TestConfiguration[TDescriptor keyprovider.Descriptor, TConfig keyprovider.Config, TMeta any, TKeyProvider keyprovider.KeyProvider] struct { 13 // Descriptor is the descriptor for the key provider. 14 Descriptor TDescriptor 15 16 // HCLParseTestCases contains the test cases of parsing HCL configuration and then validating it using the Build() 17 // function. 18 HCLParseTestCases map[string]HCLParseTestCase[TConfig, TKeyProvider] 19 20 // ConfigStructT validates that a certain config results or does not result in a valid Build() call. 21 ConfigStructTestCases map[string]ConfigStructTestCase[TConfig, TKeyProvider] 22 23 // MetadataStructTestCases test various metadata values for correct handling. 24 MetadataStructTestCases map[string]MetadataStructTestCase[TConfig, TMeta] 25 26 // ProvideTestCase exercises the entire chain and generates two keys. 27 ProvideTestCase ProvideTestCase[TConfig, TMeta] 28 } 29 30 // HCLParseTestCase contains a test case that parses HCL into a configuration. 31 type HCLParseTestCase[TConfig keyprovider.Config, TKeyProvider keyprovider.KeyProvider] struct { 32 // HCL contains the code that should be parsed into the configuration structure. 33 HCL string 34 // ValidHCL indicates that the HCL block should be parsable into the configuration structure, but not necessarily 35 // result in a valid Build() call. 36 ValidHCL bool 37 // ValidBuild indicates that calling the Build() function should not result in an error. 38 ValidBuild bool 39 // Validate is an extra optional validation function that can check if the configuration contains the correct 40 // values parsed from HCL. If ValidBuild is true, the key provider will be passed as well. 41 Validate func(config TConfig, keyProvider TKeyProvider) error 42 } 43 44 // ConfigStructTestCase validates that the config struct is behaving correctly when Build() is called. 45 type ConfigStructTestCase[TConfig keyprovider.Config, TKeyProvider keyprovider.KeyProvider] struct { 46 Config TConfig 47 ValidBuild bool 48 Validate func(keyProvider TKeyProvider) error 49 } 50 51 // MetadataStructTestCase is a test case for metadata. 52 type MetadataStructTestCase[TConfig keyprovider.Config, TMeta any] struct { 53 // Config contains a valid configuration that should be used to construct the key provider. 54 ValidConfig TConfig 55 // Meta contains the metadata for this test case. 56 Meta TMeta 57 // IsPresent indicates that the supplied metadata in Meta should be treated as present and the Provide() function 58 // should either return an error or a decryption key. If IsPresent is false, the Provide() function must not 59 // return an error or a decryption key. 60 IsPresent bool 61 // IsValid indicates that, if IsPresent is true, the metadata should be valid and the Provide() function should not 62 // exit with a *keyprovider.ErrInvalidMetadata error. 63 IsValid bool 64 } 65 66 // ProvideTestCase provides a test configuration Provide() test where a key is requested and then 67 // subsequently compared. 68 type ProvideTestCase[TConfig keyprovider.Config, TMeta any] struct { 69 // ValidConfig is a valid configuration that the integration test can use to generate keys. 70 ValidConfig TConfig 71 // ExpectedOutput indicates what keys are expected as an output when the integration test is ran with full metadata. 72 ExpectedOutput *keyprovider.Output 73 // ValidateKeys is a function that compares an encryption and a decryption key. The function should return an error 74 // if the two keys don't belong together. If you do not provide this function, bytes.Equal will be used. 75 ValidateKeys func(decryptionKey []byte, encryptionKey []byte) error 76 // ValidateMetadata is a function that validates that the resulting metadata is correct. 77 ValidateMetadata func(meta TMeta) error 78 }