Skip to content

Commit

Permalink
chore: refactor and purify the OCI library within Zarf. (#2235)
Browse files Browse the repository at this point in the history
## Description

Move OCI lib out, so it can be more easily used with other projects and
contributed to by multiple teams.

Fixes: #2252 #2288 

## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [X] Other (security config, docs update, etc)

## Checklist before merging

- [ ] Test, docs, adr added or updated as needed
- [ ] [Contributor Guide
Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow)
followed

---------

Co-authored-by: razzle <harry@razzle.cloud>
Co-authored-by: Lucas Rodriguez <lucas.rodriguez9616@gmail.com>
Co-authored-by: Lucas Rodriguez <lucas.rodriguez@defenseunicorns.com>
Co-authored-by: Wayne Starr <Racer159@users.noreply.github.com>
  • Loading branch information
5 people committed Mar 4, 2024
1 parent c46123c commit bc7987c
Show file tree
Hide file tree
Showing 57 changed files with 927 additions and 813 deletions.
9 changes: 5 additions & 4 deletions src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/packager/sources"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/pkg/zoci"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -91,7 +92,7 @@ func findInitPackage(initPackageName string) (string, error) {

// Create the cache directory if it doesn't exist
if utils.InvalidPath(config.GetAbsCachePath()) {
if err := utils.CreateDirectory(config.GetAbsCachePath(), 0755); err != nil {
if err := utils.CreateDirectory(config.GetAbsCachePath(), helpers.ReadExecuteAllWriteUser); err != nil {
message.Fatalf(err, lang.CmdInitErrUnableCreateCache, config.GetAbsCachePath())
}
}
Expand Down Expand Up @@ -119,7 +120,7 @@ func downloadInitPackage(cacheDirectory string) (string, error) {
}

var confirmDownload bool
url := oci.GetInitPackageURL(config.CLIVersion)
url := zoci.GetInitPackageURL(config.CLIVersion)

// Give the user the choice to download the init-package and note that this does require an internet connection
message.Question(fmt.Sprintf(lang.CmdInitPullAsk, url))
Expand All @@ -138,11 +139,11 @@ func downloadInitPackage(cacheDirectory string) (string, error) {

// If the user wants to download the init-package, download it
if confirmDownload {
remote, err := oci.NewOrasRemote(url, oci.PlatformForArch(config.GetArch()))
remote, err := zoci.NewRemote(url, oci.PlatformForArch(config.GetArch()))
if err != nil {
return "", err
}
source := sources.OCISource{OrasRemote: remote}
source := &sources.OCISource{Remote: remote}
return source.Collect(cacheDirectory)
}
// Otherwise, exit and tell the user to manually download the init-package
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/tools/helm/repo_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"strings"
"time"

"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/gofrs/flock"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -216,7 +217,7 @@ func (o *repoAddOptions) run(out io.Writer) error {

f.Update(&c)

if err := f.WriteFile(o.repoFile, 0600); err != nil {
if err := f.WriteFile(o.repoFile, helpers.ReadWriteUser); err != nil {
return err
}
fmt.Fprintf(out, "%q has been added to your repositories\n", o.name)
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/tools/helm/repo_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"
"path/filepath"

"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/pkg/errors"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -100,7 +101,7 @@ func index(dir, url, mergeTo string) error {
var i2 *repo.IndexFile
if _, err := os.Stat(mergeTo); os.IsNotExist(err) {
i2 = repo.NewIndexFile()
i2.WriteFile(mergeTo, 0644)
i2.WriteFile(mergeTo, helpers.ReadAllWriteUser)
} else {
i2, err = repo.LoadIndexFile(mergeTo)
if err != nil {
Expand All @@ -110,5 +111,5 @@ func index(dir, url, mergeTo string) error {
i.Merge(i2)
}
i.SortEntries()
return i.WriteFile(out, 0644)
return i.WriteFile(out, helpers.ReadAllWriteUser)
}
3 changes: 2 additions & 1 deletion src/cmd/tools/helm/repo_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"os"
"path/filepath"

"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/pkg/errors"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -72,7 +73,7 @@ func (o *repoRemoveOptions) run(out io.Writer) error {
if !r.Remove(name) {
return errors.Errorf("no repo named %q found", name)
}
if err := r.WriteFile(o.repoFile, 0600); err != nil {
if err := r.WriteFile(o.repoFile, helpers.ReadWriteUser); err != nil {
return err
}

Expand Down
18 changes: 10 additions & 8 deletions src/cmd/tools/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/oci"
"github.com/defenseunicorns/zarf/src/pkg/packager/sources"
"github.com/defenseunicorns/zarf/src/pkg/pki"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/pkg/zoci"
"github.com/defenseunicorns/zarf/src/types"
"github.com/sigstore/cosign/v2/pkg/cosign"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -181,14 +183,14 @@ var downloadInitCmd = &cobra.Command{
Use: "download-init",
Short: lang.CmdToolsDownloadInitShort,
Run: func(_ *cobra.Command, _ []string) {
url := oci.GetInitPackageURL(config.CLIVersion)
url := zoci.GetInitPackageURL(config.CLIVersion)

remote, err := oci.NewOrasRemote(url, oci.PlatformForArch(config.GetArch()))
remote, err := zoci.NewRemote(url, oci.PlatformForArch(config.GetArch()))
if err != nil {
message.Fatalf(err, lang.CmdToolsDownloadInitErr, err.Error())
}

source := &sources.OCISource{OrasRemote: remote}
source := &sources.OCISource{Remote: remote}

_, err = source.Collect(outputDirectory)
if err != nil {
Expand All @@ -204,13 +206,13 @@ var generatePKICmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
pki := pki.GeneratePKI(args[0], subAltNames...)
if err := os.WriteFile("tls.ca", pki.CA, 0644); err != nil {
if err := os.WriteFile("tls.ca", pki.CA, helpers.ReadAllWriteUser); err != nil {
message.Fatalf(err, lang.ErrWritingFile, "tls.ca", err.Error())
}
if err := os.WriteFile("tls.crt", pki.Cert, 0644); err != nil {
if err := os.WriteFile("tls.crt", pki.Cert, helpers.ReadAllWriteUser); err != nil {
message.Fatalf(err, lang.ErrWritingFile, "tls.crt", err.Error())
}
if err := os.WriteFile("tls.key", pki.Key, 0600); err != nil {
if err := os.WriteFile("tls.key", pki.Key, helpers.ReadWriteUser); err != nil {
message.Fatalf(err, lang.ErrWritingFile, "tls.key", err.Error())
}
message.Successf(lang.CmdToolsGenPkiSuccess, args[0])
Expand Down Expand Up @@ -278,10 +280,10 @@ var generateKeyCmd = &cobra.Command{
}

// Write the key file contents to disk
if err := os.WriteFile(prvKeyFileName, keyBytes.PrivateBytes, 0600); err != nil {
if err := os.WriteFile(prvKeyFileName, keyBytes.PrivateBytes, helpers.ReadWriteUser); err != nil {
message.Fatalf(err, lang.ErrWritingFile, prvKeyFileName, err.Error())
}
if err := os.WriteFile(pubKeyFileName, keyBytes.PublicBytes, 0644); err != nil {
if err := os.WriteFile(pubKeyFileName, keyBytes.PublicBytes, helpers.ReadAllWriteUser); err != nil {
message.Fatalf(err, lang.ErrWritingFile, pubKeyFileName, err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion src/extensions/bigbang/bigbang.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func addBigBangManifests(YOLO bool, manifestDir string, cfg *extensions.BigBang)
return err
}

if err := utils.WriteFile(path, out); err != nil {
if err := os.WriteFile(path, out, helpers.ReadWriteUser); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion src/extensions/bigbang/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func getFlux(baseDir string, cfg *extensions.BigBang) (manifest types.ZarfManife
fluxKustomization.Patches = append(fluxKustomization.Patches, krustytypes.Patch{Path: absFluxPatchPath})
}

if err := utils.WriteYaml(kustomizePath, fluxKustomization, 0600); err != nil {
if err := utils.WriteYaml(kustomizePath, fluxKustomization, helpers.ReadWriteUser); err != nil {
return manifest, images, fmt.Errorf("unable to write kustomization: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions src/internal/packager/helm/post-render.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/defenseunicorns/zarf/src/internal/packager/template"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/releaseutil"
Expand Down Expand Up @@ -61,8 +62,7 @@ func (r *renderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) {
}
path := filepath.Join(tempDir, "chart.yaml")

// Write the context to a file for processing
if err := utils.WriteFile(path, renderedManifests.Bytes()); err != nil {
if err := os.WriteFile(path, renderedManifests.Bytes(), helpers.ReadWriteUser); err != nil {
return nil, fmt.Errorf("unable to write the post-render file for the helm chart")
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal/packager/helm/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (h *Helm) DownloadPublishedChart(cosignKeyPath string) error {

// Download the file into a temp directory since we don't control what name helm creates here
temp := filepath.Join(h.chartPath, "temp")
if err = utils.CreateDirectory(temp, 0700); err != nil {
if err = utils.CreateDirectory(temp, helpers.ReadWriteExecuteUser); err != nil {
return fmt.Errorf("unable to create helm chart temp directory: %w", err)
}
defer os.RemoveAll(temp)
Expand Down
29 changes: 13 additions & 16 deletions src/internal/packager/images/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"os"
"path/filepath"
"strings"
"sync"

"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/transform"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/name"
Expand Down Expand Up @@ -67,7 +67,7 @@ func (i *ImageConfig) PullAll() ([]ImgInfo, error) {
logs.Warn.SetOutput(&message.DebugWriter{})
logs.Progress.SetOutput(&message.DebugWriter{})

metadataImageConcurrency := utils.NewConcurrencyTools[ImgInfo, error](len(i.ImageList))
metadataImageConcurrency := helpers.NewConcurrencyTools[ImgInfo, error](len(i.ImageList))

defer metadataImageConcurrency.Cancel()

Expand Down Expand Up @@ -123,7 +123,7 @@ func (i *ImageConfig) PullAll() ([]ImgInfo, error) {
}

// Create the ImagePath directory
if err := utils.CreateDirectory(i.ImagesPath, 0755); err != nil {
if err := utils.CreateDirectory(i.ImagesPath, helpers.ReadExecuteAllWriteUser); err != nil {
return nil, fmt.Errorf("failed to create image path %s: %w", i.ImagesPath, err)
}

Expand Down Expand Up @@ -178,16 +178,13 @@ func (i *ImageConfig) PullAll() ([]ImgInfo, error) {
spinner.Success()

// Create a thread to update a progress bar as we save the image files to disk
doneSaving := make(chan int)
errorSaving := make(chan int)
var progressBarWaitGroup sync.WaitGroup
progressBarWaitGroup.Add(1)
doneSaving := make(chan error)
updateText := fmt.Sprintf("Pulling %d images", imageCount)
go utils.RenderProgressBarForLocalDirWrite(i.ImagesPath, totalBytes, &progressBarWaitGroup, doneSaving, errorSaving, updateText, updateText)
go utils.RenderProgressBarForLocalDirWrite(i.ImagesPath, totalBytes, doneSaving, updateText, updateText)

// Spawn a goroutine for each layer to write it to disk using crane

layerWritingConcurrency := utils.NewConcurrencyTools[bool, error](len(processedLayers))
layerWritingConcurrency := helpers.NewConcurrencyTools[bool, error](len(processedLayers))

defer layerWritingConcurrency.Cancel()

Expand Down Expand Up @@ -318,8 +315,8 @@ func (i *ImageConfig) PullAll() ([]ImgInfo, error) {

onLayerWritingError := func(err error) error {
// Send a signal to the progress bar that we're done and wait for the thread to finish
errorSaving <- 1
progressBarWaitGroup.Wait()
doneSaving <- err
<-doneSaving
message.WarnErr(err, "Failed to write image layers, trying again up to 3 times...")
if strings.HasPrefix(err.Error(), "expected blob size") {
message.Warnf("Potential image cache corruption: %s - try clearing cache with \"zarf tools clear-cache\"", err.Error())
Expand All @@ -331,7 +328,7 @@ func (i *ImageConfig) PullAll() ([]ImgInfo, error) {
return nil, err
}

imageSavingConcurrency := utils.NewConcurrencyTools[digestInfo, error](len(refInfoToImage))
imageSavingConcurrency := helpers.NewConcurrencyTools[digestInfo, error](len(refInfoToImage))

defer imageSavingConcurrency.Cancel()

Expand Down Expand Up @@ -382,8 +379,8 @@ func (i *ImageConfig) PullAll() ([]ImgInfo, error) {

onImageSavingError := func(err error) error {
// Send a signal to the progress bar that we're done and wait for the thread to finish
errorSaving <- 1
progressBarWaitGroup.Wait()
doneSaving <- err
<-doneSaving
message.WarnErr(err, "Failed to write image config or manifest, trying again up to 3 times...")
return err
}
Expand Down Expand Up @@ -418,8 +415,8 @@ func (i *ImageConfig) PullAll() ([]ImgInfo, error) {
}

// Send a signal to the progress bar that we're done and wait for the thread to finish
doneSaving <- 1
progressBarWaitGroup.Wait()
doneSaving <- nil
<-doneSaving

return imgInfoList, nil
}
Expand Down
3 changes: 2 additions & 1 deletion src/internal/packager/images/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/transform"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/logs"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (i *ImageConfig) PushToZarfRegistry() error {
httpTransport.ResponseHeaderTimeout = 10 * time.Second
progressBar := message.NewProgressBar(totalSize, fmt.Sprintf("Pushing %d images to the zarf registry", len(i.ImageList)))
defer progressBar.Stop()
craneTransport := utils.NewTransport(httpTransport, progressBar)
craneTransport := helpers.NewTransport(httpTransport, progressBar)

pushOptions := config.GetCraneOptions(i.Insecure, i.Architectures...)
pushOptions = append(pushOptions, config.GetCraneAuthOption(i.RegInfo.PushUsername, i.RegInfo.PushPassword))
Expand Down
5 changes: 3 additions & 2 deletions src/internal/packager/kustomize/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package kustomize

import (
"fmt"
"os"

"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"sigs.k8s.io/kustomize/api/krusty"
krustytypes "sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/filesys"
Expand Down Expand Up @@ -39,5 +40,5 @@ func Build(path string, destination string, kustomizeAllowAnyDirectory bool) err
return fmt.Errorf("problem converting kustomization to yaml: %w", err)
}

return utils.WriteFile(destination, yaml)
return os.WriteFile(destination, yaml, helpers.ReadWriteUser)
}
5 changes: 3 additions & 2 deletions src/internal/packager/sbom/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/transform"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
v1 "github.com/google/go-containerregistry/pkg/v1"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func Catalog(componentSBOMs map[string]*layout.ComponentSBOM, imageList []transf
defer builder.spinner.Stop()

// Ensure the sbom directory exists
_ = utils.CreateDirectory(builder.outputDir, 0700)
_ = utils.CreateDirectory(builder.outputDir, helpers.ReadWriteExecuteUser)

// Generate a list of images and files for the sbom viewer
json, err := builder.generateJSONList(componentSBOMs, imageList)
Expand Down Expand Up @@ -151,7 +152,7 @@ func (b *Builder) createImageSBOM(img v1.Image, src string) ([]byte, error) {
imageCachePath := filepath.Join(b.cachePath, layout.ImagesDir)

// Ensure the image cache directory exists.
if err := utils.CreateDirectory(imageCachePath, 0700); err != nil {
if err := utils.CreateDirectory(imageCachePath, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}

Expand Down
3 changes: 2 additions & 1 deletion src/internal/packager/sbom/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func OutputSBOMFiles(sourceDir, outputDir, packageName string) (string, error) {
return "", err
}

if err := utils.CreateDirectory(packagePath, 0700); err != nil {
if err := utils.CreateDirectory(packagePath, helpers.ReadWriteExecuteUser); err != nil {
return "", err
}

Expand Down
3 changes: 2 additions & 1 deletion src/pkg/cluster/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
corev1 "k8s.io/api/core/v1"
)
Expand All @@ -28,7 +29,7 @@ func (c *Cluster) HandleDataInjection(wg *sync.WaitGroup, data types.ZarfDataInj
defer wg.Done()

injectionCompletionMarker := filepath.Join(componentPath.DataInjections, config.GetDataInjectionMarker())
if err := utils.WriteFile(injectionCompletionMarker, []byte("🦄")); err != nil {
if err := os.WriteFile(injectionCompletionMarker, []byte("🦄"), helpers.ReadWriteUser); err != nil {
message.WarnErrf(err, "Unable to create the data injection completion marker")
return
}
Expand Down

0 comments on commit bc7987c

Please sign in to comment.