-
Notifications
You must be signed in to change notification settings - Fork 365
Include principal name in Polaris tokens #2389
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.service.auth; | ||
|
|
||
| import com.google.common.base.Splitter; | ||
| import jakarta.annotation.Nonnull; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.polaris.immutables.PolarisImmutable; | ||
| import org.immutables.value.Value; | ||
|
|
||
| /** | ||
| * A specialized {@link PolarisCredential} used for internal authentication, when Polaris is the | ||
| * identity provider. | ||
| * | ||
| * <p>Such credentials are created by the Polaris service itself, from a JWT token previously issued | ||
| * by Polaris itself. | ||
| * | ||
| * @see JWTBroker | ||
| */ | ||
| @PolarisImmutable | ||
| abstract class InternalPolarisToken implements PolarisCredential { | ||
|
|
||
| private static final Splitter SCOPE_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults(); | ||
|
|
||
| static InternalPolarisToken of( | ||
| String principalName, Long principalId, String clientId, String scope) { | ||
| return ImmutableInternalPolarisToken.builder() | ||
| .principalName(principalName) | ||
| .principalId(principalId) | ||
| .clientId(clientId) | ||
| .scope(scope) | ||
| .build(); | ||
| } | ||
|
|
||
| @Nonnull // switch from nullable to non-nullable | ||
| @Override | ||
| @SuppressWarnings("NullableProblems") | ||
| public abstract String getPrincipalName(); | ||
|
|
||
| @Nonnull // switch from nullable to non-nullable | ||
| @Override | ||
| @SuppressWarnings("NullableProblems") | ||
| public abstract Long getPrincipalId(); | ||
|
|
||
| @Value.Lazy | ||
| @Override | ||
| public Set<String> getPrincipalRoles() { | ||
| // Polaris stores roles in the scope claim | ||
| return SCOPE_SPLITTER.splitToStream(getScope()).collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| abstract String getClientId(); | ||
|
|
||
| abstract String getScope(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,12 +20,10 @@ | |
|
|
||
| import com.auth0.jwt.JWT; | ||
| import com.auth0.jwt.algorithms.Algorithm; | ||
| import com.auth0.jwt.exceptions.JWTVerificationException; | ||
| import com.auth0.jwt.interfaces.DecodedJWT; | ||
| import com.auth0.jwt.interfaces.JWTVerifier; | ||
| import java.time.Instant; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import org.apache.iceberg.exceptions.NotAuthorizedException; | ||
|
|
@@ -60,34 +58,22 @@ public abstract class JWTBroker implements TokenBroker { | |
| public abstract Algorithm getAlgorithm(); | ||
|
|
||
| @Override | ||
| public DecodedToken verify(String token) { | ||
| public PolarisCredential verify(String token) { | ||
| return verifyInternal(token); | ||
| } | ||
|
|
||
| private InternalPolarisToken verifyInternal(String token) { | ||
| JWTVerifier verifier = JWT.require(getAlgorithm()).withClaim(CLAIM_KEY_ACTIVE, true).build(); | ||
|
|
||
| try { | ||
| DecodedJWT decodedJWT = verifier.verify(token); | ||
| return new DecodedToken() { | ||
| @Override | ||
| public Long getPrincipalId() { | ||
| return decodedJWT.getClaim("principalId").asLong(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getClientId() { | ||
| return decodedJWT.getClaim("client_id").asString(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getSub() { | ||
| return decodedJWT.getSubject(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getScope() { | ||
| return decodedJWT.getClaim("scope").asString(); | ||
| } | ||
| }; | ||
|
|
||
| } catch (JWTVerificationException e) { | ||
| return InternalPolarisToken.of( | ||
| decodedJWT.getSubject(), | ||
| decodedJWT.getClaim(CLAIM_KEY_PRINCIPAL_ID).asLong(), | ||
dimas-b marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| decodedJWT.getClaim(CLAIM_KEY_CLIENT_ID).asString(), | ||
| decodedJWT.getClaim(CLAIM_KEY_SCOPE).asString()); | ||
|
|
||
| } catch (Exception e) { | ||
| throw (NotAuthorizedException) | ||
| new NotAuthorizedException("Failed to verify the token").initCause(e); | ||
| } | ||
|
|
@@ -110,26 +96,26 @@ public TokenResponse generateFromToken( | |
| if (subjectToken == null || subjectToken.isBlank()) { | ||
| return new TokenResponse(OAuthTokenErrorResponse.Error.invalid_request); | ||
| } | ||
| DecodedToken decodedToken; | ||
| InternalPolarisToken decodedToken; | ||
| try { | ||
| decodedToken = verify(subjectToken); | ||
| decodedToken = verifyInternal(subjectToken); | ||
| } catch (NotAuthorizedException e) { | ||
| LOGGER.error("Failed to verify the token", e.getCause()); | ||
| return new TokenResponse(Error.invalid_client); | ||
| } | ||
| EntityResult principalLookup = | ||
| metaStoreManager.loadEntity( | ||
| polarisCallContext, | ||
| 0L, | ||
| Objects.requireNonNull(decodedToken.getPrincipalId()), | ||
| PolarisEntityType.PRINCIPAL); | ||
| polarisCallContext, 0L, decodedToken.getPrincipalId(), PolarisEntityType.PRINCIPAL); | ||
| if (!principalLookup.isSuccess() | ||
| || principalLookup.getEntity().getType() != PolarisEntityType.PRINCIPAL) { | ||
| return new TokenResponse(OAuthTokenErrorResponse.Error.unauthorized_client); | ||
| } | ||
| String tokenString = | ||
| generateTokenString( | ||
| decodedToken.getClientId(), decodedToken.getScope(), decodedToken.getPrincipalId()); | ||
| decodedToken.getPrincipalName(), | ||
| decodedToken.getPrincipalId(), | ||
| decodedToken.getClientId(), | ||
| decodedToken.getScope()); | ||
| return new TokenResponse( | ||
| tokenString, TokenType.ACCESS_TOKEN.getValue(), maxTokenGenerationInSeconds); | ||
| } | ||
|
|
@@ -156,16 +142,18 @@ public TokenResponse generateFromClientSecrets( | |
| if (principal.isEmpty()) { | ||
| return new TokenResponse(OAuthTokenErrorResponse.Error.unauthorized_client); | ||
| } | ||
| String tokenString = generateTokenString(clientId, scope, principal.get().getId()); | ||
| String tokenString = | ||
| generateTokenString(principal.get().getName(), principal.get().getId(), clientId, scope); | ||
| return new TokenResponse( | ||
| tokenString, TokenType.ACCESS_TOKEN.getValue(), maxTokenGenerationInSeconds); | ||
| } | ||
|
|
||
| private String generateTokenString(String clientId, String scope, Long principalId) { | ||
| private String generateTokenString( | ||
| String principalName, long principalId, String clientId, String scope) { | ||
| Instant now = Instant.now(); | ||
| return JWT.create() | ||
| .withIssuer(ISSUER_KEY) | ||
|
Contributor
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. side note: it might be worth setting
Contributor
Author
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. We could definitely include the realm in the token, but I'm not sure about using the https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3 But, it could also contain a simple string as well, and the interpretation of that value is application-specific. Maybe a separate claim e.g. |
||
| .withSubject(String.valueOf(principalId)) | ||
| .withSubject(principalName) | ||
singhpk234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .withIssuedAt(now) | ||
| .withExpiresAt(now.plus(maxTokenGenerationInSeconds, ChronoUnit.SECONDS)) | ||
| .withJWTId(UUID.randomUUID().toString()) | ||
|
|
||
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.
I guess this might have issues during rolling upgrades as old tokens might still have
sub== principal ID.I do not think it is a critical issue, but might be worth mentioning in
CHANGELOG.md. WDYT?Uh oh!
There was an error while loading. Please reload this page.
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.
I think we are fine: the
DefaultAuthenticatorinspectsPolarisCredential.getPrincipalId()andPolarisCredential.getPrincipalName(), and these fields are mapped as follows: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.
So, if a token generated by an old
JWTBrokeris processed by a newJWTBroker,getPrincipalName()would return the string representation of the principal ID which would result in a failed lookup, butgetPrincipalId()would still return the principal ID in numeric form, and that lookup would succeed.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 point! Thanks for the clarification 👍