Skeleton contracts / contracts (pull_request) Blocked by required conditions
95 lines
3.6 KiB
Python
95 lines
3.6 KiB
Python
"""Проверяет префиксы S3. Ключи не печатает, только код ответа и код ошибки."""
|
|
import datetime
|
|
import hashlib
|
|
import hmac
|
|
import json
|
|
import ssl
|
|
import sys
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
|
|
|
|
def request(method, endpoint, access, secret, bucket, object_key, body=b"", cafile=""):
|
|
host = urllib.parse.urlparse(endpoint).netloc
|
|
now = datetime.datetime.now(datetime.timezone.utc)
|
|
amz_date = now.strftime("%Y%m%dT%H%M%SZ")
|
|
day = amz_date[:8]
|
|
payload_hash = hashlib.sha256(body).hexdigest()
|
|
path = "/" + urllib.parse.quote(bucket, safe="")
|
|
if object_key is not None:
|
|
path += "/" + urllib.parse.quote(object_key, safe="/")
|
|
canonical_headers = (
|
|
f"host:{host}\n"
|
|
f"x-amz-content-sha256:{payload_hash}\n"
|
|
f"x-amz-date:{amz_date}\n"
|
|
)
|
|
signed = "host;x-amz-content-sha256;x-amz-date"
|
|
canonical = "\n".join([method, path, "", canonical_headers, signed, payload_hash])
|
|
scope = f"{day}/us-east-1/s3/aws4_request"
|
|
string_to_sign = "\n".join([
|
|
"AWS4-HMAC-SHA256",
|
|
amz_date,
|
|
scope,
|
|
hashlib.sha256(canonical.encode()).hexdigest(),
|
|
])
|
|
|
|
def mac(key, msg):
|
|
return hmac.new(key, msg.encode(), hashlib.sha256).digest()
|
|
|
|
signing = mac(("AWS4" + secret).encode(), day)
|
|
signing = mac(signing, "us-east-1")
|
|
signing = mac(signing, "s3")
|
|
signing = mac(signing, "aws4_request")
|
|
signature = hmac.new(signing, string_to_sign.encode(), hashlib.sha256).hexdigest()
|
|
auth = (
|
|
f"AWS4-HMAC-SHA256 Credential={access}/{scope}, "
|
|
f"SignedHeaders={signed}, Signature={signature}"
|
|
)
|
|
data = body if method in ("PUT", "POST") else None
|
|
req = urllib.request.Request(endpoint + path, data=data, method=method)
|
|
req.add_header("x-amz-date", amz_date)
|
|
req.add_header("x-amz-content-sha256", payload_hash)
|
|
req.add_header("Authorization", auth)
|
|
ctx = ssl.create_default_context(cafile=cafile)
|
|
try:
|
|
with urllib.request.urlopen(req, context=ctx, timeout=15) as response:
|
|
return response.status, ""
|
|
except urllib.error.HTTPError as error:
|
|
raw = error.read().decode("utf-8", "replace")
|
|
code = ""
|
|
start = raw.find("<Code>")
|
|
end = raw.find("</Code>")
|
|
if start >= 0 and end > start:
|
|
code = raw[start + 6:end]
|
|
return error.code, code
|
|
|
|
|
|
def main():
|
|
doc = json.load(open(sys.argv[1]))
|
|
cafile = sys.argv[2]
|
|
endpoint = "https://s3:8333"
|
|
keys = {}
|
|
for item in doc["identities"]:
|
|
cred = item["credentials"][0]
|
|
keys[item["name"]] = (cred["accessKey"], cred["secretKey"])
|
|
worker = keys["worker"]
|
|
api = keys["api"]
|
|
admin = keys["admin"]
|
|
checks = [
|
|
("worker-staging", "PUT", worker, "messenger-attachments", "staging/grade7-probe", b"ok"),
|
|
("worker-forbidden", "PUT", worker, "messenger-attachments", "forbidden/grade7-probe", b"no"),
|
|
("worker-create-bucket", "PUT", worker, "grade7-worker-bucket", None, b""),
|
|
("api-create-bucket", "PUT", api, "grade7-api-bucket", None, b""),
|
|
("admin-create-bucket", "PUT", admin, "grade7-admin-bucket", None, b""),
|
|
]
|
|
for label, method, cred, bucket, key, body in checks:
|
|
status, code = request(method, endpoint, cred[0], cred[1], bucket, key, body, cafile)
|
|
print(f"{label} {status} {code}")
|
|
status, code = request("DELETE", endpoint, admin[0], admin[1], "grade7-admin-bucket", None, b"", cafile)
|
|
print(f"admin-delete-bucket {status} {code}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|