Skip to content

Commit

Permalink
Only allow arg substitution in Labels field
Browse files Browse the repository at this point in the history
Earlier commits allowed substitution in various places in the Image
Config. This commit reverts most of those changes and allows arg
substitution only in the `Labels` field of the image config.

Signed-off-by: Peter Engelbert <[email protected]>
  • Loading branch information
pmengelbert committed Feb 5, 2025
1 parent c0a7cea commit b0ade70
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
47 changes: 5 additions & 42 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,8 @@ func (s *Spec) SubstituteArgs(env map[string]string, opts ...SubstituteOpt) erro
}
}

if s.Image != nil {
if err := s.Image.processBuildArgs(lex, args, cfg.AllowArg); err != nil {
appendErr(errors.Wrap(err, "package config"))
}
if err := s.Image.processBuildArgs(lex, args, cfg.AllowArg); err != nil {
appendErr(errors.Wrap(err, "package config"))
}

if err := s.Dependencies.processBuildArgs(args, cfg.AllowArg); err != nil {
Expand Down Expand Up @@ -460,23 +458,11 @@ func (b *ArtifactBuild) processBuildArgs(lex *shell.Lex, args map[string]string,
}

func (img *ImageConfig) processBuildArgs(lex *shell.Lex, args map[string]string, allowArg func(string) bool) error {
var errs error
for _, p := range []*string{&img.Base, &img.Cmd, &img.User, &img.Entrypoint, &img.StopSignal, &img.WorkingDir} {
updated, err := expandArgs(lex, *p, args, allowArg)
if err != nil {
errs = goerrors.Join(errs, errors.Wrap(err, "imgconfig"))
}
*p = updated
if img == nil {
return nil
}

for i, s := range img.Env {
updated, err := expandArgs(lex, s, args, allowArg)
if err != nil {
errs = goerrors.Join(errs, errors.Wrapf(err, "env %s", s))
continue
}
img.Env[i] = updated
}
var errs error

for k, v := range img.Labels {
updated, err := expandArgs(lex, v, args, allowArg)
Expand All @@ -487,28 +473,5 @@ func (img *ImageConfig) processBuildArgs(lex *shell.Lex, args map[string]string,
img.Labels[k] = updated
}

if img.Post != nil {
for k, sl := range img.Post.Symlinks {
updatedK, err := expandArgs(lex, k, args, allowArg)
if err != nil {
errs = goerrors.Join(errs, errors.Wrapf(err, "symlink oldpath"))
continue
}

updatedV, err := expandArgs(lex, sl.Path, args, allowArg)
if err != nil {
errs = goerrors.Join(errs, errors.Wrapf(err, "symlink newpath"))
continue
}

sl.Path = updatedV
img.Post.Symlinks[updatedK] = sl
// remove the old key if a substitution happened
if updatedK != k {
delete(img.Post.Symlinks, k)
}
}
}

return errs
}
6 changes: 2 additions & 4 deletions target.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ func (t *Target) processBuildArgs(lex *shell.Lex, args map[string]string, allowA
}
}

if t.Image != nil {
if err := t.Image.processBuildArgs(lex, args, allowArg); err != nil {
errs = append(errs, errors.Wrap(err, "package config"))
}
if err := t.Image.processBuildArgs(lex, args, allowArg); err != nil {
errs = append(errs, errors.Wrap(err, "package config"))
}

if err := t.Dependencies.processBuildArgs(args, allowArg); err != nil {
Expand Down

0 comments on commit b0ade70

Please sign in to comment.