Skip to content

feat(identity): Create columns for AuthProvider in IdentityProvider - #26930

Closed
leedongwei wants to merge 2 commits into
masterfrom
dlee/auth-create-columns
Closed

leedongwei wants to merge 2 commits into
masterfrom
dlee/auth-create-columns

Conversation

@leedongwei

@leedongwei leedongwei commented Jun 28, 2021 •

Copy link
Copy Markdown
Contributor

This is a series of PR to merge IdentityProvider and AuthProvider into 1 model/table.

At the moment, they will need to go through COPS to disconnect their Google identities from their Sentry account. This will make it easier for users to manage their external identities from Slack/MS Teams/Okta/Google/etc.

Part 1: IdentityProvider add new columns (#26930) ← Here
Part 2: IdentityProvider dual-write to 2 columns (#26931)
Part 3: AuthProvider dual-write to 2 tables (#26932)

@github-actions

github-actions Bot commented Jun 28, 2021 •

Copy link
Copy Markdown
Contributor

This PR has a migration; here is the generated SQL

BEGIN;
--
-- Create model IdentityProviderSsoConfig
--
CREATE TABLE "sentry_identityproviderssoconfig" ("id" bigserial NOT NULL PRIMARY KEY, "default_team_id" bigint NULL, "default_role" integer NOT NULL CHECK ("default_role" >= 0), "default_global_access" boolean NOT NULL, "flags" bigint NOT NULL);
--
-- Add field last_sync to identityprovider
--
ALTER TABLE "sentry_identityprovider" ADD COLUMN "last_sync" timestamp with time zone NULL;
--
-- Add field identityprovider to identityproviderssoconfig
--
ALTER TABLE "sentry_identityproviderssoconfig" ADD COLUMN "identityprovider_id" bigint NOT NULL;
--
-- Add field organization to identityprovider
--
ALTER TABLE "sentry_identityprovider" ADD COLUMN "organization_id" bigint NULL;
--
-- Add field authprovider to identityprovider
--
ALTER TABLE "sentry_identityprovider" ADD COLUMN "authprovider_id" bigint NULL;
ALTER TABLE "sentry_identityproviderssoconfig" ADD CONSTRAINT "sentry_identityprovi_default_team_id_0fad5285_fk_sentry_te" FOREIGN KEY ("default_team_id") REFERENCES "sentry_team" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "sentry_identityproviderssoconfig_default_team_id_0fad5285" ON "sentry_identityproviderssoconfig" ("default_team_id");
CREATE INDEX "sentry_identityproviderssoconfig_identityprovider_id_4fa9e8a9" ON "sentry_identityproviderssoconfig" ("identityprovider_id");
ALTER TABLE "sentry_identityproviderssoconfig" ADD CONSTRAINT "sentry_identityprovi_identityprovider_id_4fa9e8a9_fk_sentry_id" FOREIGN KEY ("identityprovider_id") REFERENCES "sentry_identityprovider" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "sentry_identityprovider_organization_id_8e46f82d" ON "sentry_identityprovider" ("organization_id");
ALTER TABLE "sentry_identityprovider" ADD CONSTRAINT "sentry_identityprovi_organization_id_8e46f82d_fk_sentry_or" FOREIGN KEY ("organization_id") REFERENCES "sentry_organization" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "sentry_identityprovider_authprovider_id_d28c33c2" ON "sentry_identityprovider" ("authprovider_id");
ALTER TABLE "sentry_identityprovider" ADD CONSTRAINT "sentry_identityprovi_authprovider_id_d28c33c2_fk_sentry_au" FOREIGN KEY ("authprovider_id") REFERENCES "sentry_authprovider" ("id") DEFERRABLE INITIALLY DEFERRED;
COMMIT;

Comment thread src/sentry/models/identity.py Resolved
Comment thread src/sentry/models/identity.py Resolved
Comment thread src/sentry/models/identity.py Outdated
external_id = models.CharField(max_length=64, null=True)

# SSO-specific fields
is_sso = models.BooleanField(default=False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really safe to add a default like this - The way Django does this is to create the default at the DB level to backfill the column then remove it, and handle the default at the application level. Creating the default at the DB level requires Postgres to lock the table while it rewrites it.

What you can do here is use SeparateDatabaseAndState to have Postgres just create the column without the default, but keep the default in Django so that new rows are created as expected. Then since you'll be backfilling anyway, you can fill in the default values as part of that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated diff does not have any backfills on this table. Noted for the future.

Comment thread src/sentry/models/identity.py Resolved
@leedongwei
leedongwei requested a review from evanpurkhiser June 28, 2021 18:34
Comment thread src/sentry/models/identity.py Outdated
"sentry.Team", blank=True, null=True, on_delete=models.SET_NULL
)
sso_default_role = BoundedPositiveIntegerField(default=0)
sso_default_global_access = models.BooleanField(default=False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this is configuration and should just be part of the data. These shouldn't need to be columns because we're not going to be querying on any of this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really agree with this. If these are important attributes of the provider it makes sense for them to be fields on the model, rather than a schemaless blob

@evanpurkhiser evanpurkhiser Jun 29, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IdentityProvider model is designed to generically represent an external provider. Configuration such as auth tokens and other information about how to use the specific provider is stored in the json field. the types of IDPs are represented as provider classes in the sentry.identity module (which is where the auth provider stuff should end up moving.

I think the configuration that these attributes represent for the SSO type of identity provider does not need to be explicit fields on the model which many identity providers will not even use.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tokens etc make sense, especially since it's an encrypted field. I don't know enough about the specifics of these other config values, but if these attributes will be used across multiple providers I think it's better for them to be columns, even if they're null sometimes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be distinct separation between provider-related config and Sentry-related config.

These columns hold SSO defaults, which are relevant to Sentry application only. I've moved into a separate table for now.

The config field on this table is used for storing data coming from the providers and the schema varies based on the provider. IMO mixing both types of config data is a bad idea.

Comment thread src/sentry/models/identity.py Outdated
# To be renamed to "provider" because this is a bad column name
type = models.CharField(max_length=64)
# Replacing "type"
provider = models.CharField(max_length=64, null=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an argument for renaming this? I don't think we actually need to rename this? If we are going to rename this, I would prefer to see this done outside of this PR.

Also I actually think this name is fine no? It is the type of identity provider, as described in the doc comment above

The IdentityProvider is unique on the type of provider (eg github, slack, google, etc).

@leedongwei leedongwei Jun 28, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the different between this two sentences?

The IdentityProvider is unique on the type of provider (eg github, slack, google, etc).

vs

The IdentityProvider is unique for each provider (eg github, slack, google, etc).

What does the word "type" mean, or what does it add to the first sentence?

@evanpurkhiser evanpurkhiser Jun 28, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It adds specificity and avoids stutter. Imagine you did this

identity_provider = IdentityProvider.objects.get(id=1)

# Using provider as the provider identifier, has stutter
print(identity_provider.provider)

# Using type as the provider identifier, no stutter is more clear
print(identity_provider.type)

Comment thread src/sentry/models/identity.py Resolved
Comment thread src/sentry/models/identityprovider.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth a comment on what 50 is?

Comment thread src/sentry/models/identityprovider.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have this if unused? Just part of the spec?

Comment thread src/sentry/models/identityprovider.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We usually don't use underscores other than sentry_, which I think mimics the table name that Django would have produced. So sentry_identityproviderssoconfig

@leedongwei

Copy link
Copy Markdown
Contributor Author

Not sure why there's still a failure to generate SQL

"sentry.Team", blank=True, null=True, on_delete=models.SET_NULL
)

default_role = BoundedPositiveIntegerField(default=50) # Default to member

@wedamija wedamija Jul 1, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't really understand this - 50 is member in what context? Is this specific to a provider, or in some spec?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Member for Sentry role. I'll update the comment in a bit.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@leedongwei leedongwei closed this Sep 30, 2021
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 15, 2021
@mgaeta
mgaeta deleted the dlee/auth-create-columns branch March 18, 2022 20:28

This branch was successfully deployed

2 active deployments
Preview – sentry — 118427e5 Deployed Jul 1, 2021 by vercel[bot]
Preview – storybook — 118427e5 Deployed Jul 1, 2021 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants