この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
コンバンハ、千葉(幸)です。
AWS Config の適合パックは、AWS Config ルールや修復アクションの組み合わせをテンプレートで定義し、一括でデプロイ・管理できる機能です。
裏側では CloudFormation が呼び出されていることもあり、テンプレートは CloudFormation のものと同じような構文で書くことができます。
ただ、それをマネジメントコンソールからデプロイする際には CloudFormation のデプロイの場合とお作法が異なる部分があります。今回はそれについて書きます。
適合パックの詳細は以下をご参照ください。
目次
CloudFormation テンプレートにおけるパラメータ
パラメータを使用することで、テンプレート内の値にカスタム値を設定することができます。
一般的に、具体的な値をテンプレートに埋め込んでしまうと使い回すことが難しくなります。テンプレートのデプロイ時に都度、値をパラメータとして引き渡す方式にすることによって、複数の環境で同じテンプレートを使い回すといったことが可能になります。
例えばごくごくシンプルなものですが、以下のようなテンプレートがあったとします。
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
ProjectName:
Description: Please type the ProjectName.
Type: String
EnvType:
Description: Please type the EnvironmentType.
Type: String
SystemName:
Description: Please type the SystemName.
Type: String
Resources:
S3Bucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
BucketName: !Sub ${ProjectName}-${EnvType}-${SystemName}-s3-bucket
上記ハイライト部でパラメータを定義しており、テンプレートデプロイ時に引き渡した値が 19 行目に適用されます。
このテンプレートを CloudFormation でデプロイしようとした場合、コンソールでは以下のように表示されます。
テンプレートで定義されているパラメータのキーが表示されるため、ユーザーは値を入力するだけで済みます。助かるー。
Config 適合パック テンプレートにおけるパラメータ
同じように、パラメータが定義されている Config 適合パック用のテンプレートをデプロイしてみます。
Config 適合パックのテンプレートはいくつかサンプルが用意されていますが、パラメータが定義されているものとして以下があります。
AWS Identity And Access Management 運用のベストプラクティス - AWS Config
パラメータは以下のように定義されています。それぞれデフォルト値が 90 として設定されています。
Parameters:
AccessKeysRotatedParameterMaxAccessKeyAge:
Description: Maximum number of days without rotation. Default 90.
Type: String
Default: 90
IAMUserUnusedCredentialsCheckParameterMaxCredentialUsageAge:
Description: Maximum number of days a credential cannot be used. The default value
is 90 days.
Type: String
Default: 90
これらのパラメータは、テンプレート内で定義されている Config ルールに引き渡されます。ややこしいですが、Config ルールにおいてもパラメータ(InputParameters)があるので、そこにテンプレートのパラメータの値が設定されることになります。
AccessKeysRotated:
Properties:
ConfigRuleName: AccessKeysRotated
Description: Checks whether the active access keys are rotated within the number
of days specified in maxAccessKeyAge. The rule is non-compliant if the access
keys have not been rotated for more than maxAccessKeyAge number of days.
InputParameters:
maxAccessKeyAge:
Ref: AccessKeysRotatedParameterMaxAccessKeyAge
Source:
Owner: AWS
SourceIdentifier: ACCESS_KEYS_ROTATED
Type: AWS::Config::ConfigRule
IAMUserUnusedCredentialsCheck:
Properties:
ConfigRuleName: IAMUserUnusedCredentialsCheck
Description: Checks whether your AWS Identity and Access Management (IAM) users
have passwords or active access keys that have not been used within the specified
number of days you provided.
InputParameters:
maxCredentialUsageAge:
Ref: IAMUserUnusedCredentialsCheckParameterMaxCredentialUsageAge
Source:
Owner: AWS
SourceIdentifier: IAM_USER_UNUSED_CREDENTIALS_CHECK
Type: AWS::Config::ConfigRule
このテンプレートのデプロイを試みます。
Config の適合パックの画面よりデプロイを選択し……
サンプルテンプレートの中から先ほど確認した IAM 運用のベストプラクティス のテンプレートを選択し、次に進みます。
続いての画面でパラメータを設定できるのですが、 CloudFormation のようにパラメータのキーは表示してくれていません。
そのままデプロイすると……
ひとまずデプロイが完了します。
今回は特にパラメータの値の指定をしなかったので、それを参照する Config ルールのインプットパラメータにはデフォルトの 90 が設定されています。
この値を変更したい場合は、デプロイ時にパラメータのキーと値をそれぞれ手動で入力する必要があります。CloudFormation のように自動的にキーを表示してくれたりはしないので、少し頑張る必要があります。
実は書きたいことは少し違った
完全に裏話なのですが、本来はサンプルテンプレートをパラメータなしでデプロイした際に、エラーが発生する挙動を期待していました。
以前に試したことがあり、その際にはエラーが発生したからです。今回はなぜうまくいったんだ?と疑問だったのですが、どうやらサンプルテンプレートの内容が以前と異なっているからであることがわかりました。
2020/07/30時点で、以下のページに載っているサンプルテンプレートは日本語版と英語版で内容に差異があります。
AWS Identity And Access Management 運用のベストプラクティス - AWS Config
以前は(つまり現時点での日本語版ドキュメントの内容では)、以下ハイライト部のパラメータ 2 つが存在していました。こちらにはデフォルト値の定義はありません。
Parameters:
AccessKeysRotatedParameterMaxAccessKeyAge:
Description: Maximum number of days without rotation. Default 90.
Type: String
Default: 90
IAMPolicyBlacklistedCheckParameterPolicyArns:
Description: Comma separated list of IAM policy arns which should not be attached
to any IAM entity.
Type: String
IAMRoleManagedPolicyCheckParameterManagedPolicyArns:
Description: Comma-separated list of AWS managed policy ARNs.
Type: String
IAMUserUnusedCredentialsCheckParameterMaxCredentialUsageAge:
Description: Maximum number of days a credential cannot be used. The default value
is 90 days.
Type: String
Default: 90
それぞれのパラメータを参照するリソースも存在しました。
IAMPolicyBlacklistedCheck:
Properties:
ConfigRuleName: IAMPolicyBlacklistedCheck
Description: Checks that none of your IAM users, groups, or roles (excluding
exceptionList) have the specified policies attached.
InputParameters:
policyArns:
Ref: IAMPolicyBlacklistedCheckParameterPolicyArns
Source:
Owner: AWS
SourceIdentifier: IAM_POLICY_BLACKLISTED_CHECK
Type: AWS::Config::ConfigRule
IAMRoleManagedPolicyCheck:
Properties:
ConfigRuleName: IAMRoleManagedPolicyCheck
Description: Checks that the AWS Identity and Access Management (IAM) role is
attached to all AWS managed policies specified in the list of managed policies.
The rule is non-compliant if the IAM role is not attached to the AWS managed
policy.
InputParameters:
managedPolicyArns:
Ref: IAMRoleManagedPolicyCheckParameterManagedPolicyArns
Source:
Owner: AWS
SourceIdentifier: IAM_ROLE_MANAGED_POLICY_CHECK
Type: AWS::Config::ConfigRule
これらが存在するテンプレートの場合、パラメータにはデフォルト値が設定されていないため、デプロイ時に明示的に追加しない限り値がブランクになります。
以下は現時点の日本語版なのですが、これを手元で YAML ファイルとして保存し、アップロードしてデプロイしてみました。
折り畳み
################################################################################
#
# Conformance Pack:
# Operational Best Practices for AWS Identity and Access Management
#
# See Parameters section for names and descriptions of required parameters.
#
################################################################################
Parameters:
AccessKeysRotatedParameterMaxAccessKeyAge:
Description: Maximum number of days without rotation. Default 90.
Type: String
Default: 90
IAMPolicyBlacklistedCheckParameterPolicyArns:
Description: Comma separated list of IAM policy arns which should not be attached
to any IAM entity.
Type: String
IAMRoleManagedPolicyCheckParameterManagedPolicyArns:
Description: Comma-separated list of AWS managed policy ARNs.
Type: String
IAMUserUnusedCredentialsCheckParameterMaxCredentialUsageAge:
Description: Maximum number of days a credential cannot be used. The default value
is 90 days.
Type: String
Default: 90
Resources:
AccessKeysRotated:
Properties:
ConfigRuleName: AccessKeysRotated
Description: Checks whether the active access keys are rotated within the number
of days specified in maxAccessKeyAge. The rule is non-compliant if the access
keys have not been rotated for more than maxAccessKeyAge number of days.
InputParameters:
maxAccessKeyAge:
Ref: AccessKeysRotatedParameterMaxAccessKeyAge
Source:
Owner: AWS
SourceIdentifier: ACCESS_KEYS_ROTATED
Type: AWS::Config::ConfigRule
IAMGroupHasUsersCheck:
Properties:
ConfigRuleName: IAMGroupHasUsersCheck
Description: Checks whether IAM groups have at least one IAM user.
Source:
Owner: AWS
SourceIdentifier: IAM_GROUP_HAS_USERS_CHECK
Type: AWS::Config::ConfigRule
IAMPasswordPolicy:
Properties:
ConfigRuleName: IAMPasswordPolicy
Description: Checks whether the account password policy for IAM users meets
the specified requirements.
Source:
Owner: AWS
SourceIdentifier: IAM_PASSWORD_POLICY
Type: AWS::Config::ConfigRule
IAMPolicyBlacklistedCheck:
Properties:
ConfigRuleName: IAMPolicyBlacklistedCheck
Description: Checks that none of your IAM users, groups, or roles (excluding
exceptionList) have the specified policies attached.
InputParameters:
policyArns:
Ref: IAMPolicyBlacklistedCheckParameterPolicyArns
Source:
Owner: AWS
SourceIdentifier: IAM_POLICY_BLACKLISTED_CHECK
Type: AWS::Config::ConfigRule
IAMPolicyNoStatementsWithAdminAccess:
Properties:
ConfigRuleName: IAMPolicyNoStatementsWithAdminAccess
Description: 'Checks whether the default version of AWS Identity and Access
Management (IAM) policies do not have administrator access. If any statement
has "Effect": "Allow" with "Action": "*" over "Resource": "*", the rule is
non-compliant.'
Source:
Owner: AWS
SourceIdentifier: IAM_POLICY_NO_STATEMENTS_WITH_ADMIN_ACCESS
Type: AWS::Config::ConfigRule
IAMRoleManagedPolicyCheck:
Properties:
ConfigRuleName: IAMRoleManagedPolicyCheck
Description: Checks that the AWS Identity and Access Management (IAM) role is
attached to all AWS managed policies specified in the list of managed policies.
The rule is non-compliant if the IAM role is not attached to the AWS managed
policy.
InputParameters:
managedPolicyArns:
Ref: IAMRoleManagedPolicyCheckParameterManagedPolicyArns
Source:
Owner: AWS
SourceIdentifier: IAM_ROLE_MANAGED_POLICY_CHECK
Type: AWS::Config::ConfigRule
IAMRootAccessKeyCheck:
Properties:
ConfigRuleName: IAMRootAccessKeyCheck
Description: Checks whether the root user access key is available. The rule
is compliant if the user access key does not exist.
Source:
Owner: AWS
SourceIdentifier: IAM_ROOT_ACCESS_KEY_CHECK
Type: AWS::Config::ConfigRule
IAMUserGroupMembershipCheck:
Properties:
ConfigRuleName: IAMUserGroupMembershipCheck
Description: Checks whether IAM users are members of at least one IAM group.
Source:
Owner: AWS
SourceIdentifier: IAM_USER_GROUP_MEMBERSHIP_CHECK
Type: AWS::Config::ConfigRule
IAMUserMFAEnabled:
Properties:
ConfigRuleName: IAMUserMFAEnabled
Description: Checks whether the AWS Identity and Access Management users have
multi-factor authentication (MFA) enabled.
Source:
Owner: AWS
SourceIdentifier: IAM_USER_MFA_ENABLED
Type: AWS::Config::ConfigRule
IAMUserNoPoliciesCheck:
Properties:
ConfigRuleName: IAMUserNoPoliciesCheck
Description: Checks that none of your IAM users have policies attached. IAM
users must inherit permissions from IAM groups or roles.
Source:
Owner: AWS
SourceIdentifier: IAM_USER_NO_POLICIES_CHECK
Type: AWS::Config::ConfigRule
IAMUserUnusedCredentialsCheck:
Properties:
ConfigRuleName: IAMUserUnusedCredentialsCheck
Description: Checks whether your AWS Identity and Access Management (IAM) users
have passwords or active access keys that have not been used within the specified
number of days you provided.
InputParameters:
maxCredentialUsageAge:
Ref: IAMUserUnusedCredentialsCheckParameterMaxCredentialUsageAge
Source:
Owner: AWS
SourceIdentifier: IAM_USER_UNUSED_CREDENTIALS_CHECK
Type: AWS::Config::ConfigRule
MFAEnabledForIAMConsoleAccess:
Properties:
ConfigRuleName: MFAEnabledForIAMConsoleAccess
Description: Checks whether AWS Multi-Factor Authentication (MFA) is enabled
for all AWS Identity and Access Management (IAM) users that use a console
password. The rule is compliant if MFA is enabled.
Source:
Owner: AWS
SourceIdentifier: MFA_ENABLED_FOR_IAM_CONSOLE_ACCESS
Type: AWS::Config::ConfigRule
RootAccountHardwareMFAEnabled:
Properties:
ConfigRuleName: RootAccountHardwareMFAEnabled
Description: Checks whether your AWS account is enabled to use multi-factor
authentication (MFA) hardware device to sign in with root credentials.
Source:
Owner: AWS
SourceIdentifier: ROOT_ACCOUNT_HARDWARE_MFA_ENABLED
Type: AWS::Config::ConfigRule
RootAccountMFAEnabled:
Properties:
ConfigRuleName: RootAccountMFAEnabled
Description: Checks whether the root user of your AWS account requires multi-factor
authentication for console sign-in.
Source:
Owner: AWS
SourceIdentifier: ROOT_ACCOUNT_MFA_ENABLED
Type: AWS::Config::ConfigRule
実行してみると、想定どおりデプロイに失敗します。
Parameters: IAMPolicyBlacklistedCheckParameterPolicyArns, IAMRoleManagedPolicyCheckParameterManagedPolicyArns must have values (Service: AmazonCloudFormation; Status Code: 400; Error Code: ValidationError; Request ID: fxxxxxb-e419-4c11-b20d-09xxxxxxxx33; Proxy: null)
パラメータには値を持たせる必要がある、というエラーです。そのため、明示的にキーと値(ここでは IAM ポリシーの ARN )を入力してデプロイする必要があります。
(この辺り、何かしらパラメータを入力する必要があるという部分のハードルが高く、サンプルテンプレートから除外されたのではと想像しています。)
終わりに
適合パックデプロイ時のパラメータの引き渡しについて確認しました。どのようなパラメータが定義されているかを、デプロイ時にマネジメントコンソールから確認できないのはちょっとつらみがありますね。
ぜひ、表示してくれるようなアップデートを期待しています!
以上、千葉(幸)がお送りしました。