From d42ce1a423849f452fbdfb908b632fc5dc4d984d Mon Sep 17 00:00:00 2001 From: Pranav Singh Date: Wed, 10 Jan 2024 19:04:18 +0530 Subject: [PATCH 1/6] add validate support Signed-off-by: Pranav Singh --- models/oci/utils.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/models/oci/utils.go b/models/oci/utils.go index 1160ec60..e8ade2ad 100644 --- a/models/oci/utils.go +++ b/models/oci/utils.go @@ -8,6 +8,8 @@ import ( "github.com/fluxcd/pkg/tar" "github.com/google/go-containerregistry/pkg/crane" gcrv1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/google/go-containerregistry/pkg/v1/tarball" + "github.com/google/go-containerregistry/pkg/v1/validate" "github.com/layer5io/meshkit/utils" ) @@ -32,8 +34,19 @@ func SaveOCIArtifact(img gcrv1.Image, artifactPath, name string) error { return nil } -// uncompress the OCI Artifact tarball at the given path -func UnCompressOCIArtifact(img gcrv1.Image, destination string) error { +// uncompress the OCI Artifact tarball at the given destination path +func UnCompressOCIArtifact(source, destination string) error { + img, err := tarball.ImageFromPath(source, nil) + if err != nil { + return ErrGettingImage(err) + } + + opt := []validate.Option{} + + if err := validate.Image(img, opt...); err != nil { + return ErrValidatingImage(err) + } + layers, err := img.Layers() if err != nil { return ErrGettingLayer(err) From 293b8f87bca7e65e492a22efbfe0df85baaebd02 Mon Sep 17 00:00:00 2001 From: Pranav Singh Date: Wed, 10 Jan 2024 19:04:30 +0530 Subject: [PATCH 2/6] meshkit err Signed-off-by: Pranav Singh --- models/oci/error.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/models/oci/error.go b/models/oci/error.go index bafc7578..ffa0c183 100644 --- a/models/oci/error.go +++ b/models/oci/error.go @@ -13,6 +13,8 @@ var ( ErrGettingLayerCode = "11110" ErrCompressingLayerCode = "11111" ErrUnTaringLayerCode = "11112" + ErrGettingImageCode = "11113" + ErrValidatingImageCode = "11114" ) func ErrAppendingLayer(err error) error { @@ -38,3 +40,11 @@ func ErrCompressingLayer(err error) error { func ErrUnTaringLayer(err error) error { return errors.New(ErrUnTaringLayerCode, errors.Alert, []string{"untaring layer failed"}, []string{err.Error()}, []string{"failed to untar the layer"}, []string{"Try using a different layer", "check if image is not malformed"}) } + +func ErrGettingImage(err error) error { + return errors.New(ErrGettingImageCode, errors.Alert, []string{"getting image failed"}, []string{err.Error()}, []string{"failed to get the image"}, []string{"Try using a different image", "check if image is not malformed"}) +} + +func ErrValidatingImage(err error) error { + return errors.New(ErrValidatingImageCode, errors.Alert, []string{"validating image failed"}, []string{err.Error()}, []string{"failed to validate the image"}, []string{"Try using a different image", "check if image is not malformed"}) +} From d2988db7629d677772c718f76fa0665883eb1dff Mon Sep 17 00:00:00 2001 From: Pranav Singh Date: Wed, 10 Jan 2024 19:04:54 +0530 Subject: [PATCH 3/6] add directory walk func Signed-off-by: Pranav Singh --- utils/walker/directory.go | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 utils/walker/directory.go diff --git a/utils/walker/directory.go b/utils/walker/directory.go new file mode 100644 index 00000000..7a0223bd --- /dev/null +++ b/utils/walker/directory.go @@ -0,0 +1,41 @@ +package walker + +import ( + "io" + "os" + "path/filepath" +) + +func WalkLocalDirectory(path string) ([]*File, error) { + var files []*File + err := filepath.WalkDir(path, + func(path string, d os.DirEntry, err error) error { + if err != nil { + return err + } + if !d.IsDir() { + var f *File + file, err := os.OpenFile(path, os.O_RDONLY, 0444) + if err != nil { + return err + } + content, err := io.ReadAll(file) + if err != nil { + return err + } + + f.Name = d.Name() + f.Path = path + f.Content = string(content) + files = append(files, f) + } + return nil + }) + + if err != nil { + return nil, err + } + + return files, nil + +} From ea1de0f237d0d95a0e685c0bc112c058c23360ae Mon Sep 17 00:00:00 2001 From: Pranav Singh Date: Wed, 10 Jan 2024 19:57:56 +0530 Subject: [PATCH 4/6] fix nil err Signed-off-by: Pranav Singh --- utils/walker/directory.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/utils/walker/directory.go b/utils/walker/directory.go index 7a0223bd..6099256a 100644 --- a/utils/walker/directory.go +++ b/utils/walker/directory.go @@ -14,7 +14,6 @@ func WalkLocalDirectory(path string) ([]*File, error) { return err } if !d.IsDir() { - var f *File file, err := os.OpenFile(path, os.O_RDONLY, 0444) if err != nil { return err @@ -24,9 +23,12 @@ func WalkLocalDirectory(path string) ([]*File, error) { return err } - f.Name = d.Name() - f.Path = path - f.Content = string(content) + + f := &File{ + Content: string(content), + Name: d.Name(), + Path: path, + files = append(files, f) } return nil From 98cfb7e50d09ae76f72cefb9d51c7940be16b08d Mon Sep 17 00:00:00 2001 From: Pranav Singh Date: Wed, 10 Jan 2024 19:58:22 +0530 Subject: [PATCH 5/6] update Signed-off-by: Pranav Singh --- utils/walker/directory.go | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/walker/directory.go b/utils/walker/directory.go index 6099256a..e10fbdad 100644 --- a/utils/walker/directory.go +++ b/utils/walker/directory.go @@ -28,6 +28,7 @@ func WalkLocalDirectory(path string) ([]*File, error) { Content: string(content), Name: d.Name(), Path: path, + } files = append(files, f) } From d71589eb5eaa9b38d6907703011eb4b8a88eec20 Mon Sep 17 00:00:00 2001 From: Pranav Singh Date: Thu, 11 Jan 2024 13:57:11 +0530 Subject: [PATCH 6/6] golangci fix Signed-off-by: Pranav Singh --- models/oci/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/oci/utils.go b/models/oci/utils.go index e8ade2ad..7235f4ab 100644 --- a/models/oci/utils.go +++ b/models/oci/utils.go @@ -43,7 +43,7 @@ func UnCompressOCIArtifact(source, destination string) error { opt := []validate.Option{} - if err := validate.Image(img, opt...); err != nil { + if err = validate.Image(img, opt...); err != nil { return ErrValidatingImage(err) }