@@ -80,14 +80,27 @@ func TestPkiSecretBackendIntermediateCertificate_multiIssuer(t *testing.T) {
80
80
resourceName := "vault_pki_secret_backend_intermediate_cert_request.test"
81
81
keyName := acctest .RandomWithPrefix ("test-pki-key" )
82
82
83
- checks := []resource.TestCheckFunc {
83
+ // used to test existing key flow
84
+ store := & testPKIKeyStore {}
85
+ keyResourceName := "vault_pki_secret_backend_key.test"
86
+ updatedKeyName := acctest .RandomWithPrefix ("test-pki-key-updated" )
87
+
88
+ commonChecks := []resource.TestCheckFunc {
84
89
resource .TestCheckResourceAttr (resourceName , consts .FieldBackend , path ),
85
- resource .TestCheckResourceAttr (resourceName , consts .FieldType , "internal" ),
90
+ resource .TestCheckResourceAttrSet (resourceName , consts .FieldKeyID ),
86
91
resource .TestCheckResourceAttr (resourceName , consts .FieldCommonName , "test Intermediate CA" ),
92
+ }
93
+
94
+ internalChecks := append (commonChecks ,
95
+ resource .TestCheckResourceAttr (resourceName , consts .FieldType , "internal" ),
96
+ // keyName is only set on internal if it is passed by user
87
97
resource .TestCheckResourceAttr (resourceName , consts .FieldKeyName , keyName ),
88
- resource .TestCheckResourceAttrSet (resourceName , consts .FieldKeyID ),
98
+ )
99
+
100
+ existingChecks := append (commonChecks ,
101
+ resource .TestCheckResourceAttr (resourceName , consts .FieldType , "existing" ),
89
102
resource .TestCheckResourceAttrSet (resourceName , consts .FieldKeyRef ),
90
- }
103
+ )
91
104
92
105
resource .Test (t , resource.TestCase {
93
106
ProviderFactories : providerFactories ,
@@ -97,11 +110,27 @@ func TestPkiSecretBackendIntermediateCertificate_multiIssuer(t *testing.T) {
97
110
},
98
111
CheckDestroy : testCheckMountDestroyed ("vault_mount" , consts .MountTypePKI , consts .FieldPath ),
99
112
Steps : []resource.TestStep {
100
- // @TODO add a test step with a key_ref
101
113
{
102
- Config : testPkiSecretBackendIntermediateCertRequestConfig_multiIssuer (path , keyName ),
114
+ Config : testPkiSecretBackendIntermediateCertRequestConfig_multiIssuerInternal (path , keyName ),
103
115
Check : resource .ComposeTestCheckFunc (
104
- append (checks )... ,
116
+ append (internalChecks )... ,
117
+ ),
118
+ },
119
+ {
120
+ // Create and capture key ID
121
+ Config : testAccPKISecretBackendKey_basic (path , updatedKeyName , "rsa" , "2048" ),
122
+ Check : resource .ComposeTestCheckFunc (
123
+ testCapturePKIKeyID (keyResourceName , store ),
124
+ ),
125
+ },
126
+ {
127
+ Config : testPkiSecretBackendIntermediateCertRequestConfig_multiIssuerExisting (path , updatedKeyName ),
128
+ Check : resource .ComposeTestCheckFunc (
129
+ append (existingChecks ,
130
+ // confirm that root cert key ID is same as the key
131
+ // created in step 2; thereby confirming key_ref is passed
132
+ testPKIKeyUpdate (resourceName , store , true ),
133
+ )... ,
105
134
),
106
135
},
107
136
},
@@ -128,7 +157,26 @@ resource "vault_pki_secret_backend_intermediate_cert_request" "test" {
128
157
` , path , addConstraints )
129
158
}
130
159
131
- func testPkiSecretBackendIntermediateCertRequestConfig_multiIssuer (path , keyName string ) string {
160
+ func testPkiSecretBackendIntermediateCertRequestConfig_multiIssuerInternal (path , keyName string ) string {
161
+ return fmt .Sprintf (`
162
+ resource "vault_mount" "test" {
163
+ path = "%s"
164
+ type = "pki"
165
+ description = "test"
166
+ default_lease_ttl_seconds = 86400
167
+ max_lease_ttl_seconds = 86400
168
+ }
169
+
170
+ resource "vault_pki_secret_backend_intermediate_cert_request" "test" {
171
+ backend = vault_mount.test.path
172
+ type = "internal"
173
+ common_name = "test Intermediate CA"
174
+ key_name = "%s"
175
+ }
176
+ ` , path , keyName )
177
+ }
178
+
179
+ func testPkiSecretBackendIntermediateCertRequestConfig_multiIssuerExisting (path , keyName string ) string {
132
180
return fmt .Sprintf (`
133
181
resource "vault_mount" "test" {
134
182
path = "%s"
@@ -141,17 +189,16 @@ resource "vault_mount" "test" {
141
189
resource "vault_pki_secret_backend_key" "test" {
142
190
backend = vault_mount.test.path
143
191
type = "exported"
144
- key_name = "test "
192
+ key_name = "%s "
145
193
key_type = "rsa"
146
- key_bits = "4096 "
194
+ key_bits = "2048 "
147
195
}
148
196
149
197
resource "vault_pki_secret_backend_intermediate_cert_request" "test" {
150
198
backend = vault_mount.test.path
151
- type = "internal "
199
+ type = "existing "
152
200
common_name = "test Intermediate CA"
153
- key_ref = vault_pki_secret_backend_key.test.id
154
- key_name = "%s"
201
+ key_ref = vault_pki_secret_backend_key.test.key_id
155
202
}
156
203
` , path , keyName )
157
204
}
0 commit comments