Skip to content

Commit d61c32f

Browse files
authored
Merge pull request #306 from oldium/fix-abs-paths
Fix absolute paths in secrets and inherited volumes
2 parents a27143f + fe74f69 commit d61c32f

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
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: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ func loadServiceWithExtends(filename, name string, servicesDict map[string]inter
563563
if vol.Type != types.VolumeTypeBind {
564564
continue
565565
}
566-
baseService.Volumes[i].Source = absPath(baseFileParent, vol.Source)
566+
baseService.Volumes[i].Source = resolveMaybeUnixPath(vol.Source, baseFileParent, lookupEnv)
567567
}
568568
}
569569

@@ -667,8 +667,8 @@ func resolveEnvironment(serviceConfig *types.ServiceConfig, workingDir string, l
667667
return nil
668668
}
669669

670-
func resolveVolumePath(volume types.ServiceVolumeConfig, workingDir string, lookupEnv template.Mapping) types.ServiceVolumeConfig {
671-
filePath := expandUser(volume.Source, lookupEnv)
670+
func resolveMaybeUnixPath(path string, workingDir string, lookupEnv template.Mapping) string {
671+
filePath := expandUser(path, lookupEnv)
672672
// Check if source is an absolute path (either Unix or Windows), to
673673
// handle a Windows client with a Unix daemon or vice-versa.
674674
//
@@ -678,10 +678,21 @@ func resolveVolumePath(volume types.ServiceVolumeConfig, workingDir string, look
678678
if !paths.IsAbs(filePath) && !isAbs(filePath) {
679679
filePath = absPath(workingDir, filePath)
680680
}
681-
volume.Source = filePath
681+
return filePath
682+
}
683+
684+
func resolveVolumePath(volume types.ServiceVolumeConfig, workingDir string, lookupEnv template.Mapping) types.ServiceVolumeConfig {
685+
volume.Source = resolveMaybeUnixPath(volume.Source, workingDir, lookupEnv)
682686
return volume
683687
}
684688

689+
func resolveSecretsPath(secret types.SecretConfig, workingDir string, lookupEnv template.Mapping) types.SecretConfig {
690+
if ! secret.External.External && secret.File != "" {
691+
secret.File = resolveMaybeUnixPath(secret.File, workingDir, lookupEnv)
692+
}
693+
return secret
694+
}
695+
685696
// TODO: make this more robust
686697
func expandUser(path string, lookupEnv template.Mapping) string {
687698
if strings.HasPrefix(path, "~") {
@@ -789,11 +800,14 @@ func LoadSecrets(source map[string]interface{}, details types.ConfigDetails, res
789800
return secrets, err
790801
}
791802
for name, secret := range secrets {
792-
obj, err := loadFileObjectConfig(name, "secret", types.FileObjectConfig(secret), details, resolvePaths)
803+
obj, err := loadFileObjectConfig(name, "secret", types.FileObjectConfig(secret), details, false)
793804
if err != nil {
794805
return nil, err
795806
}
796807
secretConfig := types.SecretConfig(obj)
808+
if resolvePaths {
809+
secretConfig = resolveSecretsPath(secretConfig, details.WorkingDir, details.LookupEnv)
810+
}
797811
secrets[name] = secretConfig
798812
}
799813
return secrets, nil

loader/loader_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,12 @@ func TestLoadWithExtends(t *testing.T) {
18611861
},
18621862
Environment: types.MappingWithEquals{},
18631863
Networks: map[string]*types.ServiceNetworkConfig{"default": nil},
1864+
Volumes: []types.ServiceVolumeConfig{{
1865+
Type: "bind",
1866+
Source: "/opt/data",
1867+
Target: "/var/lib/mysql",
1868+
Bind: &types.ServiceVolumeBind{CreateHostPath: true},
1869+
}},
18641870
Scale: 1,
18651871
},
18661872
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
services:
22
imported:
33
image: nginx
4+
volumes:
5+
- /opt/data:/var/lib/mysql

0 commit comments

Comments
 (0)