Package: 12factor-vault
Vault 12factor and Django integrationThis project provides helper classes for integratingHashicorp Vaultwith your Python projects and
Django.Please note that this is still under active development and APIs are subject
to change.InstallationThis has been uploaded to the Cheeseshop aka Pypi as12factor-vault. So just add12factor-vaultto yourrequirements.txtorsetup.py.pip install12factor-vaultalso works.Environment variablesEnvironment VariableVault auth backendDirect configuration
static method on
BaseVaultAuthenticatorVAULT_TOKENToken authenticationtoken(str)VAULT_APPID, VAULT_USERIDApp-id authenticaionapp_id(str, str)VAULT_ROLEID, VAULT_SECRETIDApprole authenticationapprole(str, str, str,
bool)VAULT_SSLCERT, VAULT_SSLKEYSSL Client authenticationssl_client_cert(str,
str)The Django example below uses the following environment variables:Environment VariableDescriptionVAULT_DATABASE_PATHThe path to Vault’s credential-issuing backendVAULT_CAThe CA issuing Vault’s HTTPS SSL certificate (for
CA pinning)DATABASE_NAMEName of the database to connect to on the database
server.DATABASE_OWNERROLEThe PostgreSQL role to use forSET ROLEafter
connecting to the databaseGeneral usageBasically after configuring aBaseVaultAuthenticatorinstance which creates
authenticated Vault clients (relying on the excellenthvac library) you can use that to createVaultCredentialProviderinstances which manage leases and renew credentials
as needed (e.g. database credentials managed by one of Vault’ssecretsbackends).VaultAuth12Factoris a subclass ofBaseVaultAuthenticatorthat reads
all necessary configuration from environment variables.DjangoIntegrating with Django requires a small monkeypatch that retries failed
database connections after refreshing the database credentials from Vault. Thevault12factorDjango App will install that patch automatically. You also
have to wrap your database settings dict in aDjangoAutoRefreshDBCredentialsDictinstance that knows hot to refresh
database credentials from Vault.vault12factorwill check if an instance ofDjangoAutoRefreshDBCredentialsDictis configured insettings.DATABASESbefore monkey-patching Django. So if you want to usevault12factorbut
configure your databases in separate Django apps or other things that this code
can’t detect, you will want to callvault12factor.monkeypatch_django()yourself.Here is an example for integrating this with Django, using Vault to get
database credentials. When using PostgreSQL you will also want to look atdjango-postgresql-setrole.# in settings.pyINSTALLED_APPS+=['django_dbconn_retry','vault12factor',]fromvault12factorimport\VaultCredentialProvider,\VaultAuth12Factor,\DjangoAutoRefreshDBCredentialsDictifDEBUGandnotVaultAuth12Factor.has_envconfig():SECRET_KEY="secretsekrit"# FOR DEBUG ONLY!DATABASES={'default':{'ENGINE':'django.db.backends.sqlite3','NAME':'authserver.sqlite3',}}else:ifDEBUG:SECRET_KEY="secretsekrit"# FOR DEBUG ONLY!VAULT=VaultAuth12Factor.fromenv()CREDS=VaultCredentialProvider("https://vault.local:8200/",VAULT,os.getenv("VAULT_DATABASE_PATH","db-mydatabase/creds/fullaccess"),os.getenv("VAULT_CA",None),True,DEBUG)DATABASES={'default':DjangoAutoRefreshDBCredentialsDict(CREDS,{'ENGINE':'django.db.backends.postgresql','NAME':os.getenv("DATABASE_NAME","mydatabase"),'USER':CREDS.username,'PASSWORD':CREDS.password,'HOST':'127.0.0.1','PORT':'5432',# requires django-postgresql-setrole'SET_ROLE':os.getenv("DATABASE_OWNERROLE","mydatabaseowner")}),}LicenseCopyright (c) 2016-2017, Jonas Maurus
All rights reserved.Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.