-
Notifications
You must be signed in to change notification settings - Fork 13
/
policy.go
206 lines (172 loc) · 4.5 KB
/
policy.go
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
// Copyright 2023 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package management
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/spf13/viper"
"github.com/veraison/services/builtin"
"github.com/veraison/services/config"
"github.com/veraison/services/handler"
"github.com/veraison/services/log"
"github.com/veraison/services/plugin"
"github.com/veraison/services/policy"
)
type PolicyManager struct {
Agent policy.IAgent
Store *policy.Store
SupportedSchemes []string
}
func CreatePolicyManagerFromConfig(v *viper.Viper, name string) (*PolicyManager, error) {
subs, err := config.GetSubs(v, "*po-agent", "po-store", "*plugin")
if err != nil {
return nil, err
}
agent, err := policy.CreateAgent(subs["po-agent"], log.Named(name+"-agent"))
if err != nil {
return nil, err
}
store, err := policy.NewStore(subs["po-store"], log.Named(name+"-store"))
if err != nil {
return nil, err
}
var pluginManager plugin.IManager[handler.IEvidenceHandler]
if config.SchemeLoader == "plugins" { // nolint:gocritic
pluginManager, err = plugin.CreateGoPluginManager(
subs["plugin"], log.Named("plugin"),
"evidence-handler", handler.EvidenceHandlerRPC)
if err != nil {
log.Fatalf("plugin manager initialization failed: %v", err)
}
} else if config.SchemeLoader == "builtin" {
pluginManager, err = builtin.CreateBuiltinManager[handler.IEvidenceHandler](
subs["plugin"], log.Named("builtin"), "evidence-handler")
if err != nil {
log.Fatalf("scheme manager initialization failed: %v", err)
}
} else {
log.Panicw("invalid SchemeLoader value", "SchemeLoader", config.SchemeLoader)
}
defer pluginManager.Close()
supportedSchemes := pluginManager.GetRegisteredAttestationSchemes()
return NewPolicyManager(agent, store, supportedSchemes), nil
}
func NewPolicyManager(agent policy.IAgent, store *policy.Store, schemes []string) *PolicyManager {
return &PolicyManager{Agent: agent, Store: store, SupportedSchemes: schemes}
}
func (o *PolicyManager) IsSchemeSupported(scheme string) bool {
for _, supported := range o.SupportedSchemes {
if supported == scheme {
return true
}
}
return false
}
func (o *PolicyManager) Validate(ctx context.Context, policyRules string) error {
return o.Agent.Validate(ctx, policyRules)
}
func (o *PolicyManager) Update(
ctx context.Context,
tenantID string,
scheme string,
name string,
rules string,
) (*policy.Policy, error) {
key, err := o.resolvePolicyKey(tenantID, scheme)
if err != nil {
return nil, err
}
return o.Store.Update(key, name, o.Agent.GetBackendName(), rules)
}
func (o *PolicyManager) GetActive(
ctx context.Context,
tenantID string,
scheme string,
) (*policy.Policy, error) {
key, err := o.resolvePolicyKey(tenantID, scheme)
if err != nil {
return nil, err
}
return o.Store.GetActive(key)
}
func (o *PolicyManager) GetPolicy(
ctx context.Context,
tenantID string,
scheme string,
policyID uuid.UUID,
) (*policy.Policy, error) {
key, err := o.resolvePolicyKey(tenantID, scheme)
if err != nil {
return nil, err
}
return o.Store.GetPolicy(key, policyID)
}
func (o *PolicyManager) GetPolicies(
ctx context.Context,
tenantID string,
scheme string,
name string,
) ([]*policy.Policy, error) {
key, err := o.resolvePolicyKey(tenantID, scheme)
if err != nil {
return nil, err
}
policies, err := o.Store.Get(key)
if err != nil {
return nil, err
}
if name == "" {
return policies, nil
}
ret := make([]*policy.Policy, 0)
for _, pol := range policies {
if pol.Name == name {
ret = append(ret, pol)
}
}
return ret, nil
}
func (o *PolicyManager) Activate(
ctx context.Context,
tenantID string,
scheme string,
policyID uuid.UUID,
) error {
key, err := o.resolvePolicyKey(tenantID, scheme)
if err != nil {
return err
}
return o.Store.Activate(key, policyID)
}
func (o *PolicyManager) DeactivateAll(
ctx context.Context,
tenantID string,
scheme string,
) error {
key, err := o.resolvePolicyKey(tenantID, scheme)
if err != nil {
return err
}
return o.Store.DeactivateAll(key)
}
func (o *PolicyManager) resolvePolicyKey(
tenantID string,
scheme string,
) (policy.PolicyKey, error) {
schemeFound := false
for _, supportedScheme := range o.SupportedSchemes {
if supportedScheme == scheme {
schemeFound = true
break
}
}
if !schemeFound {
return policy.PolicyKey{}, fmt.Errorf("Unsupported attestation scheme: %q", scheme)
}
return policy.PolicyKey{
TenantId: tenantID,
Scheme: scheme,
Name: o.Agent.GetBackendName(),
}, nil
}