-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for composite AuthN and AuthZ filters
commit-id:eb5c1322
- Loading branch information
1 parent
be7f718
commit 36e82b5
Showing
4 changed files
with
146 additions
and
0 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
63 changes: 63 additions & 0 deletions
63
...nservice/src/main/java/com/pinterest/teletraan/config/CompositeAuthenticationFactory.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,63 @@ | ||
/** | ||
* Copyright (c) 2024 Pinterest, Inc. | ||
* | ||
* Licensed 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 com.pinterest.teletraan.config; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.SharedMetricRegistries; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.pinterest.teletraan.TeletraanServiceContext; | ||
import com.pinterest.teletraan.universal.security.EnvoyAuthFilter; | ||
import com.pinterest.teletraan.universal.security.EnvoyAuthenticator; | ||
import com.pinterest.teletraan.universal.security.bean.EnvoyCredentials; | ||
import com.pinterest.teletraan.universal.security.bean.TeletraanPrincipal; | ||
import io.dropwizard.auth.AuthFilter; | ||
import io.dropwizard.auth.Authenticator; | ||
import io.dropwizard.auth.CachingAuthenticator; | ||
import io.dropwizard.auth.chained.ChainedAuthFilter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.ws.rs.container.ContainerRequestFilter; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
@JsonTypeName("composite") | ||
public class CompositeAuthenticationFactory extends TokenAuthenticationFactory { | ||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
@Override | ||
public ContainerRequestFilter create(TeletraanServiceContext context) throws Exception { | ||
List<AuthFilter> tokenFilters = createAuthFilters(context); | ||
Authenticator<EnvoyCredentials, TeletraanPrincipal> authenticator = | ||
new EnvoyAuthenticator(); | ||
|
||
if (StringUtils.isNotBlank(getTokenCacheSpec())) { | ||
MetricRegistry registry = SharedMetricRegistries.getDefault(); | ||
Caffeine<Object, Object> cacheBuilder = Caffeine.from(getTokenCacheSpec()); | ||
authenticator = new CachingAuthenticator<>(registry, authenticator, cacheBuilder); | ||
} | ||
|
||
AuthFilter<EnvoyCredentials, TeletraanPrincipal> envoyAuthFilter = | ||
new EnvoyAuthFilter.Builder<TeletraanPrincipal>() | ||
.setAuthenticator(authenticator) | ||
.setAuthorizer(context.getAuthorizationFactory().create(context)) | ||
.buildAuthFilter(); | ||
|
||
List<AuthFilter> filters = new ArrayList<>(); | ||
filters.addAll(tokenFilters); | ||
filters.add(envoyAuthFilter); | ||
|
||
return new ChainedAuthFilter(filters); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...anservice/src/main/java/com/pinterest/teletraan/config/CompositeAuthorizationFactory.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,63 @@ | ||
/** | ||
* Copyright (c) 2024 Pinterest, Inc. | ||
* | ||
* Licensed 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 com.pinterest.teletraan.config; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import com.pinterest.teletraan.TeletraanServiceContext; | ||
import com.pinterest.teletraan.security.ScriptTokenRoleAuthorizer; | ||
import com.pinterest.teletraan.security.UserRoleAuthorizer; | ||
import com.pinterest.teletraan.universal.security.BasePastisAuthorizer; | ||
import com.pinterest.teletraan.universal.security.bean.ServicePrincipal; | ||
import com.pinterest.teletraan.universal.security.bean.TeletraanPrincipal; | ||
import com.pinterest.teletraan.universal.security.bean.UserPrincipal; | ||
import io.dropwizard.auth.Authorizer; | ||
|
||
@JsonTypeName("composite") | ||
public class CompositeAuthorizationFactory implements AuthorizationFactory { | ||
private static final String DEFAULT_PASTIS_SERVICE_NAME = "teletraan_dev"; | ||
|
||
@JsonProperty private String pastisServiceName = DEFAULT_PASTIS_SERVICE_NAME; | ||
|
||
public void setPastisServiceName(String pastisServiceName) { | ||
this.pastisServiceName = pastisServiceName; | ||
} | ||
|
||
public String getPastisServiceName() { | ||
return pastisServiceName; | ||
} | ||
|
||
@Override | ||
public <P extends TeletraanPrincipal> Authorizer<P> create(TeletraanServiceContext context) | ||
throws Exception { | ||
return (Authorizer<P>) | ||
BasePastisAuthorizer.builder() | ||
.factory(context.getAuthZResourceExtractorFactory()) | ||
.serviceName(pastisServiceName) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public <P extends TeletraanPrincipal> Authorizer<? extends TeletraanPrincipal> create( | ||
TeletraanServiceContext context, Class<P> principalClass) throws Exception { | ||
if (ServicePrincipal.class.equals(principalClass)) { | ||
return new ScriptTokenRoleAuthorizer(context.getAuthZResourceExtractorFactory()); | ||
} else if (UserPrincipal.class.equals(principalClass)) { | ||
return new UserRoleAuthorizer(context, context.getAuthZResourceExtractorFactory()); | ||
} | ||
return create(context); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...src/main/resources/META-INF/services/com.pinterest.teletraan.config.AuthenticationFactory
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
com.pinterest.teletraan.config.AnonymousAuthenticationFactory | ||
com.pinterest.teletraan.config.TokenAuthenticationFactory | ||
com.pinterest.teletraan.config.CompositeAuthenticationFactory |