Skip to content

Commit

Permalink
Merge pull request #306 from oldium/fix-abs-paths
Browse files Browse the repository at this point in the history
Fix absolute paths in secrets and inherited volumes
  • Loading branch information
milas authored Dec 1, 2022
2 parents a27143f + fe74f69 commit d61c32f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 2 additions & 0 deletions loader/full-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ secrets:
environment: BAR
x-bar: baz
x-foo: bar
secret5:
file: /abs/secret_data
x-bar: baz
x-foo: bar
x-nested:
Expand Down
9 changes: 9 additions & 0 deletions loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ func secrets(workingDir string) map[string]types.SecretConfig {
"x-foo": "bar",
},
},
"secret5": {
File: "/abs/secret_data",
},
}
}

Expand Down Expand Up @@ -985,6 +988,8 @@ secrets:
environment: BAR
x-bar: baz
x-foo: bar
secret5:
file: /abs/secret_data
configs:
config1:
file: %s
Expand Down Expand Up @@ -1106,6 +1111,10 @@ func fullExampleJSON(workingDir, homeDir string) string {
"name": "bar",
"environment": "BAR",
"external": false
},
"secret5": {
"file": "/abs/secret_data",
"external": false
}
},
"services": {
Expand Down
24 changes: 19 additions & 5 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func loadServiceWithExtends(filename, name string, servicesDict map[string]inter
if vol.Type != types.VolumeTypeBind {
continue
}
baseService.Volumes[i].Source = absPath(baseFileParent, vol.Source)
baseService.Volumes[i].Source = resolveMaybeUnixPath(vol.Source, baseFileParent, lookupEnv)
}
}

Expand Down Expand Up @@ -667,8 +667,8 @@ func resolveEnvironment(serviceConfig *types.ServiceConfig, workingDir string, l
return nil
}

func resolveVolumePath(volume types.ServiceVolumeConfig, workingDir string, lookupEnv template.Mapping) types.ServiceVolumeConfig {
filePath := expandUser(volume.Source, lookupEnv)
func resolveMaybeUnixPath(path string, workingDir string, lookupEnv template.Mapping) string {
filePath := expandUser(path, lookupEnv)
// Check if source is an absolute path (either Unix or Windows), to
// handle a Windows client with a Unix daemon or vice-versa.
//
Expand All @@ -678,10 +678,21 @@ func resolveVolumePath(volume types.ServiceVolumeConfig, workingDir string, look
if !paths.IsAbs(filePath) && !isAbs(filePath) {
filePath = absPath(workingDir, filePath)
}
volume.Source = filePath
return filePath
}

func resolveVolumePath(volume types.ServiceVolumeConfig, workingDir string, lookupEnv template.Mapping) types.ServiceVolumeConfig {
volume.Source = resolveMaybeUnixPath(volume.Source, workingDir, lookupEnv)
return volume
}

func resolveSecretsPath(secret types.SecretConfig, workingDir string, lookupEnv template.Mapping) types.SecretConfig {
if ! secret.External.External && secret.File != "" {
secret.File = resolveMaybeUnixPath(secret.File, workingDir, lookupEnv)
}
return secret
}

// TODO: make this more robust
func expandUser(path string, lookupEnv template.Mapping) string {
if strings.HasPrefix(path, "~") {
Expand Down Expand Up @@ -789,11 +800,14 @@ func LoadSecrets(source map[string]interface{}, details types.ConfigDetails, res
return secrets, err
}
for name, secret := range secrets {
obj, err := loadFileObjectConfig(name, "secret", types.FileObjectConfig(secret), details, resolvePaths)
obj, err := loadFileObjectConfig(name, "secret", types.FileObjectConfig(secret), details, false)
if err != nil {
return nil, err
}
secretConfig := types.SecretConfig(obj)
if resolvePaths {
secretConfig = resolveSecretsPath(secretConfig, details.WorkingDir, details.LookupEnv)
}
secrets[name] = secretConfig
}
return secrets, nil
Expand Down
6 changes: 6 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,12 @@ func TestLoadWithExtends(t *testing.T) {
},
Environment: types.MappingWithEquals{},
Networks: map[string]*types.ServiceNetworkConfig{"default": nil},
Volumes: []types.ServiceVolumeConfig{{
Type: "bind",
Source: "/opt/data",
Target: "/var/lib/mysql",
Bind: &types.ServiceVolumeBind{CreateHostPath: true},
}},
Scale: 1,
},
}
Expand Down
2 changes: 2 additions & 0 deletions loader/testdata/compose-test-extends-imported.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
services:
imported:
image: nginx
volumes:
- /opt/data:/var/lib/mysql

0 comments on commit d61c32f

Please sign in to comment.