Skip to content

Commit

Permalink
Prevent panic on invalid configuration structure in exec mixin (getpo…
Browse files Browse the repository at this point in the history
…rter#2953)

* check for successful cast before get desc

Signed-off-by: Matheus Cumpian <matheus.cumpian@hotmail.com>

---------

Signed-off-by: Matheus Cumpian <matheus.cumpian@hotmail.com>
  • Loading branch information
Matheus Cumpian authored Feb 21, 2024
1 parent 2c17959 commit d72eba0
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ and we will add you. **All** contributors belong here. 💯
- [Chaiyapruek Muangsiri](https://github.com/cmppoon)
- [Xin Fu](https://github.com/imfing)
- [KallyDev](https://github.com/kallydev)
- [Matheus Cumpian](https://github.com/heavybr)
- [Salman Shah](https://github.com/sbshah97)
- [Ray Terrill](https://github.com/rayterrill)
- [Kim Christensen](https://github.com/kichristensen)
Expand Down
1 change: 1 addition & 0 deletions pkg/exec/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestMixin_ValidateSchema(t *testing.T) {
{"invoke", "testdata/invoke-input.yaml", ""},
{"uninstall", "testdata/uninstall-input.yaml", ""},
{"invalid command", "testdata/invalid-args-input.yaml", "Additional property args is not allowed"},
{"invalid type", "testdata/install-invalid-type-input.yaml", "Invalid type"},
}

for _, tc := range testcases {
Expand Down
2 changes: 2 additions & 0 deletions pkg/exec/testdata/install-invalid-type-input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
install:
- exec: "Install a VM and collect its ID"
6 changes: 5 additions & 1 deletion pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,8 +1031,12 @@ func (s *Step) GetDescription() (string, error) {

mixinName := s.GetMixinName()
children := s.Data[mixinName]
d, ok := children.(map[string]interface{})["description"]
m, ok := children.(map[string]interface{})
if !ok {
return "", fmt.Errorf("invalid mixin type (%T) for mixin step (%s)", children, mixinName)
}
d := m["description"]
if d == nil {
return "", nil
}
desc, ok := d.(string)
Expand Down
20 changes: 20 additions & 0 deletions pkg/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@ func TestManifest_Validate_Name(t *testing.T) {
assert.EqualError(t, err, "bundle name must be set")
}

func TestManifest_Validate_Description(t *testing.T) {
c := config.NewTestConfig(t)

c.TestContext.AddTestFile("testdata/porter-with-bad-description.yaml", config.Name)

_, err := LoadManifestFrom(context.Background(), c.Config, config.Name)
assert.ErrorContains(t, err, "validation of action \"install\" failed: invalid description type (string) for mixin step (exec)")
}

func TestManifest_Validate_InvalidType(t *testing.T) {
c := config.NewTestConfig(t)

c.TestContext.AddTestFile("testdata/porter-with-bad-type.yaml", config.Name)

assert.NotPanics(t, func() {
_, err := LoadManifestFrom(context.Background(), c.Config, config.Name)
assert.ErrorContains(t, err, "validation of action \"install\" failed: invalid mixin type (string) for mixin step (exec)")
})
}

func TestManifest_Validate_SchemaVersion(t *testing.T) {
invalidVersionErr := schema.ErrInvalidSchemaVersion.Error()

Expand Down
18 changes: 18 additions & 0 deletions pkg/manifest/testdata/porter-with-bad-description.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
schemaVersion: 1.0.0
version: 0.1.0
registry: example.com
name: mybun

mixins:
- exec

install:
- exec:
description:
- I should be a string, but I am a list
command: bash

uninstall:
- exec:
description: uninstall everything
command: bash
15 changes: 15 additions & 0 deletions pkg/manifest/testdata/porter-with-bad-type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
schemaVersion: 1.0.0
version: 0.1.0
registry: example.com
name: mybun

mixins:
- exec

install:
- exec: install something

uninstall:
- exec:
description: uninstall everything
command: bash

0 comments on commit d72eba0

Please sign in to comment.