-
Notifications
You must be signed in to change notification settings - Fork 3
/
path_config_rotate_root.go
102 lines (83 loc) · 2.97 KB
/
path_config_rotate_root.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package ibmcloudsecrets
import (
"context"
"errors"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathConfigRotateRoot(b *ibmCloudSecretBackend) *framework.Path {
return &framework.Path{
Pattern: "config/rotate-root",
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathConfigRotateRootWrite,
},
},
HelpSynopsis: pathConfigRotateRootHelpSyn,
HelpDescription: pathConfigRotateRootHelpDesc,
}
}
func (b *ibmCloudSecretBackend) pathConfigRotateRootWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
config, errResp := b.getConfig(ctx, req.Storage)
if errResp != nil {
return errResp, nil
}
if config == nil || config.APIKey == "" {
return nil, errors.New("no API key was set in the configuration")
}
iam, resp := b.getIAMHelper(ctx, req.Storage)
if resp != nil {
b.Logger().Error("failed to retrieve an IAM helper", "error", resp.Error())
return resp, nil
}
adminToken, err := b.getAdminToken(ctx, req.Storage)
if err != nil {
b.Logger().Error("error obtaining the token for the configured API key", "error", err)
return nil, err
}
oldKeyDetails, err := iam.GetAPIKeyDetails(adminToken, config.APIKey)
if err != nil {
b.Logger().Error("error obtaining details about the current API key", "error", err)
return nil, err
}
// with old key, verify account == acount from the config
keyName := "vault-generated-root-credential"
keyDescription := "Generated by Vault's secret engine for IBM Cloud credentials during root key rotation."
// Generate a new service account key
newAPIKey, err := iam.CreateAPIKey(adminToken, oldKeyDetails.IAMID, oldKeyDetails.AccountID, keyName, keyDescription)
if err != nil {
return nil, err
}
// Update the configuration with the new key
config.APIKey = newAPIKey.APIKey
entry, err := logical.StorageEntryJSON("config", config)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
// Reset the backend to pick up the new key
b.reset()
// Delete the old API key
err = iam.DeleteAPIKey(adminToken, oldKeyDetails.ID)
if err != nil {
errResponse := logical.ErrorResponse("error deleting API key %s after successfully rotating the API key to key %s: %s", oldKeyDetails.ID, newAPIKey.ID, err)
return errResponse, err
}
return &logical.Response{
Data: map[string]interface{}{
apiKeyID: newAPIKey.ID,
},
}, nil
}
const pathConfigRotateRootHelpSyn = `
Request to rotate the IBM Cloud credentials used by Vault
`
const pathConfigRotateRootHelpDesc = `
This path attempts to rotate the IBM Cloud API key used by Vault
for this mount. It does this by generating a new key for the user or service ID,
replacing the internal value, and then deleting the old API key.
Note that it does not create a new service ID or user account, only a new
API key on the same IAM ID as the existing key.
`