-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka_credentials.go
278 lines (239 loc) · 7.53 KB
/
kafka_credentials.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package kafka
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/IBM/sarama"
"github.com/google/uuid"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
kafkaCredentialType = "kafka_credential"
)
// kafkaCredentials defines a secret for the Kafka credentials
type kafkaCredential struct {
Username string `json:"username"`
Password string `json:"password"`
ResourceACLs string `json:"resource_acls"`
ScramSHAVersion string `json:"scram_sha_version"`
}
// kafkaCredentials defines a secret to store for a given role
// and how it should be revoked or renewed.
func (b *kafkaBackend) kafkaCredential() *framework.Secret {
return &framework.Secret{
Type: kafkaCredentialType,
Fields: map[string]*framework.FieldSchema{
"username": {
Type: framework.TypeString,
Description: "Kafka User Username",
},
"password": {
Type: framework.TypeString,
Description: "Kafka User Password",
},
"resource_acls": {
Type: framework.TypeString,
Description: "ACLs created for the user",
},
"scram_sha_version": {
Type: framework.TypeString,
Description: "Kafka Credential SCRAM SHA Version",
},
},
Revoke: b.credentialRevoke,
Renew: b.credentialRenew,
}
}
func parseResourceACLs(acls string, principalName string) ([]*sarama.ResourceAcls, error) {
var resourceACLs []*sarama.ResourceAcls
err := json.Unmarshal([]byte(acls), &resourceACLs)
if err != nil {
return nil, err
}
for _, ra := range resourceACLs {
if ra.ResourceType == sarama.AclResourceUnknown {
return nil, errors.New("received unknown resource type")
}
if ra.ResourceName == "" {
return nil, errors.New("received empty resource name")
}
if ra.ResourcePatternType == sarama.AclPatternUnknown {
return nil, errors.New("received unknown resource pattern type")
}
if ra.Acls == nil {
return nil, errors.New("received empty resource acls")
}
for _, a := range ra.Acls {
if a.Host == "" {
return nil, errors.New("received empty host in resource ACL")
}
if a.Operation == sarama.AclOperationUnknown {
return nil, errors.New("received unknown operation in resource ACL")
}
if a.PermissionType == sarama.AclPermissionUnknown {
return nil, errors.New("received unknown permission type in resource ACL")
}
a.Principal = principalName
}
}
return resourceACLs, nil
}
func prepareACLFilters(resourceACLs []*sarama.ResourceAcls) ([]sarama.AclFilter, error) {
var aclFilters []sarama.AclFilter
for _, ra := range resourceACLs {
for _, a := range ra.Acls {
aclFilters = append(aclFilters, sarama.AclFilter{
ResourceType: ra.ResourceType,
ResourceName: &ra.ResourceName,
ResourcePatternTypeFilter: ra.ResourcePatternType,
Principal: &a.Principal,
Host: &a.Host,
Operation: a.Operation,
PermissionType: a.PermissionType,
})
}
}
return aclFilters, nil
}
func deleteCredential(ctx context.Context, c *kafkaClient, username string, scramSHAVersion string, resourceACLs string) error {
if username == "" {
return fmt.Errorf("recieved empty username for credential deletion")
}
if scramSHAVersion != SCRAMSHA512 && scramSHAVersion != SCRAMSHA256 {
return fmt.Errorf("invalid scram_sha_version for credential deletion. Can be %s, %s but recieved %s", SCRAMSHA256, SCRAMSHA512, scramSHAVersion)
}
mechanism := sarama.SCRAM_MECHANISM_SHA_512
if scramSHAVersion == SCRAMSHA256 {
mechanism = sarama.SCRAM_MECHANISM_SHA_256
}
_, err := c.DeleteUserScramCredentials([]sarama.AlterUserScramCredentialsDelete{
{
Name: username,
Mechanism: mechanism,
},
})
if err != nil {
return fmt.Errorf("error revoking Kafka credentials: %w", err)
}
if resourceACLs != "" {
// Prepare ACLs with generated username
userResourceACLs, err := parseResourceACLs(resourceACLs, username)
if err != nil {
return err
}
aclFilters, err := prepareACLFilters(userResourceACLs)
if err != nil {
return err
}
// Delete all ACLs
for _, filter := range aclFilters {
_, err = c.DeleteACL(filter, false)
if err != nil {
return err
}
}
}
return nil
}
// credentialsRevoke removes the credentials from the Vault storage API and calls the client to revoke the credentials
func (b *kafkaBackend) credentialRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
client, err := b.getClient(ctx, req.Storage)
if err != nil {
return nil, fmt.Errorf("error getting client: %w", err)
}
username := ""
usernameRaw, ok := req.Secret.InternalData["username"]
if ok {
username, ok = usernameRaw.(string)
if !ok {
return nil, fmt.Errorf("invalid value for username in secret internal data")
}
}
scramSHAVersion := ""
scramSHAVersionRaw, ok := req.Secret.InternalData["scram_sha_version"]
if ok {
scramSHAVersion, ok = scramSHAVersionRaw.(string)
if !ok {
return nil, fmt.Errorf("invalid value for scram_sha_version in secret internal data")
}
}
resourceACLs := ""
resourceACLsRaw, ok := req.Secret.InternalData["resource_acls"]
if ok {
resourceACLs, _ = resourceACLsRaw.(string)
}
if err := deleteCredential(ctx, client, username, scramSHAVersion, resourceACLs); err != nil {
return nil, err
}
return nil, nil
}
func createCredential(ctx context.Context, c *kafkaClient, roleEntry *kafkaRoleEntry, displayName string) (*kafkaCredential, error) {
name := displayName
if displayName == "" {
name = "anonymous"
}
username := roleEntry.UsernamePrefix + "-" + name + "-" + uuid.New().String()
password := uuid.New().String()
salt := uuid.New().String()
mechanism := sarama.SCRAM_MECHANISM_SHA_512
if roleEntry.ScramSHAVersion == SCRAMSHA256 {
mechanism = sarama.SCRAM_MECHANISM_SHA_256
}
_, err := c.UpsertUserScramCredentials([]sarama.AlterUserScramCredentialsUpsert{
{
Name: username,
Mechanism: mechanism,
Iterations: 8192,
Salt: []byte(salt),
Password: []byte(password),
},
})
if roleEntry.ResourceACLs != "" {
// Prepare ACLs with generated username
resourceACLs, err := parseResourceACLs(roleEntry.ResourceACLs, username)
if err != nil {
return nil, err
}
// Create all ACLs
err = c.CreateACLs(resourceACLs)
if err != nil {
return nil, err
}
}
if err != nil {
return nil, fmt.Errorf("error creating Kafka credentials: %w", err)
}
return &kafkaCredential{
Username: username,
Password: password,
// We're saving resource acls from the role instead of credential ACLs
// because we won't be able to unmarhal (to save the) and marshal credential ACLs once again
ResourceACLs: roleEntry.ResourceACLs,
ScramSHAVersion: roleEntry.ScramSHAVersion,
}, nil
}
// credentialsRenew calls the client to create a new credentials and stores it in the Vault storage API
func (b *kafkaBackend) credentialRenew(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
roleRaw, ok := req.Secret.InternalData["role"]
if !ok {
return nil, fmt.Errorf("secret is missing role internal data")
}
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
}