From 731463c28288f34d0d16e04326ff1465ad19e71d Mon Sep 17 00:00:00 2001 From: Matt Ellis Date: Thu, 8 Mar 2018 11:52:48 -0800 Subject: [PATCH] Have MakeKey fail if `namespace` contains a colon This helper method is only really used for testing, but we should not allow it to create a Key who's namespace has a colon (as ParseKey would not build something like this). --- cmd/config_test.go | 4 ++-- pkg/operations/operations_aws.go | 8 ++++---- pkg/resource/config/key.go | 5 ++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cmd/config_test.go b/cmd/config_test.go index 5b2784646..674ce2b91 100644 --- a/cmd/config_test.go +++ b/cmd/config_test.go @@ -15,6 +15,6 @@ import ( func TestPrettyKeyForProject(t *testing.T) { proj := &workspace.Project{Name: tokens.PackageName("test-package"), Runtime: "nodejs"} - assert.Equal(t, "foo", prettyKeyForProject(config.MakeKey("test-package", "foo"), proj)) - assert.Equal(t, "other-package:bar", prettyKeyForProject(config.MakeKey("other-package", "bar"), proj)) + assert.Equal(t, "foo", prettyKeyForProject(config.MustMakeKey("test-package", "foo"), proj)) + assert.Equal(t, "other-package:bar", prettyKeyForProject(config.MustMakeKey("other-package", "bar"), proj)) } diff --git a/pkg/operations/operations_aws.go b/pkg/operations/operations_aws.go index e82b7cb56..7587a0fcb 100644 --- a/pkg/operations/operations_aws.go +++ b/pkg/operations/operations_aws.go @@ -64,10 +64,10 @@ var _ Provider = (*awsOpsProvider)(nil) var ( // AWS config keys - regionKey = config.MakeKey("aws", "region") - accessKey = config.MakeKey("aws", "accessKey") - secretKey = config.MakeKey("aws", "secretKey") // nolint: gas - token = config.MakeKey("aws", "token") + regionKey = config.MustMakeKey("aws", "region") + accessKey = config.MustMakeKey("aws", "accessKey") + secretKey = config.MustMakeKey("aws", "secretKey") // nolint: gas + token = config.MustMakeKey("aws", "token") ) const ( diff --git a/pkg/resource/config/key.go b/pkg/resource/config/key.go index 2131951be..0732facef 100644 --- a/pkg/resource/config/key.go +++ b/pkg/resource/config/key.go @@ -9,6 +9,7 @@ import ( "github.com/pkg/errors" "github.com/pulumi/pulumi/pkg/tokens" + "github.com/pulumi/pulumi/pkg/util/contract" ) type Key struct { @@ -16,7 +17,9 @@ type Key struct { name string } -func MakeKey(namespace string, name string) Key { +// MustMakeKey constructs a config.Key for a given namespace and name. The namespace may not contain a `:` +func MustMakeKey(namespace string, name string) Key { + contract.Requiref(!strings.Contains(":", namespace), "namespace", "may not contain a colon") return Key{namespace: namespace, name: name} }