Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
ensure restricted prefixes cannot be used, and remove aws session fro…
Browse files Browse the repository at this point in the history
…m plugin
  • Loading branch information
ojkelly committed Jun 29, 2018
1 parent 209c15f commit a4f114b
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 24 additions & 12 deletions internal/plugins/load.go
Expand Up @@ -107,7 +107,7 @@ func loadPlugin(
)
}

if configIsValid(config, pluginName, pluginVersion, false) == false {
if configIsValid(config, pluginName, pluginVersion) == false {
printer.Fatal(
fmt.Errorf("Plugin `%s` does not have a valid config", pluginName),
"Contact the plugin author.",
Expand Down Expand Up @@ -165,7 +165,7 @@ func pluginExists(filePath string) bool {
return false
}

func configIsValid(config pluginTypes.Config, pluginName string, pluginVersion string, requiresAWSSession bool) (ok bool) {
func configIsValid(config pluginTypes.Config, pluginName string, pluginVersion string) (ok bool) {
foundIssue := false
// TODO: improve these error messages, and provide links to the docs for plugin devs
if config.Name == "" {
Expand All @@ -177,11 +177,6 @@ func configIsValid(config pluginTypes.Config, pluginName string, pluginVersion s

foundIssue = true
}
// } else if config.Name != pluginName {
// // warn
// foundIssue = true
// log.Fatal(fmt.Sprintf("%s name did not match the name in kombustion.yaml, this plugin cannot be loaded"))
// }

if config.Prefix == "" {
printer.Fatal(
Expand All @@ -192,16 +187,33 @@ func configIsValid(config pluginTypes.Config, pluginName string, pluginVersion s
foundIssue = true
}

if config.RequiresAWSSession != requiresAWSSession {
// Warn about the need to add the config val to the manifest file
// foundIssue = true
if config.Prefix == "AWS" {
printer.Fatal(
fmt.Errorf("Plugin `%s` requires an AWS session, has not been allowed one", pluginName),
fmt.Sprintf("Add `role: RoleName` to `kombustion.yaml` under `%s` to allow this plugin to assume that role.\n Reason for access: %s", pluginName, config.RequiresAWSSessionReason),
fmt.Errorf("Plugin `%s` tried to use 'AWS' as prefix, this plugin cannot be loaded", pluginName),
"'AWS' is a restricted prefix, and cannt be used by a plugin. This is an issue with the plugin.",
"",
)
foundIssue = true
}

if config.Prefix == "Custom" {
printer.Fatal(
fmt.Errorf("Plugin `%s` tried to use 'Custom' as prefix, this plugin cannot be loaded", pluginName),
"'Custom' is a restricted prefix, and cannt be used by a plugin. This is an issue with the plugin.",
"",
)
foundIssue = true
}

if config.Prefix == "Kombustion" {
printer.Fatal(
fmt.Errorf("Plugin `%s` tried to use 'Kombustion' as prefix, this plugin cannot be loaded", pluginName),
"'Kombustion' is a restricted prefix, and cannt be used by a plugin. This is an issue with the plugin.",
"",
)
foundIssue = true
}

if foundIssue == false {
ok = true
}
Expand Down
108 changes: 108 additions & 0 deletions internal/plugins/load_test.go
@@ -0,0 +1,108 @@
package plugins

import (
"fmt"
"testing"

printer "github.com/KablamoOSS/go-cli-printer"
pluginTypes "github.com/KablamoOSS/kombustion/pkg/plugins/api/types"
"github.com/stretchr/testify/assert"
)

func TestConfigValid(t *testing.T) {
printer.Test()

type input struct {
config pluginTypes.Config
pluginName string
pluginVersion string
}

tests := []struct {
input input
output bool
}{
{
input: input{
config: pluginTypes.Config{
Name: "Example",
Prefix: "Example",
Help: pluginTypes.Help{},
},
pluginName: "Example",
pluginVersion: "0.1.0",
},
output: true,
},
{
input: input{
config: pluginTypes.Config{
Name: "Example",
Prefix: "AWS",
Help: pluginTypes.Help{},
},
pluginName: "Example",
pluginVersion: "0.1.0",
},
output: false,
},
{
input: input{
config: pluginTypes.Config{
Name: "Example",
Prefix: "Custom",
Help: pluginTypes.Help{},
},
pluginName: "Example",
pluginVersion: "0.1.0",
},
output: false,
},
{
input: input{
config: pluginTypes.Config{
Name: "Example",
Prefix: "Kombustion",
Help: pluginTypes.Help{},
},
pluginName: "Example",
pluginVersion: "0.1.0",
},
output: false,
},
{
input: input{
config: pluginTypes.Config{
Name: "",
Prefix: "Kombustion",
Help: pluginTypes.Help{},
},
pluginName: "Example",
pluginVersion: "0.1.0",
},
output: false,
},
{
input: input{
config: pluginTypes.Config{
Name: "Example",
Prefix: "",
Help: pluginTypes.Help{},
},
pluginName: "Example",
pluginVersion: "0.1.0",
},
output: false,
},
}

for i, test := range tests {
assert := assert.New(t)
testOutput := configIsValid(
test.input.config,
test.input.pluginName,
test.input.pluginVersion,
)
assert.Equal(testOutput, test.output, fmt.Sprintf("Test %d:", i))
}
}
14 changes: 6 additions & 8 deletions internal/plugins/marshalling_test.go
Expand Up @@ -19,10 +19,9 @@ func TestLoadConfig(t *testing.T) {
{
input: func() []byte {
blob, err := msgpack.Marshal(&pluginTypes.Config{
Name: "example-plugin",
Version: "0.1.0",
Prefix: "Test",
RequiresAWSSession: false,
Name: "example-plugin",
Version: "0.1.0",
Prefix: "Test",
})

if err != nil {
Expand All @@ -32,10 +31,9 @@ func TestLoadConfig(t *testing.T) {
return blob
}(),
output: pluginTypes.Config{
Name: "example-plugin",
Version: "0.1.0",
Prefix: "Test",
RequiresAWSSession: false,
Name: "example-plugin",
Version: "0.1.0",
Prefix: "Test",
},
throws: false,
},
Expand Down
12 changes: 4 additions & 8 deletions pkg/plugins/api/types/types.go
Expand Up @@ -2,14 +2,10 @@ package types

// Config provides Kombustion with information about your plugin
type Config struct {
Name string
Version string
Prefix string
RequiresAWSSession bool
// This is printed to the screen if the user has not provided a role, explaining
// what the role is used for
RequiresAWSSessionReason string
Help Help
Name string
Version string
Prefix string
Help Help
}

// Help - a set of available documentation fields
Expand Down
13 changes: 12 additions & 1 deletion vendor/github.com/KablamoOSS/go-cli-printer/printer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a4f114b

Please sign in to comment.