github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/credential/credentials.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package credential
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net/http"
    23  
    24  	"github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic"
    25  	"github.com/kubewharf/katalyst-core/pkg/config/generic"
    26  )
    27  
    28  type AuthType string
    29  
    30  const (
    31  	AuthTypeBasicAuth = "Basic"
    32  	AuthTypeInsecure  = "Insecure"
    33  )
    34  
    35  // AuthInfo defines the common interface for the auth information the users are interested in.
    36  // A struct that implements AuthInfo can hold other information the corresponding protocol can provide besides the
    37  // interface defined.
    38  type AuthInfo interface {
    39  	// AuthType return the authentication protocol.
    40  	AuthType() AuthType
    41  	// SubjectName return the subject name it holds.
    42  	SubjectName() string
    43  }
    44  
    45  // Credential defines common interface for all authentication protocol(e.g., BasicAuth, JWT Token).
    46  type Credential interface {
    47  	// AuthType return the authentication protocol.
    48  	AuthType() AuthType
    49  	// Auth takes a http request parameter and uses corresponding protocol to retrieve AuthInfo from the request.
    50  	Auth(r *http.Request) (AuthInfo, error)
    51  	// AuthToken takes a raw token string parameter and uses corresponding protocol to retrieve AuthInfo from it.
    52  	AuthToken(token string) (AuthInfo, error)
    53  	// Run starts the Credential component
    54  	Run(ctx context.Context)
    55  }
    56  
    57  type NewCredentialFunc func(authConfig *generic.AuthConfiguration, dynamicConfig *dynamic.DynamicAgentConfiguration) (Credential, error)
    58  
    59  var credentialInitializer = make(map[AuthType]NewCredentialFunc)
    60  
    61  func RegisterCredentialInitializer(authType AuthType, initializer NewCredentialFunc) {
    62  	credentialInitializer[authType] = initializer
    63  }
    64  
    65  func GetCredentialInitializer() map[AuthType]NewCredentialFunc {
    66  	return credentialInitializer
    67  }
    68  
    69  func init() {
    70  	RegisterCredentialInitializer(AuthTypeBasicAuth, NewBasicAuthCredential)
    71  	RegisterCredentialInitializer(AuthTypeInsecure, NewInsecureCredential)
    72  }
    73  
    74  func GetCredential(genericConf *generic.GenericConfiguration, dynamicConfig *dynamic.DynamicAgentConfiguration) (Credential, error) {
    75  	credentialInitializer, ok := GetCredentialInitializer()[AuthType(genericConf.AuthConfiguration.AuthType)]
    76  	if ok {
    77  		cred, err := credentialInitializer(genericConf.AuthConfiguration, dynamicConfig)
    78  		if err != nil {
    79  			return nil, fmt.Errorf("initialize credential failed,type: %v, err: %v", genericConf.AuthType, err)
    80  		}
    81  
    82  		return cred, nil
    83  	} else {
    84  		return nil, fmt.Errorf("unsupported credential type: %v", genericConf.AuthConfiguration.AuthType)
    85  	}
    86  }
    87  
    88  func DefaultCredential() Credential {
    89  	return &insecureCredential{}
    90  }