forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
137 lines (112 loc) · 3.33 KB
/
auth.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
package gocb
// Authenticator provides an interface to authenticate to each service.
type Authenticator interface {
clusterMgmt() userPassPair
clusterN1ql() []userPassPair
clusterFts() []userPassPair
bucketMemd(bucket string) userPassPair
bucketMgmt(bucket string) userPassPair
bucketViews(bucket string) userPassPair
bucketN1ql(bucket string) []userPassPair
bucketFts(bucket string) []userPassPair
}
// BucketAuthenticator provides a password for a single bucket.
type BucketAuthenticator struct {
Password string
}
type userPassPair struct {
Username string `json:"user"`
Password string `json:"pass"`
}
// BucketAuthenticatorMap is a map of bucket name to BucketAuthenticator.
type BucketAuthenticatorMap map[string]BucketAuthenticator
// ClusterAuthenticator implements an Authenticator which uses a list of buckets and passwords.
type ClusterAuthenticator struct {
Buckets BucketAuthenticatorMap
Username string
Password string
}
func (ca ClusterAuthenticator) clusterMgmt() userPassPair {
return userPassPair{ca.Username, ca.Password}
}
func (ca ClusterAuthenticator) clusterAll() []userPassPair {
userPassList := make([]userPassPair, len(ca.Buckets))
for bucket, auth := range ca.Buckets {
userPassList = append(userPassList, userPassPair{
Username: bucket,
Password: auth.Password,
})
}
return userPassList
}
func (ca ClusterAuthenticator) clusterN1ql() []userPassPair {
return ca.clusterAll()
}
func (ca ClusterAuthenticator) clusterFts() []userPassPair {
return ca.clusterAll()
}
func (ca ClusterAuthenticator) bucketAll(bucket string) userPassPair {
if bucketAuth, ok := ca.Buckets[bucket]; ok {
return userPassPair{bucket, bucketAuth.Password}
}
return userPassPair{"", ""}
}
func (ca ClusterAuthenticator) bucketMemd(bucket string) userPassPair {
return ca.bucketAll(bucket)
}
func (ca ClusterAuthenticator) bucketMgmt(bucket string) userPassPair {
return ca.bucketAll(bucket)
}
func (ca ClusterAuthenticator) bucketViews(bucket string) userPassPair {
return ca.bucketAll(bucket)
}
func (ca ClusterAuthenticator) bucketN1ql(bucket string) []userPassPair {
return []userPassPair{
ca.bucketAll(bucket),
}
}
func (ca ClusterAuthenticator) bucketFts(bucket string) []userPassPair {
return []userPassPair{
ca.bucketAll(bucket),
}
}
// PasswordAuthenticator implements an Authenticator which uses an RBAC username and password.
type PasswordAuthenticator struct {
Username string
Password string
}
func (ra PasswordAuthenticator) rbacAll() userPassPair {
return userPassPair{ra.Username, ra.Password}
}
func (ra PasswordAuthenticator) clusterMgmt() userPassPair {
return ra.rbacAll()
}
func (ra PasswordAuthenticator) clusterN1ql() []userPassPair {
return []userPassPair{
ra.rbacAll(),
}
}
func (ra PasswordAuthenticator) clusterFts() []userPassPair {
return []userPassPair{
ra.rbacAll(),
}
}
func (ra PasswordAuthenticator) bucketMemd(bucket string) userPassPair {
return ra.rbacAll()
}
func (ra PasswordAuthenticator) bucketMgmt(bucket string) userPassPair {
return ra.rbacAll()
}
func (ra PasswordAuthenticator) bucketViews(bucket string) userPassPair {
return ra.rbacAll()
}
func (ra PasswordAuthenticator) bucketN1ql(bucket string) []userPassPair {
return []userPassPair{
ra.rbacAll(),
}
}
func (ra PasswordAuthenticator) bucketFts(bucket string) []userPassPair {
return []userPassPair{
ra.rbacAll(),
}
}