如何在 IBM Cloud 账户中配置 IBM Cloud Logs Routing 与 IBM Cloud Logs 之间的服务到服务( S2S )授权?
使用 IBM Cloud® Identity and Access Management (IAM)创建一项授权,允许 IBM Cloud Logs Routing 访问 IBM Cloud Logs,以便 IBM Cloud Logs Routing 服务能够将日志发送到您的 IBM Cloud Logs 实例目标(target)。
您计划配置为目标的 IBM Cloud Logs 实例必须位于与您计划配置 IBM Cloud Logs Routing 的同一账户中。
准备工作
- 了解 管理授权以授予服务间的访问权限。
- 您必须能够访问目标服务,才能管理服务之间的授权。 有关详细信息,请参阅 管理授权的权限。
目标服务始终位于创建授权的账户中。
您为“IBM Cloud Logs Routing”服务定义的授权要求您对“IBM Cloud Logs”目标拥有“Administrator”角色。
如果拥有创建策略和授权的 IAM 权限,则只能授予作为目标服务用户的访问权限级别。 例如,如果您对目标服务拥有“查看者”访问权限,则在授权时只需分配“查看者”角色即可。 如果尝试分配更高级别的权限(如管理员),可能会出现权限已授予的情况,但只会分配目标服务的最高级别权限(即查看器)。
在 IBM Cloud 账户中,为了在 IBM® Cloud Logs Routing 和 IBM Cloud Logs 之间配置服务到服务( S2S )授权,我需要配置哪些IAM服务访问角色(权限)?
您可以选择源可用于与目标交互的以下任何角色。 您只能授予作为所选目标的用户所拥有的访问权级别。
Sender: 作为发送方,您可以将日志发送到您的 IBM Cloud Logs 服务实例,但无法查询或实时查看日志。 该角色供发送日志的代理和路由器使用。
如何在 IBM Cloud 用户界面控制台中,在 IBM® Cloud Logs Routing 和 IBM Cloud Logs 之间建立服务到服务( S2S )授权?
完成以下步骤:
-
在 IBM Cloud 控制台中,单击 Manage > Access (IAM),然后选择 Authorizations。
-
单击创建。
-
配置源账户。 选择 此帐户。
-
选择 Logs Routing 作为源服务。 然后,将访问范围设置为 所有资源。
-
选择 Cloud 日志 作为目标服务。 然后,通过配置 “基于所选属性的资源”来将访问范围设置为 “所有资源”或单个实例 > 服务实例。
此类授权不支持其他属性。
-
在服务访问部分,选择发送方,为访问目标服务的源服务分配访问权限。
-
单击授权。
如果在其他账户的服务和当前账户的目标服务之间创建授权,则只能访问目标资源。 对于源账户,只需输入账号即可
如何使用 CLI 在 IBM® Cloud Logs Routing 和 IBM Cloud Logs 之间建立服务到服务( S2S )的授权
运行以下命令为 IBM Cloud Logs 服务创建授权。
ibmcloud iam authorization-policy-create logs-router logs Sender
您可以使用 [--target-service-instance-name TARGET_SERVICE_INSTANCE_NAME | --target-service-instance-id TARGET_SERVICE_INSTANCE_ID] 来对特定 IBM Cloud Logs 目标实例进行作用域设置。 不提供任何其他范围选项。
有关此命令可用的所有参数的更多信息,请参阅 ibmcloud iam authorization-policy-create。
如何使用 Terraform 在 IBM® Cloud Logs Routing 和 IBM Cloud Logs 之间建立服务间( S2S )授权
在使用 Terraform 创建授权之前,请确保已完成以下步骤:
- 安装 Terraform CLI 并为 Terraform 配置 IBM Cloud 提供商插件。 有关详细信息,请参阅 在 IBM Cloud 上开始使用 Terraform 教程。 该插件抽象了用于完成此任务的 IBM Cloud API。
- 创建一个名为
main.tf的Terraform配置文件。 在此文件中,您可以使用 HashiCorp 配置语言定义资源。 有关详细信息,请参阅 Terraform 文档。
使用以下步骤通过 Terraform 创建授权:
-
在
main.tf文件中使用ibm_iam_authorization_policy资源参数,在服务之间创建授权策略。下面的示例创建了服务之间的授权:
resource "ibm_iam_authorization_policy" "policy" { source_service_name = "logs-router" target_service_name = "logs" roles = ["Sender"] description = "Authorization Policy" transaction_id = "terraformAuthorizationPolicy" }ibm_iam_authorization_policy资源需要源服务、目标服务和角色。 源服务被授予对目标服务的访问权限,角色是访问权限的级别。 可选择添加授权说明和交易 ID。- 您可以提供
target_resource_instance_id来作用域 IBM Cloud Logs 目标实例。 - 有关更多示例,请参阅 Terraform 授权资源文档。
- 您可以提供
-
完成创建配置文件后,初始化 Terraform CLI。 有关详细信息,请参阅 初始化工作目录。
terraform init -
提供
main.tf文件中的资源。 有关详细信息,请参阅 使用 Terraform 配置基础架构。- 运行
terraform plan生成 Terraform 执行计划,以预览建议的操作。
terraform plan- 运行
terraform apply以创建计划中定义的资源。
terraform apply - 运行
如何使用 API 在 IBM® Cloud Logs Routing 和 IBM Cloud Logs 之间建立服务到服务( S2S )的授权?
您必须有权访问目标服务,才能在服务之间创建授权。 作为目标服务的用户,您只能授予自己拥有的访问级别。 您为 IBM Cloud Logs Routing 服务定义的授权要求您在 IBM Cloud Logs 目标服务中拥有 Administrator 角色。
要授权源服务访问目标服务,请使用 IAM 策略管理 API。
创建授权策略所支持的属性取决于每个服务所支持的属性。 有关每种服务支持的属性的更多信息,请参阅所用服务的文档。
curl --request POST \
--url https://iam.cloud.ibm.com/v1/policies \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"type": "authorization",
"subjects": [
{
"attributes": [
{
"name": "accountId",
"value": "<account-id>"
},
{
"name": "serviceName",
"value": "logs-router"
}
]
}
],
"roles": [
{
"role_id": "crn:v1:bluemix:public:logs::::serviceRole:Sender"
}
],
"resources": [
{
"attributes": [
{
"name": "serviceName",
"value": "logs"
},
{
"name": "serviceInstance",
"value": "$LOGS_SERVICE_INSTANCE_CRN",
"operator": "stringEquals"
}
]
}
]
}'
SubjectAttribute accountSubjectAttribute = new SubjectAttribute.Builder()
.name("accountId")
.value(exampleAccountId)
.build();
SubjectAttribute serviceNameSubjectAttribute = new SubjectAttribute.Builder()
.name("serviceName")
.value("logs-router")
.build();
PolicySubject policySubjects = new PolicySubject.Builder()
.addAttributes(accountSubjectAttribute)
.addAttributes(serviceNameSubjectAttribute)
.build();
PolicyRole policyRoles = new PolicyRole.Builder()
.roleId("crn:v1:bluemix:public:logs::::serviceRole:Sender")
.build();
ResourceAttribute accountIdResourceAttribute = new ResourceAttribute.Builder()
.name("accountId")
.value(exampleAccountId)
.operator("stringEquals")
.build();
ResourceAttribute serviceNameResourceAttribute = new ResourceAttribute.Builder()
.name("serviceName")
.value("logs")
.operator("stringEquals")
.build();
ResourceAttribute serviceInstanceResourceAttribute = new ResourceAttribute.Builder()
.name("serviceInstance")
.value(exampleTargetInstanceId)
.operator("stringEquals")
.build();
ResourceAttribute resourceTypeResourceAttribute = new ResourceAttribute.Builder()
.name("resourceType")
.value(exampleResourceType)
.operator("stringEquals")
.build();
ResourceAttribute resourceResourceAttribute = new ResourceAttribute.Builder()
.name("resource")
.value(exampleResourceId)
.operator("stringEquals")
.build();
PolicyResource policyResources = new PolicyResource.Builder()
.addAttributes(accountIdResourceAttribute)
.addAttributes(serviceNameResourceAttribute)
.build();
CreatePolicyOptions options = new CreatePolicyOptions.Builder()
.type("authorization")
.subjects(Arrays.asList(policySubjects))
.roles(Arrays.asList(policyRoles))
.resources(Arrays.asList(policyResources))
.build();
Response<Policy> response = service.createPolicy(options).execute();
Policy policy = response.getResult();
System.out.println(policy);
const policySubjects = [
{
attributes: [
{
name: 'accountId',
value: exampleAccounId,
},
{
name: 'serviceName',
value: "logs-router",
}
],
},
];
const policyRoles = [
{
role_id: 'crn:v1:bluemix:public:logs::::serviceRole:Sender',
},
];
const accountIdResourceAttribute = {
name: 'accountId',
value: exampleAccountId,
operator: 'stringEquals',
};
const serviceNameResourceAttribute = {
name: 'serviceName',
value: "logs",
operator: 'stringEquals'
};
const policyResources = [
{
attributes: [
accountIdResourceAttribute,
serviceNameResourceAttribute
],
},
];
const params = {
type: 'authorization',
subjects: policySubjects,
roles: policyRoles,
resources: policyResources,
};
iamPolicyManagementService.createPolicy(params)
.then(res => {
examplePolicyId = res.result.id;
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err)
});
policy_subjects = PolicySubject(
attributes=[SubjectAttribute(name='accountId', value=example_account_id),
SubjectAttribute(name='serviceName', value="logs-router")
])
policy_roles = PolicyRole(
role_id='crn:v1:bluemix:public:logs::::serviceRole:Sender')
account_id_resource_attribute = ResourceAttribute(
name='accountId', value=example_account_id)
service_name_resource_attribute = ResourceAttribute(
name='serviceName', value="logs")
policy_resources = PolicyResource(
attributes=[account_id_resource_attribute,
service_name_resource_attribute])
policy = iam_policy_management_service.create_policy(
type='authorization',
subjects=[policy_subjects],
roles=[policy_roles],
resources=[policy_resources]
).get_result()
print(json.dumps(policy, indent=2))
accountSubjectAttribute := &iampolicymanagementv1.SubjectAttribute{
Name: core.StringPtr("accountId"),
Value: &exampleAccountID,
Operator: core.StringPtr("stringEquals"),
}
serviceNameSubjectAttribute := &iampolicymanagementv1.SubjectAttribute{
Name: core.StringPtr("serviceName"),
Value: "logs-router",
Operator: core.StringPtr("stringEquals"),
}
policySubjects := &iampolicymanagementv1.PolicySubject{
Attributes: []iampolicymanagementv1.SubjectAttribute{*accountSubjectAttribute,
*serviceNameSubjectAttribute},
}
policyRoles := &iampolicymanagementv1.PolicyRole{
RoleID: core.StringPtr("crn:v1:bluemix:public:logs::::serviceRole:Sender"),
}
accountIDResourceAttribute := &iampolicymanagementv1.ResourceAttribute{
Name: core.StringPtr("accountId"),
Value: core.StringPtr(exampleAccountID),
Operator: core.StringPtr("stringEquals"),
}
serviceNameResourceAttribute := &iampolicymanagementv1.ResourceAttribute{
Name: core.StringPtr("serviceName"),
Value: core.StringPtr("logs"),
Operator: core.StringPtr("stringEquals"),
}
policyResources := &iampolicymanagementv1.PolicyResource{
Attributes: []iampolicymanagementv1.ResourceAttribute{
*accountIDResourceAttribute, *serviceNameResourceAttribute},
}
options := iamPolicyManagementService.NewCreatePolicyOptions(
"authorization",
[]iampolicymanagementv1.PolicySubject{*policySubjects},
[]iampolicymanagementv1.PolicyRole{*policyRoles},
[]iampolicymanagementv1.PolicyResource{*policyResources},
)
policy, response, err := iamPolicyManagementService.CreatePolicy(options)
if err != nil {
panic(err)
}
b, _ := json.MarshalIndent(policy, "", " ")
fmt.Println(string(b))