ovh-mitigation: add checking/toggling firewall

This commit is contained in:
Daniel Micay 2024-07-05 00:35:51 -04:00
parent e8403c3098
commit e3c2c1565d

View File

@ -8,14 +8,15 @@ def err(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs) print(*args, file=sys.stderr, **kwargs)
sys.exit(1) sys.exit(1)
if len(sys.argv) != 3: if len(sys.argv) != 4:
err("expected 2 arguments") err("expected 3 arguments")
with open("ovh-mitigation.json") as config: with open("ovh-mitigation.json") as config:
config = json.load(config) config = json.load(config)
action = sys.argv[1] system = sys.argv[1]
ip = sys.argv[2] action = sys.argv[2]
ip = sys.argv[3]
client = ovh.Client( client = ovh.Client(
endpoint=config["endpoint"], endpoint=config["endpoint"],
@ -24,6 +25,7 @@ client = ovh.Client(
consumer_key=config["consumer_key"], consumer_key=config["consumer_key"],
) )
if system == "mitigation":
if action == "status": if action == "status":
result = client.get(f"/ip/{ip}/mitigation") result = client.get(f"/ip/{ip}/mitigation")
if len(result) == 0: if len(result) == 0:
@ -49,3 +51,16 @@ elif action == "disable":
err(json.dumps(result, indent=4) + "\nunexpected result") err(json.dumps(result, indent=4) + "\nunexpected result")
else: else:
err("unknown action: " + action) err("unknown action: " + action)
elif system == "firewall":
if action == "status":
result = client.get(f"/ip/{ip}/firewall/{ip}")
print("enabled: " + str(result["enabled"]))
print("state: " + result["state"])
elif action == "enable":
client.put(f'/ip/{ip}/firewall/{ip}', enabled=True)
elif action == "disable":
client.put(f"/ip/{ip}/firewall/{ip}", enabled=False)
else:
err("unknown action: " + action)
else:
err("unknown system: " + system)