IAM Access Analyzer 範例使用 AWS CLI - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

IAM Access Analyzer 範例使用 AWS CLI

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 IAM Access Analyzer 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 apply-archive-rule

AWS CLI

將封存規則套用至符合封存規則條件的現有調查結果

下列apply-archive-rule範例會將封存規則套用至符合封存規則條件的現有調查結果。

aws accessanalyzer apply-archive-rule \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/UnusedAccess-ConsoleAnalyzer-organization \ --rule-name MyArchiveRule

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IAM 使用者指南中的封存規則

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ApplyArchiveRule

下列程式碼範例示範如何使用 cancel-policy-generation

AWS CLI

若要取消請求的政策產生

下列cancel-policy-generation範例會取消請求的政策產生任務 ID。

aws accessanalyzer cancel-policy-generation \ --job-id 923a56b0-ebb8-4e80-8a3c-a11ccfbcd6f2

此命令不會產生輸出。

如需詳細資訊,請參閱 IAM 使用者指南中的 Word Access Analyzer 政策產生AWS IAM

下列程式碼範例示範如何使用 check-access-not-granted

AWS CLI

檢查政策是否不允許指定的存取

下列check-access-not-granted範例會檢查政策是否不允許指定的存取。

aws accessanalyzer check-access-not-granted \ --policy-document file://myfile.json \ --access actions="s3:DeleteBucket","s3:GetBucketLocation" \ --policy-type IDENTITY_POLICY

myfile.json 的內容:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::DOC-EXAMPLE-BUCKET", "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*" ] } ] }

輸出:

{ "result": "PASS", "message": "The policy document does not grant access to perform one or more of the listed actions." }

如需詳細資訊,請參閱 IAM 使用者指南中的使用 APIs Access Analyzer Word 預覽存取權AWS IAM

下列程式碼範例示範如何使用 check-no-new-access

AWS CLI

檢查與現有政策相比,更新的政策是否允許新存取

下列check-no-new-access範例會檢查與現有政策相比,更新的政策是否允許新存取。

aws accessanalyzer check-no-new-access \ --existing-policy-document file://existing-policy.json \ --new-policy-document file://new-policy.json \ --policy-type IDENTITY_POLICY

existing-policy.json 的內容:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::DOC-EXAMPLE-BUCKET", "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*" ] } ] }

new-policy.json 的內容:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:GetObjectAcl", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::DOC-EXAMPLE-BUCKET", "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*" ] } ] }

輸出:

{ "result": "FAIL", "message": "The modified permissions grant new access compared to your existing policy.", "reasons": [ { "description": "New access in the statement with index: 0.", "statementIndex": 0 } ] }

如需詳細資訊,請參閱 IAM 使用者指南中的使用 APIs Access Analyzer Word 預覽存取權AWS IAM

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CheckNoNewAccess

下列程式碼範例示範如何使用 check-no-public-access

AWS CLI

檢查資源政策是否可以授予指定資源類型的公開存取權

下列check-no-public-access範例會檢查資源政策是否可以授予指定資源類型的公有存取權。

aws accessanalyzer check-no-public-access \ --policy-document file://check-no-public-access-myfile.json \ --resource-type AWS::S3::Bucket

myfile.json 的內容:

{ "Version": "2012-10-17", "Statement": [ { "Sid": "CheckNoPublicAccess", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::111122223333:user/JohnDoe" }, "Action": [ "s3:GetObject" ] } ] }

輸出:

{ "result": "PASS", "message": "The resource policy does not grant public access for the given resource type." }

如需詳細資訊,請參閱 IAM 使用者指南中的使用 APIs Access Analyzer Word 預覽存取權AWS IAM

下列程式碼範例示範如何使用 create-access-preview

AWS CLI

若要建立存取預覽,可讓您在部署資源許可之前預覽資源的 IAM Access Analyzer 調查結果

下列create-access-preview範例會建立存取預覽,可讓您在 AWS 帳戶中部署資源許可之前,先預覽資源的 IAM Access Analyzer 調查結果。

aws accessanalyzer create-access-preview \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account \ --configurations file://myfile.json

myfile.json 的內容:

{ "arn:aws:s3:::DOC-EXAMPLE-BUCKET": { "s3Bucket": { "bucketPolicy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"arn:aws:iam::111122223333:root\"]},\"Action\":[\"s3:PutObject\",\"s3:PutObjectAcl\"],\"Resource\":\"arn:aws:s3:::DOC-EXAMPLE-BUCKET/*\"}]}", "bucketPublicAccessBlock": { "ignorePublicAcls": true, "restrictPublicBuckets": true }, "bucketAclGrants": [ { "grantee": { "id": "79a59df900b949e55d96a1e698fbacedfd6e09d98eacf8f8d5218e7cd47ef2be" }, "permission": "READ" } ] } } }

輸出:

{ "id": "3c65eb13-6ef9-4629-8919-a32043619e6b" }

如需詳細資訊,請參閱 IAM 使用者指南中的使用 APIs Access Analyzer Word 預覽存取權AWS IAM

下列程式碼範例示範如何使用 create-analyzer

AWS CLI

若要建立分析器

下列create-analyzer範例會在您的帳戶中建立分析器 AWS 。

aws accessanalyzer create-analyzer \ --analyzer-name example \ --type ACCOUNT

輸出:

{ "arn": "arn:aws:access-analyzer:us-east-2:111122223333:analyzer/example" }

如需詳細資訊,請參閱 Word 使用者指南中的 AWS Identity and Access Management Access Analyzer 調查結果入門AWS IAM

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateAnalyzer

下列程式碼範例示範如何使用 create-archive-rule

AWS CLI

為指定的分析器建立封存規則

下列create-archive-rule範例會為 AWS 帳戶中指定的分析器建立封存規則。

aws accessanalyzer create-archive-rule \ --analyzer-name UnusedAccess-ConsoleAnalyzer-organization \ --rule-name MyRule \ --filter '{"resource": {"contains": ["Cognito"]}, "resourceType": {"eq": ["AWS::IAM::Role"]}}'

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IAM 使用者指南中的封存規則

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateArchiveRule

下列程式碼範例示範如何使用 delete-analyzer

AWS CLI

若要刪除指定的分析器

下列delete-analyzer範例會刪除您 AWS 帳戶中指定的分析器。

aws accessanalyzer delete-analyzer \ --analyzer-name example

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IAM 使用者指南中的封存規則

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteAnalyzer

下列程式碼範例示範如何使用 delete-archive-rule

AWS CLI

若要刪除指定的封存規則

下列delete-archive-rule範例會刪除您 AWS 帳戶中指定的封存規則。

aws accessanalyzer delete-archive-rule \ --analyzer-name UnusedAccess-ConsoleAnalyzer-organization \ --rule-name MyRule

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IAM 使用者指南中的封存規則

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteArchiveRule

下列程式碼範例示範如何使用 get-access-preview

AWS CLI

擷取指定分析器存取預覽的相關資訊

下列get-access-preview範例會擷取您 AWS 帳戶中指定分析器存取預覽的相關資訊。

aws accessanalyzer get-access-preview \ --access-preview-id 3c65eb13-6ef9-4629-8919-a32043619e6b \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account

輸出:

{ "accessPreview": { "id": "3c65eb13-6ef9-4629-8919-a32043619e6b", "analyzerArn": "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account", "configurations": { "arn:aws:s3:::DOC-EXAMPLE-BUCKET": { "s3Bucket": { "bucketPolicy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"arn:aws:iam::111122223333:root\"]},\"Action\":[\"s3:PutObject\",\"s3:PutObjectAcl\"],\"Resource\":\"arn:aws:s3:::DOC-EXAMPLE-BUCKET/*\"}]}", "bucketAclGrants": [ { "permission": "READ", "grantee": { "id": "79a59df900b949e55d96a1e698fbacedfd6e09d98eacf8f8d5218e7cd47ef2be" } } ], "bucketPublicAccessBlock": { "ignorePublicAcls": true, "restrictPublicBuckets": true } } } }, "createdAt": "2024-02-17T00:18:44+00:00", "status": "COMPLETED" } }

如需詳細資訊,請參閱 IAM 使用者指南中的使用 APIs Access Analyzer Word 預覽存取權AWS IAM

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetAccessPreview

下列程式碼範例示範如何使用 get-analyzed-resource

AWS CLI

擷取已分析資源的相關資訊

下列get-analyzed-resource範例會擷取您 AWS 帳戶中已分析之資源的相關資訊。

aws accessanalyzer get-analyzed-resource \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account \ --resource-arn arn:aws:s3:::DOC-EXAMPLE-BUCKET

輸出:

{ "resource": { "analyzedAt": "2024-02-15T18:01:53.002000+00:00", "isPublic": false, "resourceArn": "arn:aws:s3:::DOC-EXAMPLE-BUCKET", "resourceOwnerAccount": "111122223333", "resourceType": "AWS::S3::Bucket" } }

如需詳細資訊,請參閱 AWS IAM 使用者指南中的使用 AWS Identity and Access Management Access Analyzer

下列程式碼範例示範如何使用 get-analyzer

AWS CLI

擷取指定分析器的相關資訊

下列get-analyzer範例會擷取您 AWS 帳戶中指定分析器的相關資訊。

aws accessanalyzer get-analyzer \ --analyzer-name ConsoleAnalyzer-account

輸出:

{ "analyzer": { "arn": "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account", "createdAt": "2019-12-03T07:28:17+00:00", "lastResourceAnalyzed": "arn:aws:sns:us-west-2:111122223333:config-topic", "lastResourceAnalyzedAt": "2024-02-15T18:01:53.003000+00:00", "name": "ConsoleAnalyzer-account", "status": "ACTIVE", "tags": { "auto-delete": "no" }, "type": "ACCOUNT" } }

如需詳細資訊,請參閱 AWS IAM 使用者指南中的使用 AWS Identity and Access Management Access Analyzer

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetAnalyzer

下列程式碼範例示範如何使用 get-archive-rule

AWS CLI

擷取封存規則的相關資訊

下列get-archive-rule範例會擷取您 AWS 帳戶中封存規則的相關資訊。

aws accessanalyzer get-archive-rule \ --analyzer-name UnusedAccess-ConsoleAnalyzer-organization \ --rule-name MyArchiveRule

輸出:

{ "archiveRule": { "createdAt": "2024-02-15T00:49:27+00:00", "filter": { "resource": { "contains": [ "Cognito" ] }, "resourceType": { "eq": [ "AWS::IAM::Role" ] } }, "ruleName": "MyArchiveRule", "updatedAt": "2024-02-15T00:49:27+00:00" } }

如需詳細資訊,請參閱 AWS IAM 使用者指南中的封存規則

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetArchiveRule

下列程式碼範例示範如何使用 get-finding-v2

AWS CLI

擷取指定調查結果的相關資訊

下列get-finding-v2範例會擷取您 AWS 帳戶中指定調查結果的相關資訊。

aws accessanalyzer get-finding-v2 \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-organization \ --id 0910eedb-381e-4e95-adda-0d25c19e6e90

輸出:

{ "findingDetails": [ { "externalAccessDetails": { "action": [ "sts:AssumeRoleWithWebIdentity" ], "condition": { "cognito-identity.amazonaws.com:aud": "us-west-2:EXAMPLE0-0000-0000-0000-000000000000" }, "isPublic": false, "principal": { "Federated": "cognito-identity.amazonaws.com" } } } ], "resource": "arn:aws:iam::111122223333:role/Cognito_testpoolAuth_Role", "status": "ACTIVE", "error": null, "createdAt": "2021-02-26T21:17:50.905000+00:00", "resourceType": "AWS::IAM::Role", "findingType": "ExternalAccess", "resourceOwnerAccount": "111122223333", "analyzedAt": "2024-02-16T18:17:47.888000+00:00", "id": "0910eedb-381e-4e95-adda-0d25c19e6e90", "updatedAt": "2021-02-26T21:17:50.905000+00:00" }

如需詳細資訊,請參閱 AWS IAM 使用者指南中的檢閱調查結果

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetFindingV2

下列程式碼範例示範如何使用 get-finding

AWS CLI

擷取指定調查結果的相關資訊

下列get-finding範例會擷取您 AWS 帳戶中指定調查結果的相關資訊。

aws accessanalyzer get-finding \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-organization \ --id 0910eedb-381e-4e95-adda-0d25c19e6e90

輸出:

{ "finding": { "id": "0910eedb-381e-4e95-adda-0d25c19e6e90", "principal": { "Federated": "cognito-identity.amazonaws.com" }, "action": [ "sts:AssumeRoleWithWebIdentity" ], "resource": "arn:aws:iam::111122223333:role/Cognito_testpoolAuth_Role", "isPublic": false, "resourceType": "AWS::IAM::Role", "condition": { "cognito-identity.amazonaws.com:aud": "us-west-2:EXAMPLE0-0000-0000-0000-000000000000" }, "createdAt": "2021-02-26T21:17:50.905000+00:00", "analyzedAt": "2024-02-16T18:17:47.888000+00:00", "updatedAt": "2021-02-26T21:17:50.905000+00:00", "status": "ACTIVE", "resourceOwnerAccount": "111122223333" } }

如需詳細資訊,請參閱 AWS IAM 使用者指南中的檢閱調查結果

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetFinding

下列程式碼範例示範如何使用 get-generated-policy

AWS CLI

擷取使用 `StartPolicyGeneration` API 產生的政策

下列get-generated-policy範例會擷取使用 AWS 帳戶中的 StartPolicyGeneration API Word產生的政策。

aws accessanalyzer get-generated-policy \ --job-id c557dc4a-0338-4489-95dd-739014860ff9

輸出:

{ "generatedPolicyResult": { "generatedPolicies": [ { "policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"SupportedServiceSid0\",\"Effect\":\"Allow\",\"Action\":[\"access-analyzer:GetAnalyzer\",\"access-analyzer:ListAnalyzers\",\"access-analyzer:ListArchiveRules\",\"access-analyzer:ListFindings\",\"cloudtrail:DescribeTrails\",\"cloudtrail:GetEventDataStore\",\"cloudtrail:GetEventSelectors\",\"cloudtrail:GetInsightSelectors\",\"cloudtrail:GetTrailStatus\",\"cloudtrail:ListChannels\",\"cloudtrail:ListEventDataStores\",\"cloudtrail:ListQueries\",\"cloudtrail:ListTags\",\"cloudtrail:LookupEvents\",\"ec2:DescribeRegions\",\"iam:GetAccountSummary\",\"iam:GetOpenIDConnectProvider\",\"iam:GetRole\",\"iam:ListAccessKeys\",\"iam:ListAccountAliases\",\"iam:ListOpenIDConnectProviders\",\"iam:ListRoles\",\"iam:ListSAMLProviders\",\"kms:ListAliases\",\"s3:GetBucketLocation\",\"s3:ListAllMyBuckets\"],\"Resource\":\"*\"}]}" } ], "properties": { "cloudTrailProperties": { "endTime": "2024-02-14T22:44:40+00:00", "startTime": "2024-02-13T00:30:00+00:00", "trailProperties": [ { "allRegions": true, "cloudTrailArn": "arn:aws:cloudtrail:us-west-2:111122223333:trail/my-trail", "regions": [] } ] }, "isComplete": false, "principalArn": "arn:aws:iam::111122223333:role/Admin" } }, "jobDetails": { "completedOn": "2024-02-14T22:47:01+00:00", "jobId": "c557dc4a-0338-4489-95dd-739014860ff9", "startedOn": "2024-02-14T22:44:41+00:00", "status": "SUCCEEDED" } }

如需詳細資訊,請參閱 IAM 使用者指南中的 Word Access Analyzer 政策產生AWS IAM

下列程式碼範例示範如何使用 list-access-preview-findings

AWS CLI

擷取指定存取預覽所產生的存取預覽調查結果清單

下列list-access-preview-findings範例會擷取您 AWS 帳戶中指定存取預覽所產生的存取預覽調查結果清單。

aws accessanalyzer list-access-preview-findings \ --access-preview-id 3c65eb13-6ef9-4629-8919-a32043619e6b \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account

輸出:

{ "findings": [ { "id": "e22fc158-1c87-4c32-9464-e7f405ce8d74", "principal": { "AWS": "111122223333" }, "action": [ "s3:PutObject", "s3:PutObjectAcl" ], "condition": {}, "resource": "arn:aws:s3:::DOC-EXAMPLE-BUCKET", "isPublic": false, "resourceType": "AWS::S3::Bucket", "createdAt": "2024-02-17T00:18:46+00:00", "changeType": "NEW", "status": "ACTIVE", "resourceOwnerAccount": "111122223333", "sources": [ { "type": "POLICY" } ] } ] }

如需詳細資訊,請參閱 IAM 使用者指南APIs中的使用 Word Access Analyzer Word 預覽存取權AWS IAM

下列程式碼範例示範如何使用 list-access-previews

AWS CLI

擷取指定分析器的存取預覽清單

下列list-access-previews範例會擷取您 AWS 帳戶中指定分析器的存取預覽清單。

aws accessanalyzer list-access-previews \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account

輸出:

{ "accessPreviews": [ { "id": "3c65eb13-6ef9-4629-8919-a32043619e6b", "analyzerArn": "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account", "createdAt": "2024-02-17T00:18:44+00:00", "status": "COMPLETED" } ] }

如需詳細資訊,請參閱 IAM 使用者指南中的使用 APIs Access Analyzer Word 預覽存取權AWS IAM

下列程式碼範例示範如何使用 list-analyzed-resources

AWS CLI

列出可用的小工具

下列list-analyzed-resources範例列出您 AWS 帳戶中可用的小工具。

aws accessanalyzer list-analyzed-resources \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account \ --resource-type AWS::IAM::Role

輸出:

{ "analyzedResources": [ { "resourceArn": "arn:aws:sns:us-west-2:111122223333:Validation-Email", "resourceOwnerAccount": "111122223333", "resourceType": "AWS::SNS::Topic" }, { "resourceArn": "arn:aws:sns:us-west-2:111122223333:admin-alerts", "resourceOwnerAccount": "111122223333", "resourceType": "AWS::SNS::Topic" }, { "resourceArn": "arn:aws:sns:us-west-2:111122223333:config-topic", "resourceOwnerAccount": "111122223333", "resourceType": "AWS::SNS::Topic" }, { "resourceArn": "arn:aws:sns:us-west-2:111122223333:inspector-topic", "resourceOwnerAccount": "111122223333", "resourceType": "AWS::SNS::Topic" } ] }

如需詳細資訊,請參閱 AWS IAM 使用者指南中的使用 AWS Identity and Access Management Access Analyzer

下列程式碼範例示範如何使用 list-analyzers

AWS CLI

擷取分析器清單

下列list-analyzers範例會擷取您 AWS 帳戶中的分析器清單。

aws accessanalyzer list-analyzers

輸出:

{ "analyzers": [ { "arn": "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/UnusedAccess-ConsoleAnalyzer-organization", "createdAt": "2024-02-15T00:46:40+00:00", "name": "UnusedAccess-ConsoleAnalyzer-organization", "status": "ACTIVE", "tags": { "auto-delete": "no" }, "type": "ORGANIZATION_UNUSED_ACCESS" }, { "arn": "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-organization", "createdAt": "2020-04-25T07:43:28+00:00", "lastResourceAnalyzed": "arn:aws:s3:::DOC-EXAMPLE-BUCKET", "lastResourceAnalyzedAt": "2024-02-15T21:51:56.517000+00:00", "name": "ConsoleAnalyzer-organization", "status": "ACTIVE", "tags": { "auto-delete": "no" }, "type": "ORGANIZATION" }, { "arn": "arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account", "createdAt": "2019-12-03T07:28:17+00:00", "lastResourceAnalyzed": "arn:aws:sns:us-west-2:111122223333:config-topic", "lastResourceAnalyzedAt": "2024-02-15T18:01:53.003000+00:00", "name": "ConsoleAnalyzer-account", "status": "ACTIVE", "tags": { "auto-delete": "no" }, "type": "ACCOUNT" } ] }

如需詳細資訊,請參閱 AWS IAM 使用者指南中的使用 AWS Identity and Access Management Access Analyzer

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListAnalyzers

下列程式碼範例示範如何使用 list-archive-rules

AWS CLI

擷取為指定分析器建立的封存規則清單

下列list-archive-rules範例會擷取您 AWS 帳戶中指定分析器所建立的封存規則清單。

aws accessanalyzer list-archive-rules \ --analyzer-name UnusedAccess-ConsoleAnalyzer-organization

輸出:

{ "archiveRules": [ { "createdAt": "2024-02-15T00:49:27+00:00", "filter": { "resource": { "contains": [ "Cognito" ] }, "resourceType": { "eq": [ "AWS::IAM::Role" ] } }, "ruleName": "MyArchiveRule", "updatedAt": "2024-02-15T00:49:27+00:00" }, { "createdAt": "2024-02-15T23:27:45+00:00", "filter": { "findingType": { "eq": [ "UnusedIAMUserAccessKey" ] } }, "ruleName": "ArchiveRule-56125a39-e517-4ff8-afb1-ef06f58db612", "updatedAt": "2024-02-15T23:27:45+00:00" } ] }

如需詳細資訊,請參閱 AWS IAM 使用者指南中的使用 AWS Identity and Access Management Access Analyzer

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListArchiveRules

下列程式碼範例示範如何使用 list-findings-v2

AWS CLI

擷取指定分析器產生的調查結果清單

下列list-findings-v2範例會擷取您 AWS 帳戶中指定分析器產生的調查結果清單。此範例會篩選結果,以僅包含名稱包含 的 IAM 角色Cognito

aws accessanalyzer list-findings-v2 \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account \ --filter '{"resource": {"contains": ["Cognito"]}, "resourceType": {"eq": ["AWS::IAM::Role"]}}'

輸出:

{ "findings": [ { "analyzedAt": "2024-02-16T18:17:47.888000+00:00", "createdAt": "2021-02-26T21:17:24.710000+00:00", "id": "597f3bc2-3adc-4c18-9879-5c4b23485e46", "resource": "arn:aws:iam::111122223333:role/Cognito_testpoolUnauth_Role", "resourceType": "AWS::IAM::Role", "resourceOwnerAccount": "111122223333", "status": "ACTIVE", "updatedAt": "2021-02-26T21:17:24.710000+00:00", "findingType": "ExternalAccess" }, { "analyzedAt": "2024-02-16T18:17:47.888000+00:00", "createdAt": "2021-02-26T21:17:50.905000+00:00", "id": "ce0e221a-85b9-4d52-91ff-d7678075442f", "resource": "arn:aws:iam::111122223333:role/Cognito_testpoolAuth_Role", "resourceType": "AWS::IAM::Role", "resourceOwnerAccount": "111122223333", "status": "ACTIVE", "updatedAt": "2021-02-26T21:17:50.905000+00:00", "findingType": "ExternalAccess" } ] }

如需詳細資訊,請參閱 AWS IAM 使用者指南中的使用 AWS Identity and Access Management Access Analyzer

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListFindingsV2

下列程式碼範例示範如何使用 list-findings

AWS CLI

擷取指定分析器產生的調查結果清單

下列list-findings範例會擷取您 AWS 帳戶中指定分析器產生的調查結果清單。此範例會篩選結果,以僅包含名稱包含 的 IAM 角色Cognito

aws accessanalyzer list-findings \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account \ --filter '{"resource": {"contains": ["Cognito"]}, "resourceType": {"eq": ["AWS::IAM::Role"]}}'

輸出:

{ "findings": [ { "id": "597f3bc2-3adc-4c18-9879-5c4b23485e46", "principal": { "Federated": "cognito-identity.amazonaws.com" }, "action": [ "sts:AssumeRoleWithWebIdentity" ], "resource": "arn:aws:iam::111122223333:role/Cognito_testpoolUnauth_Role", "isPublic": false, "resourceType": "AWS::IAM::Role", "condition": { "cognito-identity.amazonaws.com:aud": "us-west-2:EXAMPLE0-0000-0000-0000-000000000000" }, "createdAt": "2021-02-26T21:17:24.710000+00:00", "analyzedAt": "2024-02-16T18:17:47.888000+00:00", "updatedAt": "2021-02-26T21:17:24.710000+00:00", "status": "ACTIVE", "resourceOwnerAccount": "111122223333" }, { "id": "ce0e221a-85b9-4d52-91ff-d7678075442f", "principal": { "Federated": "cognito-identity.amazonaws.com" }, "action": [ "sts:AssumeRoleWithWebIdentity" ], "resource": "arn:aws:iam::111122223333:role/Cognito_testpoolAuth_Role", "isPublic": false, "resourceType": "AWS::IAM::Role", "condition": { "cognito-identity.amazonaws.com:aud": "us-west-2:EXAMPLE0-0000-0000-0000-000000000000" }, "createdAt": "2021-02-26T21:17:50.905000+00:00", "analyzedAt": "2024-02-16T18:17:47.888000+00:00", "updatedAt": "2021-02-26T21:17:50.905000+00:00", "status": "ACTIVE", "resourceOwnerAccount": "111122223333" } ] }

如需詳細資訊,請參閱 AWS IAM 使用者指南中的使用 AWS Identity and Access Management Access Analyzer

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListFindings

下列程式碼範例示範如何使用 list-policy-generations

AWS CLI

列出過去七天請求的所有政策世代

下列list-policy-generations範例列出您 AWS 帳戶中過去七天請求的所有政策世代。

aws accessanalyzer list-policy-generations

輸出:

{ "policyGenerations": [ { "completedOn": "2024-02-14T23:43:38+00:00", "jobId": "923a56b0-ebb8-4e80-8a3c-a11ccfbcd6f2", "principalArn": "arn:aws:iam::111122223333:role/Admin", "startedOn": "2024-02-14T23:43:02+00:00", "status": "CANCELED" }, { "completedOn": "2024-02-14T22:47:01+00:00", "jobId": "c557dc4a-0338-4489-95dd-739014860ff9", "principalArn": "arn:aws:iam::111122223333:role/Admin", "startedOn": "2024-02-14T22:44:41+00:00", "status": "SUCCEEDED" } ] }

如需詳細資訊,請參閱 IAM 使用者指南中的 Word Access Analyzer 政策產生AWS IAM

下列程式碼範例示範如何使用 list-tags-for-resource

AWS CLI

擷取套用至指定資源的標籤清單

下列list-tags-for-resource範例會擷取套用至 AWS 帳戶中指定資源的標籤清單。

aws accessanalyzer list-tags-for-resource \ --resource-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account

輸出:

{ "tags": { "Zone-of-trust": "Account", "Name": "ConsoleAnalyzer" } }

如需詳細資訊,請參閱 IAM 使用者指南中的 Word Access Analyzer 政策產生AWS IAM

下列程式碼範例示範如何使用 start-policy-generation

AWS CLI

若要開始政策產生請求

下列start-policy-generation範例會在您的帳戶中啟動政策產生請求 AWS 。

aws accessanalyzer start-policy-generation \ --policy-generation-details '{"principalArn":"arn:aws:iam::111122223333:role/Admin"}' \ --cloud-trail-details file://myfile.json

myfile.json 的內容:

{ "accessRole": "arn:aws:iam::111122223333:role/service-role/AccessAnalyzerMonitorServiceRole", "startTime": "2024-02-13T00:30:00Z", "trails": [ { "allRegions": true, "cloudTrailArn": "arn:aws:cloudtrail:us-west-2:111122223333:trail/my-trail" } ] }

輸出:

{ "jobId": "c557dc4a-0338-4489-95dd-739014860ff9" }

如需詳細資訊,請參閱 IAM 使用者指南中的 Word Access Analyzer 政策產生AWS IAM

下列程式碼範例示範如何使用 start-resource-scan

AWS CLI

立即開始掃描套用至指定資源的政策

下列start-resource-scan範例會立即開始掃描套用至 AWS 帳戶中指定資源的政策。

aws accessanalyzer start-resource-scan \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account \ --resource-arn arn:aws:iam::111122223333:role/Cognito_testpoolAuth_Role

此命令不會產生輸出。

如需詳細資訊,請參閱 IAM 使用者指南中的 Word Access Analyzer 政策產生AWS IAM

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 StartResourceScan

下列程式碼範例示範如何使用 tag-resource

AWS CLI

將標籤新增至指定的資源

下列tag-resource範例會將標籤新增至您 AWS 帳戶中的指定資源。

aws accessanalyzer tag-resource \ --resource-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account \ --tags Environment=dev,Purpose=testing

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IAM 使用者指南中的使用 AWS Identity and Access Management Access Analyzer

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 TagResource

下列程式碼範例示範如何使用 untag-resource

AWS CLI

從指定的資源移除標籤

下列untag-resource範例會從您 AWS 帳戶中的指定資源移除標籤。

aws accessanalyzer untag-resource \ --resource-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/ConsoleAnalyzer-account \ --tag-keys Environment Purpose

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IAM 使用者指南中的使用 AWS Identity and Access Management Access Analyzer

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UntagResource

下列程式碼範例示範如何使用 update-archive-rule

AWS CLI

更新指定封存規則的條件和值

下列update-archive-rule範例會更新您 AWS 帳戶中指定封存規則的條件和值。

aws accessanalyzer update-archive-rule \ --analyzer-name UnusedAccess-ConsoleAnalyzer-organization \ --rule-name MyArchiveRule \ --filter '{"resource": {"contains": ["Cognito"]}, "resourceType": {"eq": ["AWS::IAM::Role"]}}'

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IAM 使用者指南中的封存規則

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateArchiveRule

下列程式碼範例示範如何使用 update-findings

AWS CLI

更新指定調查結果的狀態

下列update-findings範例會更新您 AWS 帳戶中指定調查結果的狀態。

aws accessanalyzer update-findings \ --analyzer-arn arn:aws:access-analyzer:us-west-2:111122223333:analyzer/UnusedAccess-ConsoleAnalyzer-organization \ --ids 4f319ac3-2e0c-4dc4-bf51-7013a086b6ae 780d586a-2cce-4f72-aff6-359d450e7500 \ --status ARCHIVED

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IAM 使用者指南中的使用 AWS Identity and Access Management Access Analyzer

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateFindings

下列程式碼範例示範如何使用 validate-policy

AWS CLI

請求驗證政策並傳回調查結果清單

下列validate-policy範例會請求驗證政策,並傳回調查結果清單。範例中的政策是用於 Web 身分聯合的 Amazon Cognito 角色的角色信任政策。信任政策產生的調查結果與空Sid元素值和不相符的政策主體相關,因為使用不正確的擔任角色動作 sts:AssumeRole。與 Cognito 搭配使用的正確擔任角色動作為 sts:AssumeRoleWithWebIdentity

aws accessanalyzer validate-policy \ --policy-document file://myfile.json \ --policy-type RESOURCE_POLICY

myfile.json 的內容:

{ "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "Federated": "cognito-identity.amazonaws.com" }, "Action": [ "sts:AssumeRole", "sts:TagSession" ], "Condition": { "StringEquals": { "cognito-identity.amazonaws.com:aud": "us-west-2_EXAMPLE" } } } ] }

輸出:

{ "findings": [ { "findingDetails": "Add a value to the empty string in the Sid element.", "findingType": "SUGGESTION", "issueCode": "EMPTY_SID_VALUE", "learnMoreLink": "https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-reference-policy-checks.html#access-analyzer-reference-policy-checks-suggestion-empty-sid-value", "locations": [ { "path": [ { "value": "Statement" }, { "index": 0 }, { "value": "Sid" } ], "span": { "end": { "column": 21, "line": 5, "offset": 81 }, "start": { "column": 19, "line": 5, "offset": 79 } } } ] }, { "findingDetails": "The sts:AssumeRole action is invalid with the following principal(s): cognito-identity.amazonaws.com. Use a SAML provider principal with the sts:AssumeRoleWithSAML action or use an OIDC provider principal with the sts:AssumeRoleWithWebIdentity action. Ensure the provider is Federated if you use either of the two options.", "findingType": "ERROR", "issueCode": "MISMATCHED_ACTION_FOR_PRINCIPAL", "learnMoreLink": "https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-reference-policy-checks.html#access-analyzer-reference-policy-checks-error-mismatched-action-for-principal", "locations": [ { "path": [ { "value": "Statement" }, { "index": 0 }, { "value": "Action" }, { "index": 0 } ], "span": { "end": { "column": 32, "line": 11, "offset": 274 }, "start": { "column": 16, "line": 11, "offset": 258 } } }, { "path": [ { "value": "Statement" }, { "index": 0 }, { "value": "Principal" }, { "value": "Federated" } ], "span": { "end": { "column": 61, "line": 8, "offset": 202 }, "start": { "column": 29, "line": 8, "offset": 170 } } } ] }, { "findingDetails": "The following actions: sts:TagSession are not supported by the condition key cognito-identity.amazonaws.com:aud. The condition will not be evaluated for these actions. We recommend that you move these actions to a different statement without this condition key.", "findingType": "ERROR", "issueCode": "UNSUPPORTED_ACTION_FOR_CONDITION_KEY", "learnMoreLink": "https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-reference-policy-checks.html#access-analyzer-reference-policy-checks-error-unsupported-action-for-condition-key", "locations": [ { "path": [ { "value": "Statement" }, { "index": 0 }, { "value": "Action" }, { "index": 1 } ], "span": { "end": { "column": 32, "line": 12, "offset": 308 }, "start": { "column": 16, "line": 12, "offset": 292 } } }, { "path": [ { "value": "Statement" }, { "index": 0 }, { "value": "Condition" }, { "value": "StringEquals" }, { "value": "cognito-identity.amazonaws.com:aud" } ], "span": { "end": { "column": 79, "line": 16, "offset": 464 }, "start": { "column": 58, "line": 16, "offset": 443 } } } ] } ] }

如需詳細資訊,請參閱 AWS IAM 使用者指南中的檢查以驗證政策

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ValidatePolicy