Skip to content

Commit

Permalink
env,unset: support syntax to error on unset vars
Browse files Browse the repository at this point in the history
Docker supports ${SOME_VAR:?} syntax, which will give an error at build if SOME_VAR is unset.

```dockerfile
FROM alpine

ARG USERID

USER ${USERID:?}
```

```console
Failed to process `${USERID:?}`: USERID is not allowed to be
unset
```

Closes: containers/buildah#4284

Ref: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02

Signed-off-by: Aditya R <arajan@redhat.com>
  • Loading branch information
flouthoc committed Feb 3, 2023
1 parent 542deff commit 4c3b32e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
17 changes: 17 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,23 @@ func TestBuilder(t *testing.T) {
Labels: map[string]string{"test": "value"},
},
},
{
Dockerfile: "dockerclient/testdata/Dockerfile.unset",
From: "busybox",
Image: &docker.Image{
ID: "busybox2",
Config: &docker.Config{
Env: []string{},
},
},
ErrFn: func(err error) bool {
return err != nil && strings.Contains(err.Error(), "is not allowed to be unset")
},
Config: docker.Config{
Env: []string{},
Labels: map[string]string{"test": ""},
},
},
{
Dockerfile: "dockerclient/testdata/Dockerfile.args",
Args: map[string]string{"BAR": "first"},
Expand Down
5 changes: 5 additions & 0 deletions dockerclient/testdata/Dockerfile.unset
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM busybox

ARG FOO
ENV FOO=${FOO:?}
LABEL test="$FOO"
9 changes: 8 additions & 1 deletion shell_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,14 @@ func (sw *shellWord) processDollar() (string, error) {
newValue = word
}
return newValue, nil

case '?':
if newValue == "" {
newValue = word
}
if newValue == "" {
return "", fmt.Errorf("Failed to process `%s`: %s is not allowed to be unset", sw.word, name)
}
return newValue, nil
default:
return "", fmt.Errorf("Unsupported modifier (%c) in substitution: %s", modifier, sw.word)
}
Expand Down

0 comments on commit 4c3b32e

Please sign in to comment.