Skip to content

Commit

Permalink
adding format validation to env variables in project descriptor v0.2
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Bustamante <jbustamante@vmware.com>
  • Loading branch information
jjbustamante committed Sep 7, 2022
1 parent ca3f513 commit a4d08d1
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 25 deletions.
38 changes: 28 additions & 10 deletions pkg/cnb/env_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,45 @@ type envBuildVariable struct {
}

func (a *envBuildVariable) UnmarshalTOML(f interface{}) error {
var (
env []envVariable
err error
)
switch v := f.(type) {
case map[string]interface{}:
if envs, ok := v["build"].([]map[string]interface{}); ok {
a.Env = buildEnv(envs)
env, err = buildEnv(envs)
if err != nil {
return err
}
}
return nil
case []map[string]interface{}:
a.Env = buildEnv(v)
return nil
env, err = buildEnv(v)
if err != nil {
return err
}
default:
return errors.New("environment variables in project descriptor could not be parsed")
}
a.Env = env
return nil
}

func buildEnv(v []map[string]interface{}) []envVariable {
func buildEnv(v []map[string]interface{}) ([]envVariable, error) {
var e []envVariable
for _, env := range v {
e = append(e, envVariable{
Name: env["name"].(string),
Value: env["value"].(string),
})
if name, nameOk := env["name"].(string); nameOk {
if value, valueOk := env["value"].(string); valueOk {
e = append(e, envVariable{
Name: name,
Value: value,
})
} else {
return nil, errors.Errorf("environment variable '%s' is not a string value", name)
}
} else {
return nil, errors.New("environment variable 'name' is not a string")
}
}
return e
return e, nil
}
3 changes: 2 additions & 1 deletion pkg/cnb/project_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func processFiles(appDir string, d build) error {
return nil
})
}

func getFileFilter(d build) (func(string) bool, error) {
if d.Exclude != nil && d.Include != nil {
return nil, fmt.Errorf("project descriptor cannot have both include and exclude defined")
Expand Down Expand Up @@ -182,6 +183,6 @@ type descriptorV1 struct {

type cnbTableV1 struct {
build `toml:",inline"`
Env []envVariable `toml:"env"`
Buildpacks []buildpack `toml:"buildpacks"`
Env []envVariable `toml:"env"`
}
96 changes: 82 additions & 14 deletions pkg/cnb/project_descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,9 @@ uri = "check-this-out.com"
when("the descriptor has build env vars", func() {
when("vars where set with older v0.2 project.toml", func() {
when("io.buildpacks.env.build format is used", func() {
it.Before(func() {
ioutil.WriteFile(projectToml, []byte(`
when("format is valid", func() {
it.Before(func() {
ioutil.WriteFile(projectToml, []byte(`
[_]
schema-version = "0.2"
[[io.buildpacks.env.build]]
Expand All @@ -245,18 +246,52 @@ value = "valueC"
name = "keyC"
value = "valueAnotherC"
`), 0644)
})
it("writes all env var files to the platform dir", func() {
assert.Nil(t, cnb.ProcessProjectDescriptor(appDir, descriptorPath, platformDir, logger))
checkEnvVar(t, platformDir, "keyA", "valueA")
checkEnvVar(t, platformDir, "keyB", "valueB")
checkEnvVar(t, platformDir, "keyC", "valueAnotherC")
})
})
it("writes all env var files to the platform dir", func() {
assert.Nil(t, cnb.ProcessProjectDescriptor(appDir, descriptorPath, platformDir, logger))
checkEnvVar(t, platformDir, "keyA", "valueA")
checkEnvVar(t, platformDir, "keyB", "valueB")
checkEnvVar(t, platformDir, "keyC", "valueAnotherC")
when("format is invalid", func() {
when("'value' is invalid", func() {
it.Before(func() {
ioutil.WriteFile(projectToml, []byte(`
[_]
schema-version = "0.2"
[[io.buildpacks.env.build]]
name = "KeyA"
value = 1
`), 0644)
})
it("writes all env var files to the platform dir", func() {
err := cnb.ProcessProjectDescriptor(appDir, descriptorPath, platformDir, logger)
assert.EqualError(t, err, "environment variable 'KeyA' is not a string value")
})
})
when("'name' is invalid", func() {
it.Before(func() {
ioutil.WriteFile(projectToml, []byte(`
[_]
schema-version = "0.2"
[[io.buildpacks.env.build]]
name = 1
value = "ValueA"
`), 0644)
})
it("writes all env var files to the platform dir", func() {
err := cnb.ProcessProjectDescriptor(appDir, descriptorPath, platformDir, logger)
assert.EqualError(t, err, "environment variable 'name' is not a string")
})
})
})
})

when("io.buildpacks.env format is used", func() {
it.Before(func() {
ioutil.WriteFile(projectToml, []byte(`
when("format is valid", func() {
it.Before(func() {
ioutil.WriteFile(projectToml, []byte(`
[_]
schema-version = "0.2"
[[io.buildpacks.env]]
Expand All @@ -272,12 +307,45 @@ value = "valueC"
[[io.buildpacks.env]]
name = "keyC"
value = "valueAnotherC"`), 0644)
})
it("writes all env var files to the platform dir", func() {
assert.Nil(t, cnb.ProcessProjectDescriptor(appDir, descriptorPath, platformDir, logger))
checkEnvVar(t, platformDir, "keyA", "valueA")
checkEnvVar(t, platformDir, "keyB", "valueB")
checkEnvVar(t, platformDir, "keyC", "valueAnotherC")
})
})
it("writes all env var files to the platform dir", func() {
assert.Nil(t, cnb.ProcessProjectDescriptor(appDir, descriptorPath, platformDir, logger))
checkEnvVar(t, platformDir, "keyA", "valueA")
checkEnvVar(t, platformDir, "keyB", "valueB")
checkEnvVar(t, platformDir, "keyC", "valueAnotherC")
when("format is invalid", func() {
when("'value' is invalid", func() {
it.Before(func() {
ioutil.WriteFile(projectToml, []byte(`
[_]
schema-version = "0.2"
[[io.buildpacks.env]]
name = "KeyA"
value = 1
`), 0644)
})
it("writes all env var files to the platform dir", func() {
err := cnb.ProcessProjectDescriptor(appDir, descriptorPath, platformDir, logger)
assert.EqualError(t, err, "environment variable 'KeyA' is not a string value")
})
})
when("'name' is invalid", func() {
it.Before(func() {
ioutil.WriteFile(projectToml, []byte(`
[_]
schema-version = "0.2"
[[io.buildpacks.env]]
name = 1
value = "ValueA"
`), 0644)
})
it("writes all env var files to the platform dir", func() {
err := cnb.ProcessProjectDescriptor(appDir, descriptorPath, platformDir, logger)
assert.EqualError(t, err, "environment variable 'name' is not a string")
})
})
})
})
})
Expand Down

0 comments on commit a4d08d1

Please sign in to comment.