/
path_cab_config.go
206 lines (181 loc) · 5.48 KB
/
path_cab_config.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package gcpcab
import (
"context"
"encoding/json"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func (b *backend) pathCABConfigCRUD() *framework.Path {
return &framework.Path{
Pattern: "cab/config/" + framework.GenericNameRegex("name"),
HelpSynopsis: "Configure CAB root configuration policy",
HelpDescription: `
see: https://cloud.google.com/iam/docs/downscoping-short-lived-credentials
`,
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: `
Configuration Name.
`,
},
"project": &framework.FieldSchema{
Type: framework.TypeString,
Description: `
GCP ProjectID for the Impersonation and CAB
`,
},
"restricted": &framework.FieldSchema{
Type: framework.TypeBool,
Default: true,
Description: `
Is this CAB token restricted to the path parameters.
`,
},
"scopes": &framework.FieldSchema{
Type: framework.TypeCommaStringSlice,
Default: []string{"https://www.googleapis.com/auth/cloud-platform"},
Description: `
Scopes to apply to the Impersonated TOken
`,
},
"delegates": &framework.FieldSchema{
Type: framework.TypeCommaStringSlice,
Default: []string{},
Description: `
Delegates for Impersonation
`,
},
"duration": &framework.FieldSchema{
Type: framework.TypeInt,
Default: 3600,
Description: `
Duration of the Impersonated Token
`,
},
"bindings": &framework.FieldSchema{
Type: framework.TypeString,
Default: "{}",
Description: `
Bindings to apply to the CAB
`,
},
"target_service_account": &framework.FieldSchema{
Type: framework.TypeString,
Description: `
Service Account to issue the CAB for
`,
},
"raw_token": &framework.FieldSchema{
Type: framework.TypeBool,
Default: false,
Description: `
Return raw access_token without CAB (i.,e just impersonation)
`,
},
},
// ExistenceCheck: b.pathKeysExistenceCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: withFieldValidator(b.pathCABConfigRead),
logical.UpdateOperation: withFieldValidator(b.pathCABConfigWrite),
logical.DeleteOperation: withFieldValidator(b.pathCABConfigDelete),
},
}
}
// pathKeysConfigRead corresponds to GET cab/config/:name and is used to
// show information about the key configuration in Vault.
func (b *backend) pathCABConfigRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
key := d.Get("name").(string)
k, err := b.CABKey(ctx, req.Storage, "cab/config/"+key)
if err != nil {
if err == ErrKeyNotFound {
return logical.ErrorResponse(err.Error()), logical.ErrInvalidRequest
}
return nil, err
}
data := map[string]interface{}{
"target_service_account": k.TargetServiceAccount,
"restricted": k.Restricted,
"scopes": k.Scopes,
"duration": k.Duration,
"project": k.Project,
"bindings": k.Bindings,
"delegates": k.Delegates,
"raw_token": k.RawToken,
}
return &logical.Response{
Data: data,
}, nil
}
// pathKeysConfigWrite corresponds to PUT/POST /generatekey/config/:name and
// configures information about the key in Vault.
func (b *backend) pathCABConfigWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
key := d.Get("name").(string)
k, err := b.CABKey(ctx, req.Storage, key)
if err != nil {
//return nil, err
k = &ConfigSpec{}
}
if v, ok := d.GetOk("project"); ok {
if v.(string) == "" {
return logical.ErrorResponse("Project Config cannot be null"), logical.ErrInvalidRequest
} else {
k.Project = v.(string)
}
}
if v, ok := d.GetOk("restricted"); ok {
k.Restricted = v.(bool)
}
if v, ok := d.GetOk("scopes"); ok {
k.Scopes = v.([]string)
}
if v, ok := d.GetOk("delegates"); ok {
k.Delegates = v.([]string)
}
if v, ok := d.GetOk("duration"); ok {
k.Duration = v.(int)
}
if v, ok := d.GetOk("target_service_account"); ok {
if v.(string) == "" {
return logical.ErrorResponse("target_service_account Config cannot be null"), logical.ErrInvalidRequest
} else {
k.TargetServiceAccount = v.(string)
}
}
if v, ok := d.GetOk("bindings"); ok {
if v.(string) == "" {
return logical.ErrorResponse("bindings cannot be null"), logical.ErrInvalidRequest
} else {
var cab AccessBoundary
s := v.(string)
err = json.Unmarshal([]byte(s), &cab)
if err != nil {
return logical.ErrorResponse("Could Not parse CAB Bindings File"), logical.ErrInvalidRequest
}
k.Bindings = cab
}
}
if v, ok := d.GetOk("raw_token"); ok {
k.RawToken = v.(bool)
}
if k.Restricted && k.RawToken {
return logical.ErrorResponse("Cannot request raw_token for restricted key"), logical.ErrInvalidRequest
}
// Save it
entry, err := logical.StorageEntryJSON("cab/config/"+key, k)
if err != nil {
return nil, errwrap.Wrapf("failed to create storage entry: {{err}}", err)
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, errwrap.Wrapf("failed to write to storage: {{err}}", err)
}
return nil, nil
}
func (b *backend) pathCABConfigDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
key := d.Get("name").(string)
if err := req.Storage.Delete(ctx, "cab/config/"+key); err != nil {
return nil, errwrap.Wrapf("failed to delete from storage: {{err}}", err)
}
return nil, nil
}