/
default.go
375 lines (328 loc) · 10.8 KB
/
default.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// @author Couchbase <info@couchbase.com>
// @copyright 2014 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cbauth
import (
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"net/rpc"
"net/url"
"os"
"path/filepath"
"sync"
"time"
"github.com/couchbase/cbauth/cbauthimpl"
"github.com/couchbase/cbauth/httpreq"
"github.com/couchbase/cbauth/revrpc"
"github.com/couchbase/clog"
)
// Default variable holds default authenticator. Default authenticator
// is constructed automatically from environment variables passed by
// ns_server. It is nil if your process was not (correctly) spawned by
// ns_server.
var Default Authenticator
type restartableAuthImpl struct {
l sync.RWMutex
a ExternalAuthenticator
s *revrpc.Service
}
func (r *restartableAuthImpl) getAuth() ExternalAuthenticator {
r.l.RLock()
defer r.l.RUnlock()
return r.a
}
func (r *restartableAuthImpl) setAuth(a ExternalAuthenticator,
s *revrpc.Service) {
r.l.Lock()
defer r.l.Unlock()
r.a = a
r.s = s
}
func (r *restartableAuthImpl) disconnect() error {
r.l.Lock()
defer r.l.Unlock()
if r.s == nil {
return nil
}
err := r.s.Disconnect()
if err != nil {
return err
}
r.s = nil
return nil
}
var externalAuth = restartableAuthImpl{}
var errDisconnected = errors.New("revrpc connection to ns_server was closed")
var errUnrecoverable = errors.New("revrpc connection cannot be established")
const waitBeforeStale = time.Minute
func getCbauthErrorPolicy(svc *cbauthimpl.Svc,
external bool) revrpc.ErrorPolicyFn {
if external {
defPolicy := getCbauthErrorPolicy(svc, false)
return func(err error) error {
if err == io.EOF {
cbauthimpl.ResetSvc(svc, &DBStaleError{err})
return errDisconnected
}
httpErr, ok := err.(*revrpc.HttpError)
if ok &&
httpErr.StatusCode == 400 &&
httpErr.Message == "Version is not supported" {
cbauthimpl.ResetSvc(svc, &DBStaleError{err})
return errUnrecoverable
}
return defPolicy(err)
}
} else {
defPolicy := revrpc.DefaultBabysitErrorPolicy.New()
// error restart policy that we're going to use simply
// resets service before delegating to default restart
// policy. That way we always mark service as stale
// right after some error occurred.
return func(err error) error {
cbauthimpl.ResetSvc(svc, &DBStaleError{err})
return defPolicy(err)
}
}
}
func runRPCForSvc(rpcsvc *revrpc.Service, svc *cbauthimpl.Svc,
policy revrpc.ErrorPolicyFn) error {
return revrpc.BabysitService(func(s *rpc.Server) error {
return s.RegisterName("AuthCacheSvc", svc)
}, rpcsvc, revrpc.FnBabysitErrorPolicy(policy))
}
func startDefault(rpcsvc *revrpc.Service, svc *cbauthimpl.Svc,
policy revrpc.ErrorPolicyFn, external bool) {
if external {
externalAuth.setAuth(&authImpl{svc}, rpcsvc)
} else {
Default = &authImpl{svc}
}
go func() {
err := runRPCForSvc(rpcsvc, svc, policy)
if errors.Is(err, errDisconnected) ||
errors.Is(err, errUnrecoverable) {
return
}
panic(err)
}()
}
func init() {
rpcsvc, err := revrpc.GetDefaultServiceFromEnv("cbauth")
if err != nil {
ErrNotInitialized = fmt.Errorf("Unable to initialize cbauth's revrpc: %s", err)
return
}
svc := newSvc()
startDefault(rpcsvc, svc, getCbauthErrorPolicy(svc, false), false)
}
func newSvc() *cbauthimpl.Svc {
return cbauthimpl.NewSVC(waitBeforeStale, &DBStaleError{})
}
// InitExternal should be used by external cbauth client to enable cbauth
// with limited functionality.
func InitExternal(service, mgmtHostPort, user, password string) error {
return InitExternalWithHeartbeat(service, mgmtHostPort, user, password,
0, 0)
}
// InitExternalWithHeartbeat should be used by external cbauth client to enable
// cbauth with limited functionality and enabling heartbeats.
// heartbeatInterval - interval in seconds at which heartbeats should be sent
// heartbeatWait - defines how many seconds we wait until declaring the
// database stale
func InitExternalWithHeartbeat(service, mgmtHostPort, user, password string,
heartbeatInterval, heartbeatWait int) error {
err := externalAuth.disconnect()
if err != nil {
clog.Warnf("failed to disconnect existing external authenticator: %s", err)
}
_, err = doInternalRetryDefaultInitWithService(service,
mgmtHostPort, user, password, true, heartbeatInterval,
heartbeatWait)
return err
}
// InternalRetryDefaultInit can be used by golang services that are
// willing to perform manual initialization of cbauth (i.e. for easier
// testing). This API is subject to change and should be used only if
// really needed. Returns false if Default Authenticator was already
// initialized.
func InternalRetryDefaultInit(mgmtHostPort, user, password string) (bool, error) {
service := filepath.Base(os.Args[0])
return InternalRetryDefaultInitWithService(service, mgmtHostPort, user, password)
}
// InternalRetryDefaultInitWithService can be used by golang services that are
// willing to perform manual initialization of cbauth (i.e. for easier
// testing). This API is subject to change and should be used only if
// really needed. Returns false if Default Authenticator was already
// initialized.
func InternalRetryDefaultInitWithService(service, mgmtHostPort, user, password string) (bool, error) {
if Default != nil {
return false, nil
}
return doInternalRetryDefaultInitWithService(service+"-cbauth",
mgmtHostPort, user, password, false, 0, 0)
}
func doInternalRetryDefaultInitWithService(
service, mgmtHostPort, user, password string,
external bool, heartbeatInterval, heartbeatWait int) (bool, error) {
host, port, err := SplitHostPort(mgmtHostPort)
if err != nil {
return false, fmt.Errorf("Failed to split hostport `%s': %s", mgmtHostPort, err)
}
var baseurl string
if external {
baseurl = fmt.Sprintf("http://%s:%d/auth/v1/%s",
host, port, service)
} else {
baseurl = fmt.Sprintf("http://%s:%d/%s", host, port, service)
}
if heartbeatInterval != 0 {
baseurl = baseurl + fmt.Sprintf("?heartbeat=%v",
heartbeatInterval)
}
u, err := url.Parse(baseurl)
if err != nil {
return false, fmt.Errorf("Failed to parse constructed url `%s': %s", baseurl, err)
}
u.User = url.UserPassword(user, password)
svc := newSvc()
svc.SetConnectInfo(mgmtHostPort, user, password, heartbeatInterval,
heartbeatWait)
startDefault(revrpc.MustService(u.String()), svc,
getCbauthErrorPolicy(svc, external), external)
return true, nil
}
// ErrNotInitialized is used to signal that ns_server environment
// variables are not set, and thus Default authenticator is not
// configured for calls that use default authenticator.
var ErrNotInitialized = errors.New("cbauth was not initialized")
// WithDefault calls given body with default authenticator. If default
// authenticator is not configured, it returns ErrNotInitialized.
func WithDefault(body func(a Authenticator) error) error {
return WithAuthenticator(nil, body)
}
// WithAuthenticator calls given body with either passed authenticator
// or default authenticator if `a' is nil. ErrNotInitialized is
// returned if a is nil and default authenticator is not configured.
func WithAuthenticator(a Authenticator, body func(a Authenticator) error) error {
if a == nil {
a = Default
if a == nil {
return ErrNotInitialized
}
}
return body(a)
}
func GetExternalAuthenticator() ExternalAuthenticator {
return externalAuth.getAuth()
}
// AuthWebCreds method extracts credentials from given http request
// using default authenticator.
func AuthWebCreds(req *http.Request) (creds Creds, err error) {
if Default == nil {
return nil, ErrNotInitialized
}
return Default.AuthWebCreds(req)
}
// AuthWebCredsGeneric method extracts credentials from an HTTP request
// that is generic (not necessarily using the net/http library)
func AuthWebCredsGeneric(req httpreq.HttpRequest) (creds Creds, err error) {
if Default == nil {
return nil, ErrNotInitialized
}
return Default.AuthWebCredsGeneric(req)
}
// Auth method constructs credentials from given user and password
// pair. Uses default authenticator.
func Auth(user, pwd string) (creds Creds, err error) {
if Default == nil {
return nil, ErrNotInitialized
}
return Default.Auth(user, pwd)
}
// GetHTTPServiceAuth returns user/password creds giving "admin"
// access to given http service inside couchbase cluster. Uses default
// authenticator.
func GetHTTPServiceAuth(hostport string) (user, pwd string, err error) {
if Default == nil {
return "", "", ErrNotInitialized
}
return Default.GetHTTPServiceAuth(hostport)
}
// GetMemcachedServiceAuth returns user/password creds given "admin"
// access to given memcached service. Uses default authenticator.
func GetMemcachedServiceAuth(hostport string) (user, pwd string, err error) {
if Default == nil {
return "", "", ErrNotInitialized
}
return Default.GetMemcachedServiceAuth(hostport)
}
// RegisterTLSRefreshCallback registers a callback to be called when any field
// of TLS settings change. The callback is called in separate routine
func RegisterTLSRefreshCallback(callback TLSRefreshCallback) error {
if Default == nil {
return ErrNotInitialized
}
Default.RegisterTLSRefreshCallback(callback)
return nil
}
func RegisterConfigRefreshCallback(callback ConfigRefreshCallback) error {
if Default == nil {
return ErrNotInitialized
}
Default.RegisterConfigRefreshCallback(callback)
return nil
}
// GetClientCertAuthType returns TLS cert type
func GetClientCertAuthType() (tls.ClientAuthType, error) {
if Default == nil {
return tls.NoClientCert, ErrNotInitialized
}
return Default.GetClientCertAuthType()
}
func GetClusterEncryptionConfig() (ClusterEncryptionConfig, error) {
if Default == nil {
return ClusterEncryptionConfig{}, ErrNotInitialized
}
return Default.GetClusterEncryptionConfig()
}
func GetUserUuid(user, domain string) (string, error) {
if Default == nil {
return "", ErrNotInitialized
}
return Default.GetUserUuid(user, domain)
}
func GetUserBuckets(user, domain string) ([]string, error) {
if Default == nil {
return []string{}, ErrNotInitialized
}
return Default.GetUserBuckets(user, domain)
}
func GetGuardrailStatuses() (GuardrailStatuses, error) {
if Default == nil {
return GuardrailStatuses{}, ErrNotInitialized
}
return Default.GetGuardrailStatuses()
}
// GetTLSConfig returns current tls config that contains cipher suites,
// min TLS version, etc.
func GetTLSConfig() (TLSConfig, error) {
if Default == nil {
return TLSConfig{}, ErrNotInitialized
}
return Default.GetTLSConfig()
}