Skip to content

Commit 90ff2f5

Browse files
authored
Merge pull request #25 from reecetech/bugfix/FC-3427-refresh-credentials-on-expiry
Refresh credentials from Vault every hour or as specified in settings
2 parents c299c78 + 87c62b1 commit 90ff2f5

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

README.rst

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ Settings Required
2323

2424
Do not provide `USER` and `PASSWORD`. Instead provide these settings:
2525

26-
============================ =========== ===========
27-
Setting Required Description
28-
============================ =========== ===========
29-
`VAULT_ADDR` Yes The HTTPS endpoint for Vault
30-
`VAULT_PATH` Yes The path in Vault to the KV v2 secret storing the Informix credentials
31-
`VAULT_K8S_AUTH_MOUNT_POINT` No The Vault mount point to use for Kubernetes authentication, default value: ``kubernetes``
32-
`VAULT_K8S_JWT` No The path to the JWT in a K8s container, default vault: ``/var/run/secrets/kubernetes.io/serviceaccount/token``
33-
`VAULT_K8S_ROLE` Conditional Provide the K8s role *if* using K8s JWT authentication to Vault
34-
`VAULT_KVV2_MOUNT_POINT` No The Vault mount point to use for KVv2 secrets, default value: ``secret``
35-
`VAULT_TOKEN` Conditional Provide the token *if* using basic token authentication to Vault
36-
============================ =========== ===========
26+
=================================== =========== ===========
27+
Setting Required Description
28+
=================================== =========== ===========
29+
`VAULT_ADDR` Yes The HTTPS endpoint for Vault
30+
`VAULT_PATH` Yes The path in Vault to the KV v2 secret storing the Informix credentials
31+
`VAULT_K8S_AUTH_MOUNT_POINT` No The Vault mount point to use for Kubernetes authentication, default value: ``kubernetes``
32+
`VAULT_K8S_JWT` No The path to the JWT in a K8s container, default value: ``/var/run/secrets/kubernetes.io/serviceaccount/token``
33+
`VAULT_K8S_ROLE` Conditional Provide the K8s role *if* using K8s JWT authentication to Vault
34+
`VAULT_KVV2_MOUNT_POINT` No The Vault mount point to use for KVv2 secrets, default value: ``secret``
35+
`VAULT_TOKEN` Conditional Provide the token *if* using basic token authentication to Vault
36+
`VAULT_MAXIMUM_CREDENTIAL_LIFETIME` No Time interval (seconds) to force retrieving credentials from Vault, default value: ``3600``
37+
=================================== =========== ===========

django_informixdb_vault/base.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
import os
7+
from datetime import datetime
78

89
import hvac
910

@@ -172,7 +173,19 @@ def get_connection_params(self):
172173
username = self.settings_dict['USER']
173174
password = self.settings_dict['PASSWORD']
174175

175-
if (username and password):
176+
maximum_credential_lifetime = \
177+
self.settings_dict.get('VAULT_MAXIMUM_CREDENTIAL_LIFETIME', 3600)
178+
if maximum_credential_lifetime and 'CREDENTIALS_START_TIME' in self.settings_dict:
179+
elapsed = datetime.now() - self.settings_dict['CREDENTIALS_START_TIME']
180+
credentials_need_refresh = elapsed.seconds >= maximum_credential_lifetime
181+
elif maximum_credential_lifetime:
182+
# Settings configured for refreshes but we don't yet have a credential start time
183+
credentials_need_refresh = True
184+
else:
185+
# Don't refresh if VAULT_MAXIMUM_CREDENTIAL_LIFETIME is set to None
186+
credentials_need_refresh = False
187+
188+
if username and password and not credentials_need_refresh:
176189
conn_params['USER'] = username
177190
conn_params['PASSWORD'] = password
178191

@@ -185,6 +198,7 @@ def get_connection_params(self):
185198
)
186199
self.settings_dict['USER'] = username
187200
self.settings_dict['PASSWORD'] = password
201+
self.settings_dict['CREDENTIALS_START_TIME'] = datetime.now()
188202

189203
conn_params['USER'] = username
190204
conn_params['PASSWORD'] = password

0 commit comments

Comments
 (0)