AuditLogs requires the database to fetch an organization by name to convert to organization uuid. Supported query parameters: - request_id: UUID (can be used to search for associated audits e.g. connect/disconnect or open/close) - resource_id: UUID - resource_target: string - username: string - em
(ctx context.Context, db database.Store, query string)
| 34 | // - action: string (enum) |
| 35 | // - build_reason: string (enum) |
| 36 | func AuditLogs(ctx context.Context, db database.Store, query string) (database.GetAuditLogsOffsetParams, |
| 37 | database.CountAuditLogsParams, []codersdk.ValidationError, |
| 38 | ) { |
| 39 | // Always lowercase for all searches. |
| 40 | query = strings.ToLower(query) |
| 41 | values, errors := searchTerms(query, func(term string, values url.Values) error { |
| 42 | values.Add("resource_type", term) |
| 43 | return nil |
| 44 | }) |
| 45 | if len(errors) > 0 { |
| 46 | // nolint:exhaustruct // We don't need to initialize these structs because we return an error. |
| 47 | return database.GetAuditLogsOffsetParams{}, database.CountAuditLogsParams{}, errors |
| 48 | } |
| 49 | |
| 50 | const dateLayout = "2006-01-02" |
| 51 | parser := httpapi.NewQueryParamParser() |
| 52 | filter := database.GetAuditLogsOffsetParams{ |
| 53 | RequestID: parser.UUID(values, uuid.Nil, "request_id"), |
| 54 | ResourceID: parser.UUID(values, uuid.Nil, "resource_id"), |
| 55 | ResourceTarget: parser.String(values, "", "resource_target"), |
| 56 | Username: parser.String(values, "", "username"), |
| 57 | Email: parser.String(values, "", "email"), |
| 58 | DateFrom: parser.Time(values, time.Time{}, "date_from", dateLayout), |
| 59 | DateTo: parser.Time(values, time.Time{}, "date_to", dateLayout), |
| 60 | OrganizationID: parseOrganization(ctx, db, parser, values, "organization"), |
| 61 | ResourceType: string(httpapi.ParseCustom(parser, values, "", "resource_type", httpapi.ParseEnum[database.ResourceType])), |
| 62 | Action: string(httpapi.ParseCustom(parser, values, "", "action", httpapi.ParseEnum[database.AuditAction])), |
| 63 | BuildReason: string(httpapi.ParseCustom(parser, values, "", "build_reason", httpapi.ParseEnum[database.BuildReason])), |
| 64 | } |
| 65 | if !filter.DateTo.IsZero() { |
| 66 | filter.DateTo = filter.DateTo.Add(23*time.Hour + 59*time.Minute + 59*time.Second) |
| 67 | } |
| 68 | |
| 69 | // Prepare the count filter, which uses the same parameters as the GetAuditLogsOffsetParams. |
| 70 | // nolint:exhaustruct // UserID and CountCap are not obtained from the query parameters. |
| 71 | countFilter := database.CountAuditLogsParams{ |
| 72 | RequestID: filter.RequestID, |
| 73 | ResourceID: filter.ResourceID, |
| 74 | ResourceTarget: filter.ResourceTarget, |
| 75 | Username: filter.Username, |
| 76 | Email: filter.Email, |
| 77 | DateFrom: filter.DateFrom, |
| 78 | DateTo: filter.DateTo, |
| 79 | OrganizationID: filter.OrganizationID, |
| 80 | ResourceType: filter.ResourceType, |
| 81 | Action: filter.Action, |
| 82 | BuildReason: filter.BuildReason, |
| 83 | } |
| 84 | |
| 85 | parser.ErrorExcessParams(values) |
| 86 | return filter, countFilter, parser.Errors |
| 87 | } |
| 88 | |
| 89 | func ConnectionLogs(ctx context.Context, db database.Store, query string, apiKey database.APIKey) (database.GetConnectionLogsOffsetParams, database.CountConnectionLogsParams, []codersdk.ValidationError) { |
| 90 | // Always lowercase for all searches. |