-
Notifications
You must be signed in to change notification settings - Fork 2
/
policies.go
50 lines (43 loc) · 1.17 KB
/
policies.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
package runtime
import (
"context"
"github.com/open-policy-agent/opa/server/types"
"github.com/open-policy-agent/opa/storage"
"github.com/pkg/errors"
)
func (r *Runtime) ListPolicies(ctx context.Context) ([]types.PolicyV1, error) {
policies := []types.PolicyV1{}
err := storage.Txn(ctx, r.pluginsManager.Store, storage.TransactionParams{}, func(txn storage.Transaction) error {
compiler := r.pluginsManager.GetCompiler()
ids, err := r.storage.ListPolicies(ctx, txn)
if err != nil {
return errors.Wrap(err, "failed to list policies")
}
for _, id := range ids {
policyBs, err := r.storage.GetPolicy(ctx, txn, id)
if err != nil {
return errors.Wrapf(err, "failed to get policy with ID [%s]", id)
}
policy := types.PolicyV1{
ID: id,
Raw: string(policyBs),
AST: compiler.Modules[id],
}
policies = append(policies, policy)
}
return nil
})
return policies, err
}
func (r *Runtime) GetPolicy(ctx context.Context, id string) (*types.PolicyV1, error) {
policies, err := r.ListPolicies(ctx)
if err != nil {
return nil, err
}
for _, policy := range policies {
if policy.ID == id {
return &policy, nil
}
}
return nil, err
}