Skip to content

Commit

Permalink
Add get ssh public keys example
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshidajp committed Jul 7, 2018
1 parent 985214d commit 46db5d3
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
66 changes: 66 additions & 0 deletions go/example_code/iam/iam_getpublickeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/

package main

import (
"fmt"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
)

// Usage:
// go run iam_getpublickeys.go <username>
func main() {
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-west-2")},
)

// Create a IAM service client.
svc := iam.New(sess)

// List SSH public keys.
keysResult, err := svc.ListSSHPublicKeys(&iam.ListSSHPublicKeysInput{
UserName: &os.Args[1],
})

if err != nil {
fmt.Println("Error", err)
return
}

for _, key := range keysResult.SSHPublicKeys {
if key == nil {
continue
}

// Get a SSH public key.
keyResult, err := svc.GetSSHPublicKey(&iam.GetSSHPublicKeyInput{
UserName: &os.Args[1],
SSHPublicKeyId: key.SSHPublicKeyId,
Encoding: aws.String("SSH"),
})

if err != nil {
continue
}

fmt.Printf("%s\n", *keyResult.SSHPublicKey.SSHPublicKeyBody)
}
}
34 changes: 34 additions & 0 deletions python/example_code/iam/get_pub_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

import boto3

user_name = 'your-name'

# Create IAM client
iam = boto3.client('iam')

ssh_public_keys_response = iam.list_ssh_public_keys(
UserName = user_name,
MaxItems = 100,
)

# Get SSH public key
for ssh_public_key in ssh_public_keys_response['SSHPublicKeys']:
ssh_public_key = ssh_public_key['SSHPublicKeyId']
ssh_public_key_response = iam.get_ssh_public_key(
UserName = user_name,
SSHPublicKeyId = ssh_public_key,
Encoding = 'SSH',
)
print(ssh_public_key_response['SSHPublicKey']['SSHPublicKeyBody'])
35 changes: 35 additions & 0 deletions ruby/example_code/iam/iam-ruby-example-get-ssh-public-keys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# This file is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of the
# License is located at
#
# http://aws.amazon.com/apache2.0/
#
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

require 'aws-sdk-iam' # v2: require 'aws-sdk'

user_name = 'your-name'
iam = Aws::IAM::Client.new(region: 'us-west-2')

begin
ssh_public_keys_response = iam.list_ssh_public_keys({
user_name: user_name,
max_items: 10,
})

ssh_public_keys_response.ssh_public_keys.each do |ssh_public_key|
ssh_public_key_response = iam.get_ssh_public_key({
user_name: user_name,
ssh_public_key_id: ssh_public_key.ssh_public_key_id,
encoding: "SSH",
})
puts ssh_public_key_response.ssh_public_key.ssh_public_key_body
end

rescue Aws::IAM::Errors::NoSuchEntity => ex
puts 'User does not exist'
end

0 comments on commit 46db5d3

Please sign in to comment.