feat(mimecast): new Mimecast anti-phishing integration

Email Security API, 11 commands: held-message list/release/reject, message
search + info, managed URL create/list (block), block sender, create
remediation, URL decode. HMAC-SHA1 signed auth, stdlib-only.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Guillaume BOURGEOIS
2026-07-11 23:46:35 +02:00
parent f826181704
commit 460dfaefba
12 changed files with 912 additions and 0 deletions
@@ -0,0 +1,75 @@
import json, os, sys, hmac, hashlib, base64, uuid, datetime
import urllib.request, urllib.error
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def call(uri, cfg, data_payload=None):
base_url = str(cfg.get("base_url", "")).rstrip("/")
app_id = str(cfg.get("app_id", ""))
app_key = str(cfg.get("app_key", ""))
access_key = str(cfg.get("access_key", ""))
secret_key = str(cfg.get("secret_key", ""))
request_id = str(uuid.uuid4())
hdr_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
to_sign = hdr_date + ":" + request_id + ":" + uri + ":" + app_key
sig = base64.b64encode(
hmac.new(base64.b64decode(secret_key), to_sign.encode("utf-8"), hashlib.sha1).digest()
).decode("utf-8")
headers = {
"Authorization": "MC " + access_key + ":" + sig,
"x-mc-app-id": app_id,
"x-mc-date": hdr_date,
"x-mc-req-id": request_id,
"Content-Type": "application/json",
"Accept": "application/json",
}
body = {"data": [data_payload] if data_payload is not None else []}
req = urllib.request.Request(base_url + uri, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
raw = r.read()
resp = json.loads(raw) if raw else {}
fails = resp.get("fail") if isinstance(resp, dict) else None
if fails:
# Mimecast returns errors in the 'fail' array even on HTTP 200
raise Exception("Mimecast error: " + json.dumps(fails))
return resp
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def main(cfg, inputs):
sender = inputs.get("sender")
if not sender:
raise Exception("sender is required")
recipient = inputs.get("recipient")
if not recipient:
raise Exception("recipient is required")
action = inputs.get("action")
payload = {
"sender": sender,
"to": recipient,
"action": action or "block",
}
return call("/api/managedsender/permit-or-block-sender", cfg, payload)
_run(main)
@@ -0,0 +1,77 @@
import json, os, sys, hmac, hashlib, base64, uuid, datetime
import urllib.request, urllib.error
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def call(uri, cfg, data_payload=None):
base_url = str(cfg.get("base_url", "")).rstrip("/")
app_id = str(cfg.get("app_id", ""))
app_key = str(cfg.get("app_key", ""))
access_key = str(cfg.get("access_key", ""))
secret_key = str(cfg.get("secret_key", ""))
request_id = str(uuid.uuid4())
hdr_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
to_sign = hdr_date + ":" + request_id + ":" + uri + ":" + app_key
sig = base64.b64encode(
hmac.new(base64.b64decode(secret_key), to_sign.encode("utf-8"), hashlib.sha1).digest()
).decode("utf-8")
headers = {
"Authorization": "MC " + access_key + ":" + sig,
"x-mc-app-id": app_id,
"x-mc-date": hdr_date,
"x-mc-req-id": request_id,
"Content-Type": "application/json",
"Accept": "application/json",
}
body = {"data": [data_payload] if data_payload is not None else []}
req = urllib.request.Request(base_url + uri, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
raw = r.read()
resp = json.loads(raw) if raw else {}
fails = resp.get("fail") if isinstance(resp, dict) else None
if fails:
# Mimecast returns errors in the 'fail' array even on HTTP 200
raise Exception("Mimecast error: " + json.dumps(fails))
return resp
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def main(cfg, inputs):
url = inputs.get("url")
if not url:
raise Exception("url is required")
action = inputs.get("action")
match_type = inputs.get("match_type")
payload = {
"url": url,
"action": action or "block",
"matchType": match_type or "explicit",
}
comment = inputs.get("comment")
if comment:
payload["comment"] = comment
return call("/api/ttp/url/create-managed-url", cfg, payload)
_run(main)
@@ -0,0 +1,76 @@
import json, os, sys, hmac, hashlib, base64, uuid, datetime
import urllib.request, urllib.error
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def call(uri, cfg, data_payload=None):
base_url = str(cfg.get("base_url", "")).rstrip("/")
app_id = str(cfg.get("app_id", ""))
app_key = str(cfg.get("app_key", ""))
access_key = str(cfg.get("access_key", ""))
secret_key = str(cfg.get("secret_key", ""))
request_id = str(uuid.uuid4())
hdr_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
to_sign = hdr_date + ":" + request_id + ":" + uri + ":" + app_key
sig = base64.b64encode(
hmac.new(base64.b64decode(secret_key), to_sign.encode("utf-8"), hashlib.sha1).digest()
).decode("utf-8")
headers = {
"Authorization": "MC " + access_key + ":" + sig,
"x-mc-app-id": app_id,
"x-mc-date": hdr_date,
"x-mc-req-id": request_id,
"Content-Type": "application/json",
"Accept": "application/json",
}
body = {"data": [data_payload] if data_payload is not None else []}
req = urllib.request.Request(base_url + uri, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
raw = r.read()
resp = json.loads(raw) if raw else {}
fails = resp.get("fail") if isinstance(resp, dict) else None
if fails:
# Mimecast returns errors in the 'fail' array even on HTTP 200
raise Exception("Mimecast error: " + json.dumps(fails))
return resp
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def main(cfg, inputs):
message_hash = inputs.get("message_hash")
message_id = inputs.get("message_id")
if not message_hash and not message_id:
raise Exception("message_hash or message_id is required")
payload = {}
if message_hash:
payload["hash"] = message_hash
if message_id:
payload["messageId"] = message_id
reason = inputs.get("reason")
if reason:
payload["reason"] = reason
return call("/api/ttp/remediation/create", cfg, payload)
_run(main)
@@ -0,0 +1,65 @@
import json, os, sys, hmac, hashlib, base64, uuid, datetime
import urllib.request, urllib.error
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def call(uri, cfg, data_payload=None):
base_url = str(cfg.get("base_url", "")).rstrip("/")
app_id = str(cfg.get("app_id", ""))
app_key = str(cfg.get("app_key", ""))
access_key = str(cfg.get("access_key", ""))
secret_key = str(cfg.get("secret_key", ""))
request_id = str(uuid.uuid4())
hdr_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
to_sign = hdr_date + ":" + request_id + ":" + uri + ":" + app_key
sig = base64.b64encode(
hmac.new(base64.b64decode(secret_key), to_sign.encode("utf-8"), hashlib.sha1).digest()
).decode("utf-8")
headers = {
"Authorization": "MC " + access_key + ":" + sig,
"x-mc-app-id": app_id,
"x-mc-date": hdr_date,
"x-mc-req-id": request_id,
"Content-Type": "application/json",
"Accept": "application/json",
}
body = {"data": [data_payload] if data_payload is not None else []}
req = urllib.request.Request(base_url + uri, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
raw = r.read()
resp = json.loads(raw) if raw else {}
fails = resp.get("fail") if isinstance(resp, dict) else None
if fails:
# Mimecast returns errors in the 'fail' array even on HTTP 200
raise Exception("Mimecast error: " + json.dumps(fails))
return resp
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def main(cfg, inputs):
url = inputs.get("url")
if not url:
raise Exception("url is required")
return call("/api/ttp/url/decode-url", cfg, {"url": url})
_run(main)
@@ -0,0 +1,65 @@
import json, os, sys, hmac, hashlib, base64, uuid, datetime
import urllib.request, urllib.error
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def call(uri, cfg, data_payload=None):
base_url = str(cfg.get("base_url", "")).rstrip("/")
app_id = str(cfg.get("app_id", ""))
app_key = str(cfg.get("app_key", ""))
access_key = str(cfg.get("access_key", ""))
secret_key = str(cfg.get("secret_key", ""))
request_id = str(uuid.uuid4())
hdr_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
to_sign = hdr_date + ":" + request_id + ":" + uri + ":" + app_key
sig = base64.b64encode(
hmac.new(base64.b64decode(secret_key), to_sign.encode("utf-8"), hashlib.sha1).digest()
).decode("utf-8")
headers = {
"Authorization": "MC " + access_key + ":" + sig,
"x-mc-app-id": app_id,
"x-mc-date": hdr_date,
"x-mc-req-id": request_id,
"Content-Type": "application/json",
"Accept": "application/json",
}
body = {"data": [data_payload] if data_payload is not None else []}
req = urllib.request.Request(base_url + uri, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
raw = r.read()
resp = json.loads(raw) if raw else {}
fails = resp.get("fail") if isinstance(resp, dict) else None
if fails:
# Mimecast returns errors in the 'fail' array even on HTTP 200
raise Exception("Mimecast error: " + json.dumps(fails))
return resp
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def main(cfg, inputs):
message_id = inputs.get("message_id")
if not message_id:
raise Exception("message_id is required")
return call("/api/message-finder/get-message-info", cfg, {"id": message_id})
_run(main)
@@ -0,0 +1,72 @@
import json, os, sys, hmac, hashlib, base64, uuid, datetime
import urllib.request, urllib.error
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def call(uri, cfg, data_payload=None):
base_url = str(cfg.get("base_url", "")).rstrip("/")
app_id = str(cfg.get("app_id", ""))
app_key = str(cfg.get("app_key", ""))
access_key = str(cfg.get("access_key", ""))
secret_key = str(cfg.get("secret_key", ""))
request_id = str(uuid.uuid4())
hdr_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
to_sign = hdr_date + ":" + request_id + ":" + uri + ":" + app_key
sig = base64.b64encode(
hmac.new(base64.b64decode(secret_key), to_sign.encode("utf-8"), hashlib.sha1).digest()
).decode("utf-8")
headers = {
"Authorization": "MC " + access_key + ":" + sig,
"x-mc-app-id": app_id,
"x-mc-date": hdr_date,
"x-mc-req-id": request_id,
"Content-Type": "application/json",
"Accept": "application/json",
}
body = {"data": [data_payload] if data_payload is not None else []}
req = urllib.request.Request(base_url + uri, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
raw = r.read()
resp = json.loads(raw) if raw else {}
fails = resp.get("fail") if isinstance(resp, dict) else None
if fails:
# Mimecast returns errors in the 'fail' array even on HTTP 200
raise Exception("Mimecast error: " + json.dumps(fails))
return resp
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def main(cfg, inputs):
admin = inputs.get("admin")
if admin is None:
admin = True
limit = inputs.get("limit")
if limit is None:
limit = 100
payload = {
"admin": bool(admin),
"pagination": {"pageSize": int(limit)},
}
return call("/api/gateway/get-hold-message-list", cfg, payload)
_run(main)
@@ -0,0 +1,62 @@
import json, os, sys, hmac, hashlib, base64, uuid, datetime
import urllib.request, urllib.error
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def call(uri, cfg, data_payload=None):
base_url = str(cfg.get("base_url", "")).rstrip("/")
app_id = str(cfg.get("app_id", ""))
app_key = str(cfg.get("app_key", ""))
access_key = str(cfg.get("access_key", ""))
secret_key = str(cfg.get("secret_key", ""))
request_id = str(uuid.uuid4())
hdr_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
to_sign = hdr_date + ":" + request_id + ":" + uri + ":" + app_key
sig = base64.b64encode(
hmac.new(base64.b64decode(secret_key), to_sign.encode("utf-8"), hashlib.sha1).digest()
).decode("utf-8")
headers = {
"Authorization": "MC " + access_key + ":" + sig,
"x-mc-app-id": app_id,
"x-mc-date": hdr_date,
"x-mc-req-id": request_id,
"Content-Type": "application/json",
"Accept": "application/json",
}
body = {"data": [data_payload] if data_payload is not None else []}
req = urllib.request.Request(base_url + uri, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
raw = r.read()
resp = json.loads(raw) if raw else {}
fails = resp.get("fail") if isinstance(resp, dict) else None
if fails:
# Mimecast returns errors in the 'fail' array even on HTTP 200
raise Exception("Mimecast error: " + json.dumps(fails))
return resp
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def main(cfg, inputs):
return call("/api/ttp/url/get-all-managed-urls", cfg, None)
_run(main)
@@ -0,0 +1,69 @@
import json, os, sys, hmac, hashlib, base64, uuid, datetime
import urllib.request, urllib.error
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def call(uri, cfg, data_payload=None):
base_url = str(cfg.get("base_url", "")).rstrip("/")
app_id = str(cfg.get("app_id", ""))
app_key = str(cfg.get("app_key", ""))
access_key = str(cfg.get("access_key", ""))
secret_key = str(cfg.get("secret_key", ""))
request_id = str(uuid.uuid4())
hdr_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
to_sign = hdr_date + ":" + request_id + ":" + uri + ":" + app_key
sig = base64.b64encode(
hmac.new(base64.b64decode(secret_key), to_sign.encode("utf-8"), hashlib.sha1).digest()
).decode("utf-8")
headers = {
"Authorization": "MC " + access_key + ":" + sig,
"x-mc-app-id": app_id,
"x-mc-date": hdr_date,
"x-mc-req-id": request_id,
"Content-Type": "application/json",
"Accept": "application/json",
}
body = {"data": [data_payload] if data_payload is not None else []}
req = urllib.request.Request(base_url + uri, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
raw = r.read()
resp = json.loads(raw) if raw else {}
fails = resp.get("fail") if isinstance(resp, dict) else None
if fails:
# Mimecast returns errors in the 'fail' array even on HTTP 200
raise Exception("Mimecast error: " + json.dumps(fails))
return resp
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def main(cfg, inputs):
message_id = inputs.get("message_id")
if not message_id:
raise Exception("message_id is required")
payload = {"id": message_id}
reason = inputs.get("reason")
if reason:
payload["message"] = reason
return call("/api/gateway/hold-reject", cfg, payload)
_run(main)
@@ -0,0 +1,65 @@
import json, os, sys, hmac, hashlib, base64, uuid, datetime
import urllib.request, urllib.error
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def call(uri, cfg, data_payload=None):
base_url = str(cfg.get("base_url", "")).rstrip("/")
app_id = str(cfg.get("app_id", ""))
app_key = str(cfg.get("app_key", ""))
access_key = str(cfg.get("access_key", ""))
secret_key = str(cfg.get("secret_key", ""))
request_id = str(uuid.uuid4())
hdr_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
to_sign = hdr_date + ":" + request_id + ":" + uri + ":" + app_key
sig = base64.b64encode(
hmac.new(base64.b64decode(secret_key), to_sign.encode("utf-8"), hashlib.sha1).digest()
).decode("utf-8")
headers = {
"Authorization": "MC " + access_key + ":" + sig,
"x-mc-app-id": app_id,
"x-mc-date": hdr_date,
"x-mc-req-id": request_id,
"Content-Type": "application/json",
"Accept": "application/json",
}
body = {"data": [data_payload] if data_payload is not None else []}
req = urllib.request.Request(base_url + uri, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
raw = r.read()
resp = json.loads(raw) if raw else {}
fails = resp.get("fail") if isinstance(resp, dict) else None
if fails:
# Mimecast returns errors in the 'fail' array even on HTTP 200
raise Exception("Mimecast error: " + json.dumps(fails))
return resp
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def main(cfg, inputs):
message_id = inputs.get("message_id")
if not message_id:
raise Exception("message_id is required")
return call("/api/gateway/hold-release", cfg, {"id": message_id})
_run(main)
@@ -0,0 +1,80 @@
import json, os, sys, hmac, hashlib, base64, uuid, datetime
import urllib.request, urllib.error
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def call(uri, cfg, data_payload=None):
base_url = str(cfg.get("base_url", "")).rstrip("/")
app_id = str(cfg.get("app_id", ""))
app_key = str(cfg.get("app_key", ""))
access_key = str(cfg.get("access_key", ""))
secret_key = str(cfg.get("secret_key", ""))
request_id = str(uuid.uuid4())
hdr_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
to_sign = hdr_date + ":" + request_id + ":" + uri + ":" + app_key
sig = base64.b64encode(
hmac.new(base64.b64decode(secret_key), to_sign.encode("utf-8"), hashlib.sha1).digest()
).decode("utf-8")
headers = {
"Authorization": "MC " + access_key + ":" + sig,
"x-mc-app-id": app_id,
"x-mc-date": hdr_date,
"x-mc-req-id": request_id,
"Content-Type": "application/json",
"Accept": "application/json",
}
body = {"data": [data_payload] if data_payload is not None else []}
req = urllib.request.Request(base_url + uri, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
raw = r.read()
resp = json.loads(raw) if raw else {}
fails = resp.get("fail") if isinstance(resp, dict) else None
if fails:
# Mimecast returns errors in the 'fail' array even on HTTP 200
raise Exception("Mimecast error: " + json.dumps(fails))
return resp
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def main(cfg, inputs):
from_addr = inputs.get("from")
to_addr = inputs.get("to")
subject = inputs.get("subject")
start = inputs.get("start")
end = inputs.get("end")
payload = {}
if from_addr:
payload["from"] = from_addr
if to_addr:
payload["to"] = to_addr
if subject:
payload["subject"] = subject
if start:
payload["start"] = start
if end:
payload["end"] = end
return call("/api/message-finder/search", cfg, payload)
_run(main)
@@ -0,0 +1,63 @@
import json, os, sys, hmac, hashlib, base64, uuid, datetime
import urllib.request, urllib.error
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def call(uri, cfg, data_payload=None):
base_url = str(cfg.get("base_url", "")).rstrip("/")
app_id = str(cfg.get("app_id", ""))
app_key = str(cfg.get("app_key", ""))
access_key = str(cfg.get("access_key", ""))
secret_key = str(cfg.get("secret_key", ""))
request_id = str(uuid.uuid4())
hdr_date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
to_sign = hdr_date + ":" + request_id + ":" + uri + ":" + app_key
sig = base64.b64encode(
hmac.new(base64.b64decode(secret_key), to_sign.encode("utf-8"), hashlib.sha1).digest()
).decode("utf-8")
headers = {
"Authorization": "MC " + access_key + ":" + sig,
"x-mc-app-id": app_id,
"x-mc-date": hdr_date,
"x-mc-req-id": request_id,
"Content-Type": "application/json",
"Accept": "application/json",
}
body = {"data": [data_payload] if data_payload is not None else []}
req = urllib.request.Request(base_url + uri, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=60) as r:
raw = r.read()
resp = json.loads(raw) if raw else {}
fails = resp.get("fail") if isinstance(resp, dict) else None
if fails:
# Mimecast returns errors in the 'fail' array even on HTTP 200
raise Exception("Mimecast error: " + json.dumps(fails))
return resp
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def main(cfg, inputs):
call("/api/account/get-account", cfg, None)
return {"ok": True}
_run(main)