-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5774 from stack72/f-aws-iam-user-sshkey
provider/aws: Add `aws_iam_user_ssh_key` resource
- Loading branch information
Showing
5 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/iam" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsIamUserSshKey() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsIamUserSshKeyCreate, | ||
Read: resourceAwsIamUserSshKeyRead, | ||
Update: resourceAwsIamUserSshKeyUpdate, | ||
Delete: resourceAwsIamUserSshKeyDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"ssh_public_key_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"fingerprint": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"username": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"public_key": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"encoding": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validateIamUserSSHKeyEncoding, | ||
}, | ||
|
||
"status": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsIamUserSshKeyCreate(d *schema.ResourceData, meta interface{}) error { | ||
iamconn := meta.(*AWSClient).iamconn | ||
username := d.Get("username").(string) | ||
publicKey := d.Get("public_key").(string) | ||
|
||
request := &iam.UploadSSHPublicKeyInput{ | ||
UserName: aws.String(username), | ||
SSHPublicKeyBody: aws.String(publicKey), | ||
} | ||
|
||
log.Println("[DEBUG] Create IAM User SSH Key Request:", request) | ||
createResp, err := iamconn.UploadSSHPublicKey(request) | ||
if err != nil { | ||
return fmt.Errorf("Error creating IAM User SSH Key %s: %s", username, err) | ||
} | ||
|
||
d.Set("ssh_public_key_id", createResp.SSHPublicKey.SSHPublicKeyId) | ||
d.SetId(*createResp.SSHPublicKey.SSHPublicKeyId) | ||
|
||
return resourceAwsIamUserSshKeyRead(d, meta) | ||
} | ||
|
||
func resourceAwsIamUserSshKeyRead(d *schema.ResourceData, meta interface{}) error { | ||
iamconn := meta.(*AWSClient).iamconn | ||
username := d.Get("username").(string) | ||
request := &iam.GetSSHPublicKeyInput{ | ||
UserName: aws.String(username), | ||
SSHPublicKeyId: aws.String(d.Id()), | ||
Encoding: aws.String(d.Get("encoding").(string)), | ||
} | ||
|
||
getResp, err := iamconn.GetSSHPublicKey(request) | ||
if err != nil { | ||
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" { // XXX test me | ||
log.Printf("[WARN] No IAM user ssh key (%s) found", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error reading IAM User SSH Key %s: %s", d.Id(), err) | ||
} | ||
|
||
d.Set("fingerprint", getResp.SSHPublicKey.Fingerprint) | ||
d.Set("status", getResp.SSHPublicKey.Status) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsIamUserSshKeyUpdate(d *schema.ResourceData, meta interface{}) error { | ||
if d.HasChange("status") { | ||
iamconn := meta.(*AWSClient).iamconn | ||
|
||
request := &iam.UpdateSSHPublicKeyInput{ | ||
UserName: aws.String(d.Get("username").(string)), | ||
SSHPublicKeyId: aws.String(d.Id()), | ||
Status: aws.String(d.Get("status").(string)), | ||
} | ||
|
||
log.Println("[DEBUG] Update IAM User SSH Key request:", request) | ||
_, err := iamconn.UpdateSSHPublicKey(request) | ||
if err != nil { | ||
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" { | ||
log.Printf("[WARN] No IAM user ssh key by ID (%s) found", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error updating IAM User SSH Key %s: %s", d.Id(), err) | ||
} | ||
return resourceAwsIamUserRead(d, meta) | ||
} | ||
return nil | ||
} | ||
|
||
func resourceAwsIamUserSshKeyDelete(d *schema.ResourceData, meta interface{}) error { | ||
iamconn := meta.(*AWSClient).iamconn | ||
|
||
request := &iam.DeleteSSHPublicKeyInput{ | ||
UserName: aws.String(d.Get("username").(string)), | ||
SSHPublicKeyId: aws.String(d.Id()), | ||
} | ||
|
||
log.Println("[DEBUG] Delete IAM User SSH Key request:", request) | ||
if _, err := iamconn.DeleteSSHPublicKey(request); err != nil { | ||
return fmt.Errorf("Error deleting IAM User SSH Key %s: %s", d.Id(), err) | ||
} | ||
return nil | ||
} | ||
|
||
func validateIamUserSSHKeyEncoding(v interface{}, k string) (ws []string, errors []error) { | ||
value := v.(string) | ||
encodingTypes := map[string]bool{ | ||
"PEM": true, | ||
"SSH": true, | ||
} | ||
|
||
if !encodingTypes[value] { | ||
errors = append(errors, fmt.Errorf("IAM User SSH Key Encoding can only be PEM or SSH")) | ||
} | ||
return | ||
} |
143 changes: 143 additions & 0 deletions
143
builtin/providers/aws/resource_aws_iam_user_ssh_key_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/iam" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSUserSSHKey_basic(t *testing.T) { | ||
var conf iam.GetSSHPublicKeyOutput | ||
|
||
ri := acctest.RandInt() | ||
config := fmt.Sprintf(testAccAWSSSHKeyConfig_sshEncoding, ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSUserSSHKeyDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSUserSSHKeyExists("aws_iam_user_ssh_key.user", &conf), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSUserSSHKey_pemEncoding(t *testing.T) { | ||
var conf iam.GetSSHPublicKeyOutput | ||
|
||
ri := acctest.RandInt() | ||
config := fmt.Sprintf(testAccAWSSSHKeyConfig_pemEncoding, ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSUserSSHKeyDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSUserSSHKeyExists("aws_iam_user_ssh_key.user", &conf), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSUserSSHKeyDestroy(s *terraform.State) error { | ||
iamconn := testAccProvider.Meta().(*AWSClient).iamconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_iam_user_ssh_key" { | ||
continue | ||
} | ||
|
||
username := rs.Primary.Attributes["username"] | ||
encoding := rs.Primary.Attributes["encoding"] | ||
_, err := iamconn.GetSSHPublicKey(&iam.GetSSHPublicKeyInput{ | ||
SSHPublicKeyId: aws.String(rs.Primary.ID), | ||
UserName: aws.String(username), | ||
Encoding: aws.String(encoding), | ||
}) | ||
if err == nil { | ||
return fmt.Errorf("still exist.") | ||
} | ||
|
||
// Verify the error is what we want | ||
ec2err, ok := err.(awserr.Error) | ||
if !ok { | ||
return err | ||
} | ||
if ec2err.Code() != "NoSuchEntity" { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckAWSUserSSHKeyExists(n string, res *iam.GetSSHPublicKeyOutput) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No SSHPublicKeyID is set") | ||
} | ||
|
||
iamconn := testAccProvider.Meta().(*AWSClient).iamconn | ||
|
||
username := rs.Primary.Attributes["username"] | ||
encoding := rs.Primary.Attributes["encoding"] | ||
resp, err := iamconn.GetSSHPublicKey(&iam.GetSSHPublicKeyInput{ | ||
SSHPublicKeyId: aws.String(rs.Primary.ID), | ||
UserName: aws.String(username), | ||
Encoding: aws.String(encoding), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*res = *resp | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccAWSSSHKeyConfig_sshEncoding = ` | ||
resource "aws_iam_user" "user" { | ||
name = "test-user-%d" | ||
path = "/" | ||
} | ||
resource "aws_iam_user_ssh_key" "user" { | ||
username = "${aws_iam_user.user.name}" | ||
encoding = "SSH" | ||
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" | ||
} | ||
` | ||
|
||
const testAccAWSSSHKeyConfig_pemEncoding = ` | ||
resource "aws_iam_user" "user" { | ||
name = "test-user-%d" | ||
path = "/" | ||
} | ||
resource "aws_iam_user_ssh_key" "user" { | ||
username = "${aws_iam_user.user.name}" | ||
encoding = "PEM" | ||
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" | ||
} | ||
` |
43 changes: 43 additions & 0 deletions
43
website/source/docs/providers/aws/r/iam_user_ssh_key.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_iam_user_ssh_key" | ||
sidebar_current: "docs-aws-resource-iam-user-ssh-key" | ||
description: |- | ||
Uploads an SSH public key and associates it with the specified IAM user. | ||
--- | ||
|
||
# aws\_iam\_user\_ssh\_key | ||
|
||
Uploads an SSH public key and associates it with the specified IAM user. | ||
|
||
## Example Usage | ||
|
||
``` | ||
resource "aws_iam_user" "user" { | ||
name = "test-user" | ||
path = "/" | ||
} | ||
resource "aws_iam_user_ssh_key" "user" { | ||
username = "${aws_iam_user.user.name}" | ||
encoding = "PEM" | ||
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 mytest@mydomain.com" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `username` - (Required) The name of the IAM user to associate the SSH public key with. | ||
* `encoding` - (Required) Specifies the public key encoding format to use in the response. To retrieve the public key in ssh-rsa format, use SSH . To retrieve the public key in PEM format, use PEM . | ||
* `public_key` - (Required) The SSH public key. The public key must be encoded in ssh-rsa format or PEM format. | ||
* `status` - (Optional) The status to assign to the SSH public key. Active means the key can be used for authentication with an AWS CodeCommit repository. Inactive means the key cannot be used. Default is `active`. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `ssh_public_key_id` - The unique identifier for the SSH public key. | ||
* `fingerprint` - The MD5 message digest of the SSH public key. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters