From 9741ceca77f7996e0e2bf4dcf0c3574c04efa30b Mon Sep 17 00:00:00 2001 From: vivekhiwarkar Date: Wed, 17 Aug 2022 12:42:00 +0530 Subject: [PATCH] Adding audit points --- pkg/sentry/kubeconfig/kubeconfig.go | 2 + pkg/service/apikey.go | 2 + pkg/service/audit_utils.go | 59 +++++++++++++++++++++++++++-- pkg/service/user.go | 1 + 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/pkg/sentry/kubeconfig/kubeconfig.go b/pkg/sentry/kubeconfig/kubeconfig.go index 1cb715a..3c980e3 100644 --- a/pkg/sentry/kubeconfig/kubeconfig.go +++ b/pkg/sentry/kubeconfig/kubeconfig.go @@ -17,6 +17,7 @@ import ( commonv3 "github.com/paralus/paralus/proto/types/commonpb/v3" sentry "github.com/paralus/paralus/proto/types/sentry" + //"go.uber.org/zap" clientcmdapiv1 "k8s.io/client-go/tools/clientcmd/api/v1" "sigs.k8s.io/yaml" @@ -342,6 +343,7 @@ func GetConfigForUser(ctx context.Context, bs service.BootstrapService, aps serv message, groups) */ + //service.DownloadKubeconfigAuditEvent(ctx, zap.L(), username) return yaml.JSONToYAML(jb) } diff --git a/pkg/service/apikey.go b/pkg/service/apikey.go index 6cd14ef..e1c976d 100644 --- a/pkg/service/apikey.go +++ b/pkg/service/apikey.go @@ -105,6 +105,7 @@ func (s *apiKeyService) Get(ctx context.Context, req *rpcv3.ApiKeyRequest) (*mod if err == sql.ErrNoRows { return nil, nil } + GenerateApiKeyAuditEvent(ctx, s.al, AuditActionGenerate, req.Username) return &apikey, err } @@ -114,5 +115,6 @@ func (s *apiKeyService) GetByKey(ctx context.Context, req *rpcv3.ApiKeyRequest) if err != nil { return nil, err } + GenerateApiKeyAuditEvent(ctx, s.al, AuditActionGenerate, req.Username) return &apikey, err } diff --git a/pkg/service/audit_utils.go b/pkg/service/audit_utils.go index 9db7faa..fcf4027 100644 --- a/pkg/service/audit_utils.go +++ b/pkg/service/audit_utils.go @@ -16,9 +16,11 @@ import ( ) const ( - AuditActionCreate = "create" - AuditActionDelete = "delete" - AuditActionUpdate = "update" + AuditActionCreate = "create" + AuditActionDelete = "delete" + AuditActionUpdate = "update" + AuditActionGenerate = "generate" + AuditActionDownload = "download" ) func CreateUserAuditEvent(ctx context.Context, al *zap.Logger, db bun.IDB, action string, name string, id uuid.UUID, rolesBefore, rolesAfter, groupsBefore, groupsAfter []uuid.UUID) { @@ -389,6 +391,57 @@ func CreateApiKeyAuditEvent(ctx context.Context, al *zap.Logger, action string, } } +func GenerateApiKeyAuditEvent(ctx context.Context, al *zap.Logger, action string, id string) { + sd, ok := GetSessionDataFromContext(ctx) + if !ok { + _log.Warn("unable to create audit event: could not fetch info from context") + return + } + + detail := &audit.EventDetail{ + Message: fmt.Sprintf("ApiKey %sd for user %s", action, id), + Meta: map[string]string{ + "apikey": id, + }, + } + if err := audit.CreateV1Event(al, sd, detail, fmt.Sprintf("apikey.%s.success", action), ""); err != nil { + _log.Warn("unable to create audit event", err) + } +} + +func DownloadKubeconfigAuditEvent(ctx context.Context, al *zap.Logger, user string) { + sd, ok := GetSessionDataFromContext(ctx) + if !ok { + _log.Warn("unable to create audit event: could not fetch info from context") + return + } + + detail := &audit.EventDetail{ + Message: fmt.Sprintf("Kubeconfig Downloaded for user %s", user), + } + _log.Infow(fmt.Sprintf("Kubeconfig Downloaded for user %s", user)) + if err := audit.CreateV1Event(al, sd, detail, fmt.Sprintf("Kubeconfig Download success %s", user), ""); err != nil { + _log.Warn("unable to create audit event", err) + } + _log.Infow("Audit event created") +} + +func DownloadCliConfigAuditEvent(ctx context.Context, al *zap.Logger, action string, user string) { + sd, ok := GetSessionDataFromContext(ctx) + if !ok { + _log.Warn("unable to create audit event: could not fetch info from context") + return + } + + detail := &audit.EventDetail{ + Message: fmt.Sprintf("CLI config Downloaded for %s", user), + } + _log.Infow(fmt.Sprintf("CLI config Downloaded for %s", user)) + if err := audit.CreateV1Event(al, sd, detail, fmt.Sprintf("CLI Config.%s.success", action), ""); err != nil { + _log.Warn("unable to create audit event", err) + } +} + func RevokeKubeconfigAuditEvent(ctx context.Context, al *zap.Logger, user string) { sd, ok := GetSessionDataFromContext(ctx) if !ok { diff --git a/pkg/service/user.go b/pkg/service/user.go index 5233222..adeef98 100644 --- a/pkg/service/user.go +++ b/pkg/service/user.go @@ -954,6 +954,7 @@ func (s *userService) RetrieveCliConfig(ctx context.Context, req *userrpcv3.ApiK Partner: part.Name, } + DownloadCliConfigAuditEvent(ctx, s.al, AuditActionCreate, req.Username) return cliConfig, nil }