Skip to content

Commit 786825f

Browse files
YrrepNojjeff-mccoy
andauthored
Add support for package deploy via cosign sget (zarf-dev#550)
* boilerplate for deploying remote packages via sget verification * Move main.go to root to embed cosign.pub * fix bugs with sget'ing a remote package * misc cleanup of sget package deployment updates * use fixed name since path is dynamic * add e2e for sget package deploy Co-authored-by: Jeff McCoy <code@jeffm.us> Co-authored-by: Minimind <882485+jeff-mccoy@users.noreply.github.com>
1 parent 939ad0f commit 786825f

File tree

9 files changed

+88
-18
lines changed

9 files changed

+88
-18
lines changed

.hooks/verify-zarf-schema.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env sh
2-
go run src/main.go tools config-schema > zarf.schema.json
2+
go run main.go tools config-schema > zarf.schema.json

Makefile

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ clean: ## Clean the build dir
4141
rm -rf build
4242

4343
build-cli-linux-amd: build-injector-registry
44-
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="$(BUILD_ARGS)" -o build/zarf src/main.go
44+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="$(BUILD_ARGS)" -o build/zarf main.go
4545

4646
build-cli-linux-arm: build-injector-registry
47-
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="$(BUILD_ARGS)" -o build/zarf-arm src/main.go
47+
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="$(BUILD_ARGS)" -o build/zarf-arm main.go
4848

4949
build-cli-mac-intel: build-injector-registry
50-
GOOS=darwin GOARCH=amd64 go build -ldflags="$(BUILD_ARGS)" -o build/zarf-mac-intel src/main.go
50+
GOOS=darwin GOARCH=amd64 go build -ldflags="$(BUILD_ARGS)" -o build/zarf-mac-intel main.go
5151

5252
build-cli-mac-apple: build-injector-registry
53-
GOOS=darwin GOARCH=arm64 go build -ldflags="$(BUILD_ARGS)" -o build/zarf-mac-apple src/main.go
53+
GOOS=darwin GOARCH=arm64 go build -ldflags="$(BUILD_ARGS)" -o build/zarf-mac-apple main.go
5454

55-
build-cli-linux: build-cli-linux-amd build-cli-linux-arm
55+
build-cli-linux: build-cli-linux-amd build-cli-linux-arm
5656

5757
build-cli: build-cli-linux-amd build-cli-linux-arm build-cli-mac-intel build-cli-mac-apple ## Build the CLI
5858

@@ -64,7 +64,7 @@ build-injector-registry:
6464
dev-agent-image:
6565
$(eval tag := defenseunicorns/dev-zarf-agent:$(shell date +%s))
6666
$(eval arch := $(shell uname -m))
67-
CGO_ENABLED=0 GOOS=linux go build -o build/zarf-linux-$(arch) src/main.go
67+
CGO_ENABLED=0 GOOS=linux go build -o build/zarf-linux-$(arch) main.go
6868
DOCKER_BUILDKIT=1 docker build --tag $(tag) --build-arg TARGETARCH=$(arch) . && \
6969
kind load docker-image zarf-agent:$(tag) && \
7070
kubectl -n zarf set image deployment/agent-hook server=$(tag)

main.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
_ "embed"
5+
6+
"github.com/defenseunicorns/zarf/src/cmd"
7+
"github.com/defenseunicorns/zarf/src/config"
8+
)
9+
10+
//go:embed cosign.pub
11+
var cosignPublicKey string
12+
13+
func main() {
14+
config.SGetPublicKey = cosignPublicKey
15+
cmd.Execute()
16+
}

src/cmd/package.go

+2
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,6 @@ func init() {
108108
packageDeployCmd.Flags().StringVar(&config.DeployOptions.Components, "components", "", "Comma-separated list of components to install. Adding this flag will skip the init prompts for which components to install")
109109
packageDeployCmd.Flags().BoolVar(&insecureDeploy, "insecure", false, "Skip shasum validation of remote package. Required if deploying a remote package and `--shasum` is not provided")
110110
packageDeployCmd.Flags().StringVar(&shasum, "shasum", "", "Shasum of the package to deploy. Required if deploying a remote package and `--insecure` is not provided")
111+
112+
packageDeployCmd.Flags().StringVar(&config.DeployOptions.SGetKeyPath, "sget", "", "Path to public sget key file for remote packages signed via cosign")
111113
}

src/config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ var (
6161
// Private vars
6262
active types.ZarfPackage
6363
state types.ZarfState
64+
65+
SGetPublicKey string
6466
)
6567

6668
func IsZarfInitConfig() bool {

src/internal/packager/common.go

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package packager
22

33
import (
4+
"context"
45
"crypto/sha256"
56
"encoding/hex"
67
"fmt"
@@ -79,10 +80,10 @@ func confirmAction(configPath, userMessage string, sbomViewFiles []string) bool
7980
if err != nil {
8081
message.Fatal(err, "Unable to open the package config file")
8182
}
82-
83+
8384
// Convert []byte to string and print to screen
8485
text := string(content)
85-
86+
8687
pterm.Println()
8788
utils.ColorPrintYAML(text)
8889

@@ -118,6 +119,11 @@ func HandleIfURL(packagePath string, shasum string, insecureDeploy bool) (string
118119
return packagePath, func() {}
119120
}
120121

122+
// Handle case where deploying remote package validated via sget
123+
if strings.HasPrefix(packagePath, "sget://") {
124+
return handleSgetPackage(packagePath)
125+
}
126+
121127
if !insecureDeploy && shasum == "" {
122128
message.Fatal(nil, "When deploying a remote package you must provide either a `--shasum` or the `--insecure` flag. Neither were provided.")
123129
}
@@ -163,6 +169,36 @@ func HandleIfURL(packagePath string, shasum string, insecureDeploy bool) (string
163169
return localPackagePath, tempPath.clean
164170
}
165171

172+
func handleSgetPackage(sgetPackagePath string) (string, func()) {
173+
// Write the package to a local file in a temp path
174+
tempPath := createPaths()
175+
176+
// Create the local file for the package
177+
localPackagePath := filepath.Join(tempPath.base, "remote.tar.zst")
178+
destinationFile, err := os.Create(localPackagePath)
179+
if err != nil {
180+
message.Fatal(err, "Unable to create the destination file")
181+
}
182+
defer destinationFile.Close()
183+
184+
// If this is a DefenseUnicorns package, use an internal sget public key
185+
if strings.HasPrefix(sgetPackagePath, "sget://defenseunicorns") {
186+
os.Setenv("DU_SGET_KEY", config.SGetPublicKey)
187+
config.DeployOptions.SGetKeyPath = "env://DU_SGET_KEY"
188+
}
189+
190+
// Remove the 'sget://' header for the actual sget call
191+
sgetPackagePath = strings.TrimPrefix(sgetPackagePath, "sget://")
192+
193+
// Sget the package
194+
err = utils.Sget(sgetPackagePath, config.DeployOptions.SGetKeyPath, destinationFile, context.TODO())
195+
if err != nil {
196+
message.Fatal(err, "Unable to get the remote package via sget")
197+
}
198+
199+
return localPackagePath, tempPath.clean
200+
}
201+
166202
func isValidFileExtension(filename string) bool {
167203
for _, extension := range config.GetValidPackageExtensions() {
168204
if strings.HasSuffix(filename, extension) {

src/main.go

-9
This file was deleted.

src/test/e2e/e2e_remote_sget_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestE2eRemoteSgete(t *testing.T) {
11+
defer e2e.cleanupAfterTest(t)
12+
13+
//run `zarf init`
14+
output, err := e2e.execZarfCommand("init", "--confirm")
15+
require.NoError(t, err, output)
16+
17+
path := fmt.Sprintf("sget://defenseunicorns/zarf-hello-world:%s", e2e.arch)
18+
19+
// Deploy the game
20+
output, err = e2e.execZarfCommand("package", "deploy", path, "--confirm")
21+
require.NoError(t, err, output)
22+
}

src/types/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ type ZarfDeployOptions struct {
155155
PackagePath string
156156
Confirm bool
157157
Components string
158+
SGetKeyPath string
158159

159160
// Zarf init is installing the k3s component
160161
ApplianceMode bool

0 commit comments

Comments
 (0)