| 88 | } |
| 89 | |
| 90 | func GenerateAuditEvent(entry LogEntry, opts ...GeneratorOption) (*EventPayload, error) { |
| 91 | options := &GeneratorOptions{} |
| 92 | for _, opt := range opts { |
| 93 | if err := opt(options); err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | descriptionTpl := entry.Description() |
| 99 | if descriptionTpl == "" { |
| 100 | return nil, errors.New("description is required") |
| 101 | } |
| 102 | |
| 103 | description, err := interpolateDescription(descriptionTpl, options) |
| 104 | if err != nil { |
| 105 | return nil, fmt.Errorf("failed to parse description template: %w", err) |
| 106 | } |
| 107 | |
| 108 | actionType := entry.ActionType() |
| 109 | if actionType == "" { |
| 110 | return nil, errors.New("action type is required") |
| 111 | } |
| 112 | |
| 113 | targetType := entry.TargetType() |
| 114 | if targetType == "" { |
| 115 | return nil, errors.New("target type is required") |
| 116 | } |
| 117 | |
| 118 | actionInfo, err := entry.ActionInfo() |
| 119 | if err != nil { |
| 120 | return nil, fmt.Errorf("failed to get action info: %w", err) |
| 121 | } |
| 122 | |
| 123 | // calculate the digest of the log entry |
| 124 | digest, err := digest(entry, options.OrgID, options.ActorID) |
| 125 | if err != nil { |
| 126 | return nil, fmt.Errorf("failed to calculate digest: %w", err) |
| 127 | } |
| 128 | |
| 129 | return &EventPayload{ |
| 130 | EventType: AuditEventType, |
| 131 | Timestamp: time.Now(), |
| 132 | Data: &AuditEventPayload{ |
| 133 | Description: description, |
| 134 | ActionType: actionType, |
| 135 | TargetType: targetType, |
| 136 | TargetID: entry.TargetID(), |
| 137 | Info: actionInfo, |
| 138 | ActorType: options.ActorType, |
| 139 | ActorID: options.ActorID, |
| 140 | ActorName: options.ActorName, |
| 141 | ActorEmail: options.ActorEmail, |
| 142 | OrgID: options.OrgID, |
| 143 | Digest: digest, |
| 144 | }, |
| 145 | }, nil |
| 146 | } |
| 147 | |