Skip to content

Commit

Permalink
Merge branch 'main' into remove-e2e-example-dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinAbro321 authored Feb 13, 2025
2 parents 6f55bdf + 8a09e8d commit 2e3ebb0
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 49 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-rust-injector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- name: "Build Rust Binary for x86_64 and arm64"
run: |
cd src/injector
make install-cross
make injector-linux
cd target
mkdir -p ../dist
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ jobs:

# Create the GitHub release notes, upload artifact backups to S3, publish homebrew recipe
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@9ed2f89a662bf1735a48bc8557fd212fa902bebf # v6.1.0
uses: goreleaser/goreleaser-action@90a3faa9d0182683851fbfa97ca1a2cb983bfca3 # v6.2.1
with:
distribution: goreleaser
version: "~> v2"
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ require (
github.com/defenseunicorns/pkg/helpers/v2 v2.0.1
github.com/defenseunicorns/pkg/oci v1.0.2
github.com/derailed/k9s v0.32.7
github.com/distribution/distribution/v3 v3.0.0-rc.2
github.com/distribution/distribution/v3 v3.0.0-rc.3
github.com/distribution/reference v0.6.0
github.com/fairwindsops/pluto/v5 v5.21.1
github.com/fatih/color v1.18.0
github.com/fluxcd/gitkit v0.6.0
github.com/fluxcd/pkg/apis/meta v1.10.0
github.com/fluxcd/source-controller/api v1.4.1
github.com/go-git/go-git/v5 v5.13.2
github.com/goccy/go-yaml v1.15.19
github.com/goccy/go-yaml v1.15.20
github.com/gofrs/flock v0.12.1
github.com/golang-cz/devslog v0.0.11
github.com/google/go-containerregistry v0.20.3
Expand Down Expand Up @@ -311,7 +311,7 @@ require (
github.com/fluxcd/pkg/apis/acl v0.3.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fvbommel/sortorder v1.1.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/gabriel-vasile/mimetype v1.4.8
github.com/gdamore/encoding v1.0.1 // indirect
github.com/github/go-spdx/v2 v2.3.2 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,8 @@ github.com/digitorus/timestamp v0.0.0-20231217203849-220c5c2851b7 h1:lxmTCgmHE1G
github.com/digitorus/timestamp v0.0.0-20231217203849-220c5c2851b7/go.mod h1:GvWntX9qiTlOud0WkQ6ewFm0LPy5JUR1Xo0Ngbd1w6Y=
github.com/dimchansky/utfbom v1.1.1 h1:vV6w1AhK4VMnhBno/TPVCoK9U/LP0PkLCS9tbxHdi/U=
github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/j0mACtrfE=
github.com/distribution/distribution/v3 v3.0.0-rc.2 h1:tTrzntanYMbd20SyvdeR83Ya1l/aBwDcA3NCIpmwemc=
github.com/distribution/distribution/v3 v3.0.0-rc.2/go.mod h1:H2zIRRXS20ylnv2HTuKILAWuANjuA60GB7MLOsQag7Y=
github.com/distribution/distribution/v3 v3.0.0-rc.3 h1:JRJso9IVLoooKX76oWR+DWCCdZlK5m4nRtDWvzB1ITg=
github.com/distribution/distribution/v3 v3.0.0-rc.3/go.mod h1:offoOgrnYs+CFwis8nE0hyzYZqRCZj5EFc5kgfszwiE=
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/cli v27.5.1+incompatible h1:JB9cieUT9YNiMITtIsguaN55PLOHhBSz3LKVc6cqWaY=
Expand Down Expand Up @@ -900,8 +900,8 @@ github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
github.com/goccy/go-json v0.10.4/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/goccy/go-yaml v1.15.19 h1:ivDxLiW6SbmqPZwSAM9Yq+Yr68C9FLbTNyuH3ITizxQ=
github.com/goccy/go-yaml v1.15.19/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/goccy/go-yaml v1.15.20 h1:eQHFLrr1lpLYAxupPD9ThZbGtncPl9nyu3nkAayEZgY=
github.com/goccy/go-yaml v1.15.20/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand Down
2 changes: 1 addition & 1 deletion site/src/content/docs/commands/zarf_connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ zarf connect { REGISTRY | GIT | connect-name } [flags]
### Options

```
--cli-only Disable browser auto-open
-h, --help help for connect
--local-port int (Optional, autogenerated if not provided) Specify the local port to bind to. E.g. local-port=42000.
--name string Specify the resource name. E.g. name=unicorns or name=unicorn-pod-7448499f4d-b5bk6. Ignored if connect-name is supplied.
--namespace string Specify the namespace. E.g. namespace=default. Ignored if connect-name is supplied. (default "zarf")
--open Enable browser auto-open
--remote-port int Specify the remote port of the resource to bind to. E.g. remote-port=8080. Ignored if connect-name is supplied.
--type string Specify the resource type. E.g. type=svc or type=pod. Ignored if connect-name is supplied. (default "svc")
```
Expand Down
15 changes: 8 additions & 7 deletions src/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
)

type connectOptions struct {
cliOnly bool
zt cluster.TunnelInfo
open bool
zt cluster.TunnelInfo
}

func newConnectCommand() *cobra.Command {
Expand All @@ -37,7 +37,7 @@ func newConnectCommand() *cobra.Command {
cmd.Flags().StringVar(&o.zt.ResourceType, "type", cluster.SvcResource, lang.CmdConnectFlagType)
cmd.Flags().IntVar(&o.zt.LocalPort, "local-port", 0, lang.CmdConnectFlagLocalPort)
cmd.Flags().IntVar(&o.zt.RemotePort, "remote-port", 0, lang.CmdConnectFlagRemotePort)
cmd.Flags().BoolVar(&o.cliOnly, "cli-only", false, lang.CmdConnectFlagCliOnly)
cmd.Flags().BoolVar(&o.open, "open", false, lang.CmdConnectFlagOpen)

// TODO(soltysh): consider splitting sub-commands into separate files
cmd.AddCommand(newConnectListCommand())
Expand All @@ -49,6 +49,7 @@ func (o *connectOptions) run(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
l := logger.From(ctx)
target := ""
// TODO: this leaves room for ignoring potential misuse
if len(args) > 0 {
target = args[0]
}
Expand Down Expand Up @@ -82,15 +83,15 @@ func (o *connectOptions) run(cmd *cobra.Command, args []string) error {

defer tunnel.Close()

if o.cliOnly {
spinner.Updatef(lang.CmdConnectEstablishedCLI, tunnel.FullURL())
l.Info("Tunnel established, waiting for user to interrupt (ctrl-c to end)", "url", tunnel.FullURL())
} else {
if o.open {
spinner.Updatef(lang.CmdConnectEstablishedWeb, tunnel.FullURL())
l.Info("Tunnel established, opening your default web browser (ctrl-c to end)", "url", tunnel.FullURL())
if err := exec.LaunchURL(tunnel.FullURL()); err != nil {
return err
}
} else {
spinner.Updatef(lang.CmdConnectEstablishedCLI, tunnel.FullURL())
l.Info("Tunnel established, waiting for user to interrupt (ctrl-c to end)", "url", tunnel.FullURL())
}

// Wait for the interrupt signal or an error.
Expand Down
2 changes: 1 addition & 1 deletion src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const (
CmdConnectFlagType = "Specify the resource type. E.g. type=svc or type=pod. Ignored if connect-name is supplied."
CmdConnectFlagLocalPort = "(Optional, autogenerated if not provided) Specify the local port to bind to. E.g. local-port=42000."
CmdConnectFlagRemotePort = "Specify the remote port of the resource to bind to. E.g. remote-port=8080. Ignored if connect-name is supplied."
CmdConnectFlagCliOnly = "Disable browser auto-open"
CmdConnectFlagOpen = "Enable browser auto-open"

CmdConnectPreparingTunnel = "Preparing a tunnel to connect to %s"
CmdConnectEstablishedCLI = "Tunnel established at %s, waiting for user to interrupt (ctrl-c to end)"
Expand Down
4 changes: 2 additions & 2 deletions src/internal/packager2/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func LoadPackage(ctx context.Context, opt LoadOptions) (*layout.PackageLayout, e
isPartial := false
switch srcType {
case "oci":
isPartial, err = pullOCI(ctx, opt.Source, tarPath, opt.Shasum, opt.Filter)
isPartial, tarPath, err = pullOCI(ctx, opt.Source, tmpDir, opt.Shasum, opt.Filter)
if err != nil {
return nil, err
}
case "http", "https":
err = pullHTTP(ctx, opt.Source, tarPath, opt.Shasum)
tarPath, err = pullHTTP(ctx, opt.Source, tmpDir, opt.Shasum)
if err != nil {
return nil, err
}
Expand Down
95 changes: 68 additions & 27 deletions src/internal/packager2/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/defenseunicorns/pkg/oci"
"github.com/gabriel-vasile/mimetype"
goyaml "github.com/goccy/go-yaml"
"github.com/mholt/archiver/v3"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -46,17 +47,17 @@ func Pull(ctx context.Context, src, dir, shasum string, filter filters.Component
return err
}
defer os.Remove(tmpDir)
tmpPath := filepath.Join(tmpDir, "data.tar.zst")
tmpPath := ""

isPartial := false
switch u.Scheme {
case "oci":
isPartial, err = pullOCI(ctx, src, tmpPath, shasum, filter)
isPartial, tmpPath, err = pullOCI(ctx, src, tmpDir, shasum, filter)
if err != nil {
return err
}
case "http", "https":
err := pullHTTP(ctx, src, tmpPath, shasum)
tmpPath, err = pullHTTP(ctx, src, tmpDir, shasum)
if err != nil {
return err
}
Expand Down Expand Up @@ -101,10 +102,10 @@ func Pull(ctx context.Context, src, dir, shasum string, filter filters.Component
return nil
}

func pullOCI(ctx context.Context, src, tarPath, shasum string, filter filters.ComponentFilterStrategy) (bool, error) {
func pullOCI(ctx context.Context, src, tarDir, shasum string, filter filters.ComponentFilterStrategy) (bool, string, error) {
tmpDir, err := utils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
return false, err
return false, "", err
}
defer os.Remove(tmpDir)
if shasum != "" {
Expand All @@ -113,54 +114,98 @@ func pullOCI(ctx context.Context, src, tarPath, shasum string, filter filters.Co
arch := config.GetArch()
remote, err := zoci.NewRemote(ctx, src, oci.PlatformForArch(arch))
if err != nil {
return false, err
return false, "", err
}
desc, err := remote.ResolveRoot(ctx)
if err != nil {
return false, fmt.Errorf("could not fetch images index: %w", err)
return false, "", fmt.Errorf("could not fetch images index: %w", err)
}
layersToPull := []ocispec.Descriptor{}
isPartial := false
tarPath := filepath.Join(tarDir, "data.tar")
pkg, err := remote.FetchZarfYAML(ctx)
if err != nil {
return false, "", err
}
if !pkg.Metadata.Uncompressed {
tarPath = fmt.Sprintf("%s.zst", tarPath)
}
if supportsFiltering(desc.Platform) {
root, err := remote.FetchRoot(ctx)
if err != nil {
return false, err
return false, "", err
}
if len(root.Layers) != len(layersToPull) {
isPartial = true
}
pkg, err := remote.FetchZarfYAML(ctx)
if err != nil {
return false, err
}
pkg.Components, err = filter.Apply(pkg)
if err != nil {
return false, err
return false, "", err
}
layersToPull, err = remote.LayersFromRequestedComponents(ctx, pkg.Components)
if err != nil {
return false, err
return false, "", err
}
}
_, err = remote.PullPackage(ctx, tmpDir, config.CommonOptions.OCIConcurrency, layersToPull...)
if err != nil {
return false, err
return false, "", err
}
allTheLayers, err := filepath.Glob(filepath.Join(tmpDir, "*"))
if err != nil {
return false, err
return false, "", err
}
err = archiver.Archive(allTheLayers, tarPath)
if err != nil {
return false, err
return false, "", err
}
return isPartial, nil
return isPartial, tarPath, nil
}

func pullHTTP(ctx context.Context, src, tarPath, shasum string) error {
func pullHTTP(ctx context.Context, src, tarDir, shasum string) (string, error) {
if shasum == "" {
return errors.New("shasum cannot be empty")
return "", errors.New("shasum cannot be empty")
}
tarPath := filepath.Join(tarDir, "data")

err := pullHTTPFile(ctx, src, tarPath)
if err != nil {
return "", err
}

received, err := helpers.GetSHA256OfFile(tarPath)
if err != nil {
return "", err
}
if received != shasum {
return "", fmt.Errorf("shasum mismatch for file %s, expected %s but got %s", tarPath, shasum, received)
}

mtype, err := mimetype.DetectFile(tarPath)
if err != nil {
return "", err
}

newPath := filepath.Join(tarDir, "data.tar")

if mtype.Is("application/x-tar") {
err = os.Rename(tarPath, newPath)
if err != nil {
return "", err
}
return newPath, nil
} else if mtype.Is("application/zstd") {
newPath = fmt.Sprintf("%s.zst", newPath)
err = os.Rename(tarPath, newPath)
if err != nil {
return "", err
}
return newPath, nil
}
return "", fmt.Errorf("unsupported file type: %s", mtype.Extension())
}

func pullHTTPFile(ctx context.Context, src, tarPath string) error {
f, err := os.Create(tarPath)
if err != nil {
return err
Expand All @@ -186,13 +231,6 @@ func pullHTTP(ctx context.Context, src, tarPath, shasum string) error {
if err != nil {
return err
}
received, err := helpers.GetSHA256OfFile(tarPath)
if err != nil {
return err
}
if received != shasum {
return fmt.Errorf("shasum mismatch for file %s, expected %s but got %s", tarPath, shasum, received)
}
return nil
}

Expand Down Expand Up @@ -236,6 +274,9 @@ func nameFromMetadata(path string) (string, error) {
} else if pkg.Metadata.Version != "" {
name = fmt.Sprintf("%s-%s", name, pkg.Metadata.Version)
}
if pkg.Metadata.Uncompressed {
return fmt.Sprintf("%s.tar", name), nil
}
return fmt.Sprintf("%s.tar.zst", name), nil
}

Expand Down
55 changes: 55 additions & 0 deletions src/internal/packager2/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,61 @@ func TestPull(t *testing.T) {
require.Equal(t, packageData, pulledData)
}

func TestPullUncompressed(t *testing.T) {
t.Parallel()

ctx := testutil.TestContext(t)
packagePath := "./testdata/uncompressed/zarf-package-test-uncompressed-amd64-0.0.1.tar"
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
file, err := os.Open(packagePath)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
//nolint:errcheck // ignore
io.Copy(rw, file)
}))
t.Cleanup(func() {
srv.Close()
})

dir := t.TempDir()
shasum := "a118a4d306acc5dd4eab2c161e78fa3dfd1e08ae1e1794a4393be98c79257f5c"
err := Pull(ctx, srv.URL, dir, shasum, filters.Empty(), "", false)
require.NoError(t, err)

packageData, err := os.ReadFile(packagePath)
require.NoError(t, err)
pulledPath := filepath.Join(dir, "zarf-package-test-uncompressed-amd64-0.0.1.tar")
pulledData, err := os.ReadFile(pulledPath)
require.NoError(t, err)
require.Equal(t, packageData, pulledData)
}

func TestPullUnsupported(t *testing.T) {
t.Parallel()

ctx := testutil.TestContext(t)
packagePath := "./testdata/uncompressed/zarf.yaml"
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
file, err := os.Open(packagePath)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
//nolint:errcheck // ignore
io.Copy(rw, file)
}))
t.Cleanup(func() {
srv.Close()
})

dir := t.TempDir()
shasum := "6e9dccce07ba9d3c45b7c872fae863c5415d296fd5e2fb72a2583530aa750ccd"
err := Pull(ctx, srv.URL, dir, shasum, filters.Empty(), "", false)
require.EqualError(t, err, "unsupported file type: .txt", "unsupported file type: .txt")
}

func TestSupportsFiltering(t *testing.T) {
t.Parallel()

Expand Down
Binary file not shown.
Loading

0 comments on commit 2e3ebb0

Please sign in to comment.