如何限制用戶僅經過HTTPS方式訪問OSS?

1、當前存在的問題

  當前OSS支持用戶使用HTTPS/HTTP協議訪問Bucket。但因爲HTTP存在安全漏洞。大型企業客戶都要求使用HTTPS方式訪問OSS,而且拒絕HTTP訪問請求。python

  目前OSS能夠經過RAM policy方式實現:限制某個用戶、角色拒絕經過HTTP協議訪問指定的Bucket和對象。可是RAM Policy是一種基於用戶的受權方式,沒法針對資源進行受權。也就是說沒法針對Bucket或者對象級別,拒絕全部用戶的HTTP請求。目前咱們正在基於Bucket Policy開發該功能,後續用戶能夠直接經過Bucket Policy設置HTTPS訪問策略。linux

2、經過RAM Policy實現「限制用戶僅經過HTTPS方式訪問OSS」

  阿里雲RAM Policy有豐富的Condition參數,能夠限制對資源的訪問。這裏咱們利用"Secure Transport"條件參數生成RAM Policy,以實現拒絕指定的用戶經過HTTP方式訪問Bucketapi

Condition 功能 合法取值
acs:SecureTransport 是不是https協議 「true」或者」false」

2.1RAM Policy示例

  • 爲了簡化配置,咱們事先給帳號賦予「AliyunOSSFullAccess」,而後模擬拒絕一切經過HTTP的請求。
  • 添加「拒絕HTTP訪問請求」RAM Policy。具體RAM Policy內容以下:
{
  "Version": "1",
  "Statement": [
    {
      "Effect": "Deny",
      "Action": [
        "oss:*"
      ],
      "Resource": [
        "acs:oss:*:*:*"
      ],
      "Condition": {
        "Bool": {
          "acs:SecureTransport": [
            "false"
          ]
        }
      }
    }
  ]
}

說明::如上Policy可以拒絕該用戶經過HTTP方式訪問OSS資源;安全

2.2用戶經過HTTPS方式訪問OSS進行測試

說明:app

  • 咱們以Python SDK上傳文件方式進行舉例說明;
  • 以下咱們在腳本中指定訪問路徑爲"https://oss-cn-beijing.aliyunc.com" ;

2.2.1經過HTTPS方式上傳文件

  • python腳本示例以下:
# -*- coding: utf-8 -*-
import oss2

# 阿里雲主帳號AccessKey擁有全部API的訪問權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或平常運維,請登陸 https://ram.console.aliyun.com 建立RAM帳號。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州爲例,其它Region請按實際狀況填寫。
bucket = oss2.Bucket(auth, 'https://oss-cn-beijing.aliyuncs.com', 'test-beijing-2018')

# <yourLocalFile>由本地文件路徑加文件名包括後綴組成,例如/users/local/myfile.txt
bucket.put_object_from_file('02.txt', '002.txt')
  • 執行結果以下
root@shanghai-02:~/figo# python putobject.py
2019-01-10 20:55:37,003 oss2.api [INFO] 140496922879744 : Init oss bucket, endpoint: https://oss-cn-beijing.aliyuncs.com, isCname: False, connect_timeout: None, app_name: , enabled_crc: True
2019-01-10 20:55:37,008 oss2.api [INFO] 140496922879744 : Put object from file, bucket: test-beijing-2018, key: 02.txt, file path: 002.txt
2019-01-10 20:55:37,009 oss2.api [INFO] 140496922879744 : Start to put object, bucket: test-beijing-2018, key: 02.txt, headers: {'Content-Type': 'text/plain'}
2019-01-10 20:55:37,212 oss2.api [INFO] 140496922879744 : Put object done, req_id: 5C3740C952FF5BAFB298BDDA, status_code: 200

說明:如上執行結果,代表文件已經上傳成功;運維

2.2.2經過HTTP方式上傳文件

說明:以下咱們在腳本中指定訪問路徑爲「http://oss-cn-beijing.aliyuncs.com測試

  • python腳本示例以下:
# -*- coding: utf-8 -*-
import oss2

# 阿里雲主帳號AccessKey擁有全部API的訪問權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或平常運維,請登陸 https://ram.console.aliyun.com 建立RAM帳號。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州爲例,其它Region請按實際狀況填寫。
bucket = oss2.Bucket(auth, 'http://oss-cn-beijing.aliyuncs.com', 'test-beijing-2018')

# <yourLocalFile>由本地文件路徑加文件名包括後綴組成,例如/users/local/myfile.txt
bucket.put_object_from_file('02.txt', '002.txt')
  • 執行結果以下
root@shanghai-02:~/figo# python  putobject.py
2019-01-10 21:14:37,499 oss2.api [INFO] 140697781880576 : Init oss bucket, endpoint: http://oss-cn-beijing.aliyuncs.com, isCname: False, connect_timeout: None, app_name: , enabled_crc: True
2019-01-10 21:14:37,501 oss2.api [INFO] 140697781880576 : Put object from file, bucket: test-beijing-2018, key: 02.txt, file path: 002.txt
2019-01-10 21:14:37,503 oss2.api [INFO] 140697781880576 : Start to put object, bucket: test-beijing-2018, key: 02.txt, headers: {'Content-Type': 'text/plain'}
2019-01-10 21:14:37,585 oss2.api [ERROR] 140697781880576 : Exception: {'status': 403, 'x-oss-request-id': '5C37453DDF97EBEDF4BDA095', 'details': {'HostId': 'test-beijing-2018.oss-cn-beijing.aliyuncs.com', 'Message': 'You have no right to access this object because of bucket acl.', 'Code': 'AccessDenied', 'RequestId': '5C37453DDF97EBEDF4BDA095'}}
Traceback (most recent call last):
  File "putobject.py", line 10, in <module>
    bucket.put_object_from_file('02.txt', '002.txt')
  File "build/bdist.linux-x86_64/egg/oss2/api.py", line 481, in put_object_from_file
  File "build/bdist.linux-x86_64/egg/oss2/api.py", line 453, in put_object
  File "build/bdist.linux-x86_64/egg/oss2/api.py", line 1579, in __do_object
  File "build/bdist.linux-x86_64/egg/oss2/api.py", line 210, in _do
oss2.exceptions.AccessDenied: {'status': 403, 'x-oss-request-id': '5C37453DDF97EBEDF4BDA095', 'details': {'HostId': 'test-beijing-2018.oss-cn-beijing.aliyuncs.com', 'Message': 'You have no right to access this object because of bucket acl.', 'Code': 'AccessDenied', 'RequestId': '5C37453DDF97EBEDF4BDA095'}}

說明ui

  • 當咱們將上傳endpoint設置爲"http://oss-cn-beijing.aliyuncs.com" 時,上傳文件失敗。說明RAM Policy已經生效;
  • 目前RAM Policy僅能限制指定的用戶經過HTTPS方式訪問。下一步OSS將在Bucket Policy中設置"Secure Transport"參數,以實現限制全部用戶經過HTTP方式訪問指定的Bucket和對象;

做者: figo168hf
原文連接
本文爲雲棲社區原創內容,未經容許不得轉載。 this

相關文章
相關標籤/搜索