1
1
package app
2
2
3
3
import (
4
- "io "
4
+ "bufio "
5
5
"os"
6
6
"path/filepath"
7
+ "strings"
7
8
"time"
8
9
9
10
"github.com/aserto-dev/runtime"
10
- containerd_content "github.com/containerd/containerd/content"
11
11
"github.com/containerd/containerd/errdefs"
12
12
"github.com/containerd/containerd/reference/docker"
13
- "github.com/google/uuid"
13
+
14
14
"github.com/opcr-io/policy/pkg/oci"
15
15
"github.com/opcr-io/policy/pkg/parser"
16
16
"github.com/opencontainers/go-digest"
17
17
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
18
18
"github.com/pkg/errors"
19
- "oras.land/oras-go/pkg /content"
19
+ orasoci "oras.land/oras-go/v2 /content/oci "
20
20
)
21
21
22
22
const (
@@ -84,11 +84,7 @@ func (c *PolicyApp) Build(ref string, path []string, annotations map[string]stri
84
84
return errors .Wrap (err , "failed to build opa policy bundle" )
85
85
}
86
86
87
- ociStore , err := content .NewOCI (c .Configuration .PoliciesRoot ())
88
- if err != nil {
89
- return err
90
- }
91
- err = ociStore .LoadIndex ()
87
+ ociStore , err := orasoci .New (c .Configuration .PoliciesRoot ())
92
88
if err != nil {
93
89
return err
94
90
}
@@ -97,27 +93,31 @@ func (c *PolicyApp) Build(ref string, path []string, annotations map[string]stri
97
93
annotations = map [string ]string {}
98
94
}
99
95
100
- parsedRef , err := docker .ParseDockerRef (ref )
96
+ familiarezedRef , err := parser .CalculatePolicyRef (ref , c .Configuration .DefaultDomain )
97
+ if err != nil {
98
+ return errors .Wrap (err , "failed to calculate policy reference" )
99
+ }
100
+
101
+ parsedRef , err := docker .ParseDockerRef (familiarezedRef )
101
102
if err != nil {
102
103
return err
103
104
}
105
+
104
106
annotations [ocispec .AnnotationTitle ] = docker .TrimNamed (parsedRef ).String ()
105
107
annotations [AnnotationPolicyRegistryType ] = PolicyTypePolicy
106
108
annotations [ocispec .AnnotationCreated ] = time .Now ().UTC ().Format (time .RFC3339 )
107
109
108
- descriptor , err := c .createImage (ociStore , outfile , annotations )
110
+ desc , err := c .createImage (ociStore , outfile , annotations )
109
111
if err != nil {
110
112
return err
111
113
}
112
114
113
- parsed , err := parser . CalculatePolicyRef ( ref , c . Configuration . DefaultDomain )
115
+ err = ociStore . Tag ( c . Context , desc , parsedRef . String () )
114
116
if err != nil {
115
- return errors . Wrap ( err , "failed to calculate policy reference" )
117
+ return err
116
118
}
117
119
118
- ociStore .AddReference (parsed , descriptor )
119
-
120
- c .UI .Normal ().WithStringValue ("reference" , ref ).Msg ("Tagging image." )
120
+ c .UI .Normal ().WithStringValue ("reference" , parsedRef .String ()).Msg ("Tagging image." )
121
121
122
122
err = ociStore .SaveIndex ()
123
123
if err != nil {
@@ -127,27 +127,19 @@ func (c *PolicyApp) Build(ref string, path []string, annotations map[string]stri
127
127
return nil
128
128
}
129
129
130
- func (c * PolicyApp ) createImage (ociStore * content. OCI , tarball string , annotations map [string ]string ) (ocispec.Descriptor , error ) {
130
+ func (c * PolicyApp ) createImage (ociStore * orasoci. Store , tarball string , annotations map [string ]string ) (ocispec.Descriptor , error ) {
131
131
descriptor := ocispec.Descriptor {}
132
-
132
+ ociStore . AutoSaveIndex = true
133
133
fDigest , err := c .fileDigest (tarball )
134
134
if err != nil {
135
135
return descriptor , err
136
136
}
137
137
138
- _ , err = ociStore . Info ( c . Context , fDigest )
139
- if err != nil && ! errors . Is ( err , errdefs . ErrNotFound ) {
138
+ tarballFile , err := os . Open ( tarball )
139
+ if err != nil {
140
140
return descriptor , err
141
141
}
142
-
143
- if err == nil {
144
- err = ociStore .Delete (c .Context , fDigest )
145
- if err != nil {
146
- return descriptor , errors .Wrap (err , "couldn't overwrite existing image" )
147
- }
148
- }
149
-
150
- tarballFile , err := os .Open (tarball )
142
+ fileInfo , err := tarballFile .Stat ()
151
143
if err != nil {
152
144
return descriptor , err
153
145
}
@@ -158,44 +150,35 @@ func (c *PolicyApp) createImage(ociStore *content.OCI, tarball string, annotatio
158
150
}
159
151
}()
160
152
161
- fileInfo , err := tarballFile .Stat ()
162
- if err != nil {
163
- return descriptor , err
164
- }
165
-
166
- descriptor = ocispec.Descriptor {
167
- MediaType : oci .MediaTypeImageLayer ,
168
- Digest : fDigest ,
169
- Size : fileInfo .Size (),
170
- Annotations : annotations ,
171
- }
153
+ descriptor .Digest = fDigest
154
+ descriptor .Size = fileInfo .Size ()
155
+ descriptor .Annotations = annotations
156
+ descriptor .MediaType = oci .MediaTypeImageLayer
172
157
173
- ociWriter , err := ociStore .Writer (
174
- c .Context ,
175
- containerd_content .WithDescriptor (descriptor ),
176
- containerd_content .WithRef (uuid .NewString ()))
177
- if err != nil {
158
+ exists , err := ociStore .Exists (c .Context , descriptor )
159
+ if err != nil && ! errors .Is (err , errdefs .ErrNotFound ) {
178
160
return descriptor , err
179
161
}
180
- defer func () {
181
- err := ociWriter .Close ()
162
+
163
+ if exists {
164
+ // Hack to remove the existing digest until ocistore deleter is implemented
165
+ // https://github.com/oras-project/oras-go/issues/454
166
+ digestPath := filepath .Join (strings .Split (descriptor .Digest .String (), ":" )... )
167
+ blob := filepath .Join (c .Configuration .PoliciesRoot (), "blobs" , digestPath )
168
+ err = os .Remove (blob )
182
169
if err != nil {
183
- c . UI . Problem (). WithErr ( err ). Msg ( "Failed to close local OCI store." )
170
+ return descriptor , err
184
171
}
185
- }()
186
-
187
- _ , err = io .Copy (ociWriter , tarballFile )
188
- if err != nil {
189
- return descriptor , err
190
172
}
191
173
192
- err = ociWriter .Commit (c .Context , fileInfo .Size (), fDigest )
174
+ reader := bufio .NewReader (tarballFile )
175
+
176
+ err = ociStore .Push (c .Context , descriptor , reader )
193
177
if err != nil {
194
178
return descriptor , err
195
179
}
196
-
197
180
c .UI .Normal ().
198
- WithStringValue ("digest" , ociWriter .Digest () .String ()).
181
+ WithStringValue ("digest" , descriptor .Digest .String ()).
199
182
Msg ("Created new image." )
200
183
201
184
return descriptor , nil
0 commit comments