-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpath_rotate_role.go
85 lines (67 loc) · 2.05 KB
/
path_rotate_role.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfc
import (
"context"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathRotateRole(b *tfBackend) []*framework.Path {
return []*framework.Path{
{
Pattern: "rotate-role/" + framework.GenericNameRegex("name"),
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: operationPrefixTerraformCloud,
OperationVerb: "rotate",
OperationSuffix: "role",
},
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeString,
Description: "Name of the team or organization role",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathRotateRole,
ForwardPerformanceStandby: true,
ForwardPerformanceSecondary: true,
},
},
HelpSynopsis: pathRotateRoleHelpSyn,
HelpDescription: pathRotateRoleHelpDesc,
},
}
}
func (b *tfBackend) pathRotateRole(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
if name == "" {
return logical.ErrorResponse("missing role name"), nil
}
roleEntry, err := b.getRole(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if roleEntry == nil {
return logical.ErrorResponse("missing role entry"), nil
}
if roleEntry.UserID != "" {
return logical.ErrorResponse("cannot rotate credentials for user roles"), nil
}
token, err := b.createToken(ctx, req.Storage, roleEntry)
if err != nil {
return nil, err
}
roleEntry.Token = token.Token
if err := setRole(ctx, req.Storage, name, roleEntry); err != nil {
return nil, err
}
return nil, nil
}
const pathRotateRoleHelpSyn = `
Request to rotate the credentials for a team or organization.
`
const pathRotateRoleHelpDesc = `
This path attempts to rotate the credentials for the given team or organization role.
This endpoint returns an error if attempting to rotate a user role.
`