From 4be9cbaeb2cd94cd1192ab3a1a74b8b3a2a691e8 Mon Sep 17 00:00:00 2001 From: Matt Dietz Date: Thu, 4 Mar 2021 09:56:27 -0600 Subject: [PATCH] Adds support for CONSUL_HTTP_TOKEN_FILE Adds support for CONSUL_HTTP_TOKEN_FILE, which mirrors functionality found into the official Go client by Hashicorp. If defined, the CONSUL_HTTP_TOKEN_FILE will override tokens passed in any other way to the client. --- consul/base.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/consul/base.py b/consul/base.py index 9d27dbd..351263d 100755 --- a/consul/base.py +++ b/consul/base.py @@ -398,6 +398,16 @@ def __init__( self.snapshot = Consul.Snapshot(self) self.status = Consul.Status(self) self.token = os.getenv('CONSUL_HTTP_TOKEN', token) + + # Token file preempts passed tokens any other way, if it exists + if 'CONSUL_HTTP_TOKEN_FILE' in os.environ: + token_file = os.environ['CONSUL_HTTP_TOKEN_FILE'] + assert os.path.exists(token_file) + with open(token_file, 'r') as f: + tok = f.readline().strip() + if len(tok) != 0: + self.token = tok + self.txn = Consul.Txn(self) class ACL(object):