-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slsa charts bypass #153
Merged
Merged
Slsa charts bypass #153
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb90473
refactoring constant paths
nicholasSUSE 07cc62d
adding path to new file: slsa.yaml
nicholasSUSE ca33f3b
small refactoring in GenerateConfigFile adding removeSlsaImages funct…
nicholasSUSE 92075b8
implementing the removal of already signed slsa images from regsync job
nicholasSUSE f3404f0
unit-testing
nicholasSUSE c31aa1d
fixes
nicholasSUSE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,10 +11,14 @@ import ( | |||||
"sort" | ||||||
"strings" | ||||||
|
||||||
"github.com/rancher/charts-build-scripts/pkg/path" | ||||||
"golang.org/x/exp/slices" | ||||||
"gopkg.in/yaml.v2" | ||||||
) | ||||||
|
||||||
// ReadSlsaYamlFunc is a function type that reads the slsa.yaml file and returns a list of images. | ||||||
type ReadSlsaYamlFunc func() ([]string, error) | ||||||
|
||||||
// chartsToIgnoreTags and systemChartsToIgnoreTags defines the charts and system charts in which a specified | ||||||
// image tag should be ignored. | ||||||
var chartsToIgnoreTags = map[string]string{ | ||||||
|
@@ -26,18 +30,17 @@ var chartsToIgnoreTags = map[string]string{ | |||||
func GenerateConfigFile() error { | ||||||
imageTagMap := make(map[string][]string) | ||||||
|
||||||
err := walkAssetsFolder(imageTagMap) | ||||||
if err != nil { | ||||||
if err := walkAssetsFolder(imageTagMap); err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
// Create the regsync config file | ||||||
err = createRegSyncConfigFile(imageTagMap) | ||||||
if err != nil { | ||||||
// Remove the images that are already signed from the imageTagMap | ||||||
if err := removeSlsaImages(imageTagMap, readSlsaYaml); err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
return nil | ||||||
// Create the regsync config file | ||||||
return createRegSyncConfigFile(imageTagMap) | ||||||
} | ||||||
|
||||||
// walkAssetsFolder walks over the assets folder, untars files, stores the values.yaml content | ||||||
|
@@ -217,3 +220,44 @@ func walkMap(inputMap interface{}, callback func(map[interface{}]interface{})) { | |||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
// removeSlsaImages removes the images that are already signed from the imageTagMap. | ||||||
func removeSlsaImages(imageTagMap map[string][]string, readSlsaYaml ReadSlsaYamlFunc) error { | ||||||
// Get the list of images that should not be synced with the registry. | ||||||
// These images are defined in the slsa.yaml file. | ||||||
// We will remove these images from the imageTagMap. | ||||||
slsaImgs, err := readSlsaYaml() | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
// The images will not be synced because they are already: | ||||||
// - Signed | ||||||
// - Synced with the registry | ||||||
if slsaImgs != nil { | ||||||
for _, img := range slsaImgs { | ||||||
delete(imageTagMap, img) | ||||||
} | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
func readSlsaYaml() ([]string, error) { | ||||||
var slsaImgs []string | ||||||
|
||||||
file, err := os.Open(path.SlsaYamlFile) | ||||||
if err != nil { | ||||||
return nil, nil // backward version compatibility | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be an error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
defer file.Close() | ||||||
|
||||||
decoder := yaml.NewDecoder(file) | ||||||
if err := decoder.Decode(&slsaImgs); err != nil { | ||||||
if err == io.EOF { | ||||||
return slsaImgs, nil // Handle EOF error gracefully | ||||||
} | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
return slsaImgs, nil | ||||||
} |
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,118 @@ | ||
package regsync | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_SlsaYaml(t *testing.T) { | ||
|
||
type input struct { | ||
imageTagMap map[string][]string | ||
readSlsaYamlMock func() ([]string, error) | ||
} | ||
|
||
type expected struct { | ||
err error | ||
imageTagMap map[string][]string | ||
} | ||
|
||
type test struct { | ||
name string | ||
input input | ||
expected expected | ||
} | ||
|
||
tests := []test{ | ||
{ | ||
name: "#1", | ||
input: input{ | ||
imageTagMap: map[string][]string{ | ||
"image1": {"tag1"}, | ||
"image2": {"tag2"}, | ||
"image3": {"tag3"}, | ||
}, | ||
readSlsaYamlMock: func() ([]string, error) { | ||
return []string{"image1"}, nil | ||
}, | ||
}, | ||
expected: expected{ | ||
err: nil, | ||
imageTagMap: map[string][]string{ | ||
"image2": {"tag2"}, | ||
"image3": {"tag3"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "#2", | ||
input: input{ | ||
imageTagMap: map[string][]string{ | ||
"image1": {"tag1", "tag2"}, | ||
"image2": {"tag2"}, | ||
"image3": {"tag3", "tag4"}, | ||
}, | ||
readSlsaYamlMock: func() ([]string, error) { | ||
return []string{"image1", "image2"}, nil | ||
}, | ||
}, | ||
expected: expected{ | ||
err: nil, | ||
imageTagMap: map[string][]string{ | ||
"image3": {"tag3", "tag4"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "#3", | ||
input: input{ | ||
imageTagMap: map[string][]string{ | ||
"image1": {"tag1", "tag2"}, | ||
}, | ||
readSlsaYamlMock: func() ([]string, error) { | ||
return []string{"image1", "image2"}, nil | ||
}, | ||
}, | ||
expected: expected{ | ||
err: nil, | ||
imageTagMap: map[string][]string{}, | ||
}, | ||
}, | ||
{ | ||
name: "#4", | ||
input: input{ | ||
imageTagMap: map[string][]string{}, | ||
readSlsaYamlMock: func() ([]string, error) { | ||
return []string{"image1", "image2"}, nil | ||
}, | ||
}, | ||
expected: expected{ | ||
err: nil, | ||
imageTagMap: map[string][]string{}, | ||
}, | ||
}, | ||
{ | ||
name: "#5", | ||
input: input{ | ||
imageTagMap: map[string][]string{}, | ||
readSlsaYamlMock: func() ([]string, error) { | ||
return []string{}, nil | ||
}, | ||
}, | ||
expected: expected{ | ||
err: nil, | ||
imageTagMap: map[string][]string{}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := removeSlsaImages(tt.input.imageTagMap, tt.input.readSlsaYamlMock) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.expected.imageTagMap, tt.input.imageTagMap) | ||
|
||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.