-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram_observer.go
More file actions
262 lines (252 loc) · 12.9 KB
/
Copy pathprogram_observer.go
File metadata and controls
262 lines (252 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package boatstack
import (
"context"
"encoding/json"
"fmt"
"github.com/operatorstack/boatstack/boatstack/delivery"
"github.com/operatorstack/boatstack/boatstack/internal/softwaredelivery/catalog"
"github.com/operatorstack/boatstack/boatstack/internal/softwaredelivery/model"
"github.com/operatorstack/boatstack/boatstack/internal/softwaredelivery/ports"
"github.com/operatorstack/boatstack/boatstack/internal/softwaredelivery/protocol"
)
type programObserver struct {
base ports.Observer
program delivery.ControlProgram
}
// ComponentRuntimeError preserves a bounded protocol error classification and
// message without granting a component control over recovery policy.
type ComponentRuntimeError struct {
Component string
Operation string
Class string
Message string
}
func (e ComponentRuntimeError) Error() string {
return fmt.Sprintf("%s %s reported %s: %s", e.Component, e.Operation, e.Class, e.Message)
}
func (o programObserver) Observe(ctx context.Context, request ports.ObservationRequest) (model.Observation, error) {
observation, err := o.base.Observe(ctx, request)
if err != nil {
return model.Observation{}, err
}
flow := o.program.ProgramRuntime()
if flow.Manifest.RuntimeMode == delivery.ProgramRuntimeProtocol {
if flow.Runtime == nil {
return model.Observation{}, fmt.Errorf("program runtime %q observer is unavailable", flow.Identity.ID)
}
if err := requireComponentRuntimeCapabilities(request.Capabilities, flow.Manifest.Capabilities, flow.Identity.ID); err != nil {
return model.Observation{}, err
}
projection := observation
projection.ProgramFingerprint = o.program.Fingerprint()
snapshot, encodeErr := json.Marshal(projection)
if encodeErr != nil {
return model.Observation{}, fmt.Errorf("encode bounded control-program observation: %w", encodeErr)
}
response, invokeErr := flow.Runtime.InvokeProgram(ctx, delivery.ProgramRuntimeRequest{
ProtocolVersion: delivery.ProgramRuntimeProtocolVersion, Operation: delivery.ProgramObserveOperation,
ProgramID: flow.Identity.ID, ProgramVersion: flow.Identity.Version,
ProgramFingerprint: o.program.Fingerprint(), CorrelationID: request.Invocation.Correlation,
RepositoryRoot: request.Invocation.InvokingPath, Snapshot: snapshot, Settings: flow.Manifest.Settings,
Capabilities: boundedComponentCapabilities(request.Capabilities, flow.Manifest.Capabilities),
})
if invokeErr != nil {
return model.Observation{}, fmt.Errorf("program runtime %q observation failed: %w", flow.Identity.ID, invokeErr)
}
if err := validateProgramRuntimeResponse(flow, delivery.ProgramObserveOperation, request.Invocation.Correlation, response); err != nil {
return model.Observation{}, err
}
declared := make(map[string]bool, len(flow.Manifest.Facts))
for _, id := range flow.Manifest.Facts {
declared[id] = true
}
observation.ProgramFacts = make(map[string]model.Fact[string], len(declared))
for _, fact := range response.Facts {
if !declared[fact.ID] {
return model.Observation{}, fmt.Errorf("program runtime %q returned undeclared fact %q", flow.Identity.ID, fact.ID)
}
if _, exists := observation.ProgramFacts[fact.ID]; exists {
return model.Observation{}, fmt.Errorf("control-program fact %q was returned more than once", fact.ID)
}
if !fact.Status.Valid() || fact.Fingerprint == "" {
return model.Observation{}, fmt.Errorf("program runtime %q returned invalid fact %q", flow.Identity.ID, fact.ID)
}
observation.ProgramFacts[fact.ID] = model.Fact[string]{
Status: fact.Status, Value: fact.Value, Detail: fact.Detail,
Evidence: []model.Evidence{{Source: "control-program:" + flow.Identity.ID, Fingerprint: fact.Fingerprint, ObservedAt: observation.ObservedAt}},
}
delete(declared, fact.ID)
}
if len(declared) != 0 {
return model.Observation{}, fmt.Errorf("program runtime %q omitted required observed facts", flow.Identity.ID)
}
}
// Extension observers receive the same core-plus-flow projection. Facts
// returned by one extension are collected into the final snapshot but are
// never exposed to another observer based on invocation order.
observation.ExtensionFacts = nil
extensionProjection := observation
for _, extension := range o.program.Extensions() {
if len(extension.Manifest.Facts) == 0 {
continue
}
// Repository-selected executable extensions remain inert until the
// observed configuration and recorded program identity prove that this
// exact composition has already crossed the Kernel admission boundary.
if extension.Manifest.ExecutableSHA256 != "" && !observation.ExecutableRuntimeAdmitted(o.program.Fingerprint()) {
continue
}
if requireComponentRuntimeCapabilities(request.Capabilities, extension.Manifest.Capabilities, extension.Identity.ID) != nil {
continue
}
if extension.Runtime == nil {
return model.Observation{}, fmt.Errorf("extension %q observer is unavailable", extension.Identity.ID)
}
projection := extensionProjection
projection.ProgramFingerprint = o.program.Fingerprint()
snapshot, err := json.Marshal(projection)
if err != nil {
return model.Observation{}, fmt.Errorf("encode bounded extension observation: %w", err)
}
response, err := extension.Runtime.Invoke(ctx, delivery.ExtensionRequest{
ProtocolVersion: delivery.ExtensionProtocolVersion, Operation: delivery.ExtensionObserveOperation,
ExtensionID: extension.Identity.ID, ExtensionVersion: extension.Identity.Version,
ProgramFingerprint: o.program.Fingerprint(), CorrelationID: request.Invocation.Correlation,
RepositoryRoot: request.Invocation.InvokingPath, Snapshot: snapshot, Settings: extension.Manifest.Settings,
Capabilities: boundedComponentCapabilities(request.Capabilities, extension.Manifest.Capabilities),
})
if err != nil {
return model.Observation{}, fmt.Errorf("extension %q observation failed: %w", extension.Identity.ID, err)
}
if err := validateExtensionResponse(extension, delivery.ExtensionObserveOperation, request.Invocation.Correlation, response); err != nil {
return model.Observation{}, err
}
declared := make(map[string]bool, len(extension.Manifest.Facts))
for _, id := range extension.Manifest.Facts {
declared[id] = true
}
if observation.ExtensionFacts == nil {
observation.ExtensionFacts = map[string]model.Fact[string]{}
}
for _, fact := range response.Facts {
if !declared[fact.ID] {
return model.Observation{}, fmt.Errorf("extension %q returned undeclared fact %q", extension.Identity.ID, fact.ID)
}
if _, exists := observation.ExtensionFacts[fact.ID]; exists {
return model.Observation{}, fmt.Errorf("extension fact %q was returned more than once", fact.ID)
}
if !fact.Status.Valid() || fact.Fingerprint == "" {
return model.Observation{}, fmt.Errorf("extension %q returned invalid fact %q", extension.Identity.ID, fact.ID)
}
observation.ExtensionFacts[fact.ID] = model.Fact[string]{
Status: fact.Status, Value: fact.Value, Detail: fact.Detail,
Evidence: []model.Evidence{{Source: "extension:" + extension.Identity.ID, Fingerprint: fact.Fingerprint, ObservedAt: observation.ObservedAt}},
}
delete(declared, fact.ID)
}
if len(declared) != 0 {
return model.Observation{}, fmt.Errorf("extension %q omitted required observed facts", extension.Identity.ID)
}
}
if request.VerifyTransitionID != "" {
transition, ok := o.program.RuntimeRegistry().Lookup(request.VerifyTransitionID)
if ok && transition.Origin.Kind == catalog.OriginControlProgram && flow.Manifest.RuntimeMode == delivery.ProgramRuntimeProtocol {
if err := requireComponentRuntimeCapabilities(request.Capabilities, flow.Manifest.Capabilities, flow.Identity.ID); err != nil {
return model.Observation{}, err
}
snapshot, encodeErr := json.Marshal(observation)
if encodeErr != nil {
return model.Observation{}, encodeErr
}
response, invokeErr := flow.Runtime.InvokeProgram(ctx, delivery.ProgramRuntimeRequest{
ProtocolVersion: delivery.ProgramRuntimeProtocolVersion, Operation: delivery.ProgramVerifyOperation,
ProgramID: flow.Identity.ID, ProgramVersion: flow.Identity.Version,
ProgramFingerprint: o.program.Fingerprint(), CorrelationID: request.Invocation.Correlation,
RepositoryRoot: request.Invocation.InvokingPath, TransitionID: transition.ID, Snapshot: snapshot, Settings: flow.Manifest.Settings,
Capabilities: boundedComponentCapabilities(request.Capabilities, flow.Manifest.Capabilities),
})
if invokeErr != nil {
return model.Observation{}, invokeErr
}
if err := validateProgramRuntimeResponse(flow, delivery.ProgramVerifyOperation, request.Invocation.Correlation, response); err != nil {
return model.Observation{}, err
}
if response.Verified == nil || !*response.Verified {
return model.Observation{}, fmt.Errorf("control-program verifier %q rejected the postcondition", transition.Verifier)
}
}
if ok && transition.Origin.Kind == catalog.OriginExtension {
extension, exists := o.program.ExtensionByID(transition.Origin.ID)
if !exists || extension.Runtime == nil {
return model.Observation{}, fmt.Errorf("extension verifier %q is unavailable", transition.Verifier)
}
if err := requireComponentRuntimeCapabilities(request.Capabilities, extension.Manifest.Capabilities, extension.Identity.ID); err != nil {
return model.Observation{}, err
}
snapshot, err := json.Marshal(observation)
if err != nil {
return model.Observation{}, err
}
response, err := extension.Runtime.Invoke(ctx, delivery.ExtensionRequest{
ProtocolVersion: delivery.ExtensionProtocolVersion, Operation: delivery.ExtensionVerifyOperation,
ExtensionID: extension.Identity.ID, ExtensionVersion: extension.Identity.Version,
ProgramFingerprint: o.program.Fingerprint(), CorrelationID: request.Invocation.Correlation,
RepositoryRoot: request.Invocation.InvokingPath, TransitionID: transition.ID, Snapshot: snapshot, Settings: extension.Manifest.Settings,
Capabilities: boundedComponentCapabilities(request.Capabilities, extension.Manifest.Capabilities),
})
if err != nil {
return model.Observation{}, err
}
if err := validateExtensionResponse(extension, delivery.ExtensionVerifyOperation, request.Invocation.Correlation, response); err != nil {
return model.Observation{}, err
}
if response.Verified == nil || !*response.Verified {
return model.Observation{}, fmt.Errorf("extension verifier %q rejected the postcondition", transition.Verifier)
}
}
}
return observation, nil
}
func requireComponentRuntimeCapabilities(granted, declared []catalog.Capability, component string) error {
if !catalog.NewCapabilitySet(declared...)[catalog.CapabilityCommandExecute] {
return fmt.Errorf("component %q does not declare command.execute", component)
}
return protocol.RequireCapability(granted, catalog.CapabilityCommandExecute, "component runtime "+component)
}
func boundedComponentCapabilities(granted, declared []catalog.Capability) []catalog.Capability {
grantedSet := catalog.NewCapabilitySet(granted...)
result := catalog.CapabilitySet{}
for _, capability := range declared {
if grantedSet[capability] {
result[capability] = true
}
}
return result.Sorted()
}
func validateProgramRuntimeResponse(flow delivery.CompiledProgramRuntime, operation delivery.ProgramRuntimeOperation, correlation string, response delivery.ProgramRuntimeResponse) error {
if response.ProtocolVersion != delivery.ProgramRuntimeProtocolVersion || response.Operation != operation ||
response.ProgramID != flow.Identity.ID || response.ProgramVersion != flow.Identity.Version || response.CorrelationID != correlation {
return fmt.Errorf("program runtime %q returned a mismatched protocol response", flow.Identity.ID)
}
if err := delivery.ValidateProgramRuntimeOperationResponse(operation, response); err != nil {
return fmt.Errorf("program runtime %q returned an invalid operation response: %w", flow.Identity.ID, err)
}
if response.ErrorClass != "" || response.Error != "" {
return ComponentRuntimeError{Component: fmt.Sprintf("program runtime %q", flow.Identity.ID), Operation: string(operation), Class: response.ErrorClass, Message: response.Error}
}
return nil
}
func validateExtensionResponse(extension delivery.CompiledExtension, operation delivery.ExtensionOperation, correlation string, response delivery.ExtensionResponse) error {
if response.ProtocolVersion != delivery.ExtensionProtocolVersion || response.Operation != operation ||
response.ExtensionID != extension.Identity.ID || response.ExtensionVersion != extension.Identity.Version ||
response.CorrelationID != correlation {
return fmt.Errorf("extension %q returned a mismatched protocol response", extension.Identity.ID)
}
if err := delivery.ValidateExtensionOperationResponse(operation, response); err != nil {
return fmt.Errorf("extension %q returned an invalid operation response: %w", extension.Identity.ID, err)
}
if response.ErrorClass != "" || response.Error != "" {
return ComponentRuntimeError{Component: fmt.Sprintf("extension %q", extension.Identity.ID), Operation: string(operation), Class: response.ErrorClass, Message: response.Error}
}
return nil
}