|
| 1 | +// |
| 2 | +// Copyright 2025 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package events |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/chainloop-dev/chainloop/app/controlplane/pkg/auditor" |
| 24 | + "github.com/google/uuid" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + _ auditor.LogEntry = (*CASBackendCreated)(nil) |
| 29 | + _ auditor.LogEntry = (*CASBackendUpdated)(nil) |
| 30 | + _ auditor.LogEntry = (*CASBackendDeleted)(nil) |
| 31 | + _ auditor.LogEntry = (*CASBackendPermanentDeleted)(nil) |
| 32 | + _ auditor.LogEntry = (*CASBackendStatusChanged)(nil) |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + CASBackendType auditor.TargetType = "CASBackend" |
| 37 | + CASBackendCreatedActionType string = "CASBackendCreated" |
| 38 | + CASBackendUpdatedActionType string = "CASBackendUpdated" |
| 39 | + CASBackendDeletedActionType string = "CASBackendSoftDeleted" |
| 40 | + CASBackendPermanentDeletedType string = "CASBackendPermanentDeleted" |
| 41 | + CASBackendStatusChangedAction string = "CASBackendStatusChanged" |
| 42 | +) |
| 43 | + |
| 44 | +// CASBackendBase contains the common fields for all CAS backend events |
| 45 | +type CASBackendBase struct { |
| 46 | + CASBackendID *uuid.UUID `json:"cas_backend_id,omitempty"` |
| 47 | + CASBackendName string `json:"cas_backend_name,omitempty"` |
| 48 | + Provider string `json:"provider,omitempty"` |
| 49 | + Location string `json:"location,omitempty"` |
| 50 | + Default bool `json:"default,omitempty"` |
| 51 | +} |
| 52 | + |
| 53 | +func (c *CASBackendBase) RequiresActor() bool { |
| 54 | + return true |
| 55 | +} |
| 56 | + |
| 57 | +func (c *CASBackendBase) TargetType() auditor.TargetType { |
| 58 | + return CASBackendType |
| 59 | +} |
| 60 | + |
| 61 | +func (c *CASBackendBase) TargetID() *uuid.UUID { |
| 62 | + return c.CASBackendID |
| 63 | +} |
| 64 | + |
| 65 | +func (c *CASBackendBase) ActionInfo() (json.RawMessage, error) { |
| 66 | + if c.CASBackendID == nil || c.CASBackendName == "" { |
| 67 | + return nil, errors.New("cas backend id and name are required") |
| 68 | + } |
| 69 | + |
| 70 | + return json.Marshal(&c) |
| 71 | +} |
| 72 | + |
| 73 | +// CASBackendCreated represents the creation of a CAS backend |
| 74 | +type CASBackendCreated struct { |
| 75 | + *CASBackendBase |
| 76 | + CASBackendDescription string `json:"description,omitempty"` |
| 77 | +} |
| 78 | + |
| 79 | +func (c *CASBackendCreated) ActionType() string { |
| 80 | + return CASBackendCreatedActionType |
| 81 | +} |
| 82 | + |
| 83 | +func (c *CASBackendCreated) ActionInfo() (json.RawMessage, error) { |
| 84 | + if _, err := c.CASBackendBase.ActionInfo(); err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + return json.Marshal(&c) |
| 89 | +} |
| 90 | + |
| 91 | +func (c *CASBackendCreated) Description() string { |
| 92 | + defaultStatus := "non-default" |
| 93 | + if c.Default { |
| 94 | + defaultStatus = "default" |
| 95 | + } |
| 96 | + return fmt.Sprintf("%s has created CAS backend %s with provider %s (%s)", auditor.GetActorIdentifier(), c.CASBackendName, c.Provider, defaultStatus) |
| 97 | +} |
| 98 | + |
| 99 | +// CASBackendUpdated represents an update to a CAS backend |
| 100 | +type CASBackendUpdated struct { |
| 101 | + *CASBackendBase |
| 102 | + NewDescription *string `json:"new_description,omitempty"` |
| 103 | + CredentialsChanged bool `json:"credentials_changed,omitempty"` |
| 104 | + PreviousDefault bool `json:"previous_default,omitempty"` |
| 105 | +} |
| 106 | + |
| 107 | +func (c *CASBackendUpdated) ActionType() string { |
| 108 | + return CASBackendUpdatedActionType |
| 109 | +} |
| 110 | + |
| 111 | +func (c *CASBackendUpdated) ActionInfo() (json.RawMessage, error) { |
| 112 | + if _, err := c.CASBackendBase.ActionInfo(); err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + return json.Marshal(&c) |
| 117 | +} |
| 118 | + |
| 119 | +func (c *CASBackendUpdated) Description() string { |
| 120 | + var credentialsInfo string |
| 121 | + if c.CredentialsChanged { |
| 122 | + // nolint: gosec |
| 123 | + credentialsInfo = " and updated credentials" |
| 124 | + } |
| 125 | + |
| 126 | + if c.PreviousDefault != c.Default { |
| 127 | + defaultStatus := "default" |
| 128 | + if !c.Default { |
| 129 | + defaultStatus = "non-default" |
| 130 | + } |
| 131 | + return fmt.Sprintf("%s has updated CAS backend %s to %s%s", |
| 132 | + auditor.GetActorIdentifier(), c.CASBackendName, defaultStatus, credentialsInfo) |
| 133 | + } |
| 134 | + |
| 135 | + return fmt.Sprintf("%s has updated CAS backend %s%s", |
| 136 | + auditor.GetActorIdentifier(), c.CASBackendName, credentialsInfo) |
| 137 | +} |
| 138 | + |
| 139 | +// CASBackendDeleted represents the deletion of a CAS backend |
| 140 | +type CASBackendDeleted struct { |
| 141 | + *CASBackendBase |
| 142 | +} |
| 143 | + |
| 144 | +func (c *CASBackendDeleted) ActionType() string { |
| 145 | + return CASBackendDeletedActionType |
| 146 | +} |
| 147 | + |
| 148 | +func (c *CASBackendDeleted) ActionInfo() (json.RawMessage, error) { |
| 149 | + if _, err := c.CASBackendBase.ActionInfo(); err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + |
| 153 | + return json.Marshal(&c) |
| 154 | +} |
| 155 | + |
| 156 | +func (c *CASBackendDeleted) Description() string { |
| 157 | + return fmt.Sprintf("%s has deleted CAS backend %s", auditor.GetActorIdentifier(), c.CASBackendName) |
| 158 | +} |
| 159 | + |
| 160 | +// CASBackendPermanentDeleted represents the permanent deletion of a CAS backend |
| 161 | +type CASBackendPermanentDeleted struct { |
| 162 | + *CASBackendBase |
| 163 | +} |
| 164 | + |
| 165 | +func (c *CASBackendPermanentDeleted) ActionType() string { |
| 166 | + return CASBackendPermanentDeletedType |
| 167 | +} |
| 168 | + |
| 169 | +func (c *CASBackendPermanentDeleted) ActionInfo() (json.RawMessage, error) { |
| 170 | + if _, err := c.CASBackendBase.ActionInfo(); err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + |
| 174 | + return json.Marshal(&c) |
| 175 | +} |
| 176 | + |
| 177 | +func (c *CASBackendPermanentDeleted) Description() string { |
| 178 | + return fmt.Sprintf("%s has permanently deleted CAS backend %s", auditor.GetActorIdentifier(), c.CASBackendName) |
| 179 | +} |
| 180 | + |
| 181 | +// CASBackendStatusChanged represents a change in the validation status of a CAS backend |
| 182 | +type CASBackendStatusChanged struct { |
| 183 | + *CASBackendBase |
| 184 | + PreviousStatus string `json:"previous_status,omitempty"` |
| 185 | + NewStatus string `json:"new_status,omitempty"` |
| 186 | + IsRecovery bool `json:"is_recovery,omitempty"` |
| 187 | +} |
| 188 | + |
| 189 | +func (c *CASBackendStatusChanged) ActionType() string { |
| 190 | + return CASBackendStatusChangedAction |
| 191 | +} |
| 192 | + |
| 193 | +func (c *CASBackendStatusChanged) ActionInfo() (json.RawMessage, error) { |
| 194 | + if _, err := c.CASBackendBase.ActionInfo(); err != nil { |
| 195 | + return nil, err |
| 196 | + } |
| 197 | + |
| 198 | + return json.Marshal(&c) |
| 199 | +} |
| 200 | + |
| 201 | +func (c *CASBackendStatusChanged) Description() string { |
| 202 | + var statusInfo string |
| 203 | + if c.IsRecovery { |
| 204 | + statusInfo = " has recovered from invalid state" |
| 205 | + } else { |
| 206 | + statusInfo = fmt.Sprintf(" status changed from %s to %s", c.PreviousStatus, c.NewStatus) |
| 207 | + } |
| 208 | + |
| 209 | + return fmt.Sprintf("CAS backend %s%s", c.CASBackendName, statusInfo) |
| 210 | +} |
| 211 | + |
| 212 | +func (c *CASBackendStatusChanged) RequiresActor() bool { |
| 213 | + // Status changes are system-generated, no actor required |
| 214 | + return false |
| 215 | +} |
0 commit comments