Skip to content

Commit

Permalink
fix(pkg/oci): trim spaces when handling required_engine_version
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Susini <[email protected]>
  • Loading branch information
loresuso authored and poiana committed Feb 12, 2024
1 parent 195752b commit 1212700
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
3 changes: 3 additions & 0 deletions build/registry/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.15.4 // indirect
github.com/aws/smithy-go v1.14.2 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v24.0.5+incompatible // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.0 // indirect
Expand All @@ -57,6 +58,7 @@ require (
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/oras-project/oras-credentials-go v0.3.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pterm/pterm v0.12.67 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
Expand All @@ -66,6 +68,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
Expand Down
10 changes: 6 additions & 4 deletions build/registry/pkg/oci/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/blang/semver"
Expand Down Expand Up @@ -67,15 +68,16 @@ func rulesfileRequirement(filePath string) (*oci.ArtifactRequirement, error) {
// In case the requirement was expressed as a numeric value,
// we convert it to semver and treat it as minor version.
tokens := strings.Split(fileScanner.Text(), ":")
reqVer, err := semver.Parse(tokens[1])
version := strings.TrimSpace(tokens[1])
reqVer, err := semver.Parse(version)
if err != nil {
reqVer, err = semver.ParseTolerant(tokens[1])
minor, err := strconv.ParseUint(version, 10, 64)
if err != nil {
return nil, fmt.Errorf("unable to parse requirement %q: expected a numeric value or a valid semver string", tokens[1])
return nil, fmt.Errorf("unable to parse requirement %q: expected a numeric value or a valid semver string", version)
}
reqVer = semver.Version{
Major: 0,
Minor: reqVer.Major,
Minor: minor,
Patch: 0,
}
}
Expand Down
39 changes: 39 additions & 0 deletions build/registry/pkg/oci/requirements_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright (C) 2024 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 oci

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRulesfileRequirement(t *testing.T) {
req, err := rulesfileRequirement("testdata/rules-failed-req.yaml")
assert.Error(t, err)

req, err = rulesfileRequirement("testdata/rules-numeric-req.yaml")
assert.NoError(t, err)
assert.Equal(t, "0.15.0", req.Version)
assert.Equal(t, "engine_version_semver", req.Name)

req, err = rulesfileRequirement("testdata/rules-semver-req.yaml")
assert.NoError(t, err)
assert.Equal(t, "0.31.0", req.Version)
assert.Equal(t, "engine_version_semver", req.Name)
}
1 change: 1 addition & 0 deletions build/registry/pkg/oci/testdata/rules-failed-req.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- required_engine_version: test
1 change: 1 addition & 0 deletions build/registry/pkg/oci/testdata/rules-numeric-req.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- required_engine_version: 15
1 change: 1 addition & 0 deletions build/registry/pkg/oci/testdata/rules-semver-req.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- required_engine_version: 0.31.0

0 comments on commit 1212700

Please sign in to comment.