login_recoder.go 2.5 KB
Newer Older
Z
zryfish 已提交
1
/*
H
hongming 已提交
2
Copyright 2020 KubeSphere Authors
Z
zryfish 已提交
3

H
hongming 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package auth
Z
zryfish 已提交
18 19

import (
H
hongming 已提交
20
	"context"
Z
zryfish 已提交
21
	"fmt"
H
hongming 已提交
22
	"k8s.io/apimachinery/pkg/api/errors"
Z
zryfish 已提交
23 24 25 26
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/klog"
	iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2"
	kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
H
hongming 已提交
27
	iamv1alpha2listers "kubesphere.io/kubesphere/pkg/client/listers/iam/v1alpha2"
Z
zryfish 已提交
28 29 30
)

type LoginRecorder interface {
H
hongming 已提交
31
	RecordLogin(username string, loginType iamv1alpha2.LoginType, provider string, sourceIP string, userAgent string, authErr error) error
Z
zryfish 已提交
32 33 34
}

type loginRecorder struct {
H
hongming 已提交
35 36
	ksClient   kubesphere.Interface
	userGetter *userGetter
Z
zryfish 已提交
37 38
}

H
hongming 已提交
39
func NewLoginRecorder(ksClient kubesphere.Interface, userLister iamv1alpha2listers.UserLister) LoginRecorder {
Z
zryfish 已提交
40
	return &loginRecorder{
H
hongming 已提交
41 42
		ksClient:   ksClient,
		userGetter: &userGetter{userLister: userLister},
Z
zryfish 已提交
43 44 45
	}
}

H
hongming 已提交
46 47 48 49 50 51 52 53 54 55 56 57
// RecordLogin Create v1alpha2.LoginRecord for existing accounts
func (l *loginRecorder) RecordLogin(username string, loginType iamv1alpha2.LoginType, provider, sourceIP, userAgent string, authErr error) error {
	// only for existing accounts, solve the problem of huge entries
	user, err := l.userGetter.findUser(username)
	if err != nil {
		// ignore not found error
		if errors.IsNotFound(err) {
			return nil
		}
		klog.Error(err)
		return err
	}
Z
zryfish 已提交
58 59
	loginEntry := &iamv1alpha2.LoginRecord{
		ObjectMeta: metav1.ObjectMeta{
H
hongming 已提交
60
			GenerateName: fmt.Sprintf("%s-", user.Name),
Z
zryfish 已提交
61
			Labels: map[string]string{
H
hongming 已提交
62
				iamv1alpha2.UserReferenceLabel: user.Name,
Z
zryfish 已提交
63 64 65
			},
		},
		Spec: iamv1alpha2.LoginRecordSpec{
66 67 68 69
			Type:      loginType,
			Provider:  provider,
			Success:   true,
			Reason:    iamv1alpha2.AuthenticatedSuccessfully,
H
hongming 已提交
70 71
			SourceIP:  sourceIP,
			UserAgent: userAgent,
Z
zryfish 已提交
72 73 74 75
		},
	}

	if authErr != nil {
76
		loginEntry.Spec.Success = false
Z
zryfish 已提交
77 78 79
		loginEntry.Spec.Reason = authErr.Error()
	}

H
hongming 已提交
80
	_, err = l.ksClient.IamV1alpha2().LoginRecords().Create(context.Background(), loginEntry, metav1.CreateOptions{})
Z
zryfish 已提交
81 82 83 84 85 86
	if err != nil {
		klog.Error(err)
		return err
	}
	return nil
}