为未经授权的 AWS 服务用户编写 python 测试
Writing python tests for unauthorized users of AWS services
我正在编写 python 测试以确保 Amazon S3(一般服务)按预期工作。
设置:CodePipeline 使用 CodeBuild 使用 CloudFormation 模板创建 S3 存储桶,然后启动另一个 CodeBuild 作业以 运行 python 对上一步创建的 S3 进行测试。
我需要为这两个需求编写测试:
"Unauthorized users shall receive a 403 response when attempting to modify an S3 bucket."
和
"Authorized users shall be able to successfully access and modify an S3 bucket."
第二个测试不是问题,但我不确定第一个测试如何写。
我的问题是:如何获得 python 测试以作为未授权用户 测试 S3 存储桶 ? CodeBuild 已经具有访问 S3 存储桶的权限,所以我不确定如何进行测试(使用 CodeBuild 运行)才能无权访问它们并获得我需要的 403。
这里有一些更详细的信息,对于那些会觉得有用的人:
管道有 4 个阶段:源、构建、部署、构建测试和删除。部署阶段是使用 CloudFormation 堆栈建立我需要测试的 S3 存储桶的阶段。构建测试阶段是我实际 运行 进行这些 python 测试的阶段,因此我想这就是我需要实施此问题的解决方案的阶段。
最终,整个过程将由一个 CloudFormation 模板启动,该模板创建一个包含所有这些阶段的管道。但目前,它只是使用来自 AWS 控制台的管道。我只是在 CloudFormation 可能(或不可能)使这项工作可行(或不可能)的情况下提及这一点,这可能(不)在 CodeBuild 中可用。
不幸的是,我没有多少 python 代码可以在这里分享,因为这是我需要编写的第一个测试,我不知道如何处理它。但我可以告诉你的是,我正在使用 Boto3 并且 运行 使用 unittest 进行测试。我通过检查 CloudFormation 中的当前堆栈并查看哪个与测试堆栈名称匹配,然后从该堆栈中获取 S3 资源来找到存储桶。这就是我正在测试的桶。所以不知何故,我需要查看那个存储桶,尝试访问它,并在一个测试中被拒绝,然后在另一个测试中获得访问权限。
#python 3.6
import os
import boto3
import unittest
rootstack = os.getenv('RootStackName') # environment variable in the build
region = 'us-west-2'
buckets = {}
class TestS3(unittest.TestCase):
def setUp(self):
self.customBucket = None
self.customBucket = buckets['customBucket']
if self.customBucket is None:
raise ValueError('Test bucket not found in test setUp!')
def test_bucket_accessible_if_authorized(self):
# Authorized user can access the bucket
self.assertEqual(????)
def test_bucket_cant_be_accessed_if_unauthorized(self):
# Unauthorized user CANNOT access the bucket
self.assertEqual(????)
if __name__=='__main__':
try:
cfn = boto3.client('cloudformation', region_name=region)
response = cfn.describe_stack_resources(StackName=rootstack)
resources = response['StackResources']
for resource in resources:
if resource['ResourceType'] == 'AWS::S3::Bucket':
print('FOUND THE CUSTOM BUCKET')
buckets['customBucket'] = resource
unittest.main(verbosity=2)
except Exception as e:
print("Unknown Error, %s" %(e))
我最终采用了在尝试访问 S3 存储桶和对象之前仅修改代码中的凭据的方法。如果我得到 AccessDenied,则确认安全性正在按预期工作。
例如:
def test_unauthorized_read(self):
# unauthorized users will 'Access Denied' if attempting to list the S3 bucket
cantListBuckets = False
# the user assigned in the next line has NO permissions to access any service
client = boto3.client(service_name='s3',
aws_access_key_id='AKIBI6SPIAUZ6LTGA4BQ',
aws_secret_access_key='HyqAJOXgqt6kCOSay/2eH6J3FYcbwjNTjTyhtHOQ'
)
try:
buckets = client.list_buckets()
except botocore.exceptions.ClientError as e:
if 'AccessDenied' in str(e):
cantListBuckets = True
self.assertTrue(cantListBuckets)
不理想...欢迎 post 任何更好的方法。
我正在编写 python 测试以确保 Amazon S3(一般服务)按预期工作。
设置:CodePipeline 使用 CodeBuild 使用 CloudFormation 模板创建 S3 存储桶,然后启动另一个 CodeBuild 作业以 运行 python 对上一步创建的 S3 进行测试。
我需要为这两个需求编写测试:
"Unauthorized users shall receive a 403 response when attempting to modify an S3 bucket."
和
"Authorized users shall be able to successfully access and modify an S3 bucket."
第二个测试不是问题,但我不确定第一个测试如何写。
我的问题是:如何获得 python 测试以作为未授权用户 测试 S3 存储桶 ? CodeBuild 已经具有访问 S3 存储桶的权限,所以我不确定如何进行测试(使用 CodeBuild 运行)才能无权访问它们并获得我需要的 403。
这里有一些更详细的信息,对于那些会觉得有用的人:
管道有 4 个阶段:源、构建、部署、构建测试和删除。部署阶段是使用 CloudFormation 堆栈建立我需要测试的 S3 存储桶的阶段。构建测试阶段是我实际 运行 进行这些 python 测试的阶段,因此我想这就是我需要实施此问题的解决方案的阶段。
最终,整个过程将由一个 CloudFormation 模板启动,该模板创建一个包含所有这些阶段的管道。但目前,它只是使用来自 AWS 控制台的管道。我只是在 CloudFormation 可能(或不可能)使这项工作可行(或不可能)的情况下提及这一点,这可能(不)在 CodeBuild 中可用。
不幸的是,我没有多少 python 代码可以在这里分享,因为这是我需要编写的第一个测试,我不知道如何处理它。但我可以告诉你的是,我正在使用 Boto3 并且 运行 使用 unittest 进行测试。我通过检查 CloudFormation 中的当前堆栈并查看哪个与测试堆栈名称匹配,然后从该堆栈中获取 S3 资源来找到存储桶。这就是我正在测试的桶。所以不知何故,我需要查看那个存储桶,尝试访问它,并在一个测试中被拒绝,然后在另一个测试中获得访问权限。
#python 3.6
import os
import boto3
import unittest
rootstack = os.getenv('RootStackName') # environment variable in the build
region = 'us-west-2'
buckets = {}
class TestS3(unittest.TestCase):
def setUp(self):
self.customBucket = None
self.customBucket = buckets['customBucket']
if self.customBucket is None:
raise ValueError('Test bucket not found in test setUp!')
def test_bucket_accessible_if_authorized(self):
# Authorized user can access the bucket
self.assertEqual(????)
def test_bucket_cant_be_accessed_if_unauthorized(self):
# Unauthorized user CANNOT access the bucket
self.assertEqual(????)
if __name__=='__main__':
try:
cfn = boto3.client('cloudformation', region_name=region)
response = cfn.describe_stack_resources(StackName=rootstack)
resources = response['StackResources']
for resource in resources:
if resource['ResourceType'] == 'AWS::S3::Bucket':
print('FOUND THE CUSTOM BUCKET')
buckets['customBucket'] = resource
unittest.main(verbosity=2)
except Exception as e:
print("Unknown Error, %s" %(e))
我最终采用了在尝试访问 S3 存储桶和对象之前仅修改代码中的凭据的方法。如果我得到 AccessDenied,则确认安全性正在按预期工作。
例如:
def test_unauthorized_read(self):
# unauthorized users will 'Access Denied' if attempting to list the S3 bucket
cantListBuckets = False
# the user assigned in the next line has NO permissions to access any service
client = boto3.client(service_name='s3',
aws_access_key_id='AKIBI6SPIAUZ6LTGA4BQ',
aws_secret_access_key='HyqAJOXgqt6kCOSay/2eH6J3FYcbwjNTjTyhtHOQ'
)
try:
buckets = client.list_buckets()
except botocore.exceptions.ClientError as e:
if 'AccessDenied' in str(e):
cantListBuckets = True
self.assertTrue(cantListBuckets)
不理想...欢迎 post 任何更好的方法。