Tutorial: Use a RAM policy to allow a user to access OSS resources only over HTTPS

Updated at:

OSS supports access to bucket resources over the HTTPS and HTTP protocols. However, HTTP access is vulnerable to security risks. To protect your OSS resources from attacks, you can enforce access over HTTPS and reject all HTTP requests. This topic describes how to use a RAM policy to restrict a user or role to accessing OSS resources only over HTTPS.

Background information

In OSS, you can use a RAM policy to restrict a user or role to accessing specified buckets and objects only over the HTTPS protocol. RAM policies are user-based authorization policies. They can grant permissions only to users and do not support resource-based authorization for buckets or objects. Therefore, you cannot use a RAM policy to reject HTTP requests from all users.

Procedure

  1. Log on to the Resource Access Management console.

    If you log on as a RAM user, make sure that the RAM user is granted the AliyunOSSFullAccess, AliyunRAMFullAccess, and AliyunSTSAssumeRoleAccess permissions by the Alibaba Cloud account. For more information about how to grant permissions to a RAM user, see Grant permissions to a RAM user.

  2. To deny a specific user access to buckets over HTTP, use the acs:SecureTransport condition key in a RAM policy. You can create this custom policy using a script. For more information, see Create a custom policy. The following code provides a sample RAM policy:

    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Deny",
          "Action": [
            "oss:*"
          ],
          "Resource": [
            "acs:oss:*:*:*"
          ],
          "Condition": {
            "Bool": {
              "acs:SecureTransport": [
                "false"
              ]
            }
          }
        }
      ]
    }
    Note

    The acs:SecureTransport condition key checks whether a request is sent over a secure channel. The value is true for HTTPS requests and false for HTTP requests. Use the Deny effect to reject HTTP requests instead of using the Allow effect to permit HTTPS requests.

  3. Grant the RAM policy to the specified RAM user.

    For more information about how to grant permissions to a RAM user, see Grant permissions to a RAM user.

  4. You can use the Python SDK to upload a file over HTTP. The following code provides an example:

    # -*- coding: utf-8 -*-
    import oss2
    
    # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using this AccessKey pair is a high-risk operation. Create and use a RAM user for API access or routine O&M. To create a RAM user, log on to the RAM console.
    auth = oss2.Auth('[$AccessKeyId]', '[$AccessKeySecret]')
    # Set the endpoint to the one for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to http://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
    bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
    
    # Specify the full path of the object and the full path of the local file. The full path of the object cannot contain the bucket name.
    # If you do not specify a local path, the file is uploaded from the local path of the project to which the sample program belongs.
    bucket.put_object_from_file('exampleobject.txt', 'D:\\localpath\\examplefile.txt')

    The following output is returned. This indicates that the file upload failed and the RAM policy is in effect.

    oss2.exceptions.AccessDenied: {'status': 403, 'x-oss-request-id': '6V37D53DMF67EBeDF5BDa095', 
    'details': {'HostId': 'examplebucket.oss-cn-hangzhou.aliyuncs.com', 
    'Message': 'You have no right to access this object because of bucket acl.',
     'Code': 'AccessDenied', 'RequestId': '6V37D53DMF67EBeDF5BDa095'}}
  5. You can use the Python SDK to upload a file over HTTPS. The following code provides an example:

    # -*- coding: utf-8 -*-
    import oss2
    
    # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using this AccessKey pair is a high-risk operation. Create and use a RAM user for API access or routine O&M. To create a RAM user, log on to the RAM console.
    auth = oss2.Auth('[$AccessKeyId]', '[$AccessKeySecret]')
    # Set the endpoint to the one for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
    bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
    
    # Specify the full path of the object and the full path of the local file. The full path of the object cannot contain the bucket name.
    # If you do not specify a local path, the file is uploaded from the local path of the project to which the sample program belongs.
    bucket.put_object_from_file('exampleobject.txt', 'D:\\localpath\\examplefile.txt')

    The following output indicates that the file was uploaded successfully.

    Put object done, req_id: 6V37D53DMF67EBeDF5BDa095, status_code: 200