/
service.go
93 lines (84 loc) · 2.48 KB
/
service.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
package main
import (
"context"
"encoding/base64"
"fmt"
"reflect"
"github.com/ca-risken/aws/proto/activity"
"github.com/ca-risken/datasource-api/pkg/message"
awsClient "github.com/ca-risken/datasource-api/proto/aws"
)
type activityService struct {
awsClient awsClient.AWSServiceClient
cloudTrailClient cloudTrailAPI
configClient configServiceAPI
}
func (a *activityService) DescribeARN(ctx context.Context, req *activity.DescribeARNRequest) (*activity.DescribeARNResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
resp, err := ParseARN(req.Arn)
if err != nil {
return nil, err
}
return &activity.DescribeARNResponse{Arn: resp}, nil
}
func (a *activityService) ListCloudTrail(ctx context.Context, req *activity.ListCloudTrailRequest) (*activity.ListCloudTrailResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
ds, err := a.getAWSDataSource(ctx, req.ProjectId, req.AwsId)
if err != nil {
return nil, err
}
resp, err := a.cloudTrailClient.lookupEvents(ctx, req, ds.AssumeRoleArn, ds.ExternalId)
if err != nil {
return nil, err
}
return resp, nil
}
func (a *activityService) ListConfigHistory(ctx context.Context, req *activity.ListConfigHistoryRequest) (*activity.ListConfigHistoryResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
ds, err := a.getAWSDataSource(ctx, req.ProjectId, req.AwsId)
if err != nil {
return nil, err
}
resp, err := a.configClient.listConfigHistory(ctx, req, ds.AssumeRoleArn, ds.ExternalId)
if err != nil {
return nil, err
}
return resp, nil
}
func (a *activityService) getAWSDataSource(ctx context.Context, projectID, awsID uint32) (*awsClient.DataSource, error) {
ds, err := a.awsClient.ListDataSource(ctx, &awsClient.ListDataSourceRequest{
ProjectId: projectID,
AwsId: awsID,
DataSource: message.AWSActivityDatasource,
})
if err != nil {
return nil, err
}
if ds == nil || len(ds.DataSource) != 1 {
return nil, fmt.Errorf("Unexpected AWS DataSource, datasource=%+v", ds)
}
return ds.DataSource[0], nil
}
func convertNilToString(v *string) string {
if v == nil || reflect.ValueOf(v).IsNil() {
return ""
}
return *v
}
func encodeBase64(v string) string {
return base64.URLEncoding.EncodeToString([]byte(v))
}
func decodeBase64(ctx context.Context, v string) string {
decoded, err := base64.URLEncoding.DecodeString(v)
if err != nil {
appLogger.Warnf(ctx, "Failed to base64 decode, value=%v", v)
return ""
}
return string(decoded)
}