From 9460728b18c648e390390d888ac856628366a521 Mon Sep 17 00:00:00 2001 From: Oyvind Albrigtsen Date: Thu, 7 Aug 2025 14:53:30 +0200 Subject: [PATCH] fence_ibm_vpc: add apikey file support --- agents/ibm_vpc/fence_ibm_vpc.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/agents/ibm_vpc/fence_ibm_vpc.py b/agents/ibm_vpc/fence_ibm_vpc.py index efda5eed7..a87e9e6dc 100755 --- a/agents/ibm_vpc/fence_ibm_vpc.py +++ b/agents/ibm_vpc/fence_ibm_vpc.py @@ -7,7 +7,7 @@ import hashlib sys.path.append("@FENCEAGENTSLIBDIR@") from fencing import * -from fencing import fail, run_delay, EC_LOGIN_DENIED, EC_STATUS, EC_GENERIC_ERROR +from fencing import fail, run_delay, EC_BAD_ARGS, EC_LOGIN_DENIED, EC_STATUS, EC_GENERIC_ERROR state = { "running": "on", @@ -315,6 +315,27 @@ def main(): #### run_delay(options) + if options["--apikey"][0] == '@': + key_file = options["--apikey"][1:] + try: + # read the API key from a file + with open(key_file, "r") as f: + try: + keys = json.loads(f.read()) + # data seems to be in json format + # return the value of the item with the key 'Apikey' + options["--apikey"] = keys.get("Apikey", "") + if not options["--apikey"]: + # backward compatibility: former key name was 'apikey' + options["--apikey"] = keys.get("apikey", "") + # data is text, return as is + except ValueError: + f.seek(0) + options["--apikey"] = f.read().strip() + except FileNotFoundError: + logging.error("Failed: Cannot open file {}".format(key_file)) + sys.exit(EC_BAD_ARGS) + conn = connect(options) atexit.register(disconnect, conn)