使用 Amazon 开发工具包获取 IAM 账户密码策略 - Amazon Identity and Access Management
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

使用 Amazon 开发工具包获取 IAM 账户密码策略

以下代码示例显示如何获取 IAM 账户密码策略。

.NET
Amazon SDK for .NET
注意

GitHub 上还有更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

using System; using Amazon.IdentityManagement; using Amazon.IdentityManagement.Model; var client = new AmazonIdentityManagementServiceClient(); try { var request = new GetAccountPasswordPolicyRequest(); var response = await client.GetAccountPasswordPolicyAsync(request); Console.WriteLine($"{response.PasswordPolicy}"); } catch (NoSuchEntityException ex) { Console.WriteLine($"Error: {ex.Message}"); }
Go
SDK for Go V2
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

// GetAccountPasswordPolicy fmt.Println("🔐 GetAccountPasswordPolicy") accountPasswordPolicy, err := service.GetAccountPasswordPolicy(context.Background(), &iam.GetAccountPasswordPolicyInput{}) if err != nil { var notexists *types.NoSuchEntityException if errors.As(err, &notexists) { fmt.Println("No password policy") } else { panic("Couldn't get account password policy! " + err.Error()) } } else { fmt.Println("Users can change password: ", accountPasswordPolicy.PasswordPolicy.AllowUsersToChangePassword) fmt.Println("Passwords expire: ", accountPasswordPolicy.PasswordPolicy.ExpirePasswords) fmt.Println("Minimum password length: ", accountPasswordPolicy.PasswordPolicy.MinimumPasswordLength) }
JavaScript
SDK for JavaScript V3
注意

GitHub 上还有更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

创建客户端。

import { IAMClient } from "@aws-sdk/client-iam"; // Set the AWS Region. const REGION = "REGION"; // For example, "us-east-1". // Create an IAM service client object. const iamClient = new IAMClient({ region: REGION }); export { iamClient };

获取账户密码策略。

// Import required AWS SDK clients and commands for Node.js. import { iamClient } from "./libs/iamClient.js"; import { GetAccountPasswordPolicyCommand } from "@aws-sdk/client-iam"; const run = async () => { try { const data = await iamClient.send(new GetAccountPasswordPolicyCommand({})); console.log("Success", data.PasswordPolicy); } catch (err) { console.log("Error", err); } }; run();
PHP
SDK for PHP
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

$uuid = uniqid(); $service = new IamService(); public function getAccountPasswordPolicy() { return $this->iamClient->getAccountPasswordPolicy(); }
Python
适用于 Python (Boto3) 的 SDK
注意

GitHub 上还有更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

def print_password_policy(): """ Prints the password policy for the account. """ try: pw_policy = iam.AccountPasswordPolicy() print("Current account password policy:") print(f"\tallow_users_to_change_password: {pw_policy.allow_users_to_change_password}") print(f"\texpire_passwords: {pw_policy.expire_passwords}") print(f"\thard_expiry: {pw_policy.hard_expiry}") print(f"\tmax_password_age: {pw_policy.max_password_age}") print(f"\tminimum_password_length: {pw_policy.minimum_password_length}") print(f"\tpassword_reuse_prevention: {pw_policy.password_reuse_prevention}") print(f"\trequire_lowercase_characters: {pw_policy.require_lowercase_characters}") print(f"\trequire_numbers: {pw_policy.require_numbers}") print(f"\trequire_symbols: {pw_policy.require_symbols}") print(f"\trequire_uppercase_characters: {pw_policy.require_uppercase_characters}") printed = True except ClientError as error: if error.response['Error']['Code'] == 'NoSuchEntity': print("The account does not have a password policy set.") else: logger.exception("Couldn't get account password policy.") raise else: return printed
Ruby
SDK for Ruby
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

# 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 end end
Rust
SDK for Rust
注意

本文档适用于预览版中的软件开发工具包。软件开发工具包可能随时发生变化,不应在生产环境中使用。

注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

pub async fn get_account_password_policy( client: &iamClient, ) -> Result<GetAccountPasswordPolicyOutput, SdkError<GetAccountPasswordPolicyError>> { let response = client.get_account_password_policy().send().await?; Ok(response) }

有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 IAM 与 Amazon 开发工具包配合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。