|
| 1 | +package planetscale |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/pkg/errors" |
| 11 | +) |
| 12 | + |
| 13 | +// AuditLogEvent represents an audit log's event type |
| 14 | +type AuditLogEvent string |
| 15 | + |
| 16 | +const ( |
| 17 | + AuditLogEventBranchCreated AuditLogEvent = "branch.created" |
| 18 | + AuditLogEventBranchDeleted AuditLogEvent = "branch.deleted" |
| 19 | + AuditLogEventDatabaseCreated AuditLogEvent = "database.created" |
| 20 | + AuditLogEventDatabaseDeleted AuditLogEvent = "database.deleted" |
| 21 | + AuditLogEventDeployRequestApproved AuditLogEvent = "deploy_request.approved" |
| 22 | + AuditLogEventDeployRequestClosed AuditLogEvent = "deploy_request.closed" |
| 23 | + AuditLogEventDeployRequestCreated AuditLogEvent = "deploy_request.created" |
| 24 | + AuditLogEventDeployRequestDeleted AuditLogEvent = "deploy_request.deleted" |
| 25 | + AuditLogEventDeployRequestQueued AuditLogEvent = "deploy_request.queued" |
| 26 | + AuditLogEventDeployRequestUnqueued AuditLogEvent = "deploy_request.unqueued" |
| 27 | + AuditLogEventIntegrationCreated AuditLogEvent = "integration.created" |
| 28 | + AuditLogEventIntegrationDeleted AuditLogEvent = "integration.deleted" |
| 29 | + AuditLogEventOrganizationInvitationCreated AuditLogEvent = "organization_invitation.created" |
| 30 | + AuditLogEventOrganizationInvitationDeleted AuditLogEvent = "organization_invitation.deleted" |
| 31 | + AuditLogEventOrganizationMembershipCreated AuditLogEvent = "organization_membership.created" |
| 32 | + AuditLogEventOrganizationJoined AuditLogEvent = "organization.joined" |
| 33 | + AuditLogEventOrganizationRemovedMember AuditLogEvent = "organization.removed_member" |
| 34 | + AuditLogEventOrganizationDisabledSSO AuditLogEvent = "organization.disabled_sso" |
| 35 | + AuditLogEventOrganizationEnabledSSO AuditLogEvent = "organization.enabled_sso" |
| 36 | + AuditLogEventOrganizationUpdatedRole AuditLogEvent = "organization.updated_role" |
| 37 | + AuditLogEventServiceTokenCreated AuditLogEvent = "service_token.created" |
| 38 | + AuditLogEventServiceTokenDeleted AuditLogEvent = "service_token.deleted" |
| 39 | + AuditLogEventServiceTokenGrantedAccess AuditLogEvent = "service_token.granted_access" |
| 40 | +) |
| 41 | + |
| 42 | +var _ AuditLogsService = &auditlogsService{} |
| 43 | + |
| 44 | +// AuditLogsService is an interface for communicating with the PlanetScale |
| 45 | +// AuditLogs API endpoints. |
| 46 | +type AuditLogsService interface { |
| 47 | + List(context.Context, *ListAuditLogsRequest) ([]*AuditLog, error) |
| 48 | +} |
| 49 | + |
| 50 | +// ListAuditLogsRequest encapsulates the request for listing the audit logs of |
| 51 | +// an organization. |
| 52 | +type ListAuditLogsRequest struct { |
| 53 | + Organization string |
| 54 | + |
| 55 | + // Events can be used to filter out only the given audit log events. |
| 56 | + Events []AuditLogEvent |
| 57 | +} |
| 58 | + |
| 59 | +// AuditLog represents a PlanetScale audit log. |
| 60 | +type AuditLog struct { |
| 61 | + ID string `json:"id"` |
| 62 | + Type string `json:"type"` |
| 63 | + |
| 64 | + ActorID string `json:"actor_id"` |
| 65 | + ActorType string `json:"actor_type"` |
| 66 | + ActorDisplayName string `json:"actor_display_name"` |
| 67 | + |
| 68 | + AuditableID string `json:"auditable_id"` |
| 69 | + AuditableType string `json:"auditable_type"` |
| 70 | + AuditableDisplayName string `json:"auditable_display_name"` |
| 71 | + |
| 72 | + AuditAction string `json:"audit_action"` |
| 73 | + Action string `json:"action"` |
| 74 | + |
| 75 | + Location string `json:"location"` |
| 76 | + RemoteIP string `json:"remote_ip"` |
| 77 | + |
| 78 | + TargetID string `json:"target_id"` |
| 79 | + TargetType string `json:"target_type"` |
| 80 | + TargetDisplayName string `json:"target_display_name"` |
| 81 | + |
| 82 | + Metadata map[string]string `json:"metadata"` |
| 83 | + |
| 84 | + CreatedAt time.Time `json:"created_at"` |
| 85 | + UpdatedAt time.Time `json:"updated_at"` |
| 86 | +} |
| 87 | + |
| 88 | +type auditlogsResponse struct { |
| 89 | + AuditLogs []*AuditLog `json:"data"` |
| 90 | +} |
| 91 | + |
| 92 | +type auditlogsService struct { |
| 93 | + client *Client |
| 94 | +} |
| 95 | + |
| 96 | +func NewAuditLogsService(client *Client) *auditlogsService { |
| 97 | + return &auditlogsService{ |
| 98 | + client: client, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// List returns the audit logs for an organization. |
| 103 | +func (o *auditlogsService) List(ctx context.Context, listReq *ListAuditLogsRequest) ([]*AuditLog, error) { |
| 104 | + if listReq.Organization == "" { |
| 105 | + return nil, errors.New("organization is not set") |
| 106 | + } |
| 107 | + |
| 108 | + path := auditlogsAPIPath(listReq.Organization) |
| 109 | + |
| 110 | + v := url.Values{} |
| 111 | + if len(listReq.Events) != 0 { |
| 112 | + for _, action := range listReq.Events { |
| 113 | + v.Add("filters[]", fmt.Sprintf("audit_action:%s", action)) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if vals := v.Encode(); vals != "" { |
| 118 | + path += "?" + vals |
| 119 | + } |
| 120 | + |
| 121 | + req, err := o.client.newRequest(http.MethodGet, path, nil) |
| 122 | + if err != nil { |
| 123 | + return nil, errors.Wrap(err, "error creating request for listing audit logs") |
| 124 | + } |
| 125 | + |
| 126 | + resp := &auditlogsResponse{} |
| 127 | + if err := o.client.do(ctx, req, &resp); err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + |
| 131 | + return resp.AuditLogs, nil |
| 132 | +} |
| 133 | + |
| 134 | +func auditlogsAPIPath(org string) string { |
| 135 | + return fmt.Sprintf("%s/%s/audit-log", organizationsAPIPath, org) |
| 136 | +} |
0 commit comments