Skip to content

Commit 7499852

Browse files
committed
include description field for user tokens
1 parent e19f219 commit 7499852

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

backend_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type testEnv struct {
4545
Organization string
4646
TeamID string
4747
UserID string
48+
Description string
4849

4950
Backend logical.Backend
5051
Context context.Context
@@ -160,7 +161,8 @@ func (e *testEnv) AddUserTokenRole(t *testing.T) {
160161
Path: "role/test-user-token",
161162
Storage: e.Storage,
162163
Data: map[string]interface{}{
163-
"user_id": e.UserID,
164+
"user_id": e.UserID,
165+
"description": e.Description,
164166
},
165167
}
166168
resp, err := e.Backend.HandleRequest(e.Context, req)

path_credentials.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ func (b *tfBackend) pathCredentialsRead(ctx context.Context, req *logical.Reques
8585
"role": roleEntry.Name,
8686
},
8787
}
88+
89+
if roleEntry.Description != "" {
90+
resp.Data["description"] = roleEntry.Description
91+
}
8892
return resp, nil
8993
}
9094

@@ -94,10 +98,16 @@ func (b *tfBackend) createUserCreds(ctx context.Context, req *logical.Request, r
9498
return nil, err
9599
}
96100

97-
resp := b.Secret(terraformTokenType).Response(map[string]interface{}{
101+
data := map[string]interface{}{
98102
"token": token.Token,
99103
"token_id": token.ID,
100-
}, map[string]interface{}{
104+
}
105+
106+
if role.Description != "" {
107+
data["description"] = role.Description
108+
}
109+
110+
resp := b.Secret(terraformTokenType).Response(data, map[string]interface{}{
101111
"token_id": token.ID,
102112
"role": role.Name,
103113
})
@@ -127,7 +137,7 @@ func (b *tfBackend) createToken(ctx context.Context, s logical.Storage, roleEntr
127137
case isTeamToken(roleEntry.TeamID):
128138
token, err = createTeamToken(ctx, client, roleEntry.TeamID)
129139
default:
130-
token, err = createUserToken(ctx, client, roleEntry.UserID)
140+
token, err = createUserToken(ctx, client, roleEntry.UserID, roleEntry.Description)
131141
}
132142

133143
if err != nil {

path_roles.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type terraformRoleEntry struct {
1818
Organization string `json:"organization,omitempty"`
1919
TeamID string `json:"team_id,omitempty"`
2020
UserID string `json:"user_id,omitempty"`
21+
Description string `json:"description,omitempty"`
2122
TTL time.Duration `json:"ttl"`
2223
MaxTTL time.Duration `json:"max_ttl"`
2324
Token string `json:"token,omitempty"`
@@ -30,6 +31,9 @@ func (r *terraformRoleEntry) toResponseData() map[string]interface{} {
3031
"ttl": r.TTL.Seconds(),
3132
"max_ttl": r.MaxTTL.Seconds(),
3233
}
34+
if r.Description != "" {
35+
respData["description"] = r.Description
36+
}
3337
if r.Organization != "" {
3438
respData["organization"] = r.Organization
3539
}
@@ -58,6 +62,10 @@ func pathRole(b *tfBackend) []*framework.Path {
5862
Description: "Name of the role",
5963
Required: true,
6064
},
65+
"description": {
66+
Type: framework.TypeString,
67+
Description: "Description of the token created by the role",
68+
},
6169
"organization": {
6270
Type: framework.TypeString,
6371
Description: "Name of the Terraform Cloud or Enterprise organization",
@@ -166,6 +174,10 @@ func (b *tfBackend) pathRolesWrite(ctx context.Context, req *logical.Request, d
166174
roleEntry.UserID = userID.(string)
167175
}
168176

177+
if description, ok := d.GetOk("description"); ok {
178+
roleEntry.Description = description.(string)
179+
}
180+
169181
if roleEntry.UserID != "" && (roleEntry.Organization != "" || roleEntry.TeamID != "") {
170182
return logical.ErrorResponse("cannot provide a user_id in combination with organization or team_id"), nil
171183
}

path_roles_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ func TestUserRole(t *testing.T) {
134134
organization := checkEnvVars(t, envVarTerraformOrganization)
135135
teamID := checkEnvVars(t, envVarTerraformTeamID)
136136
userID := checkEnvVars(t, envVarTerraformUserID)
137+
descriptionOriginal := "description1"
138+
descriptionUpdated := "description2"
137139

138140
t.Run("Create User Role - fail", func(t *testing.T) {
139141
resp, err := testTokenRoleCreate(t, b, s, roleName, map[string]interface{}{
@@ -147,8 +149,9 @@ func TestUserRole(t *testing.T) {
147149
})
148150
t.Run("Create User Role - pass", func(t *testing.T) {
149151
resp, err := testTokenRoleCreate(t, b, s, roleName, map[string]interface{}{
150-
"user_id": userID,
151-
"max_ttl": "3600",
152+
"user_id": userID,
153+
"max_ttl": "3600",
154+
"description": descriptionOriginal,
152155
})
153156

154157
require.Nil(t, err)
@@ -162,11 +165,13 @@ func TestUserRole(t *testing.T) {
162165
require.Nil(t, resp.Error())
163166
require.NotNil(t, resp)
164167
require.Equal(t, resp.Data["user_id"], userID)
168+
require.Equal(t, resp.Data["description"], descriptionOriginal)
165169
})
166170
t.Run("Update User Role", func(t *testing.T) {
167171
resp, err := testTokenRoleUpdate(t, b, s, map[string]interface{}{
168-
"ttl": "1m",
169-
"max_ttl": "5h",
172+
"ttl": "1m",
173+
"max_ttl": "5h",
174+
"description": descriptionUpdated,
170175
})
171176

172177
require.Nil(t, err)
@@ -180,6 +185,7 @@ func TestUserRole(t *testing.T) {
180185
require.Nil(t, resp.Error())
181186
require.NotNil(t, resp)
182187
require.Equal(t, resp.Data["user_id"], userID)
188+
require.Equal(t, resp.Data["description"], descriptionUpdated)
183189
})
184190
}
185191

terraform_token.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ func createTeamToken(ctx context.Context, c *client, teamID string) (*terraformT
5959
}, nil
6060
}
6161

62-
func createUserToken(ctx context.Context, c *client, userID string) (*terraformToken, error) {
63-
token, err := c.UserTokens.Create(ctx, userID, tfe.UserTokenCreateOptions{})
62+
func createUserToken(ctx context.Context, c *client, userID string, description string) (*terraformToken, error) {
63+
token, err := c.UserTokens.Create(ctx, userID, tfe.UserTokenCreateOptions{
64+
Description: description,
65+
})
6466
if err != nil {
6567
return nil, err
6668
}

0 commit comments

Comments
 (0)