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

pkg/server: make MT session cookies backwards compatible #97940

Merged
merged 1 commit into from
Mar 8, 2023

Conversation

abarganier
Copy link
Contributor

@abarganier abarganier commented Mar 2, 2023

Previous work has been done to update the session cookie to instead be named multitenant-session, which encoded not only the session ID of the currently logged in tenant, but also the name of that tenant, as well as any other tenant (and their session) who was able to be logged in to using the same credentials.

This created backwards compatibility issues, since the cookie name changed, and the auth layer began requiring the tenant name to be encoded in the cookie value. This led to scenarios where upgrading from v22.2 to v23.1 effectively invalidated any existing session cookie.

To fix this, this patch reverts the session cookie name back to session (instead of multitenant-session). Furthermore, if the authorization layer doesn't find any tenant name to be encoded in the cookie's value, it defaults to the system tenant. This enables older sessions to still be usable in the DB Console and
apiV2.

Note that this does not mean that existing legacy session cookie will be multitenant capable, which is to say that the cookie will not be updated to encode the system tenant's name, or sessions for other
tenants. To gain this multitenant session cookie,
clients will need to go through the login flow
anew.

Release note: none

Epic: CRDB-12100

Fixes: #97786

@abarganier abarganier requested review from knz, dhartunian, Santamaura and a team March 2, 2023 20:42
@abarganier abarganier requested review from a team as code owners March 2, 2023 20:42
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@abarganier abarganier removed the request for review from a team March 2, 2023 20:43
Copy link
Collaborator

@dhartunian dhartunian left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 5 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @abarganier, @knz, and @Santamaura)


pkg/server/authentication.go line 816 at r1 (raw file):

	var err error
	var sessionCookies []*http.Cookie
	for _, c := range cookies {

you can probably merge the two loops because we just need to act on the first session cookie we find.


pkg/server/authentication.go line 823 at r1 (raw file):

	for _, sessionCookie := range sessionCookies {
		found = true
		if sessionCookie.Name == SessionCookieName {

you can assume your cookie is the session cookie in here. there's no other possibility anymore.


pkg/server/authentication_test.go line 596 at r1 (raw file):

		cNames[i] = c.Name
	}
	require.ElementsMatch(t, cNames, []string{SessionCookieName, SessionCookieName, TenantSelectCookieName})

I think we can remove the duplicated SessionCookieName

@abarganier abarganier force-pushed the session-cookies branch 2 times, most recently from e0c4204 to ba64e6a Compare March 6, 2023 17:14
Copy link
Contributor Author

@abarganier abarganier left a comment

Choose a reason for hiding this comment

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

TFTR! I addressed your comments and also did some slight refactoring of the cookie parsing logic to make it more straightforward (there were lots of repeated iterations through the cookie set that I found confusing).

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dhartunian, @knz, and @Santamaura)


pkg/server/authentication.go line 816 at r1 (raw file):

Previously, dhartunian (David Hartunian) wrote…

you can probably merge the two loops because we just need to act on the first session cookie we find.

Done, although note that we do have logic that will continue iterating through the cookies if we fail to decode the 1st session cookie that we find, as per this comment:

// Multiple cookies with the same name may be included in the
// header. We continue searching even if we find a matching
// name with an invalid value

pkg/server/authentication.go line 823 at r1 (raw file):

Previously, dhartunian (David Hartunian) wrote…

you can assume your cookie is the session cookie in here. there's no other possibility anymore.

Done.


pkg/server/authentication_test.go line 596 at r1 (raw file):

Previously, dhartunian (David Hartunian) wrote…

I think we can remove the duplicated SessionCookieName

Ah, missed this! Thanks for catching.

Copy link
Collaborator

@dhartunian dhartunian left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @abarganier, @knz, and @Santamaura)


pkg/server/authentication.go line 816 at r1 (raw file):

Previously, abarganier (Alex Barganier) wrote…

Done, although note that we do have logic that will continue iterating through the cookies if we fail to decode the 1st session cookie that we find, as per this comment:

// Multiple cookies with the same name may be included in the
// header. We continue searching even if we find a matching
// name with an invalid value

Ah, true! Thx for the reminder. lol I wrote that comment.

@abarganier
Copy link
Contributor Author

TFTR!

bors r=dhartunian

@craig
Copy link
Contributor

craig bot commented Mar 8, 2023

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Mar 8, 2023

Build failed (retrying...):

@renatolabs
Copy link
Contributor

bors r-

This PR has drifted from master, please see bors failures.

@abarganier abarganier requested a review from a team as a code owner March 8, 2023 18:57
@abarganier abarganier requested review from smg260 and renatolabs and removed request for a team March 8, 2023 18:57
Previous work has been done to update the `session` cookie
to instead be named `multitenant-session`, which encoded
not only the session ID of the currently logged in tenant,
but also the name of that tenant, as well as any *other*
tenant who was able to be logged in to using the same
credentials.

This created backwards compatibility issues, since the
cookie name changed, and the auth layer began requiring
the tenant name to be encoded in the cookie value. This
led to scenarios where upgrading from v22.2 to v23.1
effectively invalidated any existing `session` cookie.

To fix this, this patch reverts the session cookie name
back to `session` (instead of `multitenant-session`).
Furthermore, if the authorization layer doesn't find
any tenant name to be encoded in the cookie's value,
it defaults to the system tenant. This enables older
sessions to still be usable in the DB Console and
apiV2.

Note that this does *not* mean that existing legacy
session cookie will be multitenant capable, which is
to say that the cookie will not be updated to encode
the system tenant's name, or sessions for other
tenants. To gain this multitenant session cookie,
clients will need to go through the login flow
anew.

Release note: none
@abarganier
Copy link
Contributor Author

abarganier commented Mar 8, 2023

Okay, fixed the drift from master and tested again. Let's try again.

bors r=dhartunian

@craig
Copy link
Contributor

craig bot commented Mar 8, 2023

Build failed:

@abarganier
Copy link
Contributor Author

Failure appears to be a flake from what I can tell. TestUpgradeSchemaChangerElements passes on my local branch.

bors retry

@craig
Copy link
Contributor

craig bot commented Mar 8, 2023

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pkg/server: Make multi-tenant UI sessions backwards compatible
4 participants