/
path_config.go
231 lines (205 loc) · 6.41 KB
/
path_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package gcpauth
import (
"context"
"fmt"
"net/http"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/authmetadata"
"github.com/hashicorp/vault/sdk/logical"
)
var (
// The default gce_alias is "instance_id". The default fields
// below are selected because they're unlikely to change often
// for a particular instance ID.
gceAuthMetadataFields = &authmetadata.Fields{
FieldName: "gce_metadata",
Default: []string{
"instance_creation_timestamp",
"instance_id",
"instance_name",
"project_id",
"project_number",
"role",
"service_account_id",
"service_account_email",
"zone",
},
AvailableToAdd: []string{},
}
// The default iam_alias is "unique_id". The default fields
// below are selected because they're unlikely to change often
// for a particular instance ID.
iamAuthMetadataFields = &authmetadata.Fields{
FieldName: "iam_metadata",
Default: []string{
"project_id",
"role",
"service_account_id",
"service_account_email",
},
AvailableToAdd: []string{},
}
)
func pathConfig(b *GcpAuthBackend) *framework.Path {
return &framework.Path{
Pattern: "config",
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: operationPrefixGoogleCloud,
},
Fields: map[string]*framework.FieldSchema{
"credentials": {
Type: framework.TypeString,
Description: `
Google credentials JSON that Vault will use to verify users against GCP APIs.
If not specified, will use application default credentials`,
DisplayAttrs: &framework.DisplayAttributes{
Name: "Credentials",
},
},
"iam_alias": {
Type: framework.TypeString,
Default: defaultIAMAlias,
Description: "Indicates what value to use when generating an alias for IAM authentications.",
},
iamAuthMetadataFields.FieldName: authmetadata.FieldSchema(iamAuthMetadataFields),
"gce_alias": {
Type: framework.TypeString,
Default: defaultGCEAlias,
Description: "Indicates what value to use when generating an alias for GCE authentications.",
},
gceAuthMetadataFields.FieldName: authmetadata.FieldSchema(gceAuthMetadataFields),
"custom_endpoint": {
Type: framework.TypeKVPairs,
Description: `Specifies overrides for various Google API Service Endpoints used in requests.`,
},
// Deprecated
"google_certs_endpoint": {
Type: framework.TypeString,
Description: `
Deprecated. This field does nothing and be removed in a future release`,
Deprecated: true,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathConfigRead,
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "read",
OperationSuffix: "auth-configuration",
},
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathConfigWrite,
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "configure",
OperationSuffix: "auth",
},
},
},
HelpSynopsis: `Configure credentials used to query the GCP IAM API to verify authenticating service accounts`,
HelpDescription: `
The GCP IAM auth backend makes queries to the GCP IAM auth backend to verify a service account
attempting login. It verifies the service account exists and retrieves a public key to verify
signed JWT requests passed in on login. The credentials should have the following permissions:
iam AUTH:
* iam.serviceAccountKeys.get
`,
}
}
func (b *GcpAuthBackend) pathConfigWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
if err := validateFields(req, d); err != nil {
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
c, err := b.config(ctx, req.Storage)
if err != nil {
return nil, err
}
if err := c.Update(d); err != nil {
return nil, logical.CodedError(http.StatusBadRequest, err.Error())
}
// Create/update the storage entry
entry, err := logical.StorageEntryJSON("config", c)
if err != nil {
return nil, fmt.Errorf("failed to generate JSON configuration: %w", err)
}
// Save the storage entry
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, fmt.Errorf("failed to persist configuration to storage: %w", err)
}
// Invalidate existing client so it reads the new configuration
b.ClearCaches()
return nil, nil
}
func (b *GcpAuthBackend) pathConfigRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
if err := validateFields(req, d); err != nil {
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
config, err := b.config(ctx, req.Storage)
if err != nil {
return nil, err
}
resp := map[string]interface{}{
gceAuthMetadataFields.FieldName: config.GCEAuthMetadata.AuthMetadata(),
iamAuthMetadataFields.FieldName: config.IAMAuthMetadata.AuthMetadata(),
}
if config.Credentials != nil {
if v := config.Credentials.ClientEmail; v != "" {
resp["client_email"] = v
}
if v := config.Credentials.ClientId; v != "" {
resp["client_id"] = v
}
if v := config.Credentials.PrivateKeyId; v != "" {
resp["private_key_id"] = v
}
if v := config.Credentials.ProjectId; v != "" {
resp["project_id"] = v
}
}
if v := config.IAMAliasType; v != "" {
resp["iam_alias"] = v
}
if v := config.GCEAliasType; v != "" {
resp["gce_alias"] = v
}
endpoints := make(map[string]string)
if v := config.APICustomEndpoint; v != "" {
endpoints["api"] = v
}
if v := config.IAMCustomEndpoint; v != "" {
endpoints["iam"] = v
}
if v := config.CRMCustomEndpoint; v != "" {
endpoints["crm"] = v
}
if v := config.ComputeCustomEndpoint; v != "" {
endpoints["compute"] = v
}
if len(endpoints) > 0 {
resp["custom_endpoint"] = endpoints
}
return &logical.Response{
Data: resp,
}, nil
}
// config reads the backend's gcpConfig from storage.
// This assumes the caller has already obtained the backend's config lock.
func (b *GcpAuthBackend) config(ctx context.Context, s logical.Storage) (*gcpConfig, error) {
config := &gcpConfig{
GCEAuthMetadata: authmetadata.NewHandler(gceAuthMetadataFields),
IAMAuthMetadata: authmetadata.NewHandler(iamAuthMetadataFields),
}
entry, err := s.Get(ctx, "config")
if err != nil {
return nil, err
}
if entry == nil {
return config, nil
}
if err := entry.DecodeJSON(config); err != nil {
return nil, err
}
return config, nil
}