-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change authentication workflow to lookup principal roles using securi…
…ty context (#623) * Change authentication workflow to lookup principal roles using security context * Refactored active role lookup into new interface * Addressed PR comments * Removed injected security context --------- Co-authored-by: Michael Collado <michael.collado@snowflake.com>
- Loading branch information
1 parent
16f0093
commit dcbb005
Showing
26 changed files
with
631 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...c/main/java/org/apache/polaris/service/dropwizard/auth/PolarisPrincipalAuthenticator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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.dropwizard.auth; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.Priorities; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.container.ContainerRequestFilter; | ||
import jakarta.ws.rs.core.SecurityContext; | ||
import java.io.IOException; | ||
import java.security.Principal; | ||
import java.util.Optional; | ||
import org.apache.iceberg.exceptions.NotAuthorizedException; | ||
import org.apache.polaris.core.auth.AuthenticatedPolarisPrincipal; | ||
import org.apache.polaris.service.auth.Authenticator; | ||
|
||
@jakarta.ws.rs.ext.Provider | ||
@Priority(Priorities.AUTHENTICATION) | ||
public class PolarisPrincipalAuthenticator implements ContainerRequestFilter { | ||
@Inject private Authenticator<String, AuthenticatedPolarisPrincipal> authenticator; | ||
|
||
@Override | ||
public void filter(ContainerRequestContext requestContext) throws IOException { | ||
String authHeader = requestContext.getHeaderString("Authorization"); | ||
if (authHeader == null) { | ||
throw new IOException("Authorization header is missing"); | ||
} | ||
int spaceIdx = authHeader.indexOf(' '); | ||
if (spaceIdx <= 0 || !authHeader.substring(0, spaceIdx).equalsIgnoreCase("Bearer")) { | ||
throw new IOException("Authorization header is not a Bearer token"); | ||
} | ||
String credential = authHeader.substring(spaceIdx + 1); | ||
Optional<AuthenticatedPolarisPrincipal> principal = authenticator.authenticate(credential); | ||
if (principal.isEmpty()) { | ||
throw new NotAuthorizedException("Unable to authenticate"); | ||
} | ||
SecurityContext securityContext = requestContext.getSecurityContext(); | ||
requestContext.setSecurityContext( | ||
new SecurityContext() { | ||
@Override | ||
public Principal getUserPrincipal() { | ||
return principal.get(); | ||
} | ||
|
||
@Override | ||
public boolean isUserInRole(String role) { | ||
return securityContext.isUserInRole(role); | ||
} | ||
|
||
@Override | ||
public boolean isSecure() { | ||
return securityContext.isSecure(); | ||
} | ||
|
||
@Override | ||
public String getAuthenticationScheme() { | ||
return securityContext.getAuthenticationScheme(); | ||
} | ||
}); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...g/apache/polaris/service/dropwizard/auth/PolarisPrincipalRoleSecurityContextProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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.dropwizard.auth; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.Priorities; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.container.ContainerRequestFilter; | ||
import jakarta.ws.rs.core.SecurityContext; | ||
import java.io.IOException; | ||
import java.security.Principal; | ||
import java.util.Set; | ||
import org.apache.polaris.core.auth.AuthenticatedPolarisPrincipal; | ||
import org.apache.polaris.service.auth.ActiveRolesProvider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Priority(Priorities.AUTHENTICATION + 1) | ||
public class PolarisPrincipalRoleSecurityContextProvider implements ContainerRequestFilter { | ||
private static final Logger LOGGER = | ||
LoggerFactory.getLogger(PolarisPrincipalRoleSecurityContextProvider.class); | ||
@Inject ActiveRolesProvider activeRolesProvider; | ||
|
||
@Override | ||
public void filter(ContainerRequestContext requestContext) throws IOException { | ||
AuthenticatedPolarisPrincipal polarisPrincipal = | ||
(AuthenticatedPolarisPrincipal) requestContext.getSecurityContext().getUserPrincipal(); | ||
if (polarisPrincipal == null) { | ||
return; | ||
} | ||
SecurityContext securityContext = | ||
createSecurityContext(requestContext.getSecurityContext(), polarisPrincipal); | ||
requestContext.setSecurityContext(securityContext); | ||
} | ||
|
||
public SecurityContext createSecurityContext( | ||
SecurityContext ctx, AuthenticatedPolarisPrincipal principal) { | ||
Set<String> validRoleNames = activeRolesProvider.getActiveRoles(principal); | ||
return new SecurityContext() { | ||
@Override | ||
public Principal getUserPrincipal() { | ||
return principal; | ||
} | ||
|
||
@Override | ||
public boolean isUserInRole(String role) { | ||
return validRoleNames.contains(role); | ||
} | ||
|
||
@Override | ||
public boolean isSecure() { | ||
return ctx.isSecure(); | ||
} | ||
|
||
@Override | ||
public String getAuthenticationScheme() { | ||
return ctx.getAuthenticationScheme(); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.