Skip to content

Commit

Permalink
fix: expand home directory for docker secrets (#8476)
Browse files Browse the repository at this point in the history
* fix: expand home directory for docker secrets

* fix: linter
  • Loading branch information
griseau authored Feb 24, 2023
1 parent 6e4c7a9 commit e53a940
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/skaffold/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
sErrors "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/errors"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/util/term"
)

Expand Down Expand Up @@ -667,7 +668,7 @@ func ToCLIBuildArgs(a *latest.DockerArtifact, evaluatedArgs map[string]*string)
for _, secret := range a.Secrets {
secretString := fmt.Sprintf("id=%s", secret.ID)
if secret.Source != "" {
secretString += ",src=" + secret.Source
secretString += ",src=" + util.ExpandHomePath(secret.Source)
}
if secret.Env != "" {
secretString += ",env=" + secret.Env
Expand Down
10 changes: 10 additions & 0 deletions pkg/skaffold/docker/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package docker

import (
"context"
"fmt"
"io"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -370,6 +371,15 @@ func TestGetBuildArgs(t *testing.T) {
},
want: []string{"--secret", "id=mysecret,src=foo.src"},
},
{
description: "secret with file source in home directory",
artifact: &latest.DockerArtifact{
Secrets: []*latest.DockerSecret{
{ID: "mysecret", Source: "~/foo.src"},
},
},
want: []string{"--secret", fmt.Sprintf("id=mysecret,src=%s", util.ExpandHomePath("~/foo.src"))},
},
{
description: "secret with env source",
artifact: &latest.DockerArtifact{
Expand Down
8 changes: 8 additions & 0 deletions pkg/skaffold/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,11 @@ func ParseNamespaceFromFlags(flgs []string) string {
}
return ""
}

func ExpandHomePath(path string) string {
if strings.HasPrefix(path, "~/") {
dirname, _ := os.UserHomeDir()
path = filepath.Join(dirname, path[2:])
}
return path
}

0 comments on commit e53a940

Please sign in to comment.