This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaudit_log.go
167 lines (158 loc) · 6.01 KB
/
audit_log.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package controller
import (
"fmt"
"strings"
"github.com/fabric8-services/admin-console/app"
"github.com/fabric8-services/admin-console/application"
"github.com/fabric8-services/admin-console/auditlog"
"github.com/fabric8-services/admin-console/configuration"
"github.com/fabric8-services/fabric8-common/auth"
"github.com/fabric8-services/fabric8-common/errors"
"github.com/fabric8-services/fabric8-common/httpsupport"
"github.com/fabric8-services/fabric8-common/log"
"github.com/dgrijalva/jwt-go"
"github.com/goadesign/goa"
goajwt "github.com/goadesign/goa/middleware/security/jwt"
)
// AuditLogsController implements the auditlogs resource.
type AuditLogsController struct {
*goa.Controller
db application.DB
config *configuration.Configuration
}
// NewAuditLogsController creates a auditlogs controller.
func NewAuditLogsController(service *goa.Service, config *configuration.Configuration, db application.DB) *AuditLogsController {
return &AuditLogsController{
Controller: service.NewController("AuditLogsController"),
config: config,
db: db,
}
}
// Create runs the create action.
func (c *AuditLogsController) Create(ctx *app.CreateAuditLogContext) error {
// check the token and make sure it belongs to `auth`
if !auth.IsSpecificServiceAccount(ctx, auth.Auth) {
return app.JSONErrorResponse(ctx, errors.NewUnauthorizedError("invalid or missing authorization token"))
}
// lookup the event type ID
eventTypeID, found := auditlog.EventTypesByName[ctx.Payload.Data.Attributes.EventType]
if !found {
return app.JSONErrorResponse(ctx, errors.NewBadParameterError("event_type", ctx.Payload.Data.Attributes.EventType))
}
log.Info(ctx, map[string]interface{}{
"username": ctx.Username,
}, "creating audit log for user")
err := application.Transactional(c.db, func(appl application.Application) error {
return appl.AuditLogs().Create(ctx, &auditlog.AuditLog{
Username: ctx.Username,
EventTypeID: eventTypeID,
EventParams: ctx.Payload.Data.Attributes.EventParams,
})
})
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
"username": ctx.Username,
}, "unable to record the auditlog for user")
return app.JSONErrorResponse(ctx, err)
}
return ctx.NoContent()
}
// ListForUser lists the audit logs for a given user
func (c *AuditLogsController) ListForUser(ctx *app.ListForUserAuditLogContext) error {
// check the token and make sure it belongs to a Red Hat employee
// retrieve the user's identity ID from the token
username, emailAddress, emailVerified, err := parseToken(ctx)
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
}, "unable to parse request token")
return app.JSONErrorResponse(ctx, err)
}
if !(strings.HasSuffix(emailAddress, "@redhat.com") && emailVerified) {
log.Error(ctx, map[string]interface{}{
"target_username": ctx.Username,
"requesting_username": username,
"email_address": emailAddress,
"email_verified": emailVerified,
}, "user is not allowed to list audit logs")
return app.JSONErrorResponse(ctx, errors.NewForbiddenError("forbidden"))
}
// log an audit log for the current user for her action
record := auditlog.AuditLog{
EventTypeID: auditlog.ListAuditLogs,
Username: username,
EventParams: auditlog.EventParams{
"user": ctx.Username,
},
}
err = application.Transactional(c.db, func(appl application.Application) error {
return appl.AuditLogs().Create(ctx, &record)
})
// search for audit logs for the request (target) user
var logs []auditlog.AuditLog
var total int
err = application.Transactional(c.db, func(appl application.Application) error {
var err error
logs, total, err = appl.AuditLogs().ListByUsername(ctx, ctx.Username, ctx.PageNumber, ctx.PageSize)
return err
})
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
"username": ctx.Username,
}, "unable to list auditlogs for user")
return app.JSONErrorResponse(ctx, err)
}
return ctx.OK(convertAuditLogs(ctx, logs, total, c.config))
}
func parseToken(ctx *app.ListForUserAuditLogContext) (string, string, bool, error) {
token := goajwt.ContextJWT(ctx)
if token == nil {
return "", "", false, errors.NewUnauthorizedError("bad or missing token")
}
claims := token.Claims.(jwt.MapClaims)
username, ok := claims["preferred_username"].(string)
if !ok {
log.Error(ctx, map[string]interface{}{}, "token is missing 'preferred_username' claim")
return "", "", false, errors.NewUnauthorizedError("bad or missing token")
}
emailAddress, ok := claims["email"].(string)
if !ok {
log.Error(ctx, map[string]interface{}{}, "token is missing 'email' claim")
return "", "", false, errors.NewUnauthorizedError("bad or missing token")
}
emailVerified, ok := claims["email_verified"].(bool)
if !ok {
log.Error(ctx, map[string]interface{}{}, "token is missing 'email_verified' claim")
return "", "", false, errors.NewUnauthorizedError("bad or missing token")
}
return username, emailAddress, emailVerified, nil
}
// convertAuditLogs converts the audit logs to their resource-API counterpart
func convertAuditLogs(ctx *app.ListForUserAuditLogContext, logs []auditlog.AuditLog, total int, config httpsupport.Configuration) *app.AuditLogList {
data := []*app.AuditLogData{}
for _, log := range logs {
fmt.Printf("event type: %v -> %s\n", log.EventTypeID, auditlog.EventTypesByID[log.EventTypeID])
fmt.Printf("event params: %v\n", log.EventParams)
data = append(data, &app.AuditLogData{
Type: "audit_logs",
Attributes: &app.AuditLogDataAttributes{
Date: log.CreatedAt.Format("2006-01-02:15:03:04"),
EventType: auditlog.EventTypesByID[log.EventTypeID],
EventParams: log.EventParams,
},
})
}
response := &app.AuditLogList{
Data: data,
Links: &app.PagingLinks{},
Meta: &app.UserListMeta{
TotalCount: total,
},
}
pageNumber, pageSize := computePagingLimits(ctx.PageNumber, ctx.PageSize)
path := httpsupport.AbsoluteURL(ctx.RequestData, ctx.Request.URL.Path, config)
setPagingLinks(response.Links, path, len(logs), pageNumber, pageSize, total)
return response
}