Skip to content

Commit 051c4e4

Browse files
authored
feat(build, buildah, dockerfile, staged): add secrets support (#6454)
Signed-off-by: Yaroslav Pershin <62902094+iapershin@users.noreply.github.com>
1 parent a6b2530 commit 051c4e4

File tree

6 files changed

+15
-12
lines changed

6 files changed

+15
-12
lines changed

pkg/build/image/dockerfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func mapDockerfileToImagesSets(ctx context.Context, cfg *dockerfile.Dockerfile,
219219
case *dockerfile.DockerfileStageInstruction[*instructions.OnbuildCommand]:
220220
stg = stage_instruction.NewOnBuild(typedInstr, dockerfileImageConfig.Dependencies, !isFirstStage, &baseStageOptions)
221221
case *dockerfile.DockerfileStageInstruction[*instructions.RunCommand]:
222-
stg = stage_instruction.NewRun(typedInstr, dockerfileImageConfig.Dependencies, !isFirstStage, &baseStageOptions)
222+
stg = stage_instruction.NewRun(typedInstr, dockerfileImageConfig.Dependencies, !isFirstStage, &baseStageOptions, dockerfileImageConfig.Secrets)
223223
case *dockerfile.DockerfileStageInstruction[*instructions.ShellCommand]:
224224
stg = stage_instruction.NewShell(typedInstr, dockerfileImageConfig.Dependencies, !isFirstStage, &baseStageOptions)
225225
case *dockerfile.DockerfileStageInstruction[*instructions.StopSignalCommand]:

pkg/build/stage/instruction/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type Run struct {
1919
*Base[*instructions.RunCommand, *backend_instruction.Run]
2020
}
2121

22-
func NewRun(i *dockerfile.DockerfileStageInstruction[*instructions.RunCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Run {
23-
return &Run{Base: NewBase(i, backend_instruction.NewRun(*i.Data, nil), dependencies, hasPrevStage, opts)}
22+
func NewRun(i *dockerfile.DockerfileStageInstruction[*instructions.RunCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions, secrets []string) *Run {
23+
return &Run{Base: NewBase(i, backend_instruction.NewRun(*i.Data, nil, secrets), dependencies, hasPrevStage, opts)}
2424
}
2525

2626
func (stg *Run) ExpandDependencies(ctx context.Context, c stage.Conveyor, baseEnv map[string]string) error {

pkg/buildah/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type RunCommandOpts struct {
7878
WorkingDir string
7979
User string
8080
Envs []string
81+
Secrets []string
8182
// Mounts as allowed to be passed from command line.
8283
GlobalMounts []*specs.Mount
8384
// Mounts as allowed in Dockerfile RUN --mount option. Have more restrictions than GlobalMounts (e.g. Source of bind-mount can't be outside of ContextDir or container root).

pkg/buildah/native_linux.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,11 @@ func (b *NativeBuildah) RunCommand(ctx context.Context, container string, comman
436436
return err
437437
}
438438

439+
buildahSecrets, err := parse.Secrets(opts.Secrets)
440+
if err != nil {
441+
return fmt.Errorf("unable to parse secrets: %w", err)
442+
}
443+
439444
runOpts := buildah.RunOptions{
440445
Env: opts.Envs,
441446
ContextDir: contextDir,
@@ -453,8 +458,7 @@ func (b *NativeBuildah) RunCommand(ctx context.Context, container string, comman
453458
Cmd: []string{},
454459
Mounts: globalMounts,
455460
RunMounts: runMounts,
456-
// TODO(ilya-lesikov):
457-
Secrets: nil,
461+
Secrets: buildahSecrets,
458462
// TODO(ilya-lesikov):
459463
SSHSources: nil,
460464
}

pkg/config/raw_image_from_dockerfile.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,6 @@ func (c *rawImageFromDockerfile) toImageFromDockerfileDirective(giterminismManag
143143
image.platform = append([]string{}, c.Platform...)
144144
image.raw = c
145145

146-
if len(c.RawSecrets) > 0 && image.Staged {
147-
return nil, fmt.Errorf("secrets are not supported for staged build yet")
148-
}
149-
150146
secrets, err := GetValidatedSecrets(c.RawSecrets, giterminismManager, c.doc)
151147
if err != nil {
152148
return nil, err

pkg/container_backend/instruction/run.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import (
1414

1515
type Run struct {
1616
instructions.RunCommand
17-
Envs []string
17+
Envs []string
18+
Secrets []string
1819
}
1920

20-
func NewRun(i instructions.RunCommand, envs []string) *Run {
21-
return &Run{RunCommand: i, Envs: envs}
21+
func NewRun(i instructions.RunCommand, envs, secrets []string) *Run {
22+
return &Run{RunCommand: i, Envs: envs, Secrets: secrets}
2223
}
2324

2425
func (i *Run) UsesBuildContext() bool {
@@ -68,6 +69,7 @@ func (i *Run) Apply(ctx context.Context, containerName string, drv buildah.Build
6869
NetworkType: i.GetNetwork(),
6970
RunMounts: i.GetMounts(),
7071
Envs: i.Envs,
72+
Secrets: i.Secrets,
7173
}); err != nil {
7274
return fmt.Errorf("error running command %v for container %s: %w", i.CmdLine, containerName, err)
7375
}

0 commit comments

Comments
 (0)