Browse Source

auth: add "IsAuthEnabled" method

Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
Gyuho Lee 7 years ago
parent
commit
f0eb772963
2 changed files with 10 additions and 7 deletions
  1. 9 6
      auth/store.go
  2. 1 1
      auth/store_test.go

+ 9 - 6
auth/store.go

@@ -94,6 +94,9 @@ type AuthStore interface {
 	// AuthDisable turns off the authentication feature
 	// AuthDisable turns off the authentication feature
 	AuthDisable()
 	AuthDisable()
 
 
+	// IsAuthEnabled returns true if the authentication feature is enabled.
+	IsAuthEnabled() bool
+
 	// Authenticate does authentication based on given user name and password
 	// Authenticate does authentication based on given user name and password
 	Authenticate(ctx context.Context, username, password string) (*pb.AuthenticateResponse, error)
 	Authenticate(ctx context.Context, username, password string) (*pb.AuthenticateResponse, error)
 
 
@@ -269,7 +272,7 @@ func (as *authStore) Close() error {
 }
 }
 
 
 func (as *authStore) Authenticate(ctx context.Context, username, password string) (*pb.AuthenticateResponse, error) {
 func (as *authStore) Authenticate(ctx context.Context, username, password string) (*pb.AuthenticateResponse, error) {
-	if !as.isAuthEnabled() {
+	if !as.IsAuthEnabled() {
 		return nil, ErrAuthNotEnabled
 		return nil, ErrAuthNotEnabled
 	}
 	}
 
 
@@ -295,7 +298,7 @@ func (as *authStore) Authenticate(ctx context.Context, username, password string
 }
 }
 
 
 func (as *authStore) CheckPassword(username, password string) (uint64, error) {
 func (as *authStore) CheckPassword(username, password string) (uint64, error) {
-	if !as.isAuthEnabled() {
+	if !as.IsAuthEnabled() {
 		return 0, ErrAuthNotEnabled
 		return 0, ErrAuthNotEnabled
 	}
 	}
 
 
@@ -732,7 +735,7 @@ func (as *authStore) RoleGrantPermission(r *pb.AuthRoleGrantPermissionRequest) (
 
 
 func (as *authStore) isOpPermitted(userName string, revision uint64, key, rangeEnd []byte, permTyp authpb.Permission_Type) error {
 func (as *authStore) isOpPermitted(userName string, revision uint64, key, rangeEnd []byte, permTyp authpb.Permission_Type) error {
 	// TODO(mitake): this function would be costly so we need a caching mechanism
 	// TODO(mitake): this function would be costly so we need a caching mechanism
-	if !as.isAuthEnabled() {
+	if !as.IsAuthEnabled() {
 		return nil
 		return nil
 	}
 	}
 
 
@@ -780,7 +783,7 @@ func (as *authStore) IsDeleteRangePermitted(authInfo *AuthInfo, key, rangeEnd []
 }
 }
 
 
 func (as *authStore) IsAdminPermitted(authInfo *AuthInfo) error {
 func (as *authStore) IsAdminPermitted(authInfo *AuthInfo) error {
-	if !as.isAuthEnabled() {
+	if !as.IsAuthEnabled() {
 		return nil
 		return nil
 	}
 	}
 	if authInfo == nil {
 	if authInfo == nil {
@@ -892,7 +895,7 @@ func delRole(tx backend.BatchTx, rolename string) {
 	tx.UnsafeDelete(authRolesBucketName, []byte(rolename))
 	tx.UnsafeDelete(authRolesBucketName, []byte(rolename))
 }
 }
 
 
-func (as *authStore) isAuthEnabled() bool {
+func (as *authStore) IsAuthEnabled() bool {
 	as.enabledMu.RLock()
 	as.enabledMu.RLock()
 	defer as.enabledMu.RUnlock()
 	defer as.enabledMu.RUnlock()
 	return as.enabled
 	return as.enabled
@@ -1064,7 +1067,7 @@ func NewTokenProvider(tokenOpts string, indexWaiter func(uint64) <-chan struct{}
 }
 }
 
 
 func (as *authStore) WithRoot(ctx context.Context) context.Context {
 func (as *authStore) WithRoot(ctx context.Context) context.Context {
-	if !as.isAuthEnabled() {
+	if !as.IsAuthEnabled() {
 		return ctx
 		return ctx
 	}
 	}
 
 

+ 1 - 1
auth/store_test.go

@@ -588,7 +588,7 @@ func TestRecoverFromSnapshot(t *testing.T) {
 		a.Close()
 		a.Close()
 	}(as2)
 	}(as2)
 
 
-	if !as2.isAuthEnabled() {
+	if !as2.IsAuthEnabled() {
 		t.Fatal("recovering authStore from existing backend failed")
 		t.Fatal("recovering authStore from existing backend failed")
 	}
 	}