Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 5d2b833

Browse files
committed
Call the validator in packager init and extract
Remove the no longer used check for the relative path Signed-off-by: Djordje Lukic <[email protected]>
1 parent 08c99b7 commit 5d2b833

File tree

3 files changed

+18
-87
lines changed

3 files changed

+18
-87
lines changed

internal/packager/extract.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/docker/app/internal"
11+
"github.com/docker/app/internal/validator"
1112
"github.com/docker/app/loader"
1213
"github.com/docker/app/types"
1314
"github.com/pkg/errors"
@@ -64,12 +65,19 @@ func Extract(name string, ops ...func(*types.App) error) (*types.App, error) {
6465
return nil, errors.Wrapf(err, "cannot locate application %q in filesystem", name)
6566
}
6667
if s.IsDir() {
68+
v := validator.NewValidatorWithDefaults()
69+
err := v.Validate(filepath.Join(appname, internal.ComposeFileName))
70+
if err != nil {
71+
return nil, err
72+
}
73+
6774
// directory: already decompressed
6875
appOpts := append(ops,
6976
types.WithPath(appname),
7077
types.WithSource(types.AppSourceSplit),
7178
)
72-
return loader.LoadFromDirectory(appname, appOpts...)
79+
app, err := loader.LoadFromDirectory(appname, appOpts...)
80+
return app, err
7381
}
7482
// not a dir: a tarball package, extract that in a temp dir
7583
app, err := loader.LoadFromTar(appname, ops...)

internal/packager/init.go

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/docker/app/internal"
1616
"github.com/docker/app/internal/compose"
17+
"github.com/docker/app/internal/validator"
1718
"github.com/docker/app/internal/yaml"
1819
"github.com/docker/app/types"
1920
"github.com/docker/app/types/metadata"
@@ -49,6 +50,11 @@ func Init(errWriter io.Writer, name string, composeFile string) (string, error)
4950
if composeFile == "" {
5051
err = initFromScratch(name)
5152
} else {
53+
v := validator.NewValidatorWithDefaults()
54+
err = v.Validate(composeFile)
55+
if err != nil {
56+
return "", err
57+
}
5258
err = initFromComposeFile(errWriter, name, composeFile)
5359
}
5460
if err != nil {
@@ -82,11 +88,11 @@ func checkComposeFileVersion(compose map[string]interface{}) error {
8288

8389
func getEnvFiles(svcName string, envFileEntry interface{}) ([]string, error) {
8490
var envFiles []string
85-
switch envFileEntry.(type) {
91+
switch envFileEntry := envFileEntry.(type) {
8692
case string:
87-
envFiles = append(envFiles, envFileEntry.(string))
93+
envFiles = append(envFiles, envFileEntry)
8894
case []interface{}:
89-
for _, env := range envFileEntry.([]interface{}) {
95+
for _, env := range envFileEntry {
9096
envFiles = append(envFiles, env.(string))
9197
}
9298
default:
@@ -125,50 +131,6 @@ func checkEnvFiles(errWriter io.Writer, appName string, cfgMap map[string]interf
125131
return nil
126132
}
127133

128-
func checkRelativePaths(cfgMap map[string]interface{}) error {
129-
services := cfgMap["services"]
130-
servicesMap, ok := services.(map[string]interface{})
131-
if !ok {
132-
return fmt.Errorf("invalid Compose file")
133-
}
134-
for svcName, svc := range servicesMap {
135-
svcContent, ok := svc.(map[string]interface{})
136-
if !ok {
137-
return fmt.Errorf("invalid service %q", svcName)
138-
}
139-
v, ok := svcContent["volumes"]
140-
if !ok {
141-
continue
142-
}
143-
volumes, ok := v.([]interface{})
144-
if !ok {
145-
return fmt.Errorf("invalid Compose file")
146-
}
147-
for _, volume := range volumes {
148-
switch volume.(type) {
149-
case string:
150-
svol := volume.(string)
151-
source := strings.TrimRight(svol, ":")
152-
if !filepath.IsAbs(source) {
153-
return fmt.Errorf("invalid service %q: can't use relative path as volume source", svcName)
154-
}
155-
case map[string]interface{}:
156-
lvol := volume.(map[string]interface{})
157-
src, ok := lvol["source"]
158-
if !ok {
159-
return fmt.Errorf("invalid volume in service %q", svcName)
160-
}
161-
if !filepath.IsAbs(src.(string)) {
162-
return fmt.Errorf("invalid service %q: can't use relative path as volume source", svcName)
163-
}
164-
default:
165-
return fmt.Errorf("invalid Compose file")
166-
}
167-
}
168-
}
169-
return nil
170-
}
171-
172134
func getParamsFromDefaultEnvFile(composeFile string, composeRaw []byte) (map[string]string, bool, error) {
173135
params := make(map[string]string)
174136
envs, err := opts.ParseEnvFile(filepath.Join(filepath.Dir(composeFile), ".env"))
@@ -217,9 +179,6 @@ func initFromComposeFile(errWriter io.Writer, name string, composeFile string) e
217179
if err := checkEnvFiles(errWriter, name, cfgMap); err != nil {
218180
return err
219181
}
220-
if err := checkRelativePaths(cfgMap); err != nil {
221-
return err
222-
}
223182
params, needsFilling, err := getParamsFromDefaultEnvFile(composeFile, composeRaw)
224183
if err != nil {
225184
return err

internal/packager/init_test.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -186,39 +186,3 @@ maintainers:
186186
)
187187
assert.Assert(t, fs.Equal(tmpdir.Path(), manifest))
188188
}
189-
190-
func TestInitRelativeVolumePath(t *testing.T) {
191-
for _, composeData := range []string{`
192-
version: '3.7'
193-
services:
194-
nginx:
195-
image: nginx
196-
volumes:
197-
- ./foo:/data
198-
`,
199-
`
200-
version: '3.7'
201-
services:
202-
nginx:
203-
image: nginx
204-
volumes:
205-
- type: bind
206-
source: ./foo
207-
target: /data
208-
`,
209-
} {
210-
inputDir := fs.NewDir(t, "app_input_",
211-
fs.WithFile(internal.ComposeFileName, composeData),
212-
)
213-
defer inputDir.Remove()
214-
215-
appName := "my.dockerapp"
216-
dir := fs.NewDir(t, "app_",
217-
fs.WithDir(appName),
218-
)
219-
defer dir.Remove()
220-
221-
err := initFromComposeFile(nil, dir.Join(appName), inputDir.Join(internal.ComposeFileName))
222-
assert.ErrorContains(t, err, "can't use relative path")
223-
}
224-
}

0 commit comments

Comments
 (0)