Merge pull request #567 from hobbeswalsh/master

Spaces in displayName break AWS IAM
This commit is contained in:
Jeff Mitchell 2015-08-26 12:37:52 -04:00
commit 9db8a5c744
2 changed files with 32 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package aws
import (
"fmt"
"math/rand"
"regexp"
"time"
"github.com/aws/aws-sdk-go/aws"
@ -46,7 +47,7 @@ func (b *backend) secretAccessKeysCreate(
// Generate a random username. We don't put the policy names in the
// username because the AWS console makes it pretty easy to see that.
username := fmt.Sprintf("vault-%s-%d-%d", displayName, time.Now().Unix(), rand.Int31n(10000))
username := fmt.Sprintf("vault-%s-%d-%d", normalizeDisplayName(displayName), time.Now().Unix(), rand.Int31n(10000))
// Write to the WAL that this user will be created. We do this before
// the user is created because if switch the order then the WAL put
@ -141,3 +142,8 @@ func secretAccessKeysRevoke(
return nil, nil
}
func normalizeDisplayName(displayName string) string {
re := regexp.MustCompile("[^a-zA-Z+=,.@_-]")
return re.ReplaceAllString(displayName, "_")
}

View File

@ -0,0 +1,25 @@
package aws
import (
"testing"
)
func TestNormalizeDisplayName(t *testing.T) {
invalidName := "^#$test name\nshould be normalized)(*"
expectedName := "___test_name_should_be_normalized___"
normalizedName := normalizeDisplayName(invalidName)
if normalizedName != expectedName {
t.Fatalf(
"normalizeDisplayName does not normalize AWS name correctly: %s",
normalizedName)
}
validName := "test_name_should_normalize_to_itself@example.com"
normalizedValidName := normalizeDisplayName(validName)
if normalizedValidName != validName {
t.Fatalf(
"normalizeDisplayName erroneously normalizes valid names: %s",
normalizedName)
}
}