This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
audit.go
125 lines (105 loc) · 4.49 KB
/
audit.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cli
import (
"strconv"
"time"
log "github.com/Sirupsen/logrus"
"github.com/hortonworks/cb-cli/cli/utils"
"github.com/hortonworks/cb-cli/client_cloudbreak/v3_workspace_id_audits"
"github.com/hortonworks/cb-cli/models_cloudbreak"
"github.com/urfave/cli"
yaml "gopkg.in/yaml.v2"
)
var auditListHeader = []string{"AuditID", "EventType", "TimeStamp", "ResourceId", "ResourceName", "ResourceType", "UserName", "Status", "Duration"}
type auditListOut struct {
Audit *models_cloudbreak.AuditEvent `json:"Audit" yaml:"Audit"`
}
func (a *auditListOut) DataAsStringArray() []string {
return []string{strconv.FormatInt(a.Audit.AuditID, 10), a.Audit.Operation.EventType, a.Audit.Operation.ZonedDateTime.String(), strconv.FormatInt(a.Audit.Operation.ResourceID, 10), a.Audit.Operation.ResourceName, a.Audit.Operation.ResourceType, a.Audit.Operation.UserName, a.Audit.Status, strconv.FormatInt(a.Audit.Duration, 10)}
}
var auditHeader = []string{"Audit"}
type auditOut struct {
Audit *models_cloudbreak.AuditEvent `json:"Audit" yaml:"Audit"`
}
type auditClient interface {
GetAuditEventsInWorkspace(params *v3_workspace_id_audits.GetAuditEventsInWorkspaceParams) (*v3_workspace_id_audits.GetAuditEventsInWorkspaceOK, error)
GetAuditEventByWorkspace(params *v3_workspace_id_audits.GetAuditEventByWorkspaceParams) (*v3_workspace_id_audits.GetAuditEventByWorkspaceOK, error)
}
func (a *auditOut) DataAsStringArray() []string {
if a.Audit.RawFlowEvent != nil && a.Audit.RawFlowEvent.Blueprint != nil {
a.Audit.RawFlowEvent.Blueprint.BlueprintJSON = "---TRUNCATED---"
}
auditYAML, err := yaml.Marshal(a.Audit)
if err != nil {
return []string{err.Error()}
}
return []string{string(auditYAML)}
}
func ListBlueprintAudits(c *cli.Context) {
listAudits("blueprints", c)
}
func ListClusterAudits(c *cli.Context) {
listAudits("stacks", c)
}
func ListCredentialAudits(c *cli.Context) {
listAudits("credentials", c)
}
func ListDatabaseAudits(c *cli.Context) {
listAudits("rdsconfigs", c)
}
func ListImagecatalogAudits(c *cli.Context) {
listAudits("imagecatalogs", c)
}
func ListLdapAudits(c *cli.Context) {
listAudits("ldapconfigs", c)
}
func ListRecipeAudits(c *cli.Context) {
listAudits("recipes", c)
}
func listAudits(resourceType string, c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "list audits")
log.Infof("[ListAudits] List all audits for a resource identified by Resource ID")
workspaceID := c.Int64(FlWorkspaceOptional.Name)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
resourceID := c.String(FlResourceID.Name)
cbClient := NewCloudbreakHTTPClientFromContext(c)
listAuditsImpl(cbClient.Cloudbreak.V3WorkspaceIDAudits, workspaceID, resourceType, resourceID, output.WriteList)
}
func listAuditsImpl(client auditClient, workspaceID int64, resourceType string, resourceIDString string, writer func([]string, []utils.Row)) {
resourceID, err := strconv.ParseInt(resourceIDString, 10, 64)
if err != nil {
utils.LogErrorMessageAndExit("Unable to parse as number: " + resourceIDString)
}
resp, err := client.GetAuditEventsInWorkspace(v3_workspace_id_audits.NewGetAuditEventsInWorkspaceParams().WithWorkspaceID(workspaceID).WithResourceType(resourceType).WithResourceID(resourceID))
if err != nil {
utils.LogErrorAndExit(err)
}
tableRows := []utils.Row{}
for _, audit := range resp.Payload {
tableRows = append(tableRows, &auditListOut{audit})
}
writer(auditListHeader, tableRows)
}
func DescribeAudit(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "describe audit")
log.Infof("[DescribeAudit] Show audit entry identified by Audit ID")
workspaceID := c.Int64(FlWorkspaceOptional.Name)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
auditID := c.String(FlAuditID.Name)
cbClient := NewCloudbreakHTTPClientFromContext(c)
describeAuditImpl(cbClient.Cloudbreak.V3WorkspaceIDAudits, workspaceID, auditID, output.WriteList)
}
func describeAuditImpl(client auditClient, workspaceID int64, auditIDString string, writer func([]string, []utils.Row)) {
auditID, err := strconv.ParseInt(auditIDString, 10, 64)
if err != nil {
utils.LogErrorMessageAndExit("Unable to parse as number: " + auditIDString)
}
resp, err := client.GetAuditEventByWorkspace(v3_workspace_id_audits.NewGetAuditEventByWorkspaceParams().WithWorkspaceID(workspaceID).WithAuditID(auditID))
if err != nil {
utils.LogErrorAndExit(err)
}
tableRows := []utils.Row{}
tableRows = append(tableRows, &auditOut{resp.Payload})
writer(auditHeader, tableRows)
}