-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow regex matching of artifacts with artifact ingester (#1716)
* Allow regex matching of artifacts with artifact ingester This changes the ingester to also allow a `tag_regex` field as part of its configuration. The idea is that folks would be able to write a profile with a configuration that matches all release-related tags for their containers. Such a configuration would look as follows: ```yaml artifact: - type: artifact_signature params: tag_regex: "v*" def: is_signed: true is_verified: true is_bundle_verified: true ``` * Handle empty tags in configuration * Add more incoming tags validations
- Loading branch information
Showing
4 changed files
with
283 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2023 Stacklok, Inc. | ||
// | ||
// 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 rule provides the CLI subcommand for managing rules | ||
|
||
package artifact | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"k8s.io/apimachinery/pkg/util/sets" | ||
) | ||
|
||
func buildTagMatcher(tags []string, tagRegex string) (tagMatcher, error) { | ||
if len(tags) > 0 && tagRegex != "" { | ||
return nil, fmt.Errorf("cannot specify both tags and tag_regex") | ||
} | ||
|
||
// tags specified, build a list matcher | ||
if len(tags) > 0 { | ||
stags := sets.New(tags...) | ||
if stags.HasAny("") { | ||
return nil, fmt.Errorf("cannot specify empty tag") | ||
} | ||
return &tagListMatcher{tags: tags}, nil | ||
} | ||
|
||
// no tags specified, but a regex was, compile it | ||
if tagRegex != "" { | ||
re, err := regexp.Compile(tagRegex) | ||
if err != nil { | ||
return nil, fmt.Errorf("error compiling tag regex: %w", err) | ||
} | ||
return &tagRegexMatcher{re: re}, nil | ||
} | ||
|
||
// no tags specified, match all | ||
return &tagAllMatcher{}, nil | ||
} | ||
|
||
type tagMatcher interface { | ||
MatchTag(tags ...string) bool | ||
} | ||
|
||
type tagRegexMatcher struct { | ||
re *regexp.Regexp | ||
} | ||
|
||
func (m *tagRegexMatcher) MatchTag(tags ...string) bool { | ||
for _, tag := range tags { | ||
if m.re.MatchString(tag) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
type tagListMatcher struct { | ||
tags []string | ||
} | ||
|
||
func (m *tagListMatcher) MatchTag(tags ...string) bool { | ||
haveTags := sets.New(tags...) | ||
return haveTags.HasAll(m.tags...) | ||
} | ||
|
||
type tagAllMatcher struct{} | ||
|
||
func (*tagAllMatcher) MatchTag(_ ...string) bool { | ||
return true | ||
} |