-
Notifications
You must be signed in to change notification settings - Fork 10
/
rm.go
208 lines (175 loc) · 5.1 KB
/
rm.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
207
208
package app
import (
"encoding/json"
"os"
"path/filepath"
"github.com/opcr-io/policy/oci"
"github.com/opcr-io/policy/parser"
"github.com/opcr-io/policy/pkg/errors"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
func (c *PolicyApp) Rm(existingRef string, force bool) error {
defer c.Cancel()
existingRefParsed, err := parser.CalculatePolicyRef(existingRef, c.Configuration.DefaultDomain)
if err != nil {
return err
}
confirmation := force
if !force {
c.UI.Exclamation().
WithStringValue("reference", existingRefParsed).
WithAskBoolMap("[Y/n]", &confirmation, map[string]bool{
"": true,
"y": true,
"n": false,
}).Msgf("Are you sure?")
}
if !confirmation {
c.UI.Exclamation().Msg("Operation canceled by user.")
return nil
}
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
}
ref, ok := existingRefs[existingRefParsed]
if !ok {
return errors.NotFound.WithMessage("policy [%s] not in the local store", existingRef)
}
// attach ref name annotation for comparison.
if len(ref.Annotations) == 0 || ref.Annotations[ocispec.AnnotationRefName] == "" {
oldAnnotations := ref.Annotations
ref.Annotations = make(map[string]string)
if oldAnnotations != nil {
ref.Annotations = oldAnnotations
}
ref.Annotations[ocispec.AnnotationRefName] = existingRefParsed
}
// Reload ociClient with refreshed index to update reference list.
ociClient, err = oci.NewOCI(c.Context, c.Logger, c.getHosts, c.Configuration.PoliciesRoot())
if err != nil {
return err
}
if ref.MediaType != oci.MediaTypeImageLayer {
return c.removeBasedOnManifest(ociClient, &ref, existingRefParsed)
}
err = c.removeBasedOnTarball(ociClient, &ref, existingRefs, existingRefParsed)
if err != nil {
return err
}
c.UI.Normal().
WithStringValue("reference", existingRef).
Msg("Removed reference.")
return nil
}
func (c *PolicyApp) removeBasedOnManifest(ociClient *oci.Oci, ref *ocispec.Descriptor, refString string) error {
anotherImagewithSameDigest, err := c.buildFromSameImage(ref)
if err != nil {
return err
}
err = ociClient.Untag(ref, refString)
if err != nil {
return err
}
if anotherImagewithSameDigest {
return nil
}
err = ociClient.GetStore().Delete(c.Context, *ref)
if err != nil {
return err
}
return nil
}
func (c *PolicyApp) removeBasedOnTarball(ociClient *oci.Oci, ref *ocispec.Descriptor, existingRefs map[string]ocispec.Descriptor, existingRefParsed string) error {
err := ociClient.Untag(ref, existingRefParsed)
if err != nil {
return err
}
// Check if existing images use same digest.
removeBlob := true
for _, v := range existingRefs {
if v.Digest == ref.Digest {
removeBlob = false
break
}
}
tarballStillUsed, err := c.tarballReferencedByOtherManifests(ociClient, ref)
if err != nil {
return err
}
// only remove the blob if not used by another reference.
if removeBlob && !tarballStillUsed {
// Hack to remove the existing digest until ocistore deleter is implemented
// https://github.com/oras-project/oras-go/issues/454
err := ociClient.GetStore().Delete(c.Context, *ref)
if err != nil {
return err
}
}
return nil
}
func (c *PolicyApp) tarballReferencedByOtherManifests(ociClient *oci.Oci, ref *ocispec.Descriptor) (bool, error) {
type index struct {
Version int `json:"schemaVersion"`
Manifests []ocispec.Descriptor `json:"manifests"`
}
var localIndex index
indexPath := filepath.Join(c.Configuration.PoliciesRoot(), "index.json")
indexBytes, err := os.ReadFile(indexPath)
if err != nil {
return false, err
}
err = json.Unmarshal(indexBytes, &localIndex)
if err != nil {
return false, err
}
for i := range localIndex.Manifests {
descriptor := localIndex.Manifests[i]
// check if an image built with v0.1 policy that has the same digest is present in index.json
if descriptor.MediaType == oci.MediaTypeImageLayer && descriptor.Digest == ref.Digest {
return true, nil
}
if descriptor.MediaType != oci.MediaTypeImageLayer {
manifest, err := ociClient.GetManifest(&descriptor)
if err != nil {
return false, err
}
for _, layer := range manifest.Layers {
if (layer.MediaType == ocispec.MediaTypeImageLayerGzip || layer.MediaType == ocispec.MediaTypeImageLayer) && layer.Digest == ref.Digest {
return true, nil
}
}
}
}
return false, nil
}
func (c *PolicyApp) buildFromSameImage(ref *ocispec.Descriptor) (bool, error) {
type index struct {
Version int `json:"schemaVersion"`
Manifests []ocispec.Descriptor `json:"manifests"`
}
var localIndex index
indexPath := filepath.Join(c.Configuration.PoliciesRoot(), "index.json")
indexBytes, err := os.ReadFile(indexPath)
if err != nil {
return false, err
}
err = json.Unmarshal(indexBytes, &localIndex)
if err != nil {
return false, err
}
sameDigestCount := 0
for _, image := range localIndex.Manifests {
if image.Digest == ref.Digest {
sameDigestCount++
}
}
if sameDigestCount > 1 {
return true, nil
}
return false, nil
}