-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathaudit_info.go
98 lines (76 loc) · 2.9 KB
/
audit_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package server
import (
"context"
"fmt"
"strings"
"github.com/paralus/paralus/pkg/query"
sentryrpc "github.com/paralus/paralus/proto/rpc/sentry"
"github.com/paralus/paralus/pkg/sentry/kubeconfig"
"github.com/paralus/paralus/pkg/service"
)
type auditInfoServer struct {
bs service.BootstrapService
aps service.AccountPermissionService
prs service.ProjectService
}
var _ sentryrpc.AuditInformationServiceServer = (*auditInfoServer)(nil)
// NewAuditInfoServer returns new Audit Information Server
func NewAuditInfoServer(bs service.BootstrapService, aps service.AccountPermissionService, prs service.ProjectService) sentryrpc.AuditInformationServiceServer {
return &auditInfoServer{bs: bs, aps: aps, prs: prs}
}
func (s *auditInfoServer) LookupUser(ctx context.Context, req *sentryrpc.LookupUserRequest) (*sentryrpc.LookupUserResponse, error) {
attrs := kubeconfig.GetCNAttributes(req.UserCN)
_log.Infow("lookupUser", "attrs", attrs)
if attrs.SystemUser {
return &sentryrpc.LookupUserResponse{
UserName: attrs.Username,
AccountID: attrs.AccountID,
IsSSO: kubeconfig.GetStringFromBool(attrs.IsSSO),
OrganizationID: attrs.OrganizationID,
PartnerID: attrs.PartnerID,
SessionType: kubeconfig.GetSessionTypeString(attrs.SessionType),
}, nil
}
accountID := attrs.AccountID
var userName string
account, err := s.aps.GetAccount(ctx, accountID)
if err != nil {
return nil, err
}
userName = account.Username
return &sentryrpc.LookupUserResponse{
UserName: userName,
AccountID: attrs.AccountID,
IsSSO: kubeconfig.GetStringFromBool(attrs.IsSSO),
OrganizationID: attrs.OrganizationID,
PartnerID: attrs.PartnerID,
SessionType: kubeconfig.GetSessionTypeString(attrs.SessionType),
}, nil
}
func (s *auditInfoServer) LookupCluster(ctx context.Context, req *sentryrpc.LookupClusterRequest) (*sentryrpc.LookupClusterResponse, error) {
strs := strings.SplitN(req.ClusterSNI, ".", 2)
if len(strs) != 2 {
return nil, fmt.Errorf("invalid cluster SNI %s", req.ClusterSNI)
}
clusterID, relayHost := strs[0], strs[1]
relayHost = fmt.Sprintf("*.%s", relayHost)
bat, err := s.bs.GetBootstrapAgentTemplateForHost(ctx, relayHost)
if err != nil {
return nil, err
}
ba, err := s.bs.GetBootstrapAgent(ctx, bat.Metadata.Labels["paralus.dev/connectorAgentTemplate"], query.WithName(clusterID), query.WithIgnoreScopeDefault(), query.WithDeleted())
if err != nil {
_log.Infow("unable to get bootstrap agent", "req", req, "error", err)
return nil, err
}
project, err := s.prs.GetByID(ctx, ba.Metadata.Project)
if err != nil {
_log.Warnw("unable to get project name", "id", ba.Metadata.Project, "error", err)
return nil, err
}
_log.Infow("project name in lookup cluster", "project", project.Metadata.Name)
return &sentryrpc.LookupClusterResponse{
Name: ba.Metadata.Labels["paralus.dev/clusterName"],
Project: project.Metadata.Name,
}, nil
}