All Projects → jdelic → django-postgresql-setrole

jdelic / django-postgresql-setrole

Licence: BSD-3-Clause license
Executes SET ROLE on every database connection opened by Django.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to django-postgresql-setrole

teamcity-hashicorp-vault-plugin
TeamCity plugin to support HashiCorp Vault
Stars: ✭ 23 (+21.05%)
Mutual labels:  hashicorp-vault
letsencrypt-to-vault
Renew or get Let's Encrypt certificates and send it to Hashicorp Vault
Stars: ✭ 84 (+342.11%)
Mutual labels:  hashicorp-vault
vault-pki-monitor-venafi
Venafi PKI Monitoring Secrets Engine for HashiCorp Vault that enforces security policy and provides certificate visiblity to the enterprise.
Stars: ✭ 18 (-5.26%)
Mutual labels:  hashicorp-vault
pico
A Git-driven task runner built to facilitate GitOps and Infrastructure-as-Code while securely passing secrets to tasks.
Stars: ✭ 51 (+168.42%)
Mutual labels:  hashicorp-vault
k8s-vault-webhook
A k8s vault webhook is a Kubernetes webhook that can inject secrets into Kubernetes resources by connecting to multiple secret managers
Stars: ✭ 107 (+463.16%)
Mutual labels:  hashicorp-vault
vault-converter
Support converting Vault Secrets to diffrent formats.
Stars: ✭ 15 (-21.05%)
Mutual labels:  hashicorp-vault
orchestrate-quick-start
PegaSys Orchestrate Quick start
Stars: ✭ 19 (+0%)
Mutual labels:  hashicorp-vault
hashicorp-vault-monitor
🔑 HashiCorp Vault Monitoring Tool
Stars: ✭ 22 (+15.79%)
Mutual labels:  hashicorp-vault
gke-vault-demo
This demo builds two GKE Clusters and guides you through using secrets in Vault, using Kubernetes authentication from within a pod to login to Vault, and fetching short-lived Google Service Account credentials on-demand from Vault within a pod.
Stars: ✭ 63 (+231.58%)
Mutual labels:  hashicorp-vault
hookpick
A tool to manage some operational concepts of Hashicorp Vault
Stars: ✭ 83 (+336.84%)
Mutual labels:  hashicorp-vault
infrastructure-pipeline
An example pipeline for executing HashiCorp Terraform with ephemeral cloud provider credentials managed by HashiCorp Vault
Stars: ✭ 44 (+131.58%)
Mutual labels:  hashicorp-vault
breakglass
A command line tool to provide login credentials from Hashicorp Vault
Stars: ✭ 33 (+73.68%)
Mutual labels:  hashicorp-vault
caravan
Caravan is your platform builder based on the HashiCorp stack.
Stars: ✭ 33 (+73.68%)
Mutual labels:  hashicorp-vault
Zyborg.Vault
PowerShell bindings for HashiCorp Vault
Stars: ✭ 18 (-5.26%)
Mutual labels:  hashicorp-vault
puppet-vault
Puppet module to manage Vault (https://vaultproject.io)
Stars: ✭ 41 (+115.79%)
Mutual labels:  hashicorp-vault
vault-quickstart
Some shell scripts to get vault up and running as quickly as possible
Stars: ✭ 11 (-42.11%)
Mutual labels:  hashicorp-vault
Goldfish
A HashiCorp Vault UI written with VueJS and Vault native Go API
Stars: ✭ 2,174 (+11342.11%)
Mutual labels:  hashicorp-vault
vault-consul-docker
Vault + Consul + Docker
Stars: ✭ 75 (+294.74%)
Mutual labels:  hashicorp-vault
community.hashi vault
Ansible collection for managing and working with HashiCorp Vault.
Stars: ✭ 44 (+131.58%)
Mutual labels:  hashicorp-vault
consul-vault
HashiCorp Vault service running on Consul cluster backend with HAProxy frontend
Stars: ✭ 27 (+42.11%)
Mutual labels:  hashicorp-vault

django-postgresql-setrole

A Django application that executes SET ROLE on every connection to PostgreSQL opened by Django. This is useful if you're using external authentication managers like Hashicorp Vault.

PostgreSQL's user model ("roles") assigns every object created in a database/ tablespace/schema an "owner". Said owner is the only user that can modify or drop the object. This means that user credentials leased from Vault which expire after some time, can't be used to create or migrate tables unless you use the same user name every time (which would defeat the purpose).

The solution is to create an "owner role". In layman's terms "a group that has all necessary permissions on the database and the INHERIT attribute and will act as the 'sudo' user for leased users from the authentication manager". All users created by the authentication manager will then be assigned this group and when they connect to the database execute "SET ROLE <owner role>", thereby making all objects created owned by the owner role.

How do I use this Django application?

Add postgresql_setrole to INSTALLED_APPS. Then in settings.DATABASES add

DATABASES = {
    "default": {
        ...,  # other settings
        "SET_ROLE": "mydatabaseowner",
    }.
}

Why is SET ROLE necessary?

The INHERIT attribute is not bidirectional. So if a (user) role is assigned a (group) role it inherits the group's permissions, but the group does not gain any rights on objects created by the user role.

So what you want is the (group) owner role to own everything.

How do I set this up?

On your shell as the postgres superuser:

# --- create an admin role for Vault
# no create database
# encrypt password
# do not inherit rights
# can create roles
# not a superuser
createuser -D -E -I -l -r -S vaultadmin

# --- create an owner role for your database
# no create database
# encrypt password
# do not inherit rights
# can't create roles
# not a superuser
createuser -D -E -I -L -R -S mydatabaseowner
createdb -E utf8 -O mydatabaseowner mydatabase

Then configure Vault to create roles like this:

$ vault mount -path=postgresql database
$ vault write postgresql/config/mydatabase \
                  plugin_name=postgresql-database-plugin \
                  allowed_roles="mydatabase_fullaccess" \
                  connection_url="postgresql://mydatapaseowner:[mydatabasepassword]@localhost:5432/"
$ vault write postgresql/roles/mydatabase_fullaccess -
{
    "db_name": "mydatabase",
    "default_ttl": "10m",
    "max_ttl": "1h",
    "creation_statements": "CREATE ROLE \"{{name}}\" WITH LOGIN ENCRYPTED PASSWORD '{{password}}' VALID UNTIL '{{expiration}}' IN ROLE \"mydatabaseowner\" INHERIT NOCREATEROLE NOCREATEDB NOSUPERUSER NOREPLICATION NOBYPASSRLS;",
    "revocation_statements": "DROP ROLE \"name\";"
}

Then users created by Vault when they log in must run

SET ROLE "mydatabaseowner";

This ensures that all created tables and other objects belong to mydatabaseowner.

License

Copyright (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:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. 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.
  3. 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].