-
Notifications
You must be signed in to change notification settings - Fork 12
/
repl.go
108 lines (91 loc) · 2.74 KB
/
repl.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
package app
import (
"context"
"fmt"
"path/filepath"
"time"
"github.com/aserto-dev/runtime"
"github.com/opcr-io/policy/oci"
"github.com/opcr-io/policy/parser"
"github.com/opcr-io/policy/pkg/errors"
"github.com/open-policy-agent/opa/repl"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
func (c *PolicyApp) Repl(ref string, maxErrors int) error {
defer c.Cancel()
ociClient, err := oci.NewOCI(c.Context, c.Logger, c.getHosts, c.Configuration.PoliciesRoot())
if err != nil {
return err
}
existingRefs, err := ociClient.ListReferences()
if err != nil {
return err
}
existingRefParsed, err := parser.CalculatePolicyRef(ref, c.Configuration.DefaultDomain)
if err != nil {
return err
}
descriptor, ok := existingRefs[existingRefParsed]
if !ok {
err := c.Pull(ref)
if err != nil {
return err
}
existingRefs, err := ociClient.ListReferences()
if err != nil {
return err
}
existingRefParsed, err := parser.CalculatePolicyRef(ref, c.Configuration.DefaultDomain)
if err != nil {
return err
}
descriptor, ok = existingRefs[existingRefParsed]
if !ok {
return errors.NotFound.WithMessage("policy [%s] not in the local store", ref)
}
}
// check for media type - if manifest get tarbarll digest hex.
bundleHex, err := c.getBundleHex(ociClient, &descriptor)
if err != nil {
return err
}
bundleFile := filepath.Join(c.Configuration.PoliciesRoot(), "blobs", "sha256", bundleHex)
opaRuntime, cleanup, err := runtime.NewRuntime(c.Context, c.Logger, &runtime.Config{
InstanceID: "policy-run",
LocalBundles: runtime.LocalBundlesConfig{
Paths: []string{bundleFile},
},
})
if err != nil {
return errors.ReplFailed.WithError(err)
}
defer cleanup()
err = opaRuntime.Start(c.Context)
if err != nil {
return errors.ReplFailed.WithError(err)
}
err = opaRuntime.WaitForPlugins(c.Context, time.Minute*1)
if err != nil {
return errors.ReplFailed.WithError(err)
}
loop := repl.New(opaRuntime.GetPluginsManager().Store, c.Configuration.ReplHistoryFile(), c.UI.Output(), "", maxErrors, fmt.Sprintf("running policy [%s]", ref))
loop.Loop(context.Background())
return nil
}
func (c *PolicyApp) getBundleHex(ociClient *oci.Oci, descriptor *ocispec.Descriptor) (string, error) {
var bundleHex string
// check for media type - if manifest get tarbarll digest hex.
if descriptor.MediaType == ocispec.MediaTypeImageManifest {
bundleDescriptor, _, err := ociClient.GetTarballAndConfigLayerDescriptor(c.Context, descriptor)
if err != nil {
return "", err
}
bundleHex = bundleDescriptor.Digest.Hex()
if bundleHex == "" {
return "", errors.ReplFailed.WithMessage("current manifest does not contain a MediaTypeImageLayerGzip")
}
} else {
bundleHex = descriptor.Digest.Hex()
}
return bundleHex, nil
}