feat(auth): support multiple identity provider associations (#6299)

Signed-off-by: hongming <coder.scala@gmail.com>
This commit is contained in:
hongming
2025-02-28 16:48:36 +08:00
committed by GitHub
parent 99d2408c34
commit 018f6045ee
12 changed files with 177 additions and 221 deletions

View File

@@ -8,6 +8,7 @@ package identityprovider
import (
"context"
"errors"
"fmt"
"gopkg.in/yaml.v3"
v1 "k8s.io/api/core/v1"
@@ -35,6 +36,8 @@ const (
var ErrorIdentityProviderNotFound = errors.New("the Identity provider was not found")
var IdentityProviderIsDisabled = errors.New("the Identity provider was Disabled")
type MappingMethod string
type Configuration struct {
@@ -52,6 +55,15 @@ type Configuration struct {
// The type of identity provider
Type string `json:"type" yaml:"type"`
// The hidden of login button
Hidden bool `json:"hidden" yaml:"hidden"`
// The disabled of identify provider
Disabled bool `json:"disabled" yaml:"disabled"`
// The display name of identify provider
DisplayName string `json:"displayName" yaml:"displayName"`
// The options of identify provider
ProviderOptions options.DynamicOptions `json:"provider" yaml:"provider"`
}
@@ -72,19 +84,19 @@ type configurationGetter struct {
func (o *configurationGetter) ListConfigurations(ctx context.Context) ([]*Configuration, error) {
configurations := make([]*Configuration, 0)
secrets := &v1.SecretList{}
if err := o.List(ctx, secrets, client.InNamespace(constants.KubeSphereNamespace), client.MatchingLabels{constants.GenericConfigTypeLabel: ConfigTypeIdentityProvider}); err != nil {
klog.Errorf("failed to list secrets: %v", err)
return nil, err
if err := o.List(ctx, secrets, client.InNamespace(constants.KubeSphereNamespace),
client.MatchingLabels{constants.GenericConfigTypeLabel: ConfigTypeIdentityProvider}); err != nil {
return nil, fmt.Errorf("failed to list secrets: %s", err)
}
for _, secret := range secrets.Items {
if secret.Type != SecretTypeIdentityProvider {
continue
}
if c, err := UnmarshalFrom(&secret); err != nil {
if config, err := UnmarshalFrom(&secret); err != nil {
klog.Errorf("failed to unmarshal secret data: %s", err)
continue
} else {
configurations = append(configurations, c)
configurations = append(configurations, config)
}
}
return configurations, nil
@@ -93,23 +105,25 @@ func (o *configurationGetter) ListConfigurations(ctx context.Context) ([]*Config
func (o *configurationGetter) GetConfiguration(ctx context.Context, name string) (*Configuration, error) {
configurations, err := o.ListConfigurations(ctx)
if err != nil {
klog.Errorf("failed to list identity providers: %v", err)
return nil, err
return nil, fmt.Errorf("failed to list configurations: %s", err)
}
for _, c := range configurations {
if c.Name == name {
return c, nil
for _, config := range configurations {
if config.Name == name {
if config.Disabled {
return nil, IdentityProviderIsDisabled
}
return config, nil
}
}
return nil, ErrorIdentityProviderNotFound
}
func UnmarshalFrom(secret *v1.Secret) (*Configuration, error) {
c := &Configuration{}
if err := yaml.Unmarshal(secret.Data[SecretDataKey], c); err != nil {
return nil, err
config := &Configuration{}
if err := yaml.Unmarshal(secret.Data[SecretDataKey], config); err != nil {
return nil, fmt.Errorf("failed to unmarshal secret data: %s", err)
}
return c, nil
return config, nil
}
func IsIdentityProviderConfiguration(secret *v1.Secret) bool {