Skip to content

Commit

Permalink
add new boolean arg for 'registry push' --add-floating-tags, it will …
Browse files Browse the repository at this point in the history
…auto add the tags for the major and the minor versions

Signed-off-by: Thomas Labarussias <[email protected]>
  • Loading branch information
Issif authored and poiana committed May 2, 2024
1 parent da9865e commit 27c653f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ $ falcoctl registry push --type=plugin ghcr.io/falcosecurity/plugins/plugin/clou
```
The type denotes the **artifact** type in this case *plugins*. The `ghcr.io/falcosecurity/plugins/plugin/cloudtrail:0.3.0` is the unique reference that points to the **artifact**.
Currently, *falcoctl* supports only two types of artifacts: **plugin** and **rulesfile**. Based on **artifact type** the commands accepts different flags:
* `--add-floating-tags`: add the floating tags for the major and minor versions
* `--annotation-source`: set annotation source for the artifact;
* `--depends-on`: set an artifact dependency (can be specified multiple times). Example: `--depends-on my-plugin:1.2.3`
* `--tag`: additional artifact tag. Can be repeated multiple time
Expand Down
12 changes: 12 additions & 0 deletions cmd/registry/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Example - Push artifact "myplugin.tar.gz" of type "plugin" for multiple platform
Example - Push artifact "myrulesfile.tar.gz" of type "rulesfile":
falcoctl registry push --type rulesfile --version "0.1.2" localhost:5000/myrulesfile:latest myrulesfile.tar.gz
Example - Push artifact "myrulesfile.tar.gz" of type "rulesfile" with floating tags for the major and minor versions (0 and 0.1):
falcoctl registry push --type rulesfile --version "0.1.2" localhost:5000/myrulesfile:latest myrulesfile.tar.gz \
--add-floating-tags
Example - Push artifact "myrulesfile.tar.gz" of type "rulesfile" to an insecure registry:
falcoctl registry push --type rulesfile --version "0.1.2" --plain-http localhost:5000/myrulesfile:latest myrulesfile.tar.gz
Expand Down Expand Up @@ -192,6 +196,14 @@ func (o *pushOptions) runPush(ctx context.Context, args []string) error {
return err
}

if o.AutoFloatingTags {
v, err := semver.Parse(o.Version)
if err != nil {
return fmt.Errorf("expected semver for the flag \"--version\": %w", err)
}
o.Tags = append(o.Tags, o.Version, fmt.Sprintf("%v", v.Major), fmt.Sprintf("%v.%v", v.Major, v.Minor))
}

opts := ocipusher.Options{
ocipusher.WithTags(o.Tags...),
ocipusher.WithAnnotationSource(o.AnnotationSource),
Expand Down
15 changes: 15 additions & 0 deletions cmd/registry/push/push_rulesfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,21 @@ var _ = Describe("pushing rulesfiles", func() {
map[string]string{})
})

When("with add-floating-tags and the required flags", func() {
BeforeEach(func() {
rulesfile = rulesfileyaml
args = []string{registryCmd, pushCmd, fullRepoName, rulesfile, "--config", configFile, "--type", "rulesfile", "--version", version,
"--add-floating-tags", "--plain-http"}
// Set name to the expected one.
artifactNameInConfigLayer = repoName
// The semver tags are expected to be set.
pushedTags = []string{"1.1.1", "1.1", "1"}
})
AssertSuccesBehaviour([]oci.ArtifactDependency{},
[]oci.ArtifactRequirement{},
map[string]string{})
})

When("with full flags and args but in tar.gz format", func() {
BeforeEach(func() {
rulesfile = rulesfiletgz
Expand Down
22 changes: 22 additions & 0 deletions cmd/registry/push/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var registryPushUsage = `Usage:
falcoctl registry push hostname/repo[:tag|@digest] file [flags]
Flags:
--add-floating-tags add the floating tags for the major and minor versions
--annotation-source string set annotation source for the artifact
-d, --depends-on stringArray set an artifact dependency (can be specified multiple times). Example: "--depends-on my-plugin:1.2.3"
-h, --help help for push
Expand Down Expand Up @@ -65,6 +66,10 @@ Example - Push artifact "myplugin.tar.gz" of type "plugin" for multiple platform
Example - Push artifact "myrulesfile.tar.gz" of type "rulesfile":
falcoctl registry push --type rulesfile --version "0.1.2" localhost:5000/myrulesfile:latest myrulesfile.tar.gz
Example - Push artifact "myrulesfile.tar.gz" of type "rulesfile" with floating tags for the major and minor versions (0 and 0.1):
falcoctl registry push --type rulesfile --version "0.1.2" localhost:5000/myrulesfile:latest myrulesfile.tar.gz \
--add-floating-tags
Example - Push artifact "myrulesfile.tar.gz" of type "rulesfile" to an insecure registry:
falcoctl registry push --type rulesfile --version "0.1.2" --plain-http localhost:5000/myrulesfile:latest myrulesfile.tar.gz
Expand All @@ -85,6 +90,7 @@ Usage:
falcoctl registry push hostname/repo[:tag|@digest] file [flags]
Flags:
--add-floating-tags add the floating tags for the major and minor versions
--annotation-source string set annotation source for the artifact
-d, --depends-on stringArray set an artifact dependency (can be specified multiple times). Example: "--depends-on my-plugin:1.2.3"
-h, --help help for push
Expand Down Expand Up @@ -190,6 +196,22 @@ var _ = Describe("push", func() {
"registry \"noregistry\": Get \"http://noregistry/v2/\": dial tcp: lookup noregistry")
})

When("wrong semver for --version flag with --add-floating-tags", func() {
BeforeEach(func() {
args = []string{registryCmd, pushCmd, rulesRepo, rulesfiletgz, "--config", configFile, "--type", "rulesfile",
"--version", "notSemVer", "--add-floating-tags", "--plain-http"}
})
pushAssertFailedBehavior(registryPushUsage, "ERROR expected semver for the flag \"--version\": No Major.Minor.Patch elements found")
})

When("invalid character in semver for --version flag with --add-floating-tags", func() {
BeforeEach(func() {
args = []string{registryCmd, pushCmd, rulesRepo, rulesfiletgz, "--config", configFile, "--type", "rulesfile",
"--version", "1.1.a", "--add-floating-tags", "--plain-http"}
})
pushAssertFailedBehavior(registryPushUsage, "ERROR expected semver for the flag \"--version\": Invalid character(s) found in patch number \"a\"")
})

When("missing repository", func() {
BeforeEach(func() {
args = []string{registryCmd, pushCmd, registry, rulesfiletgz, "--config", configFile, "--type", "rulesfile", "--version", "1.1.1", "--plain-http"}
Expand Down
4 changes: 4 additions & 0 deletions pkg/options/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Artifact struct {
Dependencies []string
Requirements []string
Tags []string
AutoFloatingTags bool
AnnotationSource string
}

Expand Down Expand Up @@ -64,6 +65,9 @@ func (art *Artifact) AddFlags(cmd *cobra.Command) error {
cmd.Flags().StringArrayVarP(&art.Tags, "tag", "t", nil,
"additional artifact tag. Can be repeated multiple times")

cmd.Flags().BoolVar(&art.AutoFloatingTags, "add-floating-tags", false,
"add the floating tags for the major and minor versions")

cmd.Flags().Var(&art.ArtifactType, "type",
`type of artifact to be pushed. Allowed values: "rulesfile", "plugin", "asset"`)
if err := cmd.MarkFlagRequired("type"); err != nil {
Expand Down

0 comments on commit 27c653f

Please sign in to comment.