Skip to content

Commit 3fcdf06

Browse files
leodidoona-agent
andcommitted
refactor: replace environment variable string literals with constants
Define constants for all LEEWAY_* environment variables to prevent typos and improve maintainability. This follows the existing pattern established in the codebase. Changes: - Add EnvvarDockerExportToCache, EnvvarDefaultCacheLevel, EnvvarSegmentKey, EnvvarTrace, EnvvarProvenanceKeypath, and EnvvarExperimental to cmd/root.go - Add EnvvarDockerExportToCache and EnvvarWorkspaceRoot to pkg/leeway/build.go - Add EnvvarSLSACacheVerification, EnvvarSLSASourceURI, and EnvvarEnableInFlightChecksums to pkg/leeway/workspace.go - Replace all string literal usages with constants across codebase - Update test files to use constants with leeway. package prefix Co-authored-by: Ona <no-reply@ona.com>
1 parent 04cbcc1 commit 3fcdf06

File tree

7 files changed

+78
-43
lines changed

7 files changed

+78
-43
lines changed

cmd/build.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func init() {
178178
}
179179

180180
func addBuildFlags(cmd *cobra.Command) {
181-
cacheDefault := os.Getenv("LEEWAY_DEFAULT_CACHE_LEVEL")
181+
cacheDefault := os.Getenv(EnvvarDefaultCacheLevel)
182182
if cacheDefault == "" {
183183
cacheDefault = "remote"
184184
}
@@ -204,7 +204,7 @@ func addBuildFlags(cmd *cobra.Command) {
204204
cmd.Flags().String("slsa-source-uri", "", "Expected source URI for SLSA verification (required when verification enabled)")
205205
cmd.Flags().Bool("in-flight-checksums", false, "Enable checksumming of cache artifacts to prevent TOCTU attacks")
206206
cmd.Flags().String("report", "", "Generate a HTML report after the build has finished. (e.g. --report myreport.html)")
207-
cmd.Flags().String("report-segment", os.Getenv("LEEWAY_SEGMENT_KEY"), "Report build events to segment using the segment key (defaults to $LEEWAY_SEGMENT_KEY)")
207+
cmd.Flags().String("report-segment", os.Getenv(EnvvarSegmentKey), "Report build events to segment using the segment key (defaults to $LEEWAY_SEGMENT_KEY)")
208208
cmd.Flags().Bool("report-github", os.Getenv("GITHUB_OUTPUT") != "", "Report package build success/failure to GitHub Actions using the GITHUB_OUTPUT environment variable")
209209
cmd.Flags().Bool("fixed-build-dir", true, "Use a fixed build directory for each package, instead of based on the package version, to better utilize caches based on absolute paths (defaults to true)")
210210
cmd.Flags().Bool("docker-export-to-cache", false, "Export Docker images to cache instead of pushing directly (enables SLSA L3 compliance)")
@@ -217,7 +217,7 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, cache.LocalCache) {
217217
// - Workspace auto-set: Low priority (package config can override)
218218
dockerExportEnvSet := false
219219
dockerExportEnvValue := false
220-
if envVal := os.Getenv("LEEWAY_DOCKER_EXPORT_TO_CACHE"); envVal != "" {
220+
if envVal := os.Getenv(EnvvarDockerExportToCache); envVal != "" {
221221
dockerExportEnvSet = true
222222
dockerExportEnvValue = (envVal == "true" || envVal == "1")
223223
log.WithField("value", envVal).Debug("User explicitly set LEEWAY_DOCKER_EXPORT_TO_CACHE before workspace loading")

cmd/build_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestInFlightChecksumsEnvironmentVariable(t *testing.T) {
116116
t.Run(tt.name, func(t *testing.T) {
117117
// Set environment variable using t.Setenv for proper cleanup
118118
if tt.envValue != "" {
119-
t.Setenv("LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS", tt.envValue)
119+
t.Setenv(EnvvarEnableInFlightChecksums, tt.envValue)
120120
}
121121

122122
// Create test command
@@ -136,7 +136,7 @@ func TestInFlightChecksumsEnvironmentVariable(t *testing.T) {
136136
}
137137

138138
// Test the actual logic from getBuildOpts
139-
inFlightChecksumsDefault := os.Getenv("LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS") == "true"
139+
inFlightChecksumsDefault := os.Getenv(EnvvarEnableInFlightChecksums) == "true"
140140
inFlightChecksums, err := cmd.Flags().GetBool("in-flight-checksums")
141141
if err != nil {
142142
t.Fatalf("failed to get flag: %v", err)

cmd/root.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ const (
3333

3434
// EnvvarEnableInFlightChecksums enables in-flight checksumming of cache artifacts
3535
EnvvarEnableInFlightChecksums = "LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS"
36+
37+
// EnvvarDockerExportToCache controls whether Docker images are exported to cache instead of pushed directly
38+
EnvvarDockerExportToCache = "LEEWAY_DOCKER_EXPORT_TO_CACHE"
39+
40+
// EnvvarDefaultCacheLevel sets the default cache level
41+
EnvvarDefaultCacheLevel = "LEEWAY_DEFAULT_CACHE_LEVEL"
42+
43+
// EnvvarSegmentKey configures Segment analytics key
44+
EnvvarSegmentKey = "LEEWAY_SEGMENT_KEY"
45+
46+
// EnvvarTrace enables tracing output
47+
EnvvarTrace = "LEEWAY_TRACE"
48+
49+
// EnvvarProvenanceKeypath configures provenance key path
50+
EnvvarProvenanceKeypath = "LEEWAY_PROVENANCE_KEYPATH"
51+
52+
// EnvvarExperimental enables experimental features
53+
EnvvarExperimental = "LEEWAY_EXPERIMENTAL"
3654
)
3755

3856
const (
@@ -116,7 +134,7 @@ variables have an effect on leeway:
116134
// Execute adds all child commands to the root command and sets flags appropriately.
117135
// This is called by main.main(). It only needs to happen once to the rootCmd.
118136
func Execute() {
119-
tp := os.Getenv("LEEWAY_TRACE")
137+
tp := os.Getenv(EnvvarTrace)
120138
if tp != "" {
121139
f, err := os.OpenFile(tp, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
122140
if err != nil {
@@ -166,7 +184,7 @@ func getWorkspace() (leeway.Workspace, error) {
166184
return leeway.Workspace{}, err
167185
}
168186

169-
return leeway.FindWorkspace(workspace, args, variant, os.Getenv("LEEWAY_PROVENANCE_KEYPATH"))
187+
return leeway.FindWorkspace(workspace, args, variant, os.Getenv(EnvvarProvenanceKeypath))
170188
}
171189

172190
func getBuildArgs() (leeway.Arguments, error) {
@@ -186,7 +204,7 @@ func getBuildArgs() (leeway.Arguments, error) {
186204
}
187205

188206
func addExperimentalCommand(parent, child *cobra.Command) {
189-
if os.Getenv("LEEWAY_EXPERIMENTAL") != "true" {
207+
if os.Getenv(EnvvarExperimental) != "true" {
190208
return
191209
}
192210

pkg/leeway/build.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ const (
9090
// Defaults to "network".
9191
EnvvarYarnMutex = "LEEWAY_YARN_MUTEX"
9292

93+
// EnvvarDockerExportToCache controls whether Docker images are exported to cache instead of pushed directly
94+
EnvvarDockerExportToCache = "LEEWAY_DOCKER_EXPORT_TO_CACHE"
95+
96+
// EnvvarWorkspaceRoot names the environment variable for workspace root path
97+
EnvvarWorkspaceRoot = "LEEWAY_WORKSPACE_ROOT"
98+
9399
// dockerImageNamesFiles is the name of the file store in poushed Docker build artifacts
94100
// which contains the names of the Docker images we just pushed
95101
dockerImageNamesFiles = "imgnames.txt"
@@ -1716,7 +1722,7 @@ func determineDockerExportMode(p *Package, cfg *DockerPkgConfig, buildctx *build
17161722
// Layer 5 & 4: Start with workspace default
17171723
// At this point, workspace loading already auto-set LEEWAY_DOCKER_EXPORT_TO_CACHE
17181724
// if provenance.slsa: true
1719-
envExport := os.Getenv("LEEWAY_DOCKER_EXPORT_TO_CACHE")
1725+
envExport := os.Getenv(EnvvarDockerExportToCache)
17201726
if envExport == "true" || envExport == "1" {
17211727
exportToCache = true
17221728
source = "workspace_default"
@@ -2379,7 +2385,7 @@ func executeCommandsForPackage(buildctx *buildContext, p *Package, wd string, co
23792385
}
23802386

23812387
env := append(os.Environ(), p.Environment...)
2382-
env = append(env, fmt.Sprintf("LEEWAY_WORKSPACE_ROOT=%s", p.C.W.Origin))
2388+
env = append(env, fmt.Sprintf("%s=%s", EnvvarWorkspaceRoot, p.C.W.Origin))
23832389
for _, cmd := range commands {
23842390
if len(cmd) == 0 {
23852391
continue // Skip empty commands

pkg/leeway/build_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ func TestDockerExport_PrecedenceHierarchy(t *testing.T) {
266266
t.Run(tt.name, func(t *testing.T) {
267267
// Setup environment to simulate workspace auto-set
268268
if tt.workspaceEnvSet {
269-
t.Setenv("LEEWAY_DOCKER_EXPORT_TO_CACHE", "true")
269+
t.Setenv(leeway.EnvvarDockerExportToCache, "true")
270270
} else {
271-
t.Setenv("LEEWAY_DOCKER_EXPORT_TO_CACHE", "")
271+
t.Setenv(leeway.EnvvarDockerExportToCache, "")
272272
}
273273

274274
// Create mock build context
@@ -296,7 +296,7 @@ func TestDockerExport_PrecedenceHierarchy(t *testing.T) {
296296
var source string
297297

298298
// Layer 5 & 4: Workspace default
299-
envExport := os.Getenv("LEEWAY_DOCKER_EXPORT_TO_CACHE")
299+
envExport := os.Getenv(leeway.EnvvarDockerExportToCache)
300300
if envExport == "true" || envExport == "1" {
301301
exportToCache = true
302302
source = "workspace_default"

pkg/leeway/workspace.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ import (
3030
"github.com/gitpod-io/leeway/pkg/doublestar"
3131
)
3232

33+
const (
34+
// EnvvarSLSACacheVerification enables SLSA verification for cached artifacts
35+
EnvvarSLSACacheVerification = "LEEWAY_SLSA_CACHE_VERIFICATION"
36+
37+
// EnvvarSLSASourceURI configures the expected source URI for SLSA verification
38+
EnvvarSLSASourceURI = "LEEWAY_SLSA_SOURCE_URI"
39+
40+
// EnvvarEnableInFlightChecksums enables in-flight checksumming of cache artifacts
41+
EnvvarEnableInFlightChecksums = "LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS"
42+
)
43+
3344
// Workspace is the root container of all compoments. All components are named relative
3445
// to the origin of this workspace.
3546
type Workspace struct {
@@ -77,23 +88,23 @@ func (w *Workspace) ApplySLSADefaults() {
7788
log.Info("SLSA provenance enabled - activating SLSA L3 runtime features")
7889

7990
// Auto-enable cache verification (global feature)
80-
if setEnvDefault("LEEWAY_SLSA_CACHE_VERIFICATION", "true") {
91+
if setEnvDefault(EnvvarSLSACacheVerification, "true") {
8192
log.Debug("Auto-enabled: LEEWAY_SLSA_CACHE_VERIFICATION=true")
8293
}
8394

8495
// Auto-enable in-flight checksumming (global feature)
85-
if setEnvDefault("LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS", "true") {
96+
if setEnvDefault(EnvvarEnableInFlightChecksums, "true") {
8697
log.Debug("Auto-enabled: LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS=true")
8798
}
8899

89100
// Auto-enable Docker export mode (workspace default, packages can override)
90-
if setEnvDefault("LEEWAY_DOCKER_EXPORT_TO_CACHE", "true") {
101+
if setEnvDefault(EnvvarDockerExportToCache, "true") {
91102
log.Debug("Auto-enabled: LEEWAY_DOCKER_EXPORT_TO_CACHE=true (package config can override)")
92103
}
93104

94105
// Auto-set source URI from Git origin
95106
if w.Git.Origin != "" {
96-
if setEnvDefault("LEEWAY_SLSA_SOURCE_URI", w.Git.Origin) {
107+
if setEnvDefault(EnvvarSLSASourceURI, w.Git.Origin) {
97108
log.WithField("source_uri", w.Git.Origin).Debug("Auto-set SLSA source URI from Git origin")
98109
}
99110
}

pkg/leeway/workspace_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,10 @@ func TestWorkspace_ApplySLSADefaults(t *testing.T) {
329329
gitOrigin: "github.com/gitpod-io/leeway",
330330
existingEnvVars: map[string]string{},
331331
expectedEnvVars: map[string]string{
332-
"LEEWAY_SLSA_CACHE_VERIFICATION": "true",
333-
"LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS": "true",
334-
"LEEWAY_DOCKER_EXPORT_TO_CACHE": "true",
335-
"LEEWAY_SLSA_SOURCE_URI": "github.com/gitpod-io/leeway",
332+
leeway.EnvvarSLSACacheVerification: "true",
333+
leeway.EnvvarEnableInFlightChecksums: "true",
334+
leeway.EnvvarDockerExportToCache: "true",
335+
leeway.EnvvarSLSASourceURI: "github.com/gitpod-io/leeway",
336336
},
337337
},
338338
{
@@ -341,10 +341,10 @@ func TestWorkspace_ApplySLSADefaults(t *testing.T) {
341341
provenanceSLSA: false,
342342
existingEnvVars: map[string]string{},
343343
expectedEnvVars: map[string]string{
344-
"LEEWAY_SLSA_CACHE_VERIFICATION": "",
345-
"LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS": "",
346-
"LEEWAY_DOCKER_EXPORT_TO_CACHE": "",
347-
"LEEWAY_SLSA_SOURCE_URI": "",
344+
leeway.EnvvarSLSACacheVerification: "",
345+
leeway.EnvvarEnableInFlightChecksums: "",
346+
leeway.EnvvarDockerExportToCache: "",
347+
leeway.EnvvarSLSASourceURI: "",
348348
},
349349
},
350350
{
@@ -353,10 +353,10 @@ func TestWorkspace_ApplySLSADefaults(t *testing.T) {
353353
provenanceSLSA: true,
354354
existingEnvVars: map[string]string{},
355355
expectedEnvVars: map[string]string{
356-
"LEEWAY_SLSA_CACHE_VERIFICATION": "",
357-
"LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS": "",
358-
"LEEWAY_DOCKER_EXPORT_TO_CACHE": "",
359-
"LEEWAY_SLSA_SOURCE_URI": "",
356+
leeway.EnvvarSLSACacheVerification: "",
357+
leeway.EnvvarEnableInFlightChecksums: "",
358+
leeway.EnvvarDockerExportToCache: "",
359+
leeway.EnvvarSLSASourceURI: "",
360360
},
361361
},
362362
{
@@ -365,14 +365,14 @@ func TestWorkspace_ApplySLSADefaults(t *testing.T) {
365365
provenanceSLSA: true,
366366
gitOrigin: "github.com/gitpod-io/leeway",
367367
existingEnvVars: map[string]string{
368-
"LEEWAY_SLSA_CACHE_VERIFICATION": "false",
369-
"LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS": "false",
368+
leeway.EnvvarSLSACacheVerification: "false",
369+
leeway.EnvvarEnableInFlightChecksums: "false",
370370
},
371371
expectedEnvVars: map[string]string{
372-
"LEEWAY_SLSA_CACHE_VERIFICATION": "false", // Not overridden
373-
"LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS": "false", // Not overridden
374-
"LEEWAY_DOCKER_EXPORT_TO_CACHE": "true", // Set (wasn't present)
375-
"LEEWAY_SLSA_SOURCE_URI": "github.com/gitpod-io/leeway",
372+
leeway.EnvvarSLSACacheVerification: "false", // Not overridden
373+
leeway.EnvvarEnableInFlightChecksums: "false", // Not overridden
374+
leeway.EnvvarDockerExportToCache: "true", // Set (wasn't present)
375+
leeway.EnvvarSLSASourceURI: "github.com/gitpod-io/leeway",
376376
},
377377
},
378378
{
@@ -382,10 +382,10 @@ func TestWorkspace_ApplySLSADefaults(t *testing.T) {
382382
gitOrigin: "",
383383
existingEnvVars: map[string]string{},
384384
expectedEnvVars: map[string]string{
385-
"LEEWAY_SLSA_CACHE_VERIFICATION": "true",
386-
"LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS": "true",
387-
"LEEWAY_DOCKER_EXPORT_TO_CACHE": "true",
388-
"LEEWAY_SLSA_SOURCE_URI": "", // Not set without Git origin
385+
leeway.EnvvarSLSACacheVerification: "true",
386+
leeway.EnvvarEnableInFlightChecksums: "true",
387+
leeway.EnvvarDockerExportToCache: "true",
388+
leeway.EnvvarSLSASourceURI: "", // Not set without Git origin
389389
},
390390
},
391391
}
@@ -394,10 +394,10 @@ func TestWorkspace_ApplySLSADefaults(t *testing.T) {
394394
t.Run(tt.name, func(t *testing.T) {
395395
// Clear environment variables for clean test
396396
envVarsToCheck := []string{
397-
"LEEWAY_SLSA_CACHE_VERIFICATION",
398-
"LEEWAY_ENABLE_IN_FLIGHT_CHECKSUMS",
399-
"LEEWAY_DOCKER_EXPORT_TO_CACHE",
400-
"LEEWAY_SLSA_SOURCE_URI",
397+
leeway.EnvvarSLSACacheVerification,
398+
leeway.EnvvarEnableInFlightChecksums,
399+
leeway.EnvvarDockerExportToCache,
400+
leeway.EnvvarSLSASourceURI,
401401
}
402402
for _, key := range envVarsToCheck {
403403
t.Setenv(key, "")

0 commit comments

Comments
 (0)