fix(applystatus): report only the instance's own copy so another org's read cannot overwrite it
This commit is contained in:
1 parent
749baa3e5d
commit
2d87e9f587
3 files changed
+59
No files matched your search
@@ -10,6 +10,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
@@ -91,10 +92,18 @@ func New(process string, store storecontract.ApplyStatusStore, log *slog.Logger)
|
||||
|
||||
// Report records that this process applied version of kind, or failed to. A
|
||||
// failure keeps the previous version. Errors are logged.
|
||||
//
|
||||
// Records describe the instance's own copy of a kind: they are keyed by
|
||||
// process and kind and compared with the system org's stored version. A read
|
||||
// made for another org (a workflow or playbook collection) would overwrite
|
||||
// them with a version they are not comparable to, so it reports nothing.
|
||||
func (r *Reporter) Report(ctx context.Context, kind string, version int64, applyErr error) {
|
||||
if r == nil || !slices.Contains(Applicability()[kind], r.process) {
|
||||
return
|
||||
}
|
||||
if org.OrgFromContext(ctx) != org.DefaultOrgID {
|
||||
return
|
||||
}
|
||||
status := storecontract.ApplyStatus{
|
||||
Process: r.process, Kind: kind, AppliedVersion: version, ReportedAt: r.now().UTC(),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package applystatus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
type recordingStore struct{ put []storecontract.ApplyStatus }
|
||||
|
||||
func (s *recordingStore) PutApplyStatus(_ context.Context, status storecontract.ApplyStatus) error {
|
||||
s.put = append(s.put, status)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *recordingStore) ListApplyStatus(context.Context) ([]storecontract.ApplyStatus, error) {
|
||||
return s.put, nil
|
||||
}
|
||||
|
||||
// A read made for another org must not overwrite the instance's own record.
|
||||
func TestReportKeepsToTheInstanceCopy(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
org org.OrgID
|
||||
want int
|
||||
}{
|
||||
{"no org", "", 1},
|
||||
{"system org", org.DefaultOrgID, 1},
|
||||
{"other org", "org-acme", 0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := &recordingStore{}
|
||||
New(Daemon, store, nil).Report(org.WithOrg(context.Background(), tt.org), "workflow-definitions", 3, nil)
|
||||
if len(store.put) != tt.want {
|
||||
t.Fatalf("records written = %d, want %d", len(store.put), tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -177,6 +177,14 @@ func (s *Server) readResource(ctx context.Context, orgID, kind string, into any)
|
||||
|
||||
// instanceOnlyKinds are the kinds the runtime layers from the system org's
|
||||
// copy alone. Another org's write would be stored and never applied.
|
||||
//
|
||||
// Instance-wide by design (execution infrastructure, installed extensions,
|
||||
// limits): ToolSettings, PluginSettings, ContainerRuntimePolicies,
|
||||
// AgentProfile, WorkflowExecutionSettings, ExtensionSettings, UpdateSettings,
|
||||
// AuditRetention, ScheduleSettings (the zone every cron schedule reads in).
|
||||
// Pending an org owner of the channels they configure:
|
||||
// ChannelSettings, SchedulingPolicy, TaskPolicy (outbound notification
|
||||
// webhook).
|
||||
var instanceOnlyKinds = map[string]bool{
|
||||
ChannelSettingsKind: true, SchedulingPolicyKind: true,
|
||||
TaskPolicyKind: true, ToolSettingsKind: true,
|
||||
|
||||
Reference in new issue
Block a user