feat(cisco-fmc): new Cisco Firepower Management Center integration
FMC REST API, 7 commands: list/create/delete host objects, list access policies/rules, create block access rule. Token auth (generatetoken, token + domain UUID from response headers), stdlib-only. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
import json, os, sys, base64, ssl, urllib.parse, 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 _ctx(cfg):
|
||||
if cfg.get("insecure"):
|
||||
c = ssl.create_default_context()
|
||||
c.check_hostname = False
|
||||
c.verify_mode = ssl.CERT_NONE
|
||||
return c
|
||||
return None
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.base = str(cfg.get("base_url", "")).rstrip("/")
|
||||
self.ctx = _ctx(cfg)
|
||||
self.token = None
|
||||
self.domain_uuid = None
|
||||
|
||||
def authenticate(self):
|
||||
creds = str(self.cfg.get("username", "")) + ":" + str(self.cfg.get("password", ""))
|
||||
basic = base64.b64encode(creds.encode("utf-8")).decode("utf-8")
|
||||
req = urllib.request.Request(self.base + "/api/fmc_platform/v1/auth/generatetoken",
|
||||
data=b"", headers={"Authorization": "Basic " + basic}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60, context=self.ctx) as r:
|
||||
r.read()
|
||||
self.token = r.headers.get("X-auth-access-token")
|
||||
self.domain_uuid = r.headers.get("DOMAIN_UUID")
|
||||
if not self.token or not self.domain_uuid:
|
||||
raise Exception("Authentication failed: missing token or domain UUID")
|
||||
|
||||
def _cfg_base(self):
|
||||
return self.base + "/api/fmc_config/v1/domain/" + self.domain_uuid
|
||||
|
||||
def call(self, method, path, body=None, params=None):
|
||||
# path is relative to the domain config base, e.g. /object/hosts
|
||||
url = self._cfg_base() + path
|
||||
if params:
|
||||
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||
if clean:
|
||||
url += "?" + urllib.parse.urlencode(clean)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
headers = {"X-auth-access-token": self.token, "Accept": "application/json"}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
with urllib.request.urlopen(req, timeout=90, context=self.ctx) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def _run(fn):
|
||||
try:
|
||||
cfg = _cfg()
|
||||
inputs = _inputs()
|
||||
client = Client(cfg)
|
||||
client.authenticate()
|
||||
print(json.dumps(fn(client, 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)
|
||||
|
||||
|
||||
q = lambda v: urllib.parse.quote(str(v), safe="")
|
||||
|
||||
|
||||
def _csv(v):
|
||||
return [s.strip() for s in str(v).split(",") if s.strip()]
|
||||
|
||||
|
||||
def main(client, inputs):
|
||||
policy_id = inputs.get("policy_id")
|
||||
name = inputs.get("name")
|
||||
action = inputs.get("action")
|
||||
source_object_ids = inputs.get("source_object_ids")
|
||||
destination_object_ids = inputs.get("destination_object_ids")
|
||||
enabled = inputs.get("enabled")
|
||||
if not policy_id:
|
||||
raise Exception("policy_id is required")
|
||||
if not name:
|
||||
raise Exception("name is required")
|
||||
|
||||
body = {
|
||||
"name": name,
|
||||
"action": (action or "BLOCK"),
|
||||
"enabled": (enabled if enabled is not None else True),
|
||||
"type": "AccessRule",
|
||||
}
|
||||
|
||||
if source_object_ids:
|
||||
source_ids_list = _csv(source_object_ids)
|
||||
if source_ids_list:
|
||||
body["sourceNetworks"] = {"objects": [{"id": oid, "type": "Host"} for oid in source_ids_list]}
|
||||
|
||||
if destination_object_ids:
|
||||
dest_ids_list = _csv(destination_object_ids)
|
||||
if dest_ids_list:
|
||||
body["destinationNetworks"] = {"objects": [{"id": oid, "type": "Host"} for oid in dest_ids_list]}
|
||||
|
||||
return client.call("POST", "/policy/accesspolicies/" + q(policy_id) + "/accessrules", body=body)
|
||||
|
||||
|
||||
_run(main)
|
||||
@@ -0,0 +1,90 @@
|
||||
import json, os, sys, base64, ssl, urllib.parse, 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 _ctx(cfg):
|
||||
if cfg.get("insecure"):
|
||||
c = ssl.create_default_context()
|
||||
c.check_hostname = False
|
||||
c.verify_mode = ssl.CERT_NONE
|
||||
return c
|
||||
return None
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.base = str(cfg.get("base_url", "")).rstrip("/")
|
||||
self.ctx = _ctx(cfg)
|
||||
self.token = None
|
||||
self.domain_uuid = None
|
||||
|
||||
def authenticate(self):
|
||||
creds = str(self.cfg.get("username", "")) + ":" + str(self.cfg.get("password", ""))
|
||||
basic = base64.b64encode(creds.encode("utf-8")).decode("utf-8")
|
||||
req = urllib.request.Request(self.base + "/api/fmc_platform/v1/auth/generatetoken",
|
||||
data=b"", headers={"Authorization": "Basic " + basic}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60, context=self.ctx) as r:
|
||||
r.read()
|
||||
self.token = r.headers.get("X-auth-access-token")
|
||||
self.domain_uuid = r.headers.get("DOMAIN_UUID")
|
||||
if not self.token or not self.domain_uuid:
|
||||
raise Exception("Authentication failed: missing token or domain UUID")
|
||||
|
||||
def _cfg_base(self):
|
||||
return self.base + "/api/fmc_config/v1/domain/" + self.domain_uuid
|
||||
|
||||
def call(self, method, path, body=None, params=None):
|
||||
# path is relative to the domain config base, e.g. /object/hosts
|
||||
url = self._cfg_base() + path
|
||||
if params:
|
||||
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||
if clean:
|
||||
url += "?" + urllib.parse.urlencode(clean)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
headers = {"X-auth-access-token": self.token, "Accept": "application/json"}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
with urllib.request.urlopen(req, timeout=90, context=self.ctx) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def _run(fn):
|
||||
try:
|
||||
cfg = _cfg()
|
||||
inputs = _inputs()
|
||||
client = Client(cfg)
|
||||
client.authenticate()
|
||||
print(json.dumps(fn(client, 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(client, inputs):
|
||||
name = inputs.get("name")
|
||||
value = inputs.get("value")
|
||||
description = inputs.get("description")
|
||||
if not name:
|
||||
raise Exception("name is required")
|
||||
if not value:
|
||||
raise Exception("value is required")
|
||||
body = {"name": name, "type": "Host", "value": value}
|
||||
if description:
|
||||
body["description"] = description
|
||||
return client.call("POST", "/object/hosts", body=body)
|
||||
|
||||
|
||||
_run(main)
|
||||
@@ -0,0 +1,86 @@
|
||||
import json, os, sys, base64, ssl, urllib.parse, 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 _ctx(cfg):
|
||||
if cfg.get("insecure"):
|
||||
c = ssl.create_default_context()
|
||||
c.check_hostname = False
|
||||
c.verify_mode = ssl.CERT_NONE
|
||||
return c
|
||||
return None
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.base = str(cfg.get("base_url", "")).rstrip("/")
|
||||
self.ctx = _ctx(cfg)
|
||||
self.token = None
|
||||
self.domain_uuid = None
|
||||
|
||||
def authenticate(self):
|
||||
creds = str(self.cfg.get("username", "")) + ":" + str(self.cfg.get("password", ""))
|
||||
basic = base64.b64encode(creds.encode("utf-8")).decode("utf-8")
|
||||
req = urllib.request.Request(self.base + "/api/fmc_platform/v1/auth/generatetoken",
|
||||
data=b"", headers={"Authorization": "Basic " + basic}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60, context=self.ctx) as r:
|
||||
r.read()
|
||||
self.token = r.headers.get("X-auth-access-token")
|
||||
self.domain_uuid = r.headers.get("DOMAIN_UUID")
|
||||
if not self.token or not self.domain_uuid:
|
||||
raise Exception("Authentication failed: missing token or domain UUID")
|
||||
|
||||
def _cfg_base(self):
|
||||
return self.base + "/api/fmc_config/v1/domain/" + self.domain_uuid
|
||||
|
||||
def call(self, method, path, body=None, params=None):
|
||||
# path is relative to the domain config base, e.g. /object/hosts
|
||||
url = self._cfg_base() + path
|
||||
if params:
|
||||
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||
if clean:
|
||||
url += "?" + urllib.parse.urlencode(clean)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
headers = {"X-auth-access-token": self.token, "Accept": "application/json"}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
with urllib.request.urlopen(req, timeout=90, context=self.ctx) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def _run(fn):
|
||||
try:
|
||||
cfg = _cfg()
|
||||
inputs = _inputs()
|
||||
client = Client(cfg)
|
||||
client.authenticate()
|
||||
print(json.dumps(fn(client, 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)
|
||||
|
||||
|
||||
q = lambda v: urllib.parse.quote(str(v), safe="")
|
||||
|
||||
|
||||
def main(client, inputs):
|
||||
object_id = inputs.get("object_id")
|
||||
if not object_id:
|
||||
raise Exception("object_id is required")
|
||||
return client.call("DELETE", "/object/hosts/" + q(object_id))
|
||||
|
||||
|
||||
_run(main)
|
||||
@@ -0,0 +1,81 @@
|
||||
import json, os, sys, base64, ssl, urllib.parse, 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 _ctx(cfg):
|
||||
if cfg.get("insecure"):
|
||||
c = ssl.create_default_context()
|
||||
c.check_hostname = False
|
||||
c.verify_mode = ssl.CERT_NONE
|
||||
return c
|
||||
return None
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.base = str(cfg.get("base_url", "")).rstrip("/")
|
||||
self.ctx = _ctx(cfg)
|
||||
self.token = None
|
||||
self.domain_uuid = None
|
||||
|
||||
def authenticate(self):
|
||||
creds = str(self.cfg.get("username", "")) + ":" + str(self.cfg.get("password", ""))
|
||||
basic = base64.b64encode(creds.encode("utf-8")).decode("utf-8")
|
||||
req = urllib.request.Request(self.base + "/api/fmc_platform/v1/auth/generatetoken",
|
||||
data=b"", headers={"Authorization": "Basic " + basic}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60, context=self.ctx) as r:
|
||||
r.read()
|
||||
self.token = r.headers.get("X-auth-access-token")
|
||||
self.domain_uuid = r.headers.get("DOMAIN_UUID")
|
||||
if not self.token or not self.domain_uuid:
|
||||
raise Exception("Authentication failed: missing token or domain UUID")
|
||||
|
||||
def _cfg_base(self):
|
||||
return self.base + "/api/fmc_config/v1/domain/" + self.domain_uuid
|
||||
|
||||
def call(self, method, path, body=None, params=None):
|
||||
# path is relative to the domain config base, e.g. /object/hosts
|
||||
url = self._cfg_base() + path
|
||||
if params:
|
||||
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||
if clean:
|
||||
url += "?" + urllib.parse.urlencode(clean)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
headers = {"X-auth-access-token": self.token, "Accept": "application/json"}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
with urllib.request.urlopen(req, timeout=90, context=self.ctx) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def _run(fn):
|
||||
try:
|
||||
cfg = _cfg()
|
||||
inputs = _inputs()
|
||||
client = Client(cfg)
|
||||
client.authenticate()
|
||||
print(json.dumps(fn(client, 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(client, inputs):
|
||||
limit = inputs.get("limit")
|
||||
return client.call("GET", "/policy/accesspolicies", params={"limit": int(limit or 50)})
|
||||
|
||||
|
||||
_run(main)
|
||||
@@ -0,0 +1,88 @@
|
||||
import json, os, sys, base64, ssl, urllib.parse, 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 _ctx(cfg):
|
||||
if cfg.get("insecure"):
|
||||
c = ssl.create_default_context()
|
||||
c.check_hostname = False
|
||||
c.verify_mode = ssl.CERT_NONE
|
||||
return c
|
||||
return None
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.base = str(cfg.get("base_url", "")).rstrip("/")
|
||||
self.ctx = _ctx(cfg)
|
||||
self.token = None
|
||||
self.domain_uuid = None
|
||||
|
||||
def authenticate(self):
|
||||
creds = str(self.cfg.get("username", "")) + ":" + str(self.cfg.get("password", ""))
|
||||
basic = base64.b64encode(creds.encode("utf-8")).decode("utf-8")
|
||||
req = urllib.request.Request(self.base + "/api/fmc_platform/v1/auth/generatetoken",
|
||||
data=b"", headers={"Authorization": "Basic " + basic}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60, context=self.ctx) as r:
|
||||
r.read()
|
||||
self.token = r.headers.get("X-auth-access-token")
|
||||
self.domain_uuid = r.headers.get("DOMAIN_UUID")
|
||||
if not self.token or not self.domain_uuid:
|
||||
raise Exception("Authentication failed: missing token or domain UUID")
|
||||
|
||||
def _cfg_base(self):
|
||||
return self.base + "/api/fmc_config/v1/domain/" + self.domain_uuid
|
||||
|
||||
def call(self, method, path, body=None, params=None):
|
||||
# path is relative to the domain config base, e.g. /object/hosts
|
||||
url = self._cfg_base() + path
|
||||
if params:
|
||||
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||
if clean:
|
||||
url += "?" + urllib.parse.urlencode(clean)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
headers = {"X-auth-access-token": self.token, "Accept": "application/json"}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
with urllib.request.urlopen(req, timeout=90, context=self.ctx) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def _run(fn):
|
||||
try:
|
||||
cfg = _cfg()
|
||||
inputs = _inputs()
|
||||
client = Client(cfg)
|
||||
client.authenticate()
|
||||
print(json.dumps(fn(client, 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)
|
||||
|
||||
|
||||
q = lambda v: urllib.parse.quote(str(v), safe="")
|
||||
|
||||
|
||||
def main(client, inputs):
|
||||
policy_id = inputs.get("policy_id")
|
||||
limit = inputs.get("limit")
|
||||
if not policy_id:
|
||||
raise Exception("policy_id is required")
|
||||
return client.call("GET", "/policy/accesspolicies/" + q(policy_id) + "/accessrules",
|
||||
params={"limit": int(limit or 50), "expanded": "true"})
|
||||
|
||||
|
||||
_run(main)
|
||||
@@ -0,0 +1,81 @@
|
||||
import json, os, sys, base64, ssl, urllib.parse, 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 _ctx(cfg):
|
||||
if cfg.get("insecure"):
|
||||
c = ssl.create_default_context()
|
||||
c.check_hostname = False
|
||||
c.verify_mode = ssl.CERT_NONE
|
||||
return c
|
||||
return None
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.base = str(cfg.get("base_url", "")).rstrip("/")
|
||||
self.ctx = _ctx(cfg)
|
||||
self.token = None
|
||||
self.domain_uuid = None
|
||||
|
||||
def authenticate(self):
|
||||
creds = str(self.cfg.get("username", "")) + ":" + str(self.cfg.get("password", ""))
|
||||
basic = base64.b64encode(creds.encode("utf-8")).decode("utf-8")
|
||||
req = urllib.request.Request(self.base + "/api/fmc_platform/v1/auth/generatetoken",
|
||||
data=b"", headers={"Authorization": "Basic " + basic}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60, context=self.ctx) as r:
|
||||
r.read()
|
||||
self.token = r.headers.get("X-auth-access-token")
|
||||
self.domain_uuid = r.headers.get("DOMAIN_UUID")
|
||||
if not self.token or not self.domain_uuid:
|
||||
raise Exception("Authentication failed: missing token or domain UUID")
|
||||
|
||||
def _cfg_base(self):
|
||||
return self.base + "/api/fmc_config/v1/domain/" + self.domain_uuid
|
||||
|
||||
def call(self, method, path, body=None, params=None):
|
||||
# path is relative to the domain config base, e.g. /object/hosts
|
||||
url = self._cfg_base() + path
|
||||
if params:
|
||||
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||
if clean:
|
||||
url += "?" + urllib.parse.urlencode(clean)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
headers = {"X-auth-access-token": self.token, "Accept": "application/json"}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
with urllib.request.urlopen(req, timeout=90, context=self.ctx) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def _run(fn):
|
||||
try:
|
||||
cfg = _cfg()
|
||||
inputs = _inputs()
|
||||
client = Client(cfg)
|
||||
client.authenticate()
|
||||
print(json.dumps(fn(client, 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(client, inputs):
|
||||
limit = inputs.get("limit")
|
||||
return client.call("GET", "/object/hosts", params={"limit": int(limit or 50), "expanded": "true"})
|
||||
|
||||
|
||||
_run(main)
|
||||
@@ -0,0 +1,81 @@
|
||||
import json, os, sys, base64, ssl, urllib.parse, 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 _ctx(cfg):
|
||||
if cfg.get("insecure"):
|
||||
c = ssl.create_default_context()
|
||||
c.check_hostname = False
|
||||
c.verify_mode = ssl.CERT_NONE
|
||||
return c
|
||||
return None
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.base = str(cfg.get("base_url", "")).rstrip("/")
|
||||
self.ctx = _ctx(cfg)
|
||||
self.token = None
|
||||
self.domain_uuid = None
|
||||
|
||||
def authenticate(self):
|
||||
creds = str(self.cfg.get("username", "")) + ":" + str(self.cfg.get("password", ""))
|
||||
basic = base64.b64encode(creds.encode("utf-8")).decode("utf-8")
|
||||
req = urllib.request.Request(self.base + "/api/fmc_platform/v1/auth/generatetoken",
|
||||
data=b"", headers={"Authorization": "Basic " + basic}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60, context=self.ctx) as r:
|
||||
r.read()
|
||||
self.token = r.headers.get("X-auth-access-token")
|
||||
self.domain_uuid = r.headers.get("DOMAIN_UUID")
|
||||
if not self.token or not self.domain_uuid:
|
||||
raise Exception("Authentication failed: missing token or domain UUID")
|
||||
|
||||
def _cfg_base(self):
|
||||
return self.base + "/api/fmc_config/v1/domain/" + self.domain_uuid
|
||||
|
||||
def call(self, method, path, body=None, params=None):
|
||||
# path is relative to the domain config base, e.g. /object/hosts
|
||||
url = self._cfg_base() + path
|
||||
if params:
|
||||
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
||||
if clean:
|
||||
url += "?" + urllib.parse.urlencode(clean)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
headers = {"X-auth-access-token": self.token, "Accept": "application/json"}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
with urllib.request.urlopen(req, timeout=90, context=self.ctx) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def _run(fn):
|
||||
try:
|
||||
cfg = _cfg()
|
||||
inputs = _inputs()
|
||||
client = Client(cfg)
|
||||
client.authenticate()
|
||||
print(json.dumps(fn(client, 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(client, inputs):
|
||||
client.call("GET", "/object/hosts", params={"limit": 1})
|
||||
return {"ok": True, "domain_uuid": client.domain_uuid}
|
||||
|
||||
|
||||
_run(main)
|
||||
Reference in New Issue
Block a user