-
Notifications
You must be signed in to change notification settings - Fork 61
Audit log for osctrl-admin
#727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+861
−152
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/jmpsec/osctrl/cmd/admin/sessions" | ||
| "github.com/jmpsec/osctrl/pkg/auditlog" | ||
| "github.com/jmpsec/osctrl/pkg/environments" | ||
| "github.com/jmpsec/osctrl/pkg/users" | ||
| "github.com/jmpsec/osctrl/pkg/utils" | ||
| "github.com/rs/zerolog/log" | ||
| ) | ||
|
|
||
| // AuditLogJSON to be used to populate JSON data for audit logs | ||
| type AuditLogJSON struct { | ||
| Service string `json:"service"` | ||
| Username string `json:"username"` | ||
| Line string `json:"line"` | ||
| SourceIP string `json:"sourceip"` | ||
| LogType string `json:"logtype"` | ||
| Severity string `json:"severity"` | ||
| Env string `json:"environment"` | ||
| When CreationTimes `json:"when"` | ||
| } | ||
|
|
||
| // ReturnedAudit to return a JSON with audit logs | ||
| type ReturnedAudit struct { | ||
| Data []AuditLogJSON `json:"data"` | ||
| } | ||
|
|
||
| // JSONTagsHandler for audit logs in JSON | ||
| func (h *HandlersAdmin) JSONAuditLogHandler(w http.ResponseWriter, r *http.Request) { | ||
| if h.DebugHTTPConfig.Enabled { | ||
| utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody) | ||
| } | ||
| // Get context data | ||
| ctx := r.Context().Value(sessions.ContextKey(sessions.CtxSession)).(sessions.ContextValue) | ||
| // Check permissions | ||
| if !h.Users.CheckPermissions(ctx[sessions.CtxUser], users.AdminLevel, users.NoEnvironment) { | ||
| adminErrorResponse(w, fmt.Sprintf("%s has insufficient permissions", ctx[sessions.CtxUser]), http.StatusForbidden, nil) | ||
| return | ||
| } | ||
| // Get all environments | ||
| envs, err := h.Envs.All() | ||
| if err != nil { | ||
| log.Err(err).Msg("error getting environments") | ||
| return | ||
| } | ||
| // Get audit logs | ||
| auditLogs, err := h.AuditLog.GetAll() | ||
| if err != nil { | ||
| log.Err(err).Msg("error getting audit logs") | ||
| return | ||
| } | ||
| // Prepare data to be returned | ||
| var auditLogsJSON []AuditLogJSON | ||
| for _, logEntry := range auditLogs { | ||
| auditLogsJSON = append(auditLogsJSON, AuditLogJSON{ | ||
| Service: logEntry.Service, | ||
| Username: logEntry.Username, | ||
| Line: logEntry.Line, | ||
| SourceIP: logEntry.SourceIP, | ||
| LogType: auditlog.LogTypeToString(logEntry.LogType), | ||
| Severity: auditlog.SeverityToString(logEntry.Severity), | ||
| Env: environments.EnvironmentFinderID(logEntry.EnvironmentID, envs, false), | ||
| When: CreationTimes{ | ||
| Display: utils.PastFutureTimes(logEntry.CreatedAt), | ||
| // Use Unix timestamp in seconds | ||
| Timestamp: utils.TimeTimestamp(logEntry.CreatedAt), | ||
| }, | ||
| }) | ||
| } | ||
| returned := ReturnedAudit{ | ||
| Data: auditLogsJSON, | ||
| } | ||
| // Serve JSON | ||
| utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, returned) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.