/
pull_test.go
327 lines (287 loc) · 12.4 KB
/
pull_test.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023 The Falco Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pull_test
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"oras.land/oras-go/v2/registry/remote/auth"
"github.com/falcosecurity/falcoctl/cmd"
"github.com/falcosecurity/falcoctl/pkg/oci"
"github.com/falcosecurity/falcoctl/pkg/oci/authn"
ocipusher "github.com/falcosecurity/falcoctl/pkg/oci/pusher"
out "github.com/falcosecurity/falcoctl/pkg/output"
)
//nolint:lll,unused // no need to check for line length.
var registryPullUsage = `Usage:
falcoctl registry pull hostname/repo[:tag|@digest] [flags]
Flags:
-o, --dest-dir string destination dir where to save the artifacts(default: current directory)
-h, --help help for pull
--plain-http allows interacting with remote registry via plain http requests
--platform stringArray os and architecture of the artifact in OS/ARCH format (only for plugins artifacts)
Global Flags:
--config string config file to be used for falcoctl (default "/etc/falcoctl/falcoctl.yaml")
--disable-styling Disable output styling such as spinners, progress bars and colors. Styling is automatically disabled if not attacched to a tty (default false)
-v, --verbose Enable verbose logs (default false)
`
//nolint:unused // false positive
var registryPullHelp = `Pull Falco "rulesfile" or "plugin" OCI artifacts from remote registry.
Artifact references are passed as arguments.
A reference is a fully qualified reference ("<registry>/<repository>"),
optionally followed by ":<tag>" (":latest" is assumed by default when no tag is given).
Example - Pull artifact "myplugin" for the platform where falcoctl is running (default) in the current working directory (default):
falcoctl registry pull localhost:5000/myplugin:latest
Example - Pull artifact "myplugin" for platform "linux/arm64" in the current working directory (default):
falcoctl registry pull localhost:5000/myplugin:latest --platform linux/arm64
Example - Pull artifact "myplugin" for platform "linux/arm64" in "myDir" directory:
falcoctl registry pull localhost:5000/myplugin:latest --platform linux/arm64 --dest-dir=./myDir
Example - Pull artifact "myrulesfile":
falcoctl registry pull localhost:5000/myrulesfile:latest
`
//nolint:unused // false positive
var pullAssertFailedBehavior = func(usage, specificError string) {
It("check that fails and the usage is not printed", func() {
Expect(err).To(HaveOccurred())
Expect(output).ShouldNot(gbytes.Say(regexp.QuoteMeta(usage)))
Expect(output).Should(gbytes.Say(regexp.QuoteMeta(specificError)))
})
}
//nolint:unused // false positive
var registryPullTests = Describe("pull", func() {
var (
pusher *ocipusher.Pusher
ref string
config ocipusher.Option
)
const (
// Used as flags for all the test cases.
registryCmd = "registry"
pullCmd = "pull"
dep1 = "myplugin:1.2.3"
dep2 = "myplugin1:1.2.3|otherplugin:3.2.1"
req = "engine_version:15"
anSource = "myrepo.com/rules.git"
artifact = "generic-repo"
repo = "/" + artifact
tag = "tag"
repoAndTag = repo + ":" + tag
)
// Each test gets its own root command and runs it.
// The err variable is asserted by each test.
JustBeforeEach(func() {
rootCmd = cmd.New(ctx, opt)
err = executeRoot(args)
})
JustAfterEach(func() {
Expect(output.Clear()).ShouldNot(HaveOccurred())
})
Context("help message", func() {
BeforeEach(func() {
args = []string{registryCmd, pullCmd, "--help"}
})
It("should match the saved one", func() {
Expect(output).Should(gbytes.Say(regexp.QuoteMeta(registryPullHelp)))
})
})
// Here we are testing all the failure cases using both the rulesfile and plugin artifact types.
// The common logic for the artifacts is tested once using a rulesfile artifact, no need to repeat
// the same test using a plugin artifact.
Context("failure", func() {
var (
tracker out.Tracker
options []ocipusher.Option
filePathsAndPlatforms ocipusher.Option
destDir string
)
const (
plainHTTP = true
testPluginPlatform1 = "linux/amd64"
)
When("without artifact", func() {
BeforeEach(func() {
args = []string{registryCmd, pullCmd}
})
pullAssertFailedBehavior(registryPullUsage, "ERROR accepts 1 arg(s), received 0")
})
When("unreachable registry", func() {
BeforeEach(func() {
configDir := GinkgoT().TempDir()
configFile := filepath.Join(configDir, ".config")
_, err := os.Create(configFile)
Expect(err).To(BeNil())
args = []string{registryCmd, pullCmd, "noregistry/testrules", "--plain-http", "--config", configFile}
})
pullAssertFailedBehavior(registryPullUsage, "ERROR unable to connect to remote registry")
})
When("invalid repository", func() {
newReg := registry + "/wrong:latest"
BeforeEach(func() {
configDir := GinkgoT().TempDir()
configFile := filepath.Join(configDir, ".config")
_, err := os.Create(configFile)
Expect(err).To(BeNil())
args = []string{registryCmd, pullCmd, newReg, "--plain-http", "--config", configFile}
})
pullAssertFailedBehavior(registryPullUsage, fmt.Sprintf("ERROR %s: not found", newReg))
})
When("unwritable --dest-dir", func() {
BeforeEach(func() {
configDir := GinkgoT().TempDir()
configFile := filepath.Join(configDir, ".config")
_, err := os.Create(configFile)
Expect(err).To(BeNil())
destDir = GinkgoT().TempDir()
err = os.Chmod(destDir, 0o555)
Expect(err).To(BeNil())
pusher = ocipusher.NewPusher(authn.NewClient(authn.WithCredentials(&auth.EmptyCredential)), plainHTTP, tracker)
ref = registry + repoAndTag
config = ocipusher.WithArtifactConfig(oci.ArtifactConfig{})
filePathsAndPlatforms = ocipusher.WithFilepathsAndPlatforms([]string{plugintgz}, []string{testPluginPlatform1})
options = []ocipusher.Option{filePathsAndPlatforms, config}
result, err := pusher.Push(ctx, oci.Plugin, ref, options...)
Expect(err).To(BeNil())
Expect(result).ToNot(BeNil())
args = []string{registryCmd, pullCmd, ref, "--plain-http",
"--platform", testPluginPlatform1, "--dest-dir", destDir,
"--config", configFile,
}
})
It("check that fails and the usage is not printed", func() {
tmp := strings.Split(repoAndTag, "/")
artNameAndTag := tmp[len(tmp)-1]
tmp = strings.Split(artNameAndTag, ":")
artName := tmp[0]
tag := tmp[1]
expectedError := fmt.Sprintf(
"ERROR unable to pull artifact generic-repo with %s tag from repo %s: failed to create file",
tag, artName)
Expect(err).To(HaveOccurred())
Expect(output).ShouldNot(gbytes.Say(regexp.QuoteMeta(registryPullUsage)))
Expect(output).Should(gbytes.Say(regexp.QuoteMeta(expectedError)))
})
})
When("--dest-dir not present (and parent not writable)", func() {
BeforeEach(func() {
configDir := GinkgoT().TempDir()
configFile := filepath.Join(configDir, ".config")
_, err := os.Create(configFile)
Expect(err).To(BeNil())
baseDir := GinkgoT().TempDir()
err = os.Chmod(baseDir, 0o555)
Expect(err).To(BeNil())
destDir = baseDir + "/dest"
pusher = ocipusher.NewPusher(authn.NewClient(authn.WithCredentials(&auth.EmptyCredential)), plainHTTP, tracker)
ref = registry + repoAndTag
config = ocipusher.WithArtifactConfig(oci.ArtifactConfig{})
filePathsAndPlatforms = ocipusher.WithFilepathsAndPlatforms([]string{plugintgz}, []string{testPluginPlatform1})
options = []ocipusher.Option{filePathsAndPlatforms, config}
result, err := pusher.Push(ctx, oci.Plugin, ref, options...)
Expect(err).To(BeNil())
Expect(result).ToNot(BeNil())
args = []string{registryCmd, pullCmd, ref, "--plain-http",
"--platform", testPluginPlatform1, "--dest-dir", destDir,
"--config", configFile,
}
})
It("check that fails and the usage is not printed", func() {
expectedError := fmt.Sprintf("ERROR unable to pull artifact %s with tag %s from repo %s: failed to ensure directories of the target path: "+
"mkdir %s: permission denied", artifact, tag, artifact, destDir)
Expect(err).To(HaveOccurred())
Expect(output).ShouldNot(gbytes.Say(regexp.QuoteMeta(registryPullUsage)))
Expect(output).Should(gbytes.Say(regexp.QuoteMeta(expectedError)))
})
})
When("wrong digest format", func() {
wrongDigest := "sha256:06f961b802bc46ee168555f066d28f4f0e9afdf3f88174c1ee6f9de004fc30a0"
BeforeEach(func() {
configDir := GinkgoT().TempDir()
configFile := filepath.Join(configDir, ".config")
_, err := os.Create(configFile)
Expect(err).To(BeNil())
pusher = ocipusher.NewPusher(authn.NewClient(authn.WithCredentials(&auth.EmptyCredential)), plainHTTP, tracker)
ref = registry + repoAndTag
config = ocipusher.WithArtifactConfig(oci.ArtifactConfig{})
filePathsAndPlatforms = ocipusher.WithFilepathsAndPlatforms([]string{plugintgz}, []string{testPluginPlatform1})
options = []ocipusher.Option{filePathsAndPlatforms, config}
result, err := pusher.Push(ctx, oci.Plugin, ref, options...)
Expect(err).To(BeNil())
Expect(result).ToNot(BeNil())
ref = registry + repoAndTag + "@" + wrongDigest
args = []string{registryCmd, pullCmd, ref, "--plain-http",
"--platform", testPluginPlatform1, "--config", configFile}
})
It("check that fails and the usage is not printed", func() {
expectedError := fmt.Sprintf("ERROR %s: not found", registry+repo+"@"+wrongDigest)
Expect(err).To(HaveOccurred())
Expect(output).ShouldNot(gbytes.Say(regexp.QuoteMeta(registryPullUsage)))
Expect(output).Should(gbytes.Say(regexp.QuoteMeta(expectedError)))
})
})
When("missing repository", func() {
BeforeEach(func() {
configDir := GinkgoT().TempDir()
configFile := filepath.Join(configDir, ".config")
_, err := os.Create(configFile)
Expect(err).To(BeNil())
ref = repoAndTag
args = []string{registryCmd, pullCmd, ref, "--plain-http", "--config", configFile}
})
It("check that fails and the usage is not printed", func() {
expectedError := fmt.Sprintf("ERROR cannot extract registry name from ref %q", ref)
Expect(err).To(HaveOccurred())
Expect(output).ShouldNot(gbytes.Say(regexp.QuoteMeta(registryPullUsage)))
Expect(output).Should(gbytes.Say(regexp.QuoteMeta(expectedError)))
})
})
When("invalid repository", func() {
newReg := registry + "/wrong@something"
BeforeEach(func() {
configDir := GinkgoT().TempDir()
configFile := filepath.Join(configDir, ".config")
_, err := os.Create(configFile)
Expect(err).To(BeNil())
args = []string{registryCmd, pullCmd, newReg, "--plain-http", "--config", configFile}
})
pullAssertFailedBehavior(registryPullUsage, fmt.Sprintf("ERROR unable to create new repository with ref %s: "+
"invalid reference: invalid digest %q: invalid checksum digest format\n", newReg, "something"))
})
When("invalid platform", func() {
BeforeEach(func() {
configDir := GinkgoT().TempDir()
configFile := filepath.Join(configDir, ".config")
_, err := os.Create(configFile)
Expect(err).To(BeNil())
pusher = ocipusher.NewPusher(authn.NewClient(authn.WithCredentials(&auth.EmptyCredential)), plainHTTP, tracker)
ref = registry + repoAndTag
config = ocipusher.WithArtifactConfig(oci.ArtifactConfig{})
filePathsAndPlatforms = ocipusher.WithFilepathsAndPlatforms([]string{plugintgz}, []string{testPluginPlatform1})
options = []ocipusher.Option{filePathsAndPlatforms, config}
result, err := pusher.Push(ctx, oci.Plugin, ref, options...)
Expect(err).To(BeNil())
Expect(result).ToNot(BeNil())
ref = registry + repoAndTag
args = []string{registryCmd, pullCmd, ref, "--plain-http",
"--platform", "linux/unknown", "--config", configFile}
})
pullAssertFailedBehavior(registryPullUsage, "not found: no matching manifest was found in the manifest list")
})
})
})