forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
985214d
commit 46db5d3
Showing
3 changed files
with
135 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
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) | ||
} | ||
} |
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,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
35
ruby/example_code/iam/iam-ruby-example-get-ssh-public-keys.rb
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,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 |