-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
397fd48
commit 1449799
Showing
3 changed files
with
55 additions
and
18 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 |
---|---|---|
@@ -1,17 +1,37 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX - License - Identifier: Apache - 2.0 | ||
require "aws-sdk-iam" | ||
require "logger" | ||
|
||
# snippet-start:[ruby.example_code.iam.GetAccountPasswordPolicy] | ||
# Prints the password policy for the account. | ||
def print_account_password_policy | ||
policy = @iam_resource.account_password_policy | ||
policy.load | ||
puts("The account password policy is:") | ||
puts(policy.data.to_h) | ||
rescue Aws::Errors::ServiceError => e | ||
if e.code == "NoSuchEntity" | ||
puts("The account does not have a password policy.") | ||
else | ||
puts("Couldn't print the account password policy. Here's why:") | ||
puts("\t#{e.code}: #{e.message}") | ||
raise | ||
# Class to manage IAM account password policies | ||
class PasswordPolicyManager | ||
attr_accessor :iam_client, :logger | ||
|
||
def initialize(iam_client) | ||
@iam_client = iam_client | ||
@logger = Logger.new($stdout) | ||
@logger.progname = "IAMPolicyManager" | ||
end | ||
|
||
# Retrieves and logs the account password policy | ||
def print_account_password_policy | ||
begin | ||
response = @iam_client.get_account_password_policy | ||
@logger.info("The account password policy is: #{response.password_policy.to_h}") | ||
rescue Aws::IAM::Errors::NoSuchEntity | ||
@logger.info("The account does not have a password policy.") | ||
rescue Aws::Errors::ServiceError => e | ||
@logger.error("Couldn't print the account password policy. Error: #{e.code} - #{e.message}") | ||
raise | ||
end | ||
end | ||
end | ||
# snippet-end:[ruby.example_code.iam.GetAccountPasswordPolicy] | ||
|
||
# Example usage: | ||
if $PROGRAM_NAME == __FILE__ | ||
iam_client = Aws::IAM::Client.new | ||
iam_policy_manager = PasswordPolicyManager.new(iam_client) | ||
iam_policy_manager.print_account_password_policy | ||
end |
22 changes: 22 additions & 0 deletions
22
ruby/example_code/iam/spec/get_account_password_policy_spec.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,22 @@ | ||
require "rspec" | ||
require_relative "../get_account_password_policy" | ||
require "aws-sdk-iam" | ||
|
||
describe PasswordPolicyManager do | ||
let(:iam_client) { Aws::IAM::Client.new } | ||
let(:iam_policy_manager) { PasswordPolicyManager.new(iam_client) } | ||
|
||
describe "#print_account_password_policy" do | ||
context "when the account has a password policy" do | ||
it "logs the password policy details" do | ||
expect { iam_policy_manager.print_account_password_policy }.not_to raise_error | ||
end | ||
end | ||
|
||
context "when the account does not have a password policy" do | ||
it "logs a specific message" do | ||
expect { iam_policy_manager.print_account_password_policy }.not_to raise_error | ||
end | ||
end | ||
end | ||
end |