Skip to content

Commit 34517b6

Browse files
committed
Fix OIDC Key Destroy test and fix key existence check
To account for hashicorp/vault#7267 which will be released with Vault 1.2.2
1 parent f2e6895 commit 34517b6

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

vault/resource_identity_oidc_key.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vault
33
import (
44
"fmt"
55
"log"
6+
"strings"
67

78
"github.com/hashicorp/terraform/helper/schema"
89
"github.com/hashicorp/vault/api"
@@ -182,11 +183,20 @@ func identityOidcKeyPath(name string) string {
182183
func identityOidcKeyApiRead(name string, client *api.Client) (map[string]interface{}, error) {
183184
path := identityOidcKeyPath(name)
184185
resp, err := client.Logical().Read(path)
186+
187+
log.Printf("[DEBUG] Read IdentityOidcKey %s", name)
188+
189+
// Vault incorrectly returns 400 for deleted key. In the meantime, we will look into
190+
// the error string to check this.
191+
// Fixed by https://github.com/hashicorp/vault/pull/7267 and slated for Vault 1.2.2
185192
if err != nil {
186-
return nil, fmt.Errorf("error reading IdentityOidcKey %s: %s", name, err)
193+
if !strings.Contains(err.Error(), "no named key found") {
194+
return nil, fmt.Errorf("error reading IdentityOidcKey %s: %s", name, err)
195+
}
196+
// Key was not found and we set `resp` to nil
197+
resp = nil
187198
}
188199

189-
log.Printf("[DEBUG] Read IdentityOidcKey %s", name)
190200
if resp == nil {
191201
log.Printf("[WARN] IdentityOidcKey %s not found", name)
192202
return nil, nil

vault/resource_identity_oidc_key_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ func testAccCheckIdentityOidcKeyDestroy(s *terraform.State) error {
8686
if rs.Type != "vault_identity_oidc_key" {
8787
continue
8888
}
89-
secret, err := client.Logical().Read(identityEntityIDPath(rs.Primary.ID))
89+
resp, err := identityOidcKeyApiRead(rs.Primary.Attributes["name"], client)
90+
9091
if err != nil {
9192
return fmt.Errorf("error checking for identity oidc key %q: %s", rs.Primary.ID, err)
9293
}
93-
if secret != nil {
94+
if resp != nil {
9495
return fmt.Errorf("identity oidc key %q still exists", rs.Primary.ID)
9596
}
9697
}
@@ -110,12 +111,11 @@ func testAccIdentityOidcKeyCheckAttrs() resource.TestCheckFunc {
110111
}
111112

112113
id := instanceState.ID
113-
114114
path := identityOidcKeyPath(id)
115115
client := testProvider.Meta().(*api.Client)
116-
resp, err := client.Logical().Read(path)
116+
resp, err := identityOidcKeyApiRead(id, client)
117117
if err != nil {
118-
return fmt.Errorf("%q doesn't exist", path)
118+
return fmt.Errorf("%q doesn't exist", id)
119119
}
120120

121121
attrs := map[string]string{
@@ -125,36 +125,36 @@ func testAccIdentityOidcKeyCheckAttrs() resource.TestCheckFunc {
125125
"allowed_client_ids": "allowed_client_ids",
126126
}
127127
for stateAttr, apiAttr := range attrs {
128-
if resp.Data[apiAttr] == nil && instanceState.Attributes[stateAttr] == "" {
128+
if resp[apiAttr] == nil && instanceState.Attributes[stateAttr] == "" {
129129
continue
130130
}
131131
var match bool
132-
switch resp.Data[apiAttr].(type) {
132+
switch resp[apiAttr].(type) {
133133
case json.Number:
134-
apiData, err := resp.Data[apiAttr].(json.Number).Int64()
134+
apiData, err := resp[apiAttr].(json.Number).Int64()
135135
if err != nil {
136-
return fmt.Errorf("expected API field %s to be an int, was %q", apiAttr, resp.Data[apiAttr])
136+
return fmt.Errorf("expected API field %s to be an int, was %q", apiAttr, resp[apiAttr])
137137
}
138138
stateData, err := strconv.ParseInt(instanceState.Attributes[stateAttr], 10, 64)
139139
if err != nil {
140140
return fmt.Errorf("expected state field %s to be an int, was %q", stateAttr, instanceState.Attributes[stateAttr])
141141
}
142142
match = apiData == stateData
143143
case bool:
144-
if _, ok := resp.Data[apiAttr]; !ok && instanceState.Attributes[stateAttr] == "" {
144+
if _, ok := resp[apiAttr]; !ok && instanceState.Attributes[stateAttr] == "" {
145145
match = true
146146
} else {
147147
stateData, err := strconv.ParseBool(instanceState.Attributes[stateAttr])
148148
if err != nil {
149149
return fmt.Errorf("expected state field %s to be a bool, was %q", stateAttr, instanceState.Attributes[stateAttr])
150150
}
151-
match = resp.Data[apiAttr] == stateData
151+
match = resp[apiAttr] == stateData
152152
}
153153
case []interface{}:
154-
apiData := resp.Data[apiAttr].([]interface{})
154+
apiData := resp[apiAttr].([]interface{})
155155
length := instanceState.Attributes[stateAttr+".#"]
156156
if length == "" {
157-
if len(resp.Data[apiAttr].([]interface{})) != 0 {
157+
if len(resp[apiAttr].([]interface{})) != 0 {
158158
return fmt.Errorf("expected state field %s to have %d entries, had 0", stateAttr, len(apiData))
159159
}
160160
match = true
@@ -184,10 +184,10 @@ func testAccIdentityOidcKeyCheckAttrs() resource.TestCheckFunc {
184184
match = true
185185
}
186186
default:
187-
match = resp.Data[apiAttr] == instanceState.Attributes[stateAttr]
187+
match = resp[apiAttr] == instanceState.Attributes[stateAttr]
188188
}
189189
if !match {
190-
return fmt.Errorf("expected %s (%s in state) of %q to be %q, got %q", apiAttr, stateAttr, path, instanceState.Attributes[stateAttr], resp.Data[apiAttr])
190+
return fmt.Errorf("expected %s (%s in state) of %q to be %q, got %q", apiAttr, stateAttr, path, instanceState.Attributes[stateAttr], resp[apiAttr])
191191
}
192192
}
193193
return nil

0 commit comments

Comments
 (0)