-
-
Notifications
You must be signed in to change notification settings - Fork 217
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
add ability to create domain-scoped API keys #28493
Changes from 9 commits
a76af8a
ca0284d
0b38aea
eb5cb7c
061d9fd
e01c200
9b468a1
d3fe422
2263690
168076b
be674a9
09281b0
4521699
2799e5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -263,21 +263,31 @@ def __init__(self, **kwargs): | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
class HQApiKeyForm(forms.Form): | ||||||||||||||||||||||||||||||
ALL_DOMAINS = '*' | ||||||||||||||||||||||||||||||
name = forms.CharField() | ||||||||||||||||||||||||||||||
ip_allowlist = SimpleArrayField( | ||||||||||||||||||||||||||||||
forms.GenericIPAddressField(), | ||||||||||||||||||||||||||||||
label=ugettext_lazy("Allowed IP Addresses (comma separated)"), | ||||||||||||||||||||||||||||||
required=False | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
domain = forms.ChoiceField( | ||||||||||||||||||||||||||||||
required=False, | ||||||||||||||||||||||||||||||
help_text=ugettext_lazy("Limit the key's access to a single project space") | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def __init__(self, *args, **kwargs): | ||||||||||||||||||||||||||||||
self.couch_user = kwargs.pop('couch_user') | ||||||||||||||||||||||||||||||
super().__init__(*args, **kwargs) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
user_domains = self.couch_user.get_domains() | ||||||||||||||||||||||||||||||
all_domains = (self.ALL_DOMAINS, _('All Domains')) | ||||||||||||||||||||||||||||||
self.fields['domain'].choices = [all_domains] + [(d, d) for d in user_domains] | ||||||||||||||||||||||||||||||
self.helper = HQFormHelper() | ||||||||||||||||||||||||||||||
self.helper.layout = Layout( | ||||||||||||||||||||||||||||||
crispy.Fieldset( | ||||||||||||||||||||||||||||||
ugettext_lazy("Add New API Key"), | ||||||||||||||||||||||||||||||
crispy.Field('name'), | ||||||||||||||||||||||||||||||
crispy.Field('domain'), | ||||||||||||||||||||||||||||||
crispy.Field('ip_allowlist'), | ||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||
hqcrispy.FormActions( | ||||||||||||||||||||||||||||||
|
@@ -294,10 +304,15 @@ def create_key(self, user): | |||||||||||||||||||||||||||||
HQApiKey.objects.get(name=self.cleaned_data['name'], user=user) | ||||||||||||||||||||||||||||||
raise DuplicateApiKeyName | ||||||||||||||||||||||||||||||
except HQApiKey.DoesNotExist: | ||||||||||||||||||||||||||||||
if self.cleaned_data['domain'] and self.cleaned_data['domain'] != self.ALL_DOMAINS: | ||||||||||||||||||||||||||||||
domain = self.cleaned_data['domain'] | ||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||
domain = '' | ||||||||||||||||||||||||||||||
new_key = HQApiKey.objects.create( | ||||||||||||||||||||||||||||||
name=self.cleaned_data['name'], | ||||||||||||||||||||||||||||||
ip_allowlist=self.cleaned_data['ip_allowlist'], | ||||||||||||||||||||||||||||||
user=user, | ||||||||||||||||||||||||||||||
domain=domain, | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: if line 266 was changed to
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good call 09281b0 |
||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
return new_key | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 2.2.13 on 2020-09-04 14:25 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('users', '0023_hqapikey_role_id'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='hqapikey', | ||
name='domain', | ||
field=models.CharField(blank=True, default='', max_length=255), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2998,6 +2998,7 @@ class HQApiKey(models.Model): | |
name = models.CharField(max_length=255, blank=True, default='') | ||
created = models.DateTimeField(default=timezone.now) | ||
ip_allowlist = ArrayField(models.GenericIPAddressField(), default=list) | ||
domain = models.CharField(max_length=255, blank=True, default='') | ||
role_id = models.CharField(max_length=40, blank=True, default='') | ||
|
||
class Meta(object): | ||
|
@@ -3022,4 +3023,6 @@ def role(self): | |
return UserRole.get(self.role_id) | ||
except ResourceNotFound: | ||
logging.exception('no role with id %s found in domain %s' % (self.role_id, self.domain)) | ||
elif self.domain: | ||
return CouchUser.from_django_user(self.user).get_domain_membership(self.domain).role | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When enterprise permissions are on, this will grant access to both their domain and any controlled domain (so, COVID state-level users will have access to all county domains). That's probably what you want, just wanted to confirm. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't consider this use case and don't know much about how the enterprise permissions work to have an opinion. Do you think it's the right behavior given the expectations of those roles as they're being used by the team? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think the current behavior matches what the team will expect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having said that, I might as well verify that assumption. I'll report back if the current behavior is not what they expect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you report back either way? Would feel better about having the loop closed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current code is the correct behavior. |
||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: user-facing content usually says "project" or "project space" instead of "domain."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch. be674a9