-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpath_credentials.go
More file actions
186 lines (154 loc) · 5.07 KB
/
path_credentials.go
File metadata and controls
186 lines (154 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright IBM Corp. 2020, 2025
// SPDX-License-Identifier: MPL-2.0
package tfc
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathCredentials(b *tfBackend) *framework.Path {
return &framework.Path{
Pattern: "creds/" + framework.GenericNameRegex("name"),
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: operationPrefixTerraformCloud,
OperationVerb: "generate",
},
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeLowerCaseString,
Description: "Name of the role",
Required: true,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathCredentialsRead,
DisplayAttrs: &framework.DisplayAttributes{
OperationSuffix: "credentials",
},
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathCredentialsRead,
DisplayAttrs: &framework.DisplayAttributes{
OperationSuffix: "credentials2",
},
},
},
HelpSynopsis: pathCredentialsHelpSyn,
HelpDescription: pathCredentialsHelpDesc,
}
}
func (b *tfBackend) terraformToken() *framework.Secret {
return &framework.Secret{
Type: terraformTokenType,
Fields: map[string]*framework.FieldSchema{
"token": {
Type: framework.TypeString,
Description: "Terraform Token",
},
},
Revoke: b.terraformTokenRevoke,
Renew: b.terraformTokenRenew,
}
}
func (b *tfBackend) pathCredentialsRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
roleName := d.Get("name").(string)
roleEntry, err := b.getRole(ctx, req.Storage, roleName)
if err != nil {
return nil, fmt.Errorf("error retrieving role: %w", err)
}
if roleEntry == nil {
return nil, errors.New("error retrieving role: role is nil")
}
// If a user role was configured prior to 1.20, credentialType may not be set.
// This temporary setting does not persist to the role definition
if roleEntry.CredentialType == "" && roleEntry.UserID != "" {
b.Logger().Info("role's credential type not set in storage, inferring type \"user\".", "role", roleName)
roleEntry.CredentialType = userCredentialType
}
if roleEntry.CredentialType == userCredentialType || roleEntry.CredentialType == teamCredentialType {
return b.createUserOrMultiTeamCreds(ctx, req, roleEntry)
}
resp := &logical.Response{
Data: map[string]interface{}{
"token_id": roleEntry.TokenID,
"token": roleEntry.Token,
"organization": roleEntry.Organization,
"team_id": roleEntry.TeamID,
"role": roleEntry.Name,
},
}
if roleEntry.Description != "" {
resp.Data["description"] = roleEntry.Description
}
return resp, nil
}
func (b *tfBackend) createUserOrMultiTeamCreds(ctx context.Context, req *logical.Request, role *terraformRoleEntry) (*logical.Response, error) {
token, err := b.createToken(ctx, req.Storage, role)
if err != nil {
return nil, err
}
data := map[string]interface{}{
"token": token.Token,
"token_id": token.ID,
}
if token.Description != "" {
data["description"] = token.Description
}
if !token.ExpiredAt.IsZero() {
data["expired_at"] = token.ExpiredAt
}
resp := b.Secret(terraformTokenType).Response(data, map[string]interface{}{
"token_id": token.ID,
"role": role.Name,
})
if role.TTL > 0 {
resp.Secret.TTL = role.TTL
}
if role.MaxTTL > 0 {
resp.Secret.MaxTTL = role.MaxTTL
}
return resp, nil
}
func (b *tfBackend) createToken(ctx context.Context, s logical.Storage, roleEntry *terraformRoleEntry) (*terraformToken, error) {
client, err := b.getClient(ctx, s)
if err != nil {
return nil, err
}
var token *terraformToken
switch {
case isOrgToken(roleEntry.Organization, roleEntry.TeamID):
token, err = createOrgToken(ctx, client, roleEntry.Organization)
case isTeamToken(roleEntry.TeamID):
if roleEntry.CredentialType == teamCredentialType {
token, err = createTeamTokenWithOptions(ctx, client, *roleEntry, b.System().MaxLeaseTTL())
} else {
// team_legacy tokens
token, err = createTeamLegacyToken(ctx, client, roleEntry.TeamID)
}
default:
token, err = createUserToken(ctx, client, roleEntry.UserID, roleEntry.Description)
}
if err != nil {
return nil, fmt.Errorf("error creating Terraform token: %w", err)
}
if token == nil {
return nil, errors.New("error creating Terraform token")
}
return token, nil
}
const pathCredentialsHelpSyn = `
Generate a Terraform Cloud or Enterprise API token from a specific Vault role.
`
const pathCredentialsHelpDesc = `
This path generates Terraform Cloud or Enterprise API Organization, Team, or
User Tokens based on a particular role. A role can only represent a single type
of Token; Organization, Team, or User, and so can only contain one parameter for
organization, team_id, or user_id.
If the role has the team ID configured, this path generates a team token.
If this role only has the organization configured, this path generates an
organization token.
If this role has a user ID configured, this path generates a user token.
`