-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_config_root.go
156 lines (131 loc) · 4.28 KB
/
path_config_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
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
package auth0
import (
"context"
"strings"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const configTokenKey = "config/root"
func pathConfigRoot(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config/root",
Fields: map[string]*framework.FieldSchema{
"client_secret": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Secret for auth0 Machine-To-Machine application authorized with *:client_keys and *:clients scopes",
},
"client_id": &framework.FieldSchema{
Type: framework.TypeString,
Description: "ID for auth0 Machine-To-Machine application authorized to Auth0 Management API for your domain",
},
"domain": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Auth0 domain the client id/secret is registered under",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathConfigRootRead,
logical.CreateOperation: b.pathConfigRootWrite,
logical.UpdateOperation: b.pathConfigRootWrite,
logical.DeleteOperation: b.pathConfigRootDelete,
},
ExistenceCheck: b.configTokenExistenceCheck,
}
}
func (b *backend) configTokenExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
entry, err := b.readConfigRoot(ctx, req.Storage)
if err != nil {
return false, err
}
return entry != nil, nil
}
func (b *backend) readConfigRoot(ctx context.Context, storage logical.Storage) (*rootTokenConfig, error) {
entry, err := storage.Get(ctx, configTokenKey)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
conf := &rootTokenConfig{}
if err := entry.DecodeJSON(conf); err != nil {
return nil, errwrap.Wrapf("error reading configuration: {{err}}", err)
}
return conf, nil
}
func (b *backend) pathConfigRootRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
conf, err := b.readConfigRoot(ctx, req.Storage)
if err != nil {
return nil, err
}
if conf == nil {
return logical.ErrorResponse("configuration does not exist. did you configure 'config/root'?"), nil
}
return &logical.Response{
Data: map[string]interface{}{
"domain": conf.Domain,
"client_id": conf.ClientID,
},
}, nil
}
func (b *backend) pathConfigRootWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
conf, err := b.readConfigRoot(ctx, req.Storage)
if err != nil {
return nil, err
}
if conf == nil {
conf = &rootTokenConfig{}
}
var missingOptions []string
clientSecret, ok := data.GetOk("client_secret")
if !ok {
missingOptions = append(missingOptions, "client_secret")
} else {
conf.ClientSecret = clientSecret.(string)
}
clientID, ok := data.GetOk("client_id")
if !ok {
missingOptions = append(missingOptions, "client_id")
} else {
conf.ClientID = clientID.(string)
}
domain, ok := data.GetOk("domain")
if !ok {
missingOptions = append(missingOptions, "domain")
} else {
conf.Domain = domain.(string)
}
if len(missingOptions) > 0 {
return logical.ErrorResponse("Missing %s in configuration request", strings.Join(missingOptions, ", ")), nil
}
entry, err := logical.StorageEntryJSON(configTokenKey, conf)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathConfigRootDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
if err := req.Storage.Delete(ctx, configTokenKey); err != nil {
return nil, err
}
return nil, nil
}
type rootTokenConfig struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Domain string `json:"domain"`
}
const pathConfigRootHelpSyn = `
Configure auth0 client credentials used by vault
`
const pathConfigRootHelpDesc = `
Will confugre this mount with a oauth client used by Vault for all auth0
operations on this mount. Must be configured with: TODO.
For instructions on how to setup a Machine-to-Machine Application for
Management API, see auth0's documentation
(https://auth0.com/docs/tokens/management-api-access-tokens/create-and-authorize-a-machine-to-machine-application).
`