From 5900b3f1c0fc17a0ee87dfef92f2f6e1593c24dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Nu=CC=81n=CC=83ez?= Date: Wed, 30 Jul 2014 00:22:37 +0200 Subject: [PATCH 1/2] Add AWS region validation --- builtin/providers/aws/config.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/config.go b/builtin/providers/aws/config.go index 32b3af623..8479cb9ae 100644 --- a/builtin/providers/aws/config.go +++ b/builtin/providers/aws/config.go @@ -1,6 +1,7 @@ package aws import ( + "fmt" "os" "strings" "unicode" @@ -29,12 +30,30 @@ func (c *Config) AWSAuth() (aws.Auth, error) { return auth, err } +// IsValidRegion returns true if the configured region is a valid AWS +// region and false if it's not +func (c *Config) IsValidRegion() bool { + var regions = [8]string{"us-east-1", "us-west-2", "us-west-1", "eu-west-1", + "ap-southeast-1", "ap-southeast-2", "ap-northeast-1", "sa-east-1"} + + for _, valid := range regions { + if c.Region == valid { + return true + } + } + return false +} + // AWSRegion returns the configured region. // // TODO(mitchellh): Test in some way. func (c *Config) AWSRegion() (aws.Region, error) { if c.Region != "" { - return aws.Regions[c.Region], nil + if c.IsValidRegion() { + return aws.Regions[c.Region], nil + } else { + return aws.Region{}, fmt.Errorf("Not a valid region: %s", c.Region) + } } if v := os.Getenv("AWS_REGION"); v != "" { From aad0fec940e5f8e6415853267c0d3b97c5049b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Nu=CC=81n=CC=83ez?= Date: Wed, 30 Jul 2014 00:22:50 +0200 Subject: [PATCH 2/2] Update test with valid AWS region --- builtin/providers/aws/resource_provider_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/providers/aws/resource_provider_test.go b/builtin/providers/aws/resource_provider_test.go index 5e833d1ad..eb0fb9b68 100644 --- a/builtin/providers/aws/resource_provider_test.go +++ b/builtin/providers/aws/resource_provider_test.go @@ -30,7 +30,7 @@ func TestResourceProvider_Configure(t *testing.T) { raw := map[string]interface{}{ "access_key": "foo", "secret_key": "bar", - "region": "us-east", + "region": "us-east-1", } rawConfig, err := config.NewRawConfig(raw) @@ -46,7 +46,7 @@ func TestResourceProvider_Configure(t *testing.T) { expected := Config{ AccessKey: "foo", SecretKey: "bar", - Region: "us-east", + Region: "us-east-1", } if !reflect.DeepEqual(rp.Config, expected) {