Last active
February 15, 2018 17:40
-
-
Save jirutka/02cba50495b7ab267550c4a82f123934 to your computer and use it in GitHub Desktop.
Simple script for creating users in Sentry 8 from LDAP and/or adding them to organizations. It requires plugin https://github.com/Banno/getsentry-ldap-auth.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# This is free and unencumbered software released into the public domain. | |
# | |
# Anyone is free to copy, modify, publish, use, compile, sell, or | |
# distribute this software, either in source code form or as a compiled | |
# binary, for any purpose, commercial or non-commercial, and by any | |
# means. | |
# | |
from __future__ import print_function | |
from argparse import ArgumentParser | |
# Bootstrap the Sentry environment. | |
from sentry.utils import runner | |
runner.configure() | |
# Parse CLI arguments. | |
argp = ArgumentParser(description='Create an user from LDAP and/or add her to organization(s)') | |
argp.add_argument('username', metavar='LOGIN') | |
argp.add_argument('-o', '--org', metavar='SLUG', dest='organizations', action='append', | |
help='organizations to add the user into'), | |
argp.add_argument('-r', '--role', metavar='ROLE', dest='role', | |
choices=['member', 'admin', 'manager', 'owner'], | |
help='membership role (member, admin, manager, or owner)'), | |
args = argp.parse_args() | |
# Sentry environment must be bootstrapped prior to importing these. | |
from django.conf import settings | |
from sentry_ldap_auth.backend import SentryLdapBackend | |
from sentry.models import Organization, OrganizationMember, User | |
default_org = Organization.objects.get( | |
name=settings.AUTH_LDAP_DEFAULT_SENTRY_ORGANIZATION | |
) | |
try: | |
user = User.objects.get(username=args.username) | |
if not args.organizations: | |
args.organizations = [default_org.slug] | |
except User.DoesNotExist: | |
user = SentryLdapBackend().populate_user(args.username) | |
print(u"Created user: %s (%s)" % (user.name, user.email)) | |
if args.organizations and default_org.slug not in args.organizations: | |
print(u"Removing from the default organization (%s)" % default_org.name) | |
OrganizationMember.objects.filter( | |
organization=default_org, | |
user=user | |
).delete() | |
if args.role is None: | |
args.role = getattr(settings, 'AUTH_LDAP_SENTRY_ORGANIZATION_ROLE_TYPE', 'member') | |
for org_slug in args.organizations: | |
try: | |
org = Organization.objects.get(slug=org_slug) | |
except Organization.DoesNotExist: | |
print("No such organization with slug '%s', skipping" % org_slug) | |
continue | |
if not OrganizationMember.objects.filter(organization=org, user=user): | |
print(u"Adding user to organization '%s' with role '%s'" % (org.name, args.role)) | |
OrganizationMember.objects.create( | |
organization=org, | |
user=user, | |
role=args.role, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment