-
Notifications
You must be signed in to change notification settings - Fork 0
/
grafana_token.go
115 lines (92 loc) · 3.03 KB
/
grafana_token.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
103
104
105
106
107
108
109
110
111
112
113
114
115
package vault_plugin_secrets_grafana
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
grafanaTokenType = "grafana_token"
)
type grafanaToken struct {
IsCloud bool `json:"is_cloud"`
Token string `json:"token"`
Stack string `json:"stack"` // For Grafana Cloud service accounts
Region string `json:"region"` // For Grafana Cloud access policies
AccessPolicyID string `json:"access_policy_id"` // For Grafana Cloud access policies
ServiceAccountID int64 `json:"service_account_id"` // For Grafana Cloud and Grafana service accounts
}
func (b *grafanaBackend) grafanaToken() *framework.Secret {
return &framework.Secret{
Type: grafanaTokenType,
Fields: map[string]*framework.FieldSchema{
"token": {
Type: framework.TypeString,
Description: "Grafana Token",
},
},
Revoke: b.tokenRevoke,
Renew: b.tokenRenew,
}
}
func (b *grafanaBackend) tokenRevoke(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
client, err := b.getClient(ctx, req.Storage)
if err != nil {
return nil, fmt.Errorf("error getting client: %w", err)
}
isCloud := false
if val, ok := req.Secret.InternalData["is_cloud"]; ok {
isCloud = val.(bool)
}
if isCloud {
stack := ""
if val, ok := req.Secret.InternalData["stack"]; ok {
stack = val.(string)
}
if stack != "" {
serviceAccountID := int64(req.Secret.InternalData["service_account_id"].(float64))
err := client.DeleteGrafanaServiceAccountFromCloud(stack, serviceAccountID)
if err != nil {
return nil, fmt.Errorf("error deleting grafana cloud service account: %w", err)
}
} else {
accessPolicyID := req.Secret.InternalData["access_policy_id"].(string)
region := req.Secret.InternalData["region"].(string)
err := client.DeleteCloudAccessPolicy(region, accessPolicyID)
if err != nil {
return nil, fmt.Errorf("error deleting grafana cloud access policy: %w", err)
}
}
} else {
serviceAccountID := req.Secret.InternalData["service_account_id"].(int64)
err := client.DeleteServiceAccount(serviceAccountID)
if err != nil {
return nil, fmt.Errorf("error deleting grafana service account: %w", err)
}
}
return nil, nil
}
func (b *grafanaBackend) tokenRenew(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
roleRaw, ok := req.Secret.InternalData["vault_role"]
if !ok {
return nil, fmt.Errorf("secret is missing role internal data")
}
// get the role entry
role := roleRaw.(string)
roleEntry, err := b.getRole(ctx, req.Storage, role)
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")
}
resp := &logical.Response{Secret: req.Secret}
if roleEntry.TTL > 0 {
resp.Secret.TTL = roleEntry.TTL
}
if roleEntry.MaxTTL > 0 {
resp.Secret.MaxTTL = roleEntry.MaxTTL
}
return resp, nil
}