-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
organization_auth_settings.py
295 lines (250 loc) · 11.9 KB
/
organization_auth_settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from __future__ import annotations
import logging
from django import forms
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.http.response import HttpResponse, HttpResponseBadRequest, HttpResponseBase
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from rest_framework.request import Request
from sentry import audit_log, features, roles
from sentry.auth import manager
from sentry.auth.helper import AuthHelper
from sentry.auth.services.auth import RpcAuthProvider, auth_service
from sentry.models.authprovider import AuthProvider
from sentry.models.organization import Organization
from sentry.organizations.services.organization import RpcOrganization, organization_service
from sentry.plugins.base.response import DeferredResponse
from sentry.tasks.auth import email_missing_links_control
from sentry.utils.http import absolute_uri
from sentry.web.frontend.base import ControlSiloOrganizationView, control_silo_view
ERR_NO_SSO = _("The SSO feature is not enabled for this organization.")
OK_PROVIDER_DISABLED = _("SSO authentication has been disabled.")
OK_REMINDERS_SENT = _(
"A reminder email has been sent to members who have not yet linked their accounts."
)
logger = logging.getLogger("sentry.saml_setup_error")
def auth_provider_settings_form(provider, auth_provider, organization, request):
class AuthProviderSettingsForm(forms.Form):
disabled = provider.is_partner
require_link = forms.BooleanField(
label=_("Require SSO"),
help_text=_(
"Require members use a valid linked SSO account to access this organization"
),
required=False,
disabled=disabled,
)
enable_scim = (
forms.BooleanField(
label=_("Enable SCIM"),
help_text=_("Enable SCIM to manage Memberships and Teams via your Provider"),
required=False,
disabled=disabled,
)
if provider.can_use_scim(organization.id, request.user)
else None
)
default_role = forms.ChoiceField(
label=_("Default Role"),
choices=roles.get_choices(),
help_text=_(
"The default role new members will receive when logging in for the first time."
),
disabled=disabled,
)
if provider.is_saml and provider.name != "SAML2":
# Generic SAML2 provider already includes the certificate field in it's own configure view
x509cert = forms.CharField(
label="x509 public certificate",
widget=forms.Textarea,
help_text=_("The SAML certificate for your Identity Provider"),
required=False,
disabled=disabled,
)
initial = {
"require_link": not auth_provider.flags.allow_unlinked,
"default_role": organization.default_role,
}
if provider.can_use_scim(organization.id, request.user):
initial["enable_scim"] = bool(auth_provider.flags.scim_enabled)
if provider.is_saml:
initial_idp = auth_provider.config.get("idp", {})
certificate = initial_idp.get("x509cert", "")
initial["x509cert"] = certificate
form = AuthProviderSettingsForm(
data=request.POST if request.POST.get("op") == "settings" else None, initial=initial
)
return form
@control_silo_view
class OrganizationAuthSettingsView(ControlSiloOrganizationView):
# We restrict auth settings to org:write as it allows a non-owner to
# escalate members to own by disabling the default role.
required_scope = "org:write"
def _disable_provider(
self, request: Request, organization: RpcOrganization, auth_provider: RpcAuthProvider
):
user = request.user
sending_email = ""
if hasattr(user, "email"):
sending_email = user.email
organization_service.send_sso_unlink_emails(
organization_id=organization.id,
sending_user_email=sending_email,
provider_key=auth_provider.provider,
)
auth_service.disable_provider(provider_id=auth_provider.id)
self.create_audit_entry(
request,
organization=organization,
target_object=auth_provider.id,
event=audit_log.get_event_id("SSO_DISABLE"),
data=auth_provider.get_audit_log_data(),
)
def handle_existing_provider(
self, request: Request, organization: RpcOrganization, auth_provider: RpcAuthProvider
):
provider = auth_provider.get_provider()
if request.method == "POST":
if provider.is_partner:
return HttpResponse("Can't disable partner authentication provider", status=405)
op = request.POST.get("op")
if op == "disable":
self._disable_provider(request, organization, auth_provider)
messages.add_message(request, messages.SUCCESS, OK_PROVIDER_DISABLED)
next_uri = f"/settings/{organization.slug}/auth/"
return self.redirect(next_uri)
elif op == "reinvite":
email_missing_links_control.delay(organization.id, request.user.id, provider.key)
messages.add_message(request, messages.SUCCESS, OK_REMINDERS_SENT)
next_uri = reverse(
"sentry-organization-auth-provider-settings", args=[organization.slug]
)
return self.redirect(next_uri)
form = auth_provider_settings_form(provider, auth_provider, organization, request)
if form.is_valid():
allow_unlinked = not form.cleaned_data["require_link"]
form_scim_enabled = form.cleaned_data.get("enable_scim", False)
auth_service.change_scim(
provider_id=auth_provider.id,
user_id=request.user.id,
enabled=form_scim_enabled,
allow_unlinked=allow_unlinked,
)
organization = organization_service.update_default_role(
organization_id=organization.id, default_role=form.cleaned_data["default_role"]
)
if form.initial != form.cleaned_data:
changed_data = {}
for key, value in form.cleaned_data.items():
if key == "x509cert":
original_idp = auth_provider.config.get("idp", {})
if original_idp.get("x509cert", "") != value:
auth_provider.config = {
**auth_provider.config,
"idp": {
**original_idp,
"x509cert": value,
},
}
auth_service.update_provider_config(
organization_id=organization.id,
auth_provider_id=auth_provider.id,
config=auth_provider.config,
)
changed_data["x509cert"] = f"to {value}"
elif form.initial.get(key) != value:
changed_data[key] = f"to {value}"
self.create_audit_entry(
request,
organization=organization,
target_object=auth_provider.id,
event=audit_log.get_event_id("SSO_EDIT"),
data=changed_data,
)
view = provider.get_configure_view()
response = view(request, organization, auth_provider)
if isinstance(response, DeferredResponse):
response = response.render(
request,
{
"auth_provider": auth_provider,
"organization": organization,
"provider": provider,
},
)
pending_links_count = organization_service.count_members_without_sso(
organization_id=organization.id
)
context = {
"form": form,
"pending_links_count": pending_links_count,
"login_url": organization.absolute_url(Organization.get_url(organization.slug)),
"settings_url": organization.absolute_url(
reverse("sentry-organization-settings", args=[organization.slug])
),
"auth_provider": auth_provider,
"provider_name": provider.name,
"scim_api_token": auth_provider.get_scim_token(),
"scim_url": get_scim_url(auth_provider, organization),
"content": response,
"disabled": provider.is_partner,
}
return self.respond("sentry/organization-auth-provider-settings.html", context)
def handle(self, request: Request, organization: RpcOrganization) -> HttpResponseBase: # type: ignore[override]
provider = auth_service.get_auth_provider(organization_id=organization.id)
if provider:
# if the org has SSO set up already, allow them to modify the existing provider
# regardless if the feature flag is set up. This allows orgs who might no longer
# have the SSO feature to be able to turn it off
return self.handle_existing_provider(
request=request, organization=organization, auth_provider=provider
)
if request.method == "POST":
provider_key = request.POST.get("provider")
if provider_key is None or not manager.exists(provider_key):
return HttpResponseBadRequest()
helper = AuthHelper(
request=request, # this has all our form data
organization=organization,
provider_key=provider_key, # okta, google, onelogin, etc
flow=AuthHelper.FLOW_SETUP_PROVIDER,
)
feature = helper.provider.required_feature
if feature and not features.has(feature, organization, actor=request.user):
return HttpResponse("Provider is not enabled", status=401)
if request.POST.get("init"):
helper.initialize()
if not helper.is_valid():
logger.info(
"OrganizationAuthSettingsView",
extra={
"flow": helper.flow,
"signature": helper.signature,
"step_index": helper.step_index,
"config": helper.config,
"organization": helper.organization.slug,
"provide_key": helper.provider.key,
"provider_model_id": (
helper.provider_model.id if helper.provider_model else ""
),
"request_path": helper.request.path,
"request_content_params": helper.request.content_params or "",
"request_method": helper.request.method or "",
"state_redis_key": helper.state.redis_key,
"state_state": helper.state.get_state() is not None,
},
)
return helper.error("Something unexpected happened during authentication.")
# render first time setup view
return helper.current_step()
# Otherwise user is in bad state since frontend/react should handle this case
return HttpResponseRedirect(Organization.get_url(organization.slug))
def get_scim_url(
auth_provider: AuthProvider | RpcAuthProvider, organization: Organization | RpcOrganization
) -> str | None:
if auth_provider.flags.scim_enabled:
# the SCIM protocol doesn't use trailing slashes in URLs
return absolute_uri(f"api/0/organizations/{organization.slug}/scim/v2")
else:
return None