@@ -19,6 +19,7 @@ import (
|
|||||||
"kubesphere.io/kubesphere/pkg/simple/client/mysql"
|
"kubesphere.io/kubesphere/pkg/simple/client/mysql"
|
||||||
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
|
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
|
||||||
"kubesphere.io/kubesphere/pkg/simple/client/s3"
|
"kubesphere.io/kubesphere/pkg/simple/client/s3"
|
||||||
|
fakeS3 "kubesphere.io/kubesphere/pkg/simple/client/s3/fake"
|
||||||
"kubesphere.io/kubesphere/pkg/simple/client/servicemesh"
|
"kubesphere.io/kubesphere/pkg/simple/client/servicemesh"
|
||||||
"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
|
"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -40,6 +41,9 @@ type ServerRunOptions struct {
|
|||||||
LdapOptions *ldap.Options
|
LdapOptions *ldap.Options
|
||||||
CacheOptions *cache.Options
|
CacheOptions *cache.Options
|
||||||
AuthenticateOptions *iam.AuthenticationOptions
|
AuthenticateOptions *iam.AuthenticationOptions
|
||||||
|
|
||||||
|
//
|
||||||
|
DebugMode bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerRunOptions() *ServerRunOptions {
|
func NewServerRunOptions() *ServerRunOptions {
|
||||||
@@ -64,7 +68,9 @@ func NewServerRunOptions() *ServerRunOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets) {
|
func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets) {
|
||||||
s.GenericServerRunOptions.AddFlags(fss.FlagSet("generic"), s.GenericServerRunOptions)
|
fs := fss.FlagSet("generic")
|
||||||
|
fs.BoolVar(&s.DebugMode, "debug", false, "Don't enable this if you don't know what it means.")
|
||||||
|
s.GenericServerRunOptions.AddFlags(fs, s.GenericServerRunOptions)
|
||||||
s.KubernetesOptions.AddFlags(fss.FlagSet("kubernetes"), s.KubernetesOptions)
|
s.KubernetesOptions.AddFlags(fss.FlagSet("kubernetes"), s.KubernetesOptions)
|
||||||
s.AuthenticateOptions.AddFlags(fss.FlagSet("authenticate"), s.AuthenticateOptions)
|
s.AuthenticateOptions.AddFlags(fss.FlagSet("authenticate"), s.AuthenticateOptions)
|
||||||
s.MySQLOptions.AddFlags(fss.FlagSet("mysql"), s.MySQLOptions)
|
s.MySQLOptions.AddFlags(fss.FlagSet("mysql"), s.MySQLOptions)
|
||||||
@@ -78,7 +84,7 @@ func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets) {
|
|||||||
s.MonitoringOptions.AddFlags(fss.FlagSet("monitoring"), s.MonitoringOptions)
|
s.MonitoringOptions.AddFlags(fss.FlagSet("monitoring"), s.MonitoringOptions)
|
||||||
s.LoggingOptions.AddFlags(fss.FlagSet("logging"), s.LoggingOptions)
|
s.LoggingOptions.AddFlags(fss.FlagSet("logging"), s.LoggingOptions)
|
||||||
|
|
||||||
fs := fss.FlagSet("klog")
|
fs = fss.FlagSet("klog")
|
||||||
local := flag.NewFlagSet("klog", flag.ExitOnError)
|
local := flag.NewFlagSet("klog", flag.ExitOnError)
|
||||||
klog.InitFlags(local)
|
klog.InitFlags(local)
|
||||||
local.VisitAll(func(fl *flag.Flag) {
|
local.VisitAll(func(fl *flag.Flag) {
|
||||||
@@ -89,6 +95,9 @@ func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets) {
|
|||||||
return fss
|
return fss
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fakeInterface string = "FAKE"
|
||||||
|
|
||||||
|
// NewAPIServer creates an APIServer instance using given options
|
||||||
func (s *ServerRunOptions) NewAPIServer(stopCh <-chan struct{}) (*apiserver.APIServer, error) {
|
func (s *ServerRunOptions) NewAPIServer(stopCh <-chan struct{}) (*apiserver.APIServer, error) {
|
||||||
apiServer := &apiserver.APIServer{}
|
apiServer := &apiserver.APIServer{}
|
||||||
|
|
||||||
@@ -113,11 +122,15 @@ func (s *ServerRunOptions) NewAPIServer(stopCh <-chan struct{}) (*apiserver.APIS
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.S3Options.Endpoint != "" {
|
if s.S3Options.Endpoint != "" {
|
||||||
s3Client, err := s3.NewS3Client(s.S3Options)
|
if s.S3Options.Endpoint == fakeInterface && s.DebugMode {
|
||||||
if err != nil {
|
apiServer.S3Client = fakeS3.NewFakeS3()
|
||||||
return nil, err
|
} else {
|
||||||
|
s3Client, err := s3.NewS3Client(s.S3Options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
apiServer.S3Client = s3Client
|
||||||
}
|
}
|
||||||
apiServer.S3Client = s3Client
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.DevopsOptions.Host != "" {
|
if s.DevopsOptions.Host != "" {
|
||||||
@@ -129,19 +142,28 @@ func (s *ServerRunOptions) NewAPIServer(stopCh <-chan struct{}) (*apiserver.APIS
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.LdapOptions.Host != "" {
|
if s.LdapOptions.Host != "" {
|
||||||
ldapClient, err := ldap.NewLdapClient(s.LdapOptions, stopCh)
|
if s.LdapOptions.Host == fakeInterface && s.DebugMode {
|
||||||
if err != nil {
|
apiServer.LdapClient = ldap.NewSimpleLdap()
|
||||||
return nil, err
|
} else {
|
||||||
|
ldapClient, err := ldap.NewLdapClient(s.LdapOptions, stopCh)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
apiServer.LdapClient = ldapClient
|
||||||
}
|
}
|
||||||
apiServer.LdapClient = ldapClient
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cacheClient cache.Interface
|
||||||
if s.CacheOptions.RedisURL != "" {
|
if s.CacheOptions.RedisURL != "" {
|
||||||
cacheClient, err := cache.NewRedisClient(s.CacheOptions, stopCh)
|
if s.CacheOptions.RedisURL == fakeInterface && s.DebugMode {
|
||||||
if err != nil {
|
apiServer.CacheClient = cache.NewSimpleCache()
|
||||||
return nil, err
|
} else {
|
||||||
|
cacheClient, err = cache.NewRedisClient(s.CacheOptions, stopCh)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
apiServer.CacheClient = cacheClient
|
||||||
}
|
}
|
||||||
apiServer.CacheClient = cacheClient
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.MySQLOptions.Host != "" {
|
if s.MySQLOptions.Host != "" {
|
||||||
|
|||||||
@@ -7,23 +7,30 @@ import (
|
|||||||
"kubesphere.io/kubesphere/pkg/simple/client/cache"
|
"kubesphere.io/kubesphere/pkg/simple/client/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TokenAuthenticator struct {
|
// TokenAuthenticator implements kubernetes token authenticate interface with our custom logic.
|
||||||
|
// TokenAuthenticator will retrieve user info from cache by given token. If empty or invalid token
|
||||||
|
// was given, authenticator will still give passed response at the condition user will be user.Anonymous
|
||||||
|
// and group from user.AllUnauthenticated. This helps requests be passed along the handler chain,
|
||||||
|
// because some resources are public accessible.
|
||||||
|
type tokenAuthenticator struct {
|
||||||
cacheClient cache.Interface
|
cacheClient cache.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTokenAuthenticator(cacheClient cache.Interface) authenticator.Token {
|
func NewTokenAuthenticator(cacheClient cache.Interface) authenticator.Token {
|
||||||
return &TokenAuthenticator{
|
return &tokenAuthenticator{
|
||||||
cacheClient: cacheClient,
|
cacheClient: cacheClient,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TokenAuthenticator) AuthenticateToken(ctx context.Context, token string) (*authenticator.Response, bool, error) {
|
func (t *tokenAuthenticator) AuthenticateToken(ctx context.Context, token string) (*authenticator.Response, bool, error) {
|
||||||
|
//if len(token) == 0 {
|
||||||
return &authenticator.Response{
|
return &authenticator.Response{
|
||||||
User: &user.DefaultInfo{
|
User: &user.DefaultInfo{
|
||||||
Name: "admin",
|
Name: user.Anonymous,
|
||||||
UID: "",
|
UID: "",
|
||||||
Groups: nil,
|
Groups: []string{user.AllUnauthenticated},
|
||||||
Extra: nil,
|
Extra: nil,
|
||||||
},
|
},
|
||||||
}, true, nil
|
}, true, nil
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -188,6 +188,7 @@ func (conf *Config) toMap() map[string]bool {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove invalid options before serializing to json or yaml
|
||||||
func (conf *Config) stripEmptyOptions() {
|
func (conf *Config) stripEmptyOptions() {
|
||||||
if conf.MySQLOptions != nil && conf.MySQLOptions.Host == "" {
|
if conf.MySQLOptions != nil && conf.MySQLOptions.Host == "" {
|
||||||
conf.MySQLOptions = nil
|
conf.MySQLOptions = nil
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/util/proxy"
|
"k8s.io/apimachinery/pkg/util/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Dispatcher defines how to forward request to desired cluster apiserver
|
// Dispatcher defines how to forward request to designated cluster based on cluster name
|
||||||
type Dispatcher interface {
|
type Dispatcher interface {
|
||||||
Dispatch(w http.ResponseWriter, req *http.Request)
|
Dispatch(w http.ResponseWriter, req *http.Request)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// WithAuthentication installs authentication handler to handler chain.
|
||||||
func WithAuthentication(handler http.Handler, auth authenticator.Request, failed http.Handler) http.Handler {
|
func WithAuthentication(handler http.Handler, auth authenticator.Request, failed http.Handler) http.Handler {
|
||||||
if auth == nil {
|
if auth == nil {
|
||||||
klog.Warningf("Authentication is disabled")
|
klog.Warningf("Authentication is disabled")
|
||||||
|
|||||||
@@ -29,15 +29,18 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// default re-sync period for all informer factories
|
||||||
const defaultResync = 600 * time.Second
|
const defaultResync = 600 * time.Second
|
||||||
|
|
||||||
|
// InformerFactory is a group all shared informer factories which kubesphere needed
|
||||||
|
// callers should check if the return value is nil
|
||||||
type InformerFactory interface {
|
type InformerFactory interface {
|
||||||
KubernetesSharedInformerFactory() k8sinformers.SharedInformerFactory
|
KubernetesSharedInformerFactory() k8sinformers.SharedInformerFactory
|
||||||
KubeSphereSharedInformerFactory() ksinformers.SharedInformerFactory
|
KubeSphereSharedInformerFactory() ksinformers.SharedInformerFactory
|
||||||
IstioSharedInformerFactory() istioinformers.SharedInformerFactory
|
IstioSharedInformerFactory() istioinformers.SharedInformerFactory
|
||||||
ApplicationSharedInformerFactory() applicationinformers.SharedInformerFactory
|
ApplicationSharedInformerFactory() applicationinformers.SharedInformerFactory
|
||||||
|
|
||||||
// Start all the informer factories if not nil
|
// Start shared informer factory one by one if they are not nil
|
||||||
Start(stopCh <-chan struct{})
|
Start(stopCh <-chan struct{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
pkg/simple/client/cache/cache.go
vendored
2
pkg/simple/client/cache/cache.go
vendored
@@ -2,6 +2,8 @@ package cache
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
var NeverExpire = time.Duration(0)
|
||||||
|
|
||||||
type Interface interface {
|
type Interface interface {
|
||||||
// Keys retrieves all keys match the given pattern
|
// Keys retrieves all keys match the given pattern
|
||||||
Keys(pattern string) ([]string, error)
|
Keys(pattern string) ([]string, error)
|
||||||
|
|||||||
105
pkg/simple/client/cache/simple_cache.go
vendored
105
pkg/simple/client/cache/simple_cache.go
vendored
@@ -1,40 +1,111 @@
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"kubesphere.io/kubesphere/pkg/server/errors"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrNoSuchKey = errors.New("no such key")
|
||||||
|
|
||||||
type simpleObject struct {
|
type simpleObject struct {
|
||||||
value string
|
value string
|
||||||
expire time.Time
|
neverExpire bool
|
||||||
|
expiredAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type SimpleCache struct {
|
// SimpleCache implements cache.Interface use memory objects, it should be used only for testing
|
||||||
|
type simpleCache struct {
|
||||||
store map[string]simpleObject
|
store map[string]simpleObject
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSimpleCache() Interface {
|
func NewSimpleCache() Interface {
|
||||||
return &SimpleCache{store: make(map[string]simpleObject)}
|
return &simpleCache{store: make(map[string]simpleObject)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleCache) Keys(pattern string) ([]string, error) {
|
func (s *simpleCache) Keys(pattern string) ([]string, error) {
|
||||||
panic("implement me")
|
// There is a little difference between go regexp and redis key pattern
|
||||||
|
// In redis, * means any character, while in go . means match everything.
|
||||||
|
pattern = strings.Replace(pattern, "*", ".", -1)
|
||||||
|
|
||||||
|
re, err := regexp.Compile(pattern)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var keys []string
|
||||||
|
for k, _ := range s.store {
|
||||||
|
if re.MatchString(k) {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleCache) Set(key string, value string, duration time.Duration) error {
|
func (s *simpleCache) Set(key string, value string, duration time.Duration) error {
|
||||||
panic("implement me")
|
sobject := simpleObject{
|
||||||
|
value: value,
|
||||||
|
neverExpire: false,
|
||||||
|
expiredAt: time.Now().Add(duration),
|
||||||
|
}
|
||||||
|
|
||||||
|
if duration == NeverExpire {
|
||||||
|
sobject.neverExpire = true
|
||||||
|
}
|
||||||
|
|
||||||
|
s.store[key] = sobject
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleCache) Del(keys ...string) error {
|
func (s *simpleCache) Del(keys ...string) error {
|
||||||
panic("implement me")
|
for _, key := range keys {
|
||||||
|
if _, ok := s.store[key]; ok {
|
||||||
|
delete(s.store, key)
|
||||||
|
} else {
|
||||||
|
return ErrNoSuchKey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleCache) Get(key string) (string, error) {
|
func (s *simpleCache) Get(key string) (string, error) {
|
||||||
return "", nil
|
if sobject, ok := s.store[key]; ok {
|
||||||
|
if sobject.neverExpire || time.Now().Before(sobject.expiredAt) {
|
||||||
|
return sobject.value, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", ErrNoSuchKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleCache) Exists(keys ...string) (bool, error) {
|
func (s *simpleCache) Exists(keys ...string) (bool, error) {
|
||||||
panic("implement me")
|
for _, key := range keys {
|
||||||
|
if _, ok := s.store[key]; !ok {
|
||||||
|
return false, ErrNoSuchKey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleCache) Expire(key string, duration time.Duration) error {
|
func (s *simpleCache) Expire(key string, duration time.Duration) error {
|
||||||
panic("implement me")
|
value, err := s.Get(key)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sobject := simpleObject{
|
||||||
|
value: value,
|
||||||
|
neverExpire: false,
|
||||||
|
expiredAt: time.Now().Add(duration),
|
||||||
|
}
|
||||||
|
|
||||||
|
if duration == NeverExpire {
|
||||||
|
sobject.neverExpire = true
|
||||||
|
}
|
||||||
|
|
||||||
|
s.store[key] = sobject
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
123
pkg/simple/client/cache/simple_cache_test.go
vendored
Normal file
123
pkg/simple/client/cache/simple_cache_test.go
vendored
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var dataSet = map[string]string{
|
||||||
|
"foo1": "val1",
|
||||||
|
"foo2": "val2",
|
||||||
|
"foo3": "val3",
|
||||||
|
"bar1": "val1",
|
||||||
|
"bar2": "val2",
|
||||||
|
}
|
||||||
|
|
||||||
|
// load dataset into cache
|
||||||
|
func load(client Interface, data map[string]string) error {
|
||||||
|
for k, v := range data {
|
||||||
|
err := client.Set(k, v, NeverExpire)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// dump retrieve all data in simple into a map
|
||||||
|
func dump(client Interface) (map[string]string, error) {
|
||||||
|
keys, err := client.Keys("*")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshot := make(map[string]string)
|
||||||
|
for _, key := range keys {
|
||||||
|
val, err := client.Get(key)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
snapshot[key] = val
|
||||||
|
}
|
||||||
|
|
||||||
|
return snapshot, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeleteAndExpireCache(t *testing.T) {
|
||||||
|
var testCases = []struct {
|
||||||
|
description string
|
||||||
|
deleteKeys sets.String
|
||||||
|
expireKeys sets.String
|
||||||
|
expireDuration time.Duration // never use a 0(NeverExpires) duration with expireKeys, recommend time.Millisecond * 500.
|
||||||
|
expected map[string]string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
description: "Should get all keys",
|
||||||
|
expected: map[string]string{
|
||||||
|
"foo1": "val1",
|
||||||
|
"foo2": "val2",
|
||||||
|
"foo3": "val3",
|
||||||
|
"bar1": "val1",
|
||||||
|
"bar2": "val2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Test delete should get only keys start with foo",
|
||||||
|
expected: map[string]string{
|
||||||
|
"foo1": "val1",
|
||||||
|
"foo2": "val2",
|
||||||
|
"foo3": "val3",
|
||||||
|
},
|
||||||
|
deleteKeys: sets.NewString("bar1", "bar2"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Should get only keys start with bar",
|
||||||
|
expected: map[string]string{
|
||||||
|
"bar1": "val1",
|
||||||
|
"bar2": "val2",
|
||||||
|
},
|
||||||
|
expireDuration: time.Millisecond * 500,
|
||||||
|
expireKeys: sets.NewString("foo1", "foo2", "foo3"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
cacheClient := NewSimpleCache()
|
||||||
|
|
||||||
|
t.Run(testCase.description, func(t *testing.T) {
|
||||||
|
err := load(cacheClient, dataSet)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to load dataset, got error %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(testCase.deleteKeys) != 0 {
|
||||||
|
err = cacheClient.Del(testCase.deleteKeys.List()...)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error delete keys, %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(testCase.expireKeys) != 0 && testCase.expireDuration != 0 {
|
||||||
|
for _, key := range testCase.expireKeys.List() {
|
||||||
|
err = cacheClient.Expire(key, testCase.expireDuration)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error expire keys, %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time.Sleep(testCase.expireDuration)
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err := dump(cacheClient)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error dump data, %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if diff := cmp.Diff(got, testCase.expected); len(diff) != 0 {
|
||||||
|
t.Errorf("%T differ (-got, +expected) %v", testCase.expected, diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
21
pkg/simple/client/ldap/interface.go
Normal file
21
pkg/simple/client/ldap/interface.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package ldap
|
||||||
|
|
||||||
|
import "kubesphere.io/kubesphere/pkg/api/iam"
|
||||||
|
|
||||||
|
// Interface defines CRUD behaviors of manipulating users
|
||||||
|
type Interface interface {
|
||||||
|
// Create create a new user in ldap
|
||||||
|
Create(user *iam.User) error
|
||||||
|
|
||||||
|
// Update updates a user information, return error if user not exists
|
||||||
|
Update(user *iam.User) error
|
||||||
|
|
||||||
|
// Delete deletes a user from ldap, return nil if user not exists
|
||||||
|
Delete(name string) error
|
||||||
|
|
||||||
|
// Get gets a user by its username from ldap, return ErrUserNotExists if user not exists
|
||||||
|
Get(name string) (*iam.User, error)
|
||||||
|
|
||||||
|
// Verify checks if (name, password) is valid, return ErrInvalidCredentials if not
|
||||||
|
Verify(name string, password string) error
|
||||||
|
}
|
||||||
@@ -27,23 +27,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Interface interface {
|
|
||||||
// Create create a new user in ldap
|
|
||||||
Create(user *iam.User) error
|
|
||||||
|
|
||||||
// Update updates a user information, return error if user not exists
|
|
||||||
Update(user *iam.User) error
|
|
||||||
|
|
||||||
// Delete deletes a user from ldap, return nil if user not exists
|
|
||||||
Delete(name string) error
|
|
||||||
|
|
||||||
// Get gets a user by its username from ldap
|
|
||||||
Get(name string) (*iam.User, error)
|
|
||||||
|
|
||||||
//
|
|
||||||
Verify(name string, password string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ldapAttributeObjectClass = "objectClass"
|
ldapAttributeObjectClass = "objectClass"
|
||||||
ldapAttributeCommonName = "cn"
|
ldapAttributeCommonName = "cn"
|
||||||
|
|||||||
57
pkg/simple/client/ldap/simple_ldap.go
Normal file
57
pkg/simple/client/ldap/simple_ldap.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package ldap
|
||||||
|
|
||||||
|
import "kubesphere.io/kubesphere/pkg/api/iam"
|
||||||
|
|
||||||
|
// simpleLdap is a implementation of ldap.Interface, you should never use this in production env!
|
||||||
|
type simpleLdap struct {
|
||||||
|
store map[string]*iam.User
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSimpleLdap() Interface {
|
||||||
|
return &simpleLdap{
|
||||||
|
store: map[string]*iam.User{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s simpleLdap) Create(user *iam.User) error {
|
||||||
|
s.store[user.Username] = user
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s simpleLdap) Update(user *iam.User) error {
|
||||||
|
_, err := s.Get(user.Username)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.store[user.Username] = user
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s simpleLdap) Delete(name string) error {
|
||||||
|
_, err := s.Get(name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
delete(s.store, name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s simpleLdap) Get(name string) (*iam.User, error) {
|
||||||
|
if user, ok := s.store[name]; !ok {
|
||||||
|
return nil, ErrUserNotExists
|
||||||
|
} else {
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s simpleLdap) Verify(name string, password string) error {
|
||||||
|
if user, err := s.Get(name); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
if user.Password != password {
|
||||||
|
return ErrInvalidCredentials
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
98
pkg/simple/client/ldap/simple_ldap_test.go
Normal file
98
pkg/simple/client/ldap/simple_ldap_test.go
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
package ldap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"kubesphere.io/kubesphere/pkg/api/iam"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSimpleLdap(t *testing.T) {
|
||||||
|
ldapClient := NewSimpleLdap()
|
||||||
|
|
||||||
|
foo := &iam.User{
|
||||||
|
Username: "jerry",
|
||||||
|
Email: "jerry@kubesphere.io",
|
||||||
|
Lang: "en",
|
||||||
|
Description: "Jerry is kind and gentle.",
|
||||||
|
CreateTime: time.Now(),
|
||||||
|
Groups: []string{},
|
||||||
|
Password: "P@88w0rd",
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("should create user", func(t *testing.T) {
|
||||||
|
err := ldapClient.Create(foo)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if user really created
|
||||||
|
user, err := ldapClient.Get(foo.Username)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(user, foo); len(diff) != 0 {
|
||||||
|
t.Fatalf("%T differ (-got, +want): %s", user, diff)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = ldapClient.Delete(foo.Username)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("should update user", func(t *testing.T) {
|
||||||
|
err := ldapClient.Create(foo)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
foo.Description = "Jerry needs some drinks."
|
||||||
|
err = ldapClient.Update(foo)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if user really created
|
||||||
|
user, err := ldapClient.Get(foo.Username)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(user, foo); len(diff) != 0 {
|
||||||
|
t.Fatalf("%T differ (-got, +want): %s", user, diff)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = ldapClient.Delete(foo.Username)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("should delete user", func(t *testing.T) {
|
||||||
|
err := ldapClient.Create(foo)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ldapClient.Delete(foo.Username)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ldapClient.Get(foo.Username)
|
||||||
|
if err == nil || err != ErrUserNotExists {
|
||||||
|
t.Fatalf("expected ErrUserNotExists error, got %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("should verify username and password", func(t *testing.T) {
|
||||||
|
err := ldapClient.Create(foo)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ldapClient.Verify(foo.Username, foo.Password)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("should pass but got an error %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ldapClient.Verify(foo.Username, "gibberish")
|
||||||
|
if err == nil || err != ErrInvalidCredentials {
|
||||||
|
t.Fatalf("expected error ErrInvalidCrenentials but got %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user