Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make default access policy configurable #61

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/teamvault/apps/secrets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from djorm_pgfulltext.fields import VectorField
from hashids import Hashids

from ...utils import send_mail
from ...utils import send_mail, pick_constant
from ..audit.auditlog import log
from .exceptions import PermissionError

Expand Down Expand Up @@ -295,7 +295,7 @@ class Secret(HashIDModel):

access_policy = models.PositiveSmallIntegerField(
choices=ACCESS_POLICY_CHOICES,
default=ACCESS_POLICY_REQUEST,
default=pick_constant(ACCESS_POLICY_CHOICES, settings.DEFAULT_ACCESS_POLICY),
)
allowed_groups = models.ManyToManyField(
Group,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ <h3 class="panel-title">
<div class="col-sm-4">
<div id="id_access_policy">
<div class="radio">
<label for="id_access_policy_1"><input {% if form.access_policy.value|slugify == ACCESS_POLICY_REQUEST %}checked="checked"{% endif %} id="id_access_policy_1" name="access_policy" required="required" title="" value="1" type="radio"> {% trans "default" %}
<label for="id_access_policy_1"><input {% if form.access_policy.value|slugify == ACCESS_POLICY_REQUEST %}checked="checked"{% endif %} id="id_access_policy_1" name="access_policy" required="required" title="" value="1" type="radio"> {% trans "request" %}
</label>
</div>
<div class="radio">
Expand All @@ -126,7 +126,7 @@ <h3 class="panel-title">
</div>
</div>
<div class="col-sm-6">
<p class="form-control-static">{% trans "By <em>default</em>, the secret will show up in search results for all users, but they will have to request access if they're not included in the list of groups and users below.<br><br><em>Everyone</em> will let all users access the secret without the need to grant access below.<br><br><em>Hidden</em> will reveal the existence of the secret and its contents only to users who have been granted access." %}</p>
<p class="form-control-static">{% trans "If <em>request</em> is used, the secret will show up in search results for all users, but they will have to request access if they're not included in the list of groups and users below.<br><br><em>Everyone</em> will let all users access the secret without the need to grant access below.<br><br><em>Hidden</em> will reveal the existence of the secret and its contents only to users who have been granted access." %}</p>
</div>
</div>

Expand Down
20 changes: 20 additions & 0 deletions src/teamvault/apps/settings/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ def configure_debugging(config, settings):
settings.TEMPLATE_DEBUG = False


def configure_default_access_policy(config):
"""
Called directly from the Django settings module.
"""
factory_default = "request"

pol = get_from_config(config, "teamvault", "default_access_policy", factory_default)
pol = pol.lower().strip()

if pol == "everyone":
return pol
elif pol == "hidden":
return pol
else:
return factory_default


def configure_django_secret_key(config):
"""
Called directly from the Django settings module.
Expand Down Expand Up @@ -221,6 +238,9 @@ def create_default_config(filename):
session_expire_at_browser_close = True
session_cookie_secure = False

# One of "request", "everyone" or "hidden"
default_access_policy = request

[django]
# This key has been generated for you, there is no need to change it
secret_key = {django_key}
Expand Down
5 changes: 5 additions & 0 deletions src/teamvault/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .apps.settings.config import (
configure_database,
configure_default_access_policy,
configure_django_secret_key,
configure_hashid,
configure_logging,
Expand Down Expand Up @@ -116,6 +117,10 @@

HASHID_MIN_LENGTH, HASHID_SALT = configure_hashid(CONFIG)

### Access Policies

DEFAULT_ACCESS_POLICY = configure_default_access_policy(CONFIG)

### REST Framework

REST_FRAMEWORK = {
Expand Down
8 changes: 8 additions & 0 deletions src/teamvault/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
from django.utils import translation


def pick_constant(choices, chosen_description):
for constant, description in choices:
if description == chosen_description:
return constant

raise KeyError("Can't find {} in {}".format(chosen_description, choices))


def send_mail(users_to, subject, template,
user_from=None, context={}, lang="en",
attachments=None):
Expand Down