审计访问策略
要减少帐户中的策略数并仅保留每个用户所需的最低访问权,您可以识别不常使用的访问策略。 您可以确定是除去这些策略,还是在某些情况下,您可能期望使用不频繁的策略。
访问策略是向帐户中的用户,服务标识,访问组和可信概要文件授予访问权以及对帐户资源执行操作的方式。 策略包括主体、目标和角色。 主体是要为其提供访问权的用户、服务标识或访问组。 策略的目标是要提供其访问权的资源。 此外,IBM Cloud Identity and Access Management (IAM) 角色定义了访问权限级别或策略目标允许的操作。 除了用户和服务标识的访问策略外,还有一种称为授权的策略类型,允许特定服务或服务实例访问其他服务。 您可以在 使用授权在服务之间授予访问权 文档中了解有关在服务之间分配访问权的更多信息。 有关访问策略的更多信息,请参阅 什么是 IAM 策略以及谁可以分配这些策略?。
IAM 捕获每个策略的授权信息。 此信息包括策略上次用于授予许可的时间及其使用的运行计数。
在控制台中管理不活动策略
非活动保单报告显示了最近30天或更长时间内未允许访问的保单。 不包括从未允许访问的策略。
要查看不活动策略,您需要在 AM Insight 服务,IAM Access Management 服务或所有帐户管理服务上具有“编辑者”角色或更高角色。
要在控制台中管理不活动策略,请完成以下步骤:
- 在 IBM Cloud 控制台中,单击 管理 > 访问权 (IAM),然后选择 不活动策略。
- 确定是否可以除去报告中的不活动策略。
- 要删除不活动策略,请单击 操作 图标
> 除去。
删除策略时,不再包含在授权评估中。 IAM 将所有已删除策略的副本保留 10 天。 在此时间段内,您可以随时列示和复原这些项。 要复原已删除的策略,请参阅 使用 API 复原已删除的策略。
导出用户访问策略报告
如果您是帐户所有者,在“用户管理”服务上具有“编辑者”角色或更高角色,或者在“IAM 访问权管理”服务上具有“编辑者”角色或更高角色,那么可以导出帐户中每个用户的访问策略报告。 访问策略报告列出用户拥有的所有访问策略,包括与用户所属的访问组相关联的策略。
审计用户的访问策略可确保您使用的是最少特权原则。 使用此报告可确定是否将用户分配给相应的访问策略,并执行所需操作以减少访问策略数。
要导出报告,请完成以下步骤:
- 在 IBM Cloud 控制台中,转至 管理 > 访问权 (IAM),然后选择 用户。
- 确定要审计的用户,然后单击 操作 图标
> 访问报告。
- 单击 下载 JSON 或 下载 CSV。
使用 API 列出具有最后许可信息的策略
列出所有帐户策略,包括按计数或上次使用排序的上次许可信息。 可以使用 IAM 策略管理 API 来检索策略元数据。
curl -X GET https://iam.cloud.ibm.com/v1/policies?account_id=<>&format=include_last_permit&sort=-last_modified_at \
-H 'Authorization: Bearer $TOKEN' \
-H 'Content-Type: application/json'
ListPoliciesOptions options = new ListPoliciesOptions.Builder()
.accountId(exampleAccountId)
.iamId(exampleUserId)
.format("include_last_permit")
.build();
Response<PolicyList> response = service.listPolicies(options).execute();
PolicyList policyList = response.getResult();
System.out.println(policyList);
const params = {
accountId: exampleAccountId,
iamId: exampleUserId,
format: 'include_last_permit',
};
iamPolicyManagementService.listPolicies(params)
.then(res => {
console.log('listPolicies() result:\n' + JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err)
});
policy_list = iam_policy_management_service.list_policies(
account_id=example_account_id, iam_id=example_user_id, format='include_last_permit'
).get_result()
print(json.dumps(policy_list, indent=2))
options := iamPolicyManagementService.NewListPoliciesOptions(
exampleAccountID,
)
options.SetIamID(exampleUserID)
options.SetFormat("include_last_permit")
policyList, response, err := iamPolicyManagementService.ListPolicies(options)
if err != nil {
panic(err)
}
b, _ := json.MarshalIndent(policyList, "", " ")
fmt.Println(string(b))
format=include_last_permit
: 包括每个策略的最后一个许可信息。sort=last_permit_at
: 按日期升序排序。 未用于任何许可和最早许可的策略将首先列出。
响应的格式以 JSON 表示。
{
"policies": [
{
"id": "45b226ac-490d-47f3-a785-990e0c729d93",
"type": "access",
"subjects": [...],
"roles": [...],
"resources": [...],
...
"last_modified_at": "2021-04-09T14:36:30.505Z",
"last_modified_by_id": "IBMid-310000JVN5",
"last_permit_at": null, <-- IAM has no record of this policy ever granting a permit decision. This will be reset when this policy is deleted.
"last_permit_frequency": 0,
"state": "active"
},
{
"id": "11155157-afb3-4792-9ebd-b1f5547be224",
"type": "access",
"subjects": [...],
"roles": [...],
"resources": [...],
...
"last_modified_at": "2019-05-09T15:28:07.045Z",
"last_modified_by_id": "IAM",
"last_permit_at": "2021-04-20T19:45:44.058Z",
"last_permit_frequency": 137, <-- it has been used this many times since IAM started keeping track
"state": "active"
},
...
使用 API 删除未使用的策略
您标识了一个或多个策略,这些策略在一段时间内未用于授予访问权。 您可以删除未使用的策略,如以下示例中所示。
curl -X DELETE https://iam.cloud.ibm.com/v1/policies/{examplePolicyId} \
-H 'Authorization: Bearer $TOKEN'
DeletePolicyOptions options = new DeletePolicyOptions.Builder()
.policyId(examplePolicyId)
.build();
service.deletePolicy(options).execute();
const params = {
policyId: examplePolicyId,
};
iamPolicyManagementService.deletePolicy(params)
.then(res => {
console.log('deletePolicy() response status code: ' + res.status);
})
.catch(err => {
console.warn(err)
});
response = iam_policy_management_service.delete_policy(
policy_id=example_policy_id
).get_result()
print(json.dumps(response, indent=2))
options := iamPolicyManagementService.NewDeletePolicyOptions(
examplePolicyID,
)
response, err := iamPolicyManagementService.DeletePolicy(options)
if err != nil {
panic(err)
}
有关更多信息,请参阅 按标识删除策略。
将删除该策略,并且不再包含该策略以进行授权评估。 IAM 将所有已删除策略的副本保留 10 天。
使用 API 复原已删除的策略
您发现需要最近删除的策略。 在这种情况下,您可以执行以下步骤来复原策略。
-
列出帐户中已删除的策略,并按上次修改时间排序。
curl -X GET https://iam.cloud.ibm.com/v1/policies?account_id=<>&state=deleted&sort=last_modified_at \ -H 'Authorization: Bearer $TOKEN' \ -H 'Content-Type: application/json'
ListPoliciesOptions options = new ListPoliciesOptions.Builder() .accountId(exampleAccountId) .iamId(exampleUserId) .state("deleted") .sort("last_modified_at") .build(); Response<PolicyList> response = service.listPolicies(options).execute(); PolicyList policyList = response.getResult(); System.out.println(policyList);
const params = { accountId: exampleAccountId, iamId: exampleUserId, sort: 'last_modified_at', state: 'deleted' }; iamPolicyManagementService.listPolicies(params) .then(res => { console.log('listPolicies() result:\n' + JSON.stringify(res.result, null, 2)); }) .catch(err => { console.warn(err) });
policy_list = iam_policy_management_service.list_policies( account_id=example_account_id, iam_id=example_user_id, state='deleted', sort='last_modified_at' ).get_result() print(json.dumps(policy_list, indent=2))
options := iamPolicyManagementService.NewListPoliciesOptions( exampleAccountID, ) options.SetIamID(exampleUserID) options.SetSort("last_modified_at") options.SetState("deleted") policyList, response, err := iamPolicyManagementService.ListPolicies(options) if err != nil { panic(err) } b, _ := json.MarshalIndent(policyList, "", " ") fmt.Println(string(b))
响应的格式以 JSON 表示。
{ "policies": [ { "id": examplePolicyId1, "type": "access", "subjects": [...], "roles": [...], "resources": [...], ... "last_modified_at": "2021-04-09T14:36:30.505Z", "last_modified_by_id": "IBMid-310000JVN5", "last_permit_at": null, "last_permit_frequency": 0, "state": "deleted" <-- deleted policy }, { "id": examplePolicyId2, "type": "access", "subjects": [...], "roles": [...], "resources": [...], ... "last_modified_at": "2019-05-09T15:28:07.045Z", "last_modified_by_id": "IAM", "last_permit_at": null, "last_permit_frequency": 0, "state": "deleted" <-- deleted policy }, ...
-
使用先前检索到的策略标识,可以复原策略:
curl -X PATCH 'https://iam.cloud.ibm.com/v1/policies/{examplePolicyId}' \ -H 'Authorization: Bearer $TOKEN' \ -H 'Content-Type: application/json' \ -H 'If-Match: $ETAG' \ -d '{"state": "active"}'
PatchPolicyOptions patchPolicyOptions = new PatchPolicyOptions.Builder() .policyId(examplePolicyId) .ifMatch(examplePolicyEtag) .state("active") .build(); Response<Policy> response = service.patchPolicy(patchPolicyOptions).execute(); Policy policy = response.getResult(); System.out.println(policy);
const params = { policyId: examplePolicyId, ifMatch: examplePolicyETag, state: 'active' }; iamPolicyManagementService.patchPolicy(params) .then(res => { console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { console.warn(err) });
policy = iam_policy_management_service.patch_policy( policy_id=example_policy_id, if_match=example_updated_policy_etag, state='active' ).get_result() print(json.dumps(policy, indent=2))
options := iamPolicyManagementService.NewPatchPolicyOptions( examplePolicyID, examplePolicyETag, ) options.SetState("active") policy, response, err := iamPolicyManagementService.PatchPolicy(options) if err != nil { panic(err) } b, _ := json.MarshalIndent(policy, "", " ") fmt.Println(string(b))
响应的格式以 JSON 表示。
{ "id": examplePolicyId, "type": "access", "subjects": [...], "roles": [...], "resources": [...], ... "last_modified_at": "2021-04-09T14:36:30.505Z", "last_modified_by_id": "IBMid-310000JVN5", "last_permit_at": null, "last_permit_frequency": 0, "state": "active" <-- policy is active again }
有关更多信息,请参阅 按标识复原已删除的策略。
后续步骤
从不同角度深入了解帐户中的访问权分配。 查看 审计对资源的访问权 以了解哪些身份和服务可以访问特定资源。