一、当前存在的问题
当前OSS支持用户使用HTTPS/HTTP协议访问Bucket。但由于HTTP存在安全漏洞。大型企业客户都要求使用HTTPS方式访问OSS,并且拒绝HTTP访问请求。
目前OSS可以通过RAM policy方式实现:限制某个用户、角色拒绝通过HTTP协议访问指定的Bucket和对象。但是RAM Policy是一种基于用户的授权方式,无法针对资源进行授权。也就是说无法针对Bucket或者对象级别,拒绝所有用户的HTTP请求。目前我们正在基于Bucket Policy开发该功能,后续用户可以直接通过Bucket Policy设置HTTPS访问策略。
二、通过RAM Policy实现“限制用户仅通过HTTPS方式访问OSS”
阿里云RAM Policy有丰富的Condition参数,可以限制对资源的访问。这里我们利用"Secure Transport"条件参数生成RAM Policy,以实现拒绝指定的用户通过HTTP方式访问Bucket。
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进行测试
说明:
我们以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‘)
执行结果如下:
[email protected]:~/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‘)
执行结果如下:
[email protected]:~/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‘}}
说明:
当我们将上传endpoint设置为"http://oss-cn-beijing.aliyuncs.com" 时,上传文件失败。说明RAM Policy已经生效;
目前RAM Policy仅能限制指定的用户通过HTTPS方式访问。下一步OSS将在Bucket Policy中设置"Secure Transport"参数,以实现限制所有用户通过HTTP方式访问指定的Bucket和对象;
原文地址:http://blog.51cto.com/14031893/2344287