67 lines
2.9 KiB
Python
67 lines
2.9 KiB
Python
"""Check allowed and denied S3 prefixes using the current service identity."""
|
|
import json
|
|
import http.client
|
|
import urllib.parse
|
|
from botocore.auth import S3SigV4Auth
|
|
from botocore.awsrequest import AWSRequest
|
|
from botocore.credentials import Credentials
|
|
import os
|
|
import ssl
|
|
import urllib.request
|
|
from uuid import uuid4
|
|
|
|
import boto3
|
|
from botocore.exceptions import ClientError
|
|
|
|
role = os.environ['BAO_SERVICE_ROLE']
|
|
ctx = ssl.create_default_context(cafile=os.environ['BAO_CACERT'])
|
|
ctx.load_cert_chain(os.environ['BAO_CLIENT_CERT'])
|
|
base = os.environ['BAO_ADDR'].rstrip('/')
|
|
request = urllib.request.Request(base + '/v1/auth/cert/login', data=json.dumps({'name': role}).encode(),
|
|
headers={'Content-Type': 'application/json'})
|
|
with urllib.request.urlopen(request, context=ctx) as response:
|
|
token = json.load(response)['auth']['client_token']
|
|
request = urllib.request.Request(base + '/v1/lab/data/' + role, headers={'X-Vault-Token': token})
|
|
with urllib.request.urlopen(request, context=ctx) as response:
|
|
values = json.load(response)['data']['data']
|
|
client = boto3.client('s3', endpoint_url=os.environ['S3_ENDPOINT'], region_name='us-east-1',
|
|
aws_access_key_id=values['S3_ACCESS_KEY_ID'],
|
|
aws_secret_access_key=values['S3_SECRET_ACCESS_KEY'])
|
|
bucket = os.environ['S3_BUCKET']
|
|
probe = uuid4().hex
|
|
allowed = 'staging/security-probe-' + probe
|
|
denied = ('originals/' if role == 'api' else '_bootstrap/') + 'security-probe-' + probe
|
|
client.put_object(Bucket=bucket, Key=allowed, Body=b'test')
|
|
client.delete_object(Bucket=bucket, Key=allowed)
|
|
try:
|
|
client.put_object(Bucket=bucket, Key=denied, Body=b'test')
|
|
except ClientError as error:
|
|
assert error.response['ResponseMetadata']['HTTPStatusCode'] == 403
|
|
else:
|
|
client.delete_object(Bucket=bucket, Key=denied)
|
|
raise AssertionError('disallowed prefix was writable')
|
|
finally:
|
|
client.close()
|
|
print(f'PASS {role}: allowed staging write; forbidden prefix denied')
|
|
|
|
# Repeated Authorization fields must fail even for an otherwise allowed read.
|
|
endpoint = urllib.parse.urlsplit(os.environ['S3_ENDPOINT'])
|
|
path = '/' + bucket + '/' + allowed
|
|
req = AWSRequest(method='GET', url=os.environ['S3_ENDPOINT'] + path)
|
|
S3SigV4Auth(Credentials(values['S3_ACCESS_KEY_ID'], values['S3_SECRET_ACCESS_KEY']),
|
|
's3', 'us-east-1').add_auth(req)
|
|
conn = http.client.HTTPSConnection(endpoint.hostname, endpoint.port,
|
|
context=ssl.create_default_context(cafile=os.environ['AWS_CA_BUNDLE']), timeout=10)
|
|
try:
|
|
conn.putrequest('GET', path)
|
|
for key, value in req.headers.items():
|
|
conn.putheader(key, value)
|
|
conn.putheader('Authorization', req.headers['Authorization'])
|
|
conn.endheaders()
|
|
response = conn.getresponse()
|
|
assert response.status == 403, 'duplicate Authorization not denied'
|
|
response.read()
|
|
finally:
|
|
conn.close()
|
|
print(f'PASS {role}: duplicate Authorization denied')
|