Skip to content

Commit f217344

Browse files
fix: api key read errors out when getting keys for users (#171)
## What Fix codefresh_api_key resource - failing with error because of an API behaviour change ## Why When refreshing state that includes an API key the following error appeared <img width="671" height="152" alt="image" src="https://github.com/user-attachments/assets/ddf726fd-1616-48b7-9d11-13caed9e4a4b" /> This is due to the fact the the token request was run without impersonation. Previously this used to return the proper token, but this is not the correct behavior. Because tokens are user and account scoped a switched must be made to the correct user before reading the token. This PR fixes this on the provider and does the switch the correct user before reading the token. ## Notes <!-- Add any notes here --> ## Checklist * [ ] _I have read [CONTRIBUTING.md](https://github.com/codefresh-io/terraform-provider-codefresh/blob/master/CONTRIBUTING.md)._ * [ ] _I have [allowed changes to my fork to be made](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)._ * [ ] _I have added tests, assuming new tests are warranted_. * [ ] _I understand that the `/test` comment will be ignored by the CI trigger [unless it is made by a repo admin or collaborator](https://codefresh.io/docs/docs/pipelines/triggers/git-triggers/#support-for-building-pull-requests-from-forks)._
1 parent 92fb1c3 commit f217344

File tree

2 files changed

+48
-53
lines changed

2 files changed

+48
-53
lines changed

codefresh/cfclient/api_key.go

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"log"
7-
8-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
97
)
108

119
type ApiKeySubject struct {
@@ -38,14 +36,21 @@ type TokenResponse struct {
3836
} `json:"user"`
3937
}
4038

41-
func (client *Client) GetAPIKey(keyID string) (*ApiKey, error) {
39+
func (client *Client) GetAPIKey(userID string, accountId string, keyID string) (*ApiKey, error) {
40+
41+
xAccessToken, err := client.GetXAccessToken(userID, accountId)
42+
43+
if err != nil {
44+
return nil, err
45+
}
4246

4347
opts := RequestOptions{
44-
Path: fmt.Sprintf("/auth/key/%s", keyID),
45-
Method: "GET",
48+
Path: fmt.Sprintf("/auth/key/%s", keyID),
49+
XAccessToken: xAccessToken,
50+
Method: "GET",
4651
}
4752

48-
resp, err := client.RequestAPI(&opts)
53+
resp, err := client.RequestApiXAccessToken(&opts)
4954

5055
if err != nil {
5156
return nil, err
@@ -61,14 +66,21 @@ func (client *Client) GetAPIKey(keyID string) (*ApiKey, error) {
6166
return &apiKey, nil
6267
}
6368

64-
func (client *Client) DeleteAPIKey(keyID string) error {
69+
func (client *Client) DeleteAPIKey(userID string, accountId string, keyID string) error {
70+
// login as user
71+
72+
xAccessToken, err := client.GetXAccessToken(userID, accountId)
6573

74+
if err != nil {
75+
return err
76+
}
6677
opts := RequestOptions{
67-
Path: fmt.Sprintf("/auth/key/%s", keyID),
68-
Method: "DELETE",
78+
Path: fmt.Sprintf("/auth/key/%s", keyID),
79+
Method: "DELETE",
80+
XAccessToken: xAccessToken,
6981
}
7082

71-
resp, err := client.RequestAPI(&opts)
83+
resp, err := client.RequestApiXAccessToken(&opts)
7284
if err != nil {
7385
fmt.Println(string(resp))
7486
return err
@@ -77,7 +89,7 @@ func (client *Client) DeleteAPIKey(keyID string) error {
7789
return nil
7890
}
7991

80-
func (client *Client) UpdateAPIKey(key *ApiKey) error {
92+
func (client *Client) UpdateAPIKey(userID string, accountId string, key *ApiKey) error {
8193

8294
keyID := key.ID
8395
if keyID == "" {
@@ -89,13 +101,23 @@ func (client *Client) UpdateAPIKey(key *ApiKey) error {
89101
return err
90102
}
91103

104+
var xAccessToken string
105+
106+
// login as user
107+
xAccessToken, err = client.GetXAccessToken(userID, accountId)
108+
109+
if err != nil {
110+
return err
111+
}
112+
92113
opts := RequestOptions{
93-
Path: fmt.Sprintf("/auth/key/%s", keyID),
94-
Method: "PATCH",
95-
Body: body,
114+
Path: fmt.Sprintf("/auth/key/%s", keyID),
115+
Method: "PATCH",
116+
XAccessToken: xAccessToken,
117+
Body: body,
96118
}
97119

98-
resp, err := client.RequestAPI(&opts)
120+
resp, err := client.RequestApiXAccessToken(&opts)
99121

100122
if err != nil {
101123
fmt.Println(string(resp))
@@ -110,6 +132,7 @@ func (client *Client) CreateApiKey(userID string, accountId string, apiKey *ApiK
110132

111133
// Check collaborataros
112134
account, err := client.GetAccountByID(accountId)
135+
113136
if err != nil {
114137
return "", err
115138
}
@@ -118,12 +141,7 @@ func (client *Client) CreateApiKey(userID string, accountId string, apiKey *ApiK
118141
}
119142

120143
var xAccessToken string
121-
if userID == "" {
122-
userID, err = client.createRandomUser(accountId)
123-
if err != nil {
124-
return "", err
125-
}
126-
}
144+
127145
// login as user
128146
xAccessToken, err = client.GetXAccessToken(userID, accountId)
129147
if err != nil {
@@ -333,31 +351,3 @@ func (client *Client) CreateApiKeyServiceUser(serviceUserId string, apiKey *ApiK
333351

334352
return string(resp), nil
335353
}
336-
337-
func (client *Client) createRandomUser(accountId string) (string, error) {
338-
// add user
339-
userPrefix := acctest.RandString(10)
340-
userName := "tfuser" + userPrefix
341-
userEmail := userName + "@codefresh.io"
342-
343-
user, err := client.AddNewUserToAccount(accountId, userName, userEmail)
344-
if err != nil {
345-
return "", err
346-
}
347-
userID := user.ID
348-
349-
// activate
350-
err = client.ActivateUser(userID)
351-
352-
if err != nil {
353-
return "", err
354-
}
355-
356-
// set user as account admin
357-
err = client.SetUserAsAccountAdmin(accountId, userID)
358-
if err != nil {
359-
return "", nil
360-
}
361-
return userID, nil
362-
363-
}

codefresh/resource_api_key.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ func resourceApiKeyRead(d *schema.ResourceData, meta interface{}) error {
148148
if serviceAccountId := d.Get("service_account_id").(string); serviceAccountId != "" {
149149
apiKey, err = client.GetAPIKeyServiceUser(keyID, serviceAccountId)
150150
} else {
151-
apiKey, err = client.GetAPIKey(keyID)
151+
accountID := d.Get("account_id").(string)
152+
userID := d.Get("user_id").(string)
153+
apiKey, err = client.GetAPIKey(userID, accountID, keyID)
152154
}
153155

154156
if err != nil {
@@ -178,8 +180,9 @@ func resourceApiKeyUpdate(d *schema.ResourceData, meta interface{}) error {
178180
if serviceAccountId := d.Get("service_account_id").(string); serviceAccountId != "" {
179181
err = client.UpdateAPIKeyServiceUser(&apiKey, serviceAccountId)
180182
} else {
181-
err = client.UpdateAPIKey(&apiKey)
182-
183+
accountID := d.Get("account_id").(string)
184+
userID := d.Get("user_id").(string)
185+
err = client.UpdateAPIKey(userID, accountID, &apiKey)
183186
}
184187

185188
if err != nil {
@@ -201,7 +204,9 @@ func resourceApiKeyDelete(d *schema.ResourceData, meta interface{}) error {
201204
if serviceAccountId := d.Get("service_account_id").(string); serviceAccountId != "" {
202205
err = client.DeleteAPIKeyServiceUser(d.Id(), serviceAccountId)
203206
} else {
204-
err = client.DeleteAPIKey(d.Id())
207+
accountID := d.Get("account_id").(string)
208+
userID := d.Get("user_id").(string)
209+
err = client.DeleteAPIKey(userID, accountID, d.Id())
205210
}
206211

207212
if err != nil {

0 commit comments

Comments
 (0)