Deploy Password Policy
Application Scenario
Identity and Access Management (IAM) is a basic identity authentication and access management service provided by Huawei Cloud, providing core functions such as identity management, permission management, and access control for Huawei Cloud users. By configuring password policies, you can set security policies such as password complexity requirements, validity periods, reuse rules, etc., improving account security and meeting enterprise-level security compliance requirements. This best practice will introduce how to use Terraform to automatically deploy IAM password policies, including configuration of security policies such as password length, character combination, validity period, reuse rules, etc.
Related Resources/Data Sources
This best practice involves the following main resources:
Resources
Resource/Data Source Dependencies
huaweicloud_identityv5_password_policyNote: IAM password policy resource is a global resource used to configure password security policies for IAM accounts. After password policy is configured, it will apply to all IAM users.
Operation Steps
1. Script Preparation
Prepare the TF file (e.g., main.tf) in the specified workspace for writing the current best practice script, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. Refer to the "Preparation Before Deploying Huawei Cloud Resources" document for configuration introduction.
2. Create IAM Password Policy Resource
Add the following script to the TF file (e.g., main.tf) to create IAM password policy:
Parameter Description:
maximum_consecutive_identical_chars: Maximum number of times that a character is allowed to consecutively present in a password, assigned by referencing input variable policy_max_consecutive_identical_chars, valid range is 0-32
minimum_password_age: Minimum password age (minutes), assigned by referencing input variable policy_min_password_age, valid range is 0-1440
minimum_password_length: Minimum password length, assigned by referencing input variable policy_min_password_length, valid range is 8-32
password_reuse_prevention: Password reuse prevention, assigned by referencing input variable policy_password_reuse_prevention, valid range is 0-24, default value is 3
password_not_username_or_invert: Password cannot be username or username spelled backwards, assigned by referencing input variable policy_password_not_username_or_invert, default value is false
password_validity_period: Password validity period (days), assigned by referencing input variable policy_password_validity_period, valid range is 0-180, default value is 7
password_char_combination: Minimum number of character types that a password must contain, assigned by referencing input variable policy_password_char_combination, valid range is 2-4
allow_user_to_change_password: Whether IAM users are allowed to change their own passwords, assigned by referencing input variable policy_allow_user_to_change_password, default value is true
Note: IAM password policy is a global resource, and after configuration, it will apply to all IAM users. Password policy parameters all have value range restrictions. Please configure reasonably according to actual security requirements. It is recommended to follow the principle of least privilege and set reasonable password complexity requirements to improve account security.
3. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources use input variables to assign configuration content. These input parameters need to be manually entered during subsequent deployment. At the same time, Terraform provides a method to preset these configurations through tfvars files, which can avoid repeated input during each execution.
Create a terraform.tfvars file in the working directory with the following example content:
Usage:
Save the above content as a
terraform.tfvarsfile in the working directory (this filename allows users to automatically import the content of thistfvarsfile when executing terraform commands. For other naming, you need to add.autobefore tfvars, such asvariables.auto.tfvars)Modify parameter values according to actual needs, especially:
policy_max_consecutive_identical_charscan be set to the maximum number of times that a character is allowed to consecutively present in a password, recommended to set to 2 or smallerpolicy_min_password_agecan be set to the minimum password age (minutes), recommended to set to 60 minutes or longerpolicy_min_password_lengthcan be set to the minimum password length, recommended to set to 8 or longerpolicy_password_reuse_preventioncan be set to password reuse prevention, recommended to set to 3 or largerpolicy_password_not_username_or_invertcan be set to true to prohibit password from being username or username spelled backwardspolicy_password_validity_periodcan be set to password validity period (days), recommended to set to 30-90 dayspolicy_password_char_combinationcan be set to the minimum number of character types that a password must contain, recommended to set to 2 or largerpolicy_allow_user_to_change_passwordcan be set to true to allow IAM users to change their own passwords
When executing
terraform planorterraform apply, Terraform will automatically read the variable values in this file
In addition to using the terraform.tfvars file, you can also set variable values in the following ways:
Command line parameters:
terraform apply -var="policy_min_password_length=12" -var="policy_password_char_combination=3"Environment variables:
export TF_VAR_policy_min_password_length=12andexport TF_VAR_policy_password_char_combination=3Custom named variable file:
terraform apply -var-file="custom.tfvars"
Note: If the same variable is set through multiple methods, Terraform will use variable values according to the following priority: command line parameters > variable file > environment variables > default values. After IAM password policy is configured, it will apply to all IAM users. Please configure password policy parameters reasonably according to actual security requirements.
4. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create IAM password policy:
Run
terraform initto initialize the environmentRun
terraform planto view the resource creation planAfter confirming that the resource plan is correct, run
terraform applyto start creating IAM password policyRun
terraform showto view the details of the created IAM password policy
Note: IAM password policy is a global resource, and after configuration, it will apply to all IAM users. Password policy parameters all have value range restrictions. Please ensure parameter values are within the valid range. It is recommended to understand the current password usage of IAM users before configuring password policy to avoid overly strict policies that prevent users from normal use.
Reference Information
Last updated