Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/aws: add ses_smtp_password to iam_access_key #3165

Merged
merged 1 commit into from
Sep 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions builtin/providers/aws/resource_aws_iam_access_key.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package aws

import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -32,6 +35,10 @@ func resourceAwsIamAccessKey() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"ses_smtp_password": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand All @@ -55,6 +62,10 @@ func resourceAwsIamAccessKeyCreate(d *schema.ResourceData, meta interface{}) err
if err := d.Set("secret", createResp.AccessKey.SecretAccessKey); err != nil {
return err
}

d.Set("ses_smtp_password",
sesSmtpPasswordFromSecretKey(createResp.AccessKey.SecretAccessKey))

return resourceAwsIamAccessKeyReadResult(d, &iam.AccessKeyMetadata{
AccessKeyId: createResp.AccessKey.AccessKeyId,
CreateDate: createResp.AccessKey.CreateDate,
Expand Down Expand Up @@ -115,3 +126,19 @@ func resourceAwsIamAccessKeyDelete(d *schema.ResourceData, meta interface{}) err
}
return nil
}

func sesSmtpPasswordFromSecretKey(key *string) string {
if key == nil {
return ""
}
version := byte(0x02)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the byte cast is necessary, but if it helps for clarity purposes then 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm going to leave it for verbosity's sake, but noted!

message := []byte("SendRawEmail")
hmacKey := []byte(*key)
h := hmac.New(sha256.New, hmacKey)
h.Write(message)
rawSig := h.Sum(nil)
versionedSig := make([]byte, 0, len(rawSig)+1)
versionedSig = append(versionedSig, version)
versionedSig = append(versionedSig, rawSig...)
return base64.StdEncoding.EncodeToString(versionedSig)
}
17 changes: 17 additions & 0 deletions builtin/providers/aws/resource_aws_iam_access_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,20 @@ resource "aws_iam_access_key" "a_key" {
user = "${aws_iam_user.a_user.name}"
}
`

func TestSesSmtpPasswordFromSecretKey(t *testing.T) {
cases := []struct {
Input string
Expected string
}{
{"some+secret+key", "AnkqhOiWEcszZZzTMCQbOY1sPGoLFgMH9zhp4eNgSjo4"},
{"another+secret+key", "Akwqr0Giwi8FsQFgW3DXWCC2DiiQ/jZjqLDWK8TeTBgL"},
}

for _, tc := range cases {
actual := sesSmtpPasswordFromSecretKey(&tc.Input)
if actual != tc.Expected {
t.Fatalf("%q: expected %q, got %q", tc.Input, tc.Expected, actual)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ The following attributes are exported:
* `id` - The access key ID.
* `user` - The IAM user associated with this access key.
* `secret` - The secret access key. Note that this will be written to the state file.
* `ses_smtp_password` - The secret access key converted into an SES SMTP

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, shouldn't this be encrypted with PGP in the same way the secret is?

password by applying [AWS's documented conversion
algorithm](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html#smtp-credentials-convert).
* `status` - "Active" or "Inactive". Keys are initially active, but can be made
inactive by other means.