Help Center/ Identity and Access Management/ API Reference/ API/ Custom Policy Management/ Creating a Custom Policy for Cloud Services
Updated on 2024-11-18 GMT+08:00

Creating a Custom Policy for Cloud Services

Function

This API is provided for the administrator to create a custom policy for cloud services.

The API can be called using both the global endpoint and region-specific endpoints. For IAM endpoints, see Regions and Endpoints.

Debugging

You can debug this API in API Explorer.

URI

POST /v3.0/OS-ROLE/roles

Request Parameters

Table 1 Parameters in the request header

Parameter

Mandatory

Type

Description

Content-Type

Yes

String

Fill application/json;charset=utf8 in this field.

X-Auth-Token

Yes

String

Access token issued to a user to bear its identity and permissions.

For details about the permissions required by the token, see Actions.

Table 2 Parameters in the request body

Parameter

Mandatory

Type

Description

role

Yes

Object

Custom policy information.

Table 3 role

Parameter

Mandatory

Type

Description

display_name

Yes

String

Display name of the custom policy.

type

Yes

String

Application scope of the custom policy.

  • Global services: AX
  • Region-specific projects: XA

Set the display mode of a custom policy to either AX or XA.

NOTE:
  • To assign a custom policy that contains OBS operations to a user group, create two custom policies with the scope being set to global services and region-specific projects respectively and other parameters being the same, and then attach the two policies to the user group.
  • To minimize the authorization scope, do not include actions of other cloud services in custom OBS policies.

description

Yes

String

Description of the custom policy.

description_cn

No

String

Description of the custom policy in Chinese.

policy

Yes

Object

Content of the custom policy.

Table 4 role.policy

Parameter

Mandatory

Type

Description

Version

Yes

String

Policy version. When creating a custom policy, set this parameter to 1.1.

NOTE:
  • 1.0: System-defined role. Only a limited number of service-level roles are provided for authorization.
  • 1.1: Policy. A policy defines the permissions required to perform operations on a specific cloud resource under certain conditions.

Statement

Yes

Array of objects

Statement of the policy.

Table 5 role.policy.Statement

Parameter

Mandatory

Type

Description

Action

Yes

Array of strings

Specific operation permissions on a resource. For details about supported actions, see "Permissions and Supported Actions" in the API Reference of cloud services.

NOTE:
  • The value format is Service name:Resource type:Operation, for example, vpc:ports:create.
  • Service name: indicates the product name, such as ecs, evs, or vpc. Only lowercase letters are allowed. Resource types and operations are not case-sensitive. You can use an asterisk (*) to represent all operations.
  • A custom policy (role.policy.Statement) cannot contain actions of both project-level and global services. For details about the scope of service permissions, see System Permissions.

Effect

Yes

String

Effect of the permission. The value can be Allow or Deny. If both Allow and Deny statements are found in a policy, the authentication starts from the Deny statements.

Options:

  • Allow
  • Deny

Condition

No

Map<String,Map<String,Array<String>>>

Conditions for the permission to take effect. For details, see Creating a Custom Policy.

NOTE:

Take the condition in the sample request as an example, the values of the condition key (obs:prefix) and string (public) must be equal (StringEquals).

 "Condition": {
              "StringEquals": {
                "obs:prefix": [
                  "public"
                ]
              }
            }

Resource

No

Array of strings

Cloud resource.

NOTE:
  • Format: ::::. For example, obs:::bucket:*. Asterisks are allowed.
  • The region segment can be * or a region accessible to the user. The specified resource must belong to the corresponding service that actually exists.

Response Parameters

Table 6 Parameters in the response body

Parameter

Type

Description

role

Object

Custom policy information.

Table 7 role

Parameter

Type

Description

catalog

String

Service catalog.

display_name

String

Display name of the custom policy.

description

String

Description of the custom policy.

links

Object

Resource link of the custom policy.

policy

Object

Content of the custom policy.

description_cn

String

Description of a custom policy in Chinese. This parameter is returned in the response only when description_cn is specified in the request.

domain_id

String

Account ID.

type

String

Display mode.

NOTE:
  • AX: the global service project
  • XA: region-specific projects
  • Set the display mode of a custom policy to either AX or XA.

id

String

Custom policy ID.

name

String

Name of the custom policy.

updated_time

String

Time when the custom policy was last updated.

NOTE:

The value is a Unix timestamp in millisecond, for example, 1687913793000.

created_time

String

Time when the custom policy was created.

NOTE:

The value is a Unix timestamp in millisecond, for example, 1687913793000.

Table 9 role.policy

Parameter

Type

Description

Version

String

Policy version.

NOTE:
  • 1.0: System-defined role. Only a limited number of service-level roles are provided for authorization.
  • 1.1: Policy. A policy defines the permissions required to perform operations on a specific cloud resource under certain conditions.

Statement

Array of objects

Statement of the policy. A policy can contain a maximum of eight statements.

Table 10 role.policy.Statement

Parameter

Type

Description

Action

Array of strings

Specific operation permissions on a resource.

Effect

String

Effect of the permission. The value can be Allow or Deny. If both Allow and Deny statements are found in a policy, the authentication starts from the Deny statements.

Options:

  • Allow
  • Deny

Condition

Map<String,Map<String,Array<String>>>

Conditions for the permission to take effect. If this parameter is not specified in the request, it will not be returned in the response.

Resource

Array of strings

Cloud resource. If this parameter is not specified in the request, it will not be returned in the response.

NOTE:
  • Format: ::::. For example, obs:::bucket:*. Asterisks are allowed.
  • The region segment can be * or a region accessible to the user. The specified resource must belong to the corresponding service that actually exists.

Example Request

Request to create a custom policy named IAMCloudServicePolicy that allows only projects whose names start with ap-southeast-1 to obtain ACL information about all buckets.

POST https://iam.myhuaweicloud.com/v3.0/OS-ROLE/roles
{
    "role": {
        "display_name": "IAMCloudServicePolicy",
        "type": "AX",
        "description": "IAMDescription",
        "description_cn": "Description in Chinese",
        "policy": {
            "Version": "1.1",
            "Statement": [
                {
                    "Effect": "Allow",
                    "Action": [
                        "obs:bucket:GetBucketAcl"
                    ],
                    "Condition": {
                        "StringStartWith": {
                            "g:ProjectName": [
                                "ap-southeast-1"
                            ]
                        }
                    },
                    "Resource": [
                        "obs:*:*:bucket:*"
                    ]
                }
            ]
        }
    }
}

Example Response

Status code: 201

The custom policy is created successfully.

{
    "role": {
        "catalog": "CUSTOMED",
        "display_name": "IAMCloudServicePolicy",
        "description": "IAMDescription",
        "links": {
            "self": "https://iam.myhuaweicloud.com/v3/roles/93879fd90f1046f69e6e0b31c94d2615"
        },
        "policy": {
            "Version": "1.1",
            "Statement": [
                {
                    "Action": [
                        "obs:bucket:GetBucketAcl"
                    ],
                    "Resource": [
                        "obs:*:*:bucket:*"
                    ],
                    "Effect": "Allow",
                    "Condition": {
                        "StringStartWith": {
                            "g:ProjectName": [
                                "ap-southeast-1"
                            ]
                        }
                    }
                }
            ]
        },
        "description_cn": "Description in Chinese",
        "domain_id": "d78cbac186b744899480f25bd...",
        "type": "AX",
        "id": "93879fd90f1046f69e6e0b31c9...",
        "name": "custom_d78cbac186b744899480f25bd022f468_1"
    }
}

Status Codes

Status Code

Description

201

The custom policy is created successfully.

400

Invalid parameters.

401

Authentication failed.

403

Access denied.

500

Internal server error.

Error Codes

None