-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_config_root.go
More file actions
179 lines (153 loc) · 4.88 KB
/
path_config_root.go
File metadata and controls
179 lines (153 loc) · 4.88 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
package tailscale
import (
"context"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const configRootKey = "config/root"
func pathConfigToken(b *backend) *framework.Path {
return &framework.Path{
Pattern: configRootKey,
Fields: map[string]*framework.FieldSchema{
"tailnet": {
Type: framework.TypeString,
Description: "tailnet to make API request on behalf of",
},
"token": {
Type: framework.TypeString,
Description: "token to authenticate API requests",
},
"client_id": {
Type: framework.TypeString,
Description: "Tailscale OAuth Client ID with the 'devices' scope. Preferred over 'token' if both are specified",
},
"client_secret": {
Type: framework.TypeString,
Description: "Tailscale OAuth Client Secret with the 'devices' scope. Preferred over 'token' if both are specified",
},
"base_url": {
Type: framework.TypeString,
Description: fmt.Sprintf("Base URL to use for tailscale API requests. Defaults to %s", DEFAULT_BASE_URL),
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathConfigTokenRead,
logical.CreateOperation: b.pathConfigTokenWrite,
logical.UpdateOperation: b.pathConfigTokenWrite,
logical.DeleteOperation: b.pathConfigTokenDelete,
},
ExistenceCheck: b.configTokenExistenceCheck,
}
}
func (b *backend) configTokenExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
entry, err := b.readConfigToken(ctx, req.Storage)
if err != nil {
return false, err
}
return entry != nil, nil
}
func (b *backend) readConfigToken(ctx context.Context, storage logical.Storage) (*rootTokenConfig, error) {
entry, err := storage.Get(ctx, configRootKey)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
conf := &rootTokenConfig{}
if err := entry.DecodeJSON(conf); err != nil {
return nil, fmt.Errorf("error reading nomad access configuration: %w", err)
}
return conf, nil
}
func (b *backend) pathConfigTokenRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
conf, err := b.readConfigToken(ctx, req.Storage)
if err != nil {
return nil, err
}
if conf == nil {
return logical.ErrorResponse(
fmt.Sprintf("configuration does not exist. did you configure '%s'?", configRootKey),
), nil
}
return &logical.Response{
Data: map[string]interface{}{
"tailnet": conf.Tailnet,
"token": conf.Token,
"client_id": conf.ClientID,
"client_secret": conf.ClientSecret,
},
}, nil
}
func (b *backend) pathConfigTokenWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
conf, err := b.readConfigToken(ctx, req.Storage)
if err != nil {
return nil, err
}
if conf == nil {
conf = &rootTokenConfig{}
}
tailnet, ok := data.GetOk("tailnet")
if !ok {
return logical.ErrorResponse("Missing 'tailnet' in configuration request"), nil
}
conf.Tailnet = tailnet.(string)
baseURL, baseURLOk := data.GetOk("base_url")
if !baseURLOk {
baseURL = DEFAULT_BASE_URL
}
conf.BaseURL = baseURL.(string)
clientID, clientIDOK := data.GetOk("client_id")
clientSecret, clientSecretOK := data.GetOk("client_secret")
token, tokenOk := data.GetOk("token")
if (!clientIDOK || !clientSecretOK) && !tokenOk {
return logical.ErrorResponse("Must have one of 'client_id' and 'client_secret' or 'token'"), nil
}
if tokenOk {
conf.Token = token.(string)
}
if clientIDOK {
conf.ClientID = clientID.(string)
}
if clientSecretOK {
conf.ClientSecret = clientSecret.(string)
}
entry, err := logical.StorageEntryJSON(configRootKey, conf)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
return &logical.Response{
Data: map[string]interface{}{
"tailnet": conf.Tailnet,
"token": conf.Token,
"client_id": conf.ClientID,
"client_secret": conf.ClientSecret,
},
}, nil
}
func (b *backend) pathConfigTokenDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
if err := req.Storage.Delete(ctx, configRootKey); err != nil {
return nil, err
}
return nil, nil
}
type rootTokenConfig struct {
Token string `json:"token,omitempty"`
Tailnet string `json:"tailnet,omitempty"`
ClientID string `json:"client_id,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
BaseURL string `json:"base_url,omitempty"`
}
const pathConfigTokenHelpSyn = `
Configure tailscale token and options used by vault
`
const pathConfigTokenHelpDesc = `
Will confugre this mount with the token used by Vault for all tailscale
operations on this mount.
For instructions on how to get and/or create a tailscale api token see their
documentation at https://tailscale.com/kb/1101/api/.
`