Skip to content

Commit f4cd055

Browse files
committed
Fix absolute path handling for secret files
Secret files are converted to bind mounts, so handle them in the same way as volumes - detect and handle unix-like absolute paths. Signed-off-by: Oldřich Jedlička <[email protected]>
1 parent ccdcc95 commit f4cd055

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

loader/full-example.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ secrets:
428428
environment: BAR
429429
x-bar: baz
430430
x-foo: bar
431+
secret5:
432+
file: /abs/secret_data
431433
x-bar: baz
432434
x-foo: bar
433435
x-nested:

loader/full-struct_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ func secrets(workingDir string) map[string]types.SecretConfig {
574574
"x-foo": "bar",
575575
},
576576
},
577+
"secret5": {
578+
File: "/abs/secret_data",
579+
},
577580
}
578581
}
579582

@@ -985,6 +988,8 @@ secrets:
985988
environment: BAR
986989
x-bar: baz
987990
x-foo: bar
991+
secret5:
992+
file: /abs/secret_data
988993
configs:
989994
config1:
990995
file: %s
@@ -1106,6 +1111,10 @@ func fullExampleJSON(workingDir, homeDir string) string {
11061111
"name": "bar",
11071112
"environment": "BAR",
11081113
"external": false
1114+
},
1115+
"secret5": {
1116+
"file": "/abs/secret_data",
1117+
"external": false
11091118
}
11101119
},
11111120
"services": {

loader/loader.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,8 @@ func resolveEnvironment(serviceConfig *types.ServiceConfig, workingDir string, l
660660
return nil
661661
}
662662

663-
func resolveVolumePath(volume types.ServiceVolumeConfig, workingDir string, lookupEnv template.Mapping) types.ServiceVolumeConfig {
664-
filePath := expandUser(volume.Source, lookupEnv)
663+
func resolveMaybeUnixPath(path string, workingDir string, lookupEnv template.Mapping) string {
664+
filePath := expandUser(path, lookupEnv)
665665
// Check if source is an absolute path (either Unix or Windows), to
666666
// handle a Windows client with a Unix daemon or vice-versa.
667667
//
@@ -671,10 +671,21 @@ func resolveVolumePath(volume types.ServiceVolumeConfig, workingDir string, look
671671
if !paths.IsAbs(filePath) && !isAbs(filePath) {
672672
filePath = absPath(workingDir, filePath)
673673
}
674-
volume.Source = filePath
674+
return filePath
675+
}
676+
677+
func resolveVolumePath(volume types.ServiceVolumeConfig, workingDir string, lookupEnv template.Mapping) types.ServiceVolumeConfig {
678+
volume.Source = resolveMaybeUnixPath(volume.Source, workingDir, lookupEnv)
675679
return volume
676680
}
677681

682+
func resolveSecretsPath(secret types.SecretConfig, workingDir string, lookupEnv template.Mapping) types.SecretConfig {
683+
if ! secret.External.External && secret.File != "" {
684+
secret.File = resolveMaybeUnixPath(secret.File, workingDir, lookupEnv)
685+
}
686+
return secret
687+
}
688+
678689
// TODO: make this more robust
679690
func expandUser(path string, lookupEnv template.Mapping) string {
680691
if strings.HasPrefix(path, "~") {
@@ -782,11 +793,14 @@ func LoadSecrets(source map[string]interface{}, details types.ConfigDetails, res
782793
return secrets, err
783794
}
784795
for name, secret := range secrets {
785-
obj, err := loadFileObjectConfig(name, "secret", types.FileObjectConfig(secret), details, resolvePaths)
796+
obj, err := loadFileObjectConfig(name, "secret", types.FileObjectConfig(secret), details, false)
786797
if err != nil {
787798
return nil, err
788799
}
789800
secretConfig := types.SecretConfig(obj)
801+
if resolvePaths {
802+
secretConfig = resolveSecretsPath(secretConfig, details.WorkingDir, details.LookupEnv)
803+
}
790804
secrets[name] = secretConfig
791805
}
792806
return secrets, nil

0 commit comments

Comments
 (0)