github.com/opentofu/opentofu@v1.7.1/internal/encryption/registry/compliancetest/compliance_key_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 compliancetest 7 8 import ( 9 "errors" 10 "testing" 11 12 "github.com/opentofu/opentofu/internal/encryption/keyprovider" 13 "github.com/opentofu/opentofu/internal/encryption/registry" 14 ) 15 16 func complianceTestKeyProviders(t *testing.T, factory func() registry.Registry) { 17 t.Run("registration-and-return", func(t *testing.T) { 18 complianceTestKeyProviderRegistrationAndReturn(t, factory) 19 }) 20 t.Run("register-invalid-id", func(t *testing.T) { 21 complianceTestKeyProviderInvalidID(t, factory) 22 }) 23 t.Run("duplicate-registration", func(t *testing.T) { 24 complianceTestKeyProviderDuplicateRegistration(t, factory) 25 }) 26 } 27 28 func complianceTestKeyProviderRegistrationAndReturn(t *testing.T, factory func() registry.Registry) { 29 reg := factory() 30 testKeyProvider := &testKeyProviderDescriptor{ 31 "test", 32 } 33 if err := reg.RegisterKeyProvider(testKeyProvider); err != nil { 34 t.Fatalf("Failed to register test key provider with ID %s (%v)", testKeyProvider.id, err) 35 } 36 returnedKeyProvider, err := reg.GetKeyProviderDescriptor(testKeyProvider.id) 37 if err != nil { 38 t.Fatalf("The previously registered key provider with the ID %s couldn't be fetched from the registry (%v).", testKeyProvider.id, err) 39 } 40 returnedTypedKeyProvider, ok := returnedKeyProvider.(*testKeyProviderDescriptor) 41 if !ok { 42 t.Fatalf("The returned key provider was not of the expected type of %T, but instead it was %T.", testKeyProvider, returnedKeyProvider) 43 } 44 if returnedTypedKeyProvider.id != testKeyProvider.id { 45 t.Fatalf("The returned key provider contained the wrong ID %s instead of %s", returnedTypedKeyProvider.id, testKeyProvider.id) 46 } 47 48 _, err = reg.GetKeyProviderDescriptor("nonexistent") 49 if err == nil { 50 t.Fatalf("Requesting a non-existent key provider from GetKeyProviderDescriptor did not return an error.") 51 } 52 var typedErr *registry.KeyProviderNotFoundError 53 if !errors.As(err, &typedErr) { 54 t.Fatalf( 55 "Requesting a non-existent key provider from GetKeyProviderDescriptor returned an incorrect error type of %T. This function should always return a *registry.KeyProviderNotFoundError if the key provider was not found.", 56 err, 57 ) 58 } 59 } 60 61 func complianceTestKeyProviderInvalidID(t *testing.T, factory func() registry.Registry) { 62 reg := factory() 63 testKeyProvider := &testKeyProviderDescriptor{ 64 "Hello world!", 65 } 66 err := reg.RegisterKeyProvider(testKeyProvider) 67 if err == nil { 68 t.Fatalf("Registering a key provider with the invalid ID of %s did not result in an error.", testKeyProvider.id) 69 } 70 var typedErr *registry.InvalidKeyProviderError 71 if !errors.As(err, &typedErr) { 72 t.Fatalf( 73 "Registering a key provider with an invalid ID of %s resulted in an error of type %T instead of %T. Please make sure to use the correct typed errors.", 74 testKeyProvider.id, 75 err, 76 typedErr, 77 ) 78 } 79 } 80 81 func complianceTestKeyProviderDuplicateRegistration(t *testing.T, factory func() registry.Registry) { 82 reg := factory() 83 testKeyProvider := &testKeyProviderDescriptor{ 84 "test", 85 } 86 testKeyProvider2 := &testKeyProviderDescriptor{ 87 "test", 88 } 89 if err := reg.RegisterKeyProvider(testKeyProvider); err != nil { 90 t.Fatalf("Failed to register test key provider with ID %s (%v)", testKeyProvider.id, err) 91 } 92 err := reg.RegisterKeyProvider(testKeyProvider) 93 if err == nil { 94 t.Fatalf("Re-registering the same key provider again did not result in an error.") 95 } 96 var typedErr *registry.KeyProviderAlreadyRegisteredError 97 if !errors.As(err, &typedErr) { 98 t.Fatalf( 99 "Re-registering the same key provider twice resulted in an error of the type %T instead of %T. Please make sure to use the correct typed errors.", 100 err, 101 typedErr, 102 ) 103 } 104 105 err = reg.RegisterKeyProvider(testKeyProvider2) 106 if err == nil { 107 t.Fatalf("Re-registering the a provider with a duplicate ID did not result in an error.") 108 } 109 if !errors.As(err, &typedErr) { 110 t.Fatalf( 111 "Re-registering the a key provider with a duplicate ID resulted in an error of the type %T instead of %T. Please make sure to use the correct typed errors.", 112 err, 113 typedErr, 114 ) 115 } 116 } 117 118 type testKeyProviderDescriptor struct { 119 id keyprovider.ID 120 } 121 122 func (t testKeyProviderDescriptor) ID() keyprovider.ID { 123 return t.id 124 } 125 126 func (t testKeyProviderDescriptor) ConfigStruct() keyprovider.Config { 127 return &testKeyProviderConfigStruct{} 128 } 129 130 type testKeyProviderConfigStruct struct { 131 } 132 133 func (t testKeyProviderConfigStruct) Build() (keyprovider.KeyProvider, keyprovider.KeyMeta, error) { 134 return nil, nil, keyprovider.ErrInvalidConfiguration{ 135 Message: "The Build() function is not implemented on the testKeyProviderConfigStruct", 136 } 137 }