refactor authentication (#1951)
* refactor authentication * refactor authentication
This commit is contained in:
@@ -156,8 +156,8 @@ func (s *ServerRunOptions) NewAPIServer(stopCh <-chan struct{}) (*apiserver.APIS
|
|||||||
}
|
}
|
||||||
|
|
||||||
var cacheClient cache.Interface
|
var cacheClient cache.Interface
|
||||||
if s.CacheOptions.RedisURL != "" {
|
if s.CacheOptions.Host != "" {
|
||||||
if s.CacheOptions.RedisURL == fakeInterface && s.DebugMode {
|
if s.CacheOptions.Host == fakeInterface && s.DebugMode {
|
||||||
apiServer.CacheClient = cache.NewSimpleCache()
|
apiServer.CacheClient = cache.NewSimpleCache()
|
||||||
} else {
|
} else {
|
||||||
cacheClient, err = cache.NewRedisClient(s.CacheOptions, stopCh)
|
cacheClient, err = cache.NewRedisClient(s.CacheOptions, stopCh)
|
||||||
|
|||||||
@@ -36,16 +36,17 @@ func NewAPIServerCommand() *cobra.Command {
|
|||||||
conf, err := apiserverconfig.TryLoadFromDisk()
|
conf, err := apiserverconfig.TryLoadFromDisk()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
s = &options.ServerRunOptions{
|
s = &options.ServerRunOptions{
|
||||||
KubernetesOptions: conf.KubernetesOptions,
|
GenericServerRunOptions: s.GenericServerRunOptions,
|
||||||
DevopsOptions: conf.DevopsOptions,
|
KubernetesOptions: conf.KubernetesOptions,
|
||||||
SonarQubeOptions: conf.SonarQubeOptions,
|
DevopsOptions: conf.DevopsOptions,
|
||||||
ServiceMeshOptions: conf.ServiceMeshOptions,
|
SonarQubeOptions: conf.SonarQubeOptions,
|
||||||
MySQLOptions: conf.MySQLOptions,
|
ServiceMeshOptions: conf.ServiceMeshOptions,
|
||||||
MonitoringOptions: conf.MonitoringOptions,
|
MySQLOptions: conf.MySQLOptions,
|
||||||
S3Options: conf.S3Options,
|
MonitoringOptions: conf.MonitoringOptions,
|
||||||
OpenPitrixOptions: conf.OpenPitrixOptions,
|
S3Options: conf.S3Options,
|
||||||
LoggingOptions: conf.LoggingOptions,
|
OpenPitrixOptions: conf.OpenPitrixOptions,
|
||||||
AuthenticateOptions: conf.AuthenticateOptions,
|
LoggingOptions: conf.LoggingOptions,
|
||||||
|
AuthenticateOptions: conf.AuthenticateOptions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,21 +8,21 @@ import (
|
|||||||
|
|
||||||
type AuthenticationOptions struct {
|
type AuthenticationOptions struct {
|
||||||
// authenticate rate limit will
|
// authenticate rate limit will
|
||||||
AuthenticateRateLimiterMaxTries int
|
AuthenticateRateLimiterMaxTries int `json:"authenticateRateLimiterMaxTries" yaml:"authenticateRateLimiterMaxTries"`
|
||||||
AuthenticateRateLimiterDuration time.Duration
|
AuthenticateRateLimiterDuration time.Duration `json:"authenticationRateLimiterDuration" yaml:"authenticationRateLimiterDuration"`
|
||||||
|
|
||||||
// maximum retries when authenticate failed
|
// maximum retries when authenticate failed
|
||||||
MaxAuthenticateRetries int
|
MaxAuthenticateRetries int `json:"maxAuthenticateRetries" yaml:"maxAuthenticateRetries"`
|
||||||
|
|
||||||
// token validation duration, will refresh token expiration for each user request
|
// token validation duration, will refresh token expiration for each user request
|
||||||
// 0 means never expire
|
// 0 means never expire
|
||||||
TokenExpiration time.Duration
|
TokenExpiration time.Duration `json:"tokenExpiration" yaml:"tokenExpiration"`
|
||||||
|
|
||||||
// allow multiple users login at the same time
|
// allow multiple users login at the same time
|
||||||
MultipleLogin bool
|
MultipleLogin bool `json:"multipleLogin" yaml:"multipleLogin"`
|
||||||
|
|
||||||
// secret to signed jwt token
|
// secret to signed jwt token
|
||||||
JwtSecret string
|
JwtSecret string `json:"jwtSecret" yaml:"jwtSecret"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAuthenticateOptions() *AuthenticationOptions {
|
func NewAuthenticateOptions() *AuthenticationOptions {
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ func parse(c *caddy.Controller) (*Rule, error) {
|
|||||||
return nil, c.ArgErr()
|
return nil, c.ArgErr()
|
||||||
}
|
}
|
||||||
|
|
||||||
options := &cache.Options{RedisURL: c.Val()}
|
options := &cache.Options{Host: c.Val()}
|
||||||
|
|
||||||
if err := options.Validate(); len(err) > 0 {
|
if err := options.Validate(); len(err) > 0 {
|
||||||
return nil, c.ArgErr()
|
return nil, c.ArgErr()
|
||||||
|
|||||||
@@ -143,9 +143,9 @@ func (s *APIServer) installKubeSphereAPIs() {
|
|||||||
urlruntime.Must(servicemeshv1alpha2.AddToContainer(s.container))
|
urlruntime.Must(servicemeshv1alpha2.AddToContainer(s.container))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *APIServer) Run(stopCh <-chan struct{}) error {
|
func (s *APIServer) Run(stopCh <-chan struct{}) (err error) {
|
||||||
|
|
||||||
err := s.waitForResourceSync(stopCh)
|
err = s.waitForResourceSync(stopCh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -160,10 +160,12 @@ func (s *APIServer) Run(stopCh <-chan struct{}) error {
|
|||||||
|
|
||||||
klog.V(0).Infof("Start listening on %s", s.Server.Addr)
|
klog.V(0).Infof("Start listening on %s", s.Server.Addr)
|
||||||
if s.Server.TLSConfig != nil {
|
if s.Server.TLSConfig != nil {
|
||||||
return s.Server.ListenAndServeTLS("", "")
|
err = s.Server.ListenAndServeTLS("", "")
|
||||||
} else {
|
} else {
|
||||||
return s.Server.ListenAndServe()
|
err = s.Server.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *APIServer) buildHandlerChain() {
|
func (s *APIServer) buildHandlerChain() {
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ type Config struct {
|
|||||||
// Options below are only loaded from configuration file, no command line flags for these options now.
|
// Options below are only loaded from configuration file, no command line flags for these options now.
|
||||||
KubeSphereOptions *kubesphere.Options `json:"-" yaml:"kubesphere,omitempty" mapstructure:"kubesphere"`
|
KubeSphereOptions *kubesphere.Options `json:"-" yaml:"kubesphere,omitempty" mapstructure:"kubesphere"`
|
||||||
|
|
||||||
AuthenticateOptions *iam.AuthenticationOptions `json:"authenticate,omitempty" yaml:"authenticate,omitempty" mapstructure:"authenticate"`
|
AuthenticateOptions *iam.AuthenticationOptions `json:"authentication,omitempty" yaml:"authenticate,omitempty" mapstructure:"authenticate"`
|
||||||
|
|
||||||
// Options used for enabling components, not actually used now. Once we switch Alerting/Notification API to kubesphere,
|
// Options used for enabling components, not actually used now. Once we switch Alerting/Notification API to kubesphere,
|
||||||
// we can add these options to kubesphere command lines
|
// we can add these options to kubesphere command lines
|
||||||
@@ -194,7 +194,7 @@ func (conf *Config) stripEmptyOptions() {
|
|||||||
conf.MySQLOptions = nil
|
conf.MySQLOptions = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.RedisOptions != nil && conf.RedisOptions.RedisURL == "" {
|
if conf.RedisOptions != nil && conf.RedisOptions.Host == "" {
|
||||||
conf.RedisOptions = nil
|
conf.RedisOptions = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,7 +64,10 @@ func newTestConfig() *Config {
|
|||||||
GroupSearchBase: "ou=Groups,dc=example,dc=org",
|
GroupSearchBase: "ou=Groups,dc=example,dc=org",
|
||||||
},
|
},
|
||||||
RedisOptions: &cache.Options{
|
RedisOptions: &cache.Options{
|
||||||
RedisURL: "redis://:qwerty@localhost:6379/1",
|
Host: "localhost:6379",
|
||||||
|
Port: 6379,
|
||||||
|
Password: "P@88w0rd",
|
||||||
|
DB: 0,
|
||||||
},
|
},
|
||||||
S3Options: &s3.Options{
|
S3Options: &s3.Options{
|
||||||
Endpoint: "http://minio.openpitrix-system.svc",
|
Endpoint: "http://minio.openpitrix-system.svc",
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ func (im *imOperator) Login(username, password, ip string) (*oauth2.Token, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: I think we should come up with a better strategy to prevent multiple login.
|
// TODO: I think we should come up with a better strategy to prevent multiple login.
|
||||||
tokenKey := tokenKeyForUsername(user.Username, "*")
|
tokenKey := tokenKeyForUsername(user.Username, issuedToken)
|
||||||
if !im.authenticateOptions.MultipleLogin {
|
if !im.authenticateOptions.MultipleLogin {
|
||||||
// multi login not allowed, remove the previous token
|
// multi login not allowed, remove the previous token
|
||||||
sessions, err := im.cacheClient.Keys(tokenKey)
|
sessions, err := im.cacheClient.Keys(tokenKey)
|
||||||
|
|||||||
34
pkg/simple/client/cache/options.go
vendored
34
pkg/simple/client/cache/options.go
vendored
@@ -1,20 +1,25 @@
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/go-redis/redis"
|
"fmt"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"kubesphere.io/kubesphere/pkg/utils/reflectutils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
RedisURL string
|
Host string `json:"host"`
|
||||||
|
Port int `json:"port"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
DB int `json:"db"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRedisOptions returns options points to nowhere,
|
// NewRedisOptions returns options points to nowhere,
|
||||||
// because redis is not required for some components
|
// because redis is not required for some components
|
||||||
func NewRedisOptions() *Options {
|
func NewRedisOptions() *Options {
|
||||||
return &Options{
|
return &Options{
|
||||||
RedisURL: "",
|
Host: "",
|
||||||
|
Port: 0,
|
||||||
|
Password: "",
|
||||||
|
DB: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,25 +27,20 @@ func NewRedisOptions() *Options {
|
|||||||
func (r *Options) Validate() []error {
|
func (r *Options) Validate() []error {
|
||||||
errors := make([]error, 0)
|
errors := make([]error, 0)
|
||||||
|
|
||||||
_, err := redis.ParseURL(r.RedisURL)
|
if r.Port == 0 {
|
||||||
|
errors = append(errors, fmt.Errorf("invalid service port number"))
|
||||||
if err != nil {
|
|
||||||
errors = append(errors, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors
|
return errors
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyTo apply to another options if it's a enabled option(non empty host)
|
|
||||||
func (r *Options) ApplyTo(options *Options) {
|
|
||||||
if r.RedisURL != "" {
|
|
||||||
reflectutils.Override(options, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddFlags add option flags to command line flags,
|
// AddFlags add option flags to command line flags,
|
||||||
// if redis-host left empty, the following options will be ignored.
|
// if redis-host left empty, the following options will be ignored.
|
||||||
func (r *Options) AddFlags(fs *pflag.FlagSet, s *Options) {
|
func (r *Options) AddFlags(fs *pflag.FlagSet, s *Options) {
|
||||||
fs.StringVar(&r.RedisURL, "redis-url", s.RedisURL, "Redis connection URL. If left blank, means redis is unnecessary, "+
|
fs.StringVar(&r.Host, "redis-host", s.Host, "Redis connection URL. If left blank, means redis is unnecessary, "+
|
||||||
"redis will be disabled. e.g. redis://:password@host:port/db")
|
"redis will be disabled.")
|
||||||
|
|
||||||
|
fs.IntVar(&r.Port, "redis-port", s.Port, "")
|
||||||
|
fs.StringVar(&r.Password, "redis-password", s.Password, "")
|
||||||
|
fs.IntVar(&r.DB, "redis-db", s.DB, "")
|
||||||
}
|
}
|
||||||
|
|||||||
15
pkg/simple/client/cache/redis.go
vendored
15
pkg/simple/client/cache/redis.go
vendored
@@ -18,6 +18,7 @@
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/go-redis/redis"
|
"github.com/go-redis/redis"
|
||||||
"k8s.io/klog"
|
"k8s.io/klog"
|
||||||
"time"
|
"time"
|
||||||
@@ -30,21 +31,19 @@ type Client struct {
|
|||||||
func NewRedisClient(option *Options, stopCh <-chan struct{}) (Interface, error) {
|
func NewRedisClient(option *Options, stopCh <-chan struct{}) (Interface, error) {
|
||||||
var r Client
|
var r Client
|
||||||
|
|
||||||
options, err := redis.ParseURL(option.RedisURL)
|
redisOptions := &redis.Options{
|
||||||
|
Addr: fmt.Sprintf("%s:%d", option.Host, option.Port),
|
||||||
if err != nil {
|
Password: option.Password,
|
||||||
klog.Error(err)
|
DB: option.DB,
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if stopCh == nil {
|
if stopCh == nil {
|
||||||
klog.Warningf("no stop signal passed, may cause redis connections leaked")
|
klog.Fatalf("no stop channel passed, redis connections will leak.")
|
||||||
}
|
}
|
||||||
|
|
||||||
r.client = redis.NewClient(options)
|
r.client = redis.NewClient(redisOptions)
|
||||||
|
|
||||||
if err := r.client.Ping().Err(); err != nil {
|
if err := r.client.Ping().Err(); err != nil {
|
||||||
klog.Error("unable to reach redis host", err)
|
|
||||||
r.client.Close()
|
r.client.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ func NewOptions() *Options {
|
|||||||
ManagerDN: "cn=admin,dc=example,dc=org",
|
ManagerDN: "cn=admin,dc=example,dc=org",
|
||||||
UserSearchBase: "ou=Users,dc=example,dc=org",
|
UserSearchBase: "ou=Users,dc=example,dc=org",
|
||||||
GroupSearchBase: "ou=Groups,dc=example,dc=org",
|
GroupSearchBase: "ou=Groups,dc=example,dc=org",
|
||||||
|
InitialCap: 10,
|
||||||
|
MaxCap: 100,
|
||||||
|
PoolName: "ldap",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user