|
| 1 | +package policy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +var ( |
| 9 | + ErrProtocol = fmt.Errorf("encoding error") |
| 10 | + ErrInternal = fmt.Errorf("internal error") |
| 11 | +) |
| 12 | + |
| 13 | +type API interface { |
| 14 | + // PerformInvocation is called when a component is invoked |
| 15 | + PerformInvocation(ctx context.Context, req *PerformInvocationRequest) (*Response, error) |
| 16 | + // StartComponent is called when a component is started |
| 17 | + StartComponent(ctx context.Context, req *StartComponentRequest) (*Response, error) |
| 18 | + // StartProvider is called when a provider is started |
| 19 | + StartProvider(ctx context.Context, req *StartProviderRequest) (*Response, error) |
| 20 | +} |
| 21 | + |
| 22 | +var _ API = (*APIMock)(nil) |
| 23 | + |
| 24 | +type APIMock struct { |
| 25 | + PerformInvocationFunc func(ctx context.Context, req *PerformInvocationRequest) (*Response, error) |
| 26 | + StartComponentFunc func(ctx context.Context, req *StartComponentRequest) (*Response, error) |
| 27 | + StartProviderFunc func(ctx context.Context, req *StartProviderRequest) (*Response, error) |
| 28 | +} |
| 29 | + |
| 30 | +func (m *APIMock) PerformInvocation(ctx context.Context, req *PerformInvocationRequest) (*Response, error) { |
| 31 | + return m.PerformInvocationFunc(ctx, req) |
| 32 | +} |
| 33 | + |
| 34 | +func (m *APIMock) StartComponent(ctx context.Context, req *StartComponentRequest) (*Response, error) { |
| 35 | + return m.StartComponentFunc(ctx, req) |
| 36 | +} |
| 37 | + |
| 38 | +func (m *APIMock) StartProvider(ctx context.Context, req *StartProviderRequest) (*Response, error) { |
| 39 | + return m.StartProviderFunc(ctx, req) |
| 40 | +} |
| 41 | + |
| 42 | +// Request is the structure of the request sent to the policy engine |
| 43 | +type BaseRequest[T any] struct { |
| 44 | + Id string `json:"requestId"` |
| 45 | + Kind string `json:"kind"` |
| 46 | + Version string `json:"version"` |
| 47 | + Host Host `json:"host"` |
| 48 | + Request T `json:"request"` |
| 49 | +} |
| 50 | + |
| 51 | +// Decision is a helper function to create a response |
| 52 | +func (r BaseRequest[T]) Decision(allowed bool, msg string) *Response { |
| 53 | + return &Response{ |
| 54 | + Id: r.Id, |
| 55 | + Permitted: allowed, |
| 56 | + Message: msg, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Deny is a helper function to create a response with a deny decision |
| 61 | +func (r BaseRequest[T]) Deny(msg string) *Response { |
| 62 | + return r.Decision(false, msg) |
| 63 | +} |
| 64 | + |
| 65 | +// Allow is a helper function to create a response with an allow decision |
| 66 | +func (r BaseRequest[T]) Allow(msg string) *Response { |
| 67 | + return r.Decision(true, msg) |
| 68 | +} |
| 69 | + |
| 70 | +// Response is the structure of the response sent by the policy engine |
| 71 | +type Response struct { |
| 72 | + Id string `json:"requestId"` |
| 73 | + Permitted bool `json:"permitted"` |
| 74 | + Message string `json:"message,omitempty"` |
| 75 | +} |
| 76 | + |
| 77 | +type Claims struct { |
| 78 | + PublicKey string `json:"publicKey"` |
| 79 | + Issuer string `json:"issuer"` |
| 80 | + IssuedAt int `json:"issuedAt"` |
| 81 | + ExpiresAt int `json:"expiresAt"` |
| 82 | + Expired bool `json:"expired"` |
| 83 | +} |
| 84 | + |
| 85 | +type StartComponentPayload struct { |
| 86 | + ComponentId string `json:"componentId"` |
| 87 | + ImageRef string `json:"imageRef"` |
| 88 | + MaxInstances int `json:"maxInstances"` |
| 89 | + Annotations map[string]string `json:"annotations"` |
| 90 | +} |
| 91 | + |
| 92 | +type StartComponentRequest = BaseRequest[StartComponentPayload] |
| 93 | + |
| 94 | +type StartProviderPayload struct { |
| 95 | + ProviderId string `json:"providerId"` |
| 96 | + ImageRef string `json:"imageRef"` |
| 97 | + Annotations map[string]string `json:"annotations"` |
| 98 | +} |
| 99 | + |
| 100 | +type StartProviderRequest = BaseRequest[StartProviderPayload] |
| 101 | + |
| 102 | +type PerformInvocationPayload struct { |
| 103 | + Interface string `json:"interface"` |
| 104 | + Function string `json:"function"` |
| 105 | + // NOTE(lxf): this covers components but not providers. wut?!? |
| 106 | + Target InvocationTarget `json:"target"` |
| 107 | +} |
| 108 | + |
| 109 | +type PerformInvocationRequest = BaseRequest[PerformInvocationPayload] |
| 110 | + |
| 111 | +type InvocationTarget struct { |
| 112 | + ComponentId string `json:"componentId"` |
| 113 | + ImageRef string `json:"imageRef"` |
| 114 | + MaxInstances int `json:"maxInstances"` |
| 115 | + Annotations map[string]string `json:"annotations"` |
| 116 | +} |
| 117 | + |
| 118 | +type Host struct { |
| 119 | + PublicKey string `json:"publicKey"` |
| 120 | + Lattice string `json:"lattice"` |
| 121 | + Labels map[string]string `json:"labels"` |
| 122 | +} |
0 commit comments