Skip to content

Commit 72a4845

Browse files
KNOX-3287: RAP truststore password alias improvements (#1190)
* KNOX-3287: RAP truststore password alias improvements * KNOX-3287 - Fix build failures --------- Co-authored-by: Sandeep Moré <moresandeep@gmail.com>
1 parent 47e8e7a commit 72a4845

4 files changed

Lines changed: 26 additions & 15 deletions

File tree

gateway-docker/src/main/resources/docker/gateway-entrypoint.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ saveAlias gateway_database_user "${DATABASE_CONNECTION_USER}"
102102
saveAlias gateway_database_password "${DATABASE_CONNECTION_PASSWORD}"
103103
saveAlias gateway_database_ssl_truststore_password "${DATABASE_CONNECTION_TRUSTSTORE_PASSWORD}"
104104

105+
# RemoteAuthProvider truststore password
106+
saveAlias rap_truststore_password "${RAP_TRUSTSTORE_PASSWORD}"
107+
105108
if [[ -n ${KNOX_TOKEN_HASH_KEY} ]]
106109
then
107110
saveAlias knox.token.hash.key "${KNOX_TOKEN_HASH_KEY}"

gateway-provider-security-authc-remote/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
<artifactId>log4j-api</artifactId>
5959
</dependency>
6060

61+
<dependency>
62+
<groupId>org.apache.commons</groupId>
63+
<artifactId>commons-lang3</artifactId>
64+
</dependency>
65+
6166
<dependency>
6267
<groupId>org.apache.knox</groupId>
6368
<artifactId>gateway-test-utils</artifactId>

gateway-provider-security-authc-remote/src/main/java/org/apache/knox/gateway/filter/RemoteAuthFilter.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.cache.Cache;
2121
import com.google.common.cache.CacheBuilder;
22+
import org.apache.commons.lang3.StringUtils;
2223
import org.apache.knox.gateway.RemoteAuthMessages;
2324
import org.apache.knox.gateway.audit.api.Action;
2425
import org.apache.knox.gateway.audit.api.ActionOutcome;
@@ -76,7 +77,7 @@ public class RemoteAuthFilter implements Filter {
7677
static final String DEFAULT_CONFIG_USER_HEADER = "X-Knox-Actor-ID";
7778
static final String DEFAULT_CONFIG_GROUP_HEADER = "X-Knox-Actor-Groups-*";
7879
static final String CONFIG_TRUSTSTORE_PATH = REMOTE_AUTH + "truststore.path";
79-
static final String CONFIG_TRUSTSTORE_PASSWORD = REMOTE_AUTH + "truststore.password";
80+
static final String CONFIG_TRUSTSTORE_PASSWORD_ALIAS = REMOTE_AUTH + "truststore.password.alias";
8081
static final String CONFIG_TRUSTSTORE_TYPE = REMOTE_AUTH + "truststore.type";
8182
static final String DEFAULT_TRUSTSTORE_TYPE = "JKS";
8283
static final String WILDCARD = "*";
@@ -138,7 +139,7 @@ public void init(FilterConfig filterConfig) throws ServletException {
138139

139140
private void buildTrustStore(FilterConfig filterConfig) throws ServletException {
140141
String truststorePath = filterConfig.getInitParameter(CONFIG_TRUSTSTORE_PATH);
141-
String truststorePassword = filterConfig.getInitParameter(CONFIG_TRUSTSTORE_PASSWORD);
142+
String truststorePasswordAlias = filterConfig.getInitParameter(CONFIG_TRUSTSTORE_PASSWORD_ALIAS);
142143
String truststoreType = filterConfig.getInitParameter(CONFIG_TRUSTSTORE_TYPE);
143144
if (truststoreType == null || truststoreType.isEmpty()) {
144145
truststoreType = DEFAULT_TRUSTSTORE_TYPE;
@@ -150,18 +151,12 @@ private void buildTrustStore(FilterConfig filterConfig) throws ServletException
150151
GatewayServices services = (GatewayServices) context.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
151152
if (services != null) {
152153
try {
153-
final AliasService aliasService = services.getService(ServiceType.ALIAS_SERVICE);
154+
String truststorePassword = null;
154155
if (truststorePath != null && !truststorePath.isEmpty()) {
155-
if (truststorePassword == null || truststorePassword.isEmpty()) {
156-
// let's check for an alias given the intent to specify a truststore path
157-
char[] passChars = aliasService.getPasswordFromAliasForCluster(topologyName,
158-
CONFIG_TRUSTSTORE_PASSWORD, false);
159-
if (passChars != null) {
160-
truststorePassword = new String(passChars);
161-
}
162-
if (truststorePassword == null || truststorePassword.isEmpty()) {
163-
truststorePassword = new String(aliasService.getPasswordFromAliasForGateway(CONFIG_TRUSTSTORE_PASSWORD));
164-
}
156+
final AliasService aliasService = services.getService(ServiceType.ALIAS_SERVICE);
157+
truststorePassword = getTruststorePassword(aliasService, truststorePasswordAlias, topologyName);
158+
if (StringUtils.isBlank(truststorePassword)) {
159+
truststorePassword = getTruststorePassword(aliasService, truststorePasswordAlias, AliasService.NO_CLUSTER_NAME);
165160
}
166161
}
167162
KeystoreService keystoreService = services.getService(ServiceType.KEYSTORE_SERVICE);
@@ -177,6 +172,14 @@ private void buildTrustStore(FilterConfig filterConfig) throws ServletException
177172
}
178173
}
179174

175+
private String getTruststorePassword(final AliasService aliasService, final String truststorePasswordAlias, final String topologyName) throws AliasServiceException {
176+
if (StringUtils.isNotBlank(truststorePasswordAlias)) {
177+
final char[] truststorePasswordAliasChars = aliasService.getPasswordFromAliasForCluster(topologyName, truststorePasswordAlias, false);
178+
return truststorePasswordAliasChars == null ? null : new String(truststorePasswordAliasChars);
179+
}
180+
return null;
181+
}
182+
180183
private KeyStore getTrustStore(String truststorePath, String truststoreType, String truststorePassword,
181184
KeystoreService keystoreService) throws IOException {
182185
KeyStore truststore = null;

gateway-provider-security-authc-remote/src/test/java/org/apache/knox/gateway/filter/RemoteAuthFilterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private void setUp(String trustStorePath, String trustStorePass, String trustSto
158158

159159
// Trust store config
160160
EasyMock.expect(filterConfigMock.getInitParameter(RemoteAuthFilter.CONFIG_TRUSTSTORE_PATH)).andReturn(trustStorePath).anyTimes();
161-
EasyMock.expect(filterConfigMock.getInitParameter(RemoteAuthFilter.CONFIG_TRUSTSTORE_PASSWORD)).andReturn(trustStorePass).anyTimes();
161+
EasyMock.expect(filterConfigMock.getInitParameter(RemoteAuthFilter.CONFIG_TRUSTSTORE_PASSWORD_ALIAS)).andReturn(trustStorePass).anyTimes();
162162
EasyMock.expect(filterConfigMock.getInitParameter(RemoteAuthFilter.CONFIG_TRUSTSTORE_TYPE)).andReturn(trustStoreType).anyTimes();
163163

164164
// Only replay the mocks that won't need additional expectations
@@ -411,7 +411,7 @@ public void testSuccessfulHttpsRequestWithTrustStore() throws Exception {
411411

412412
// Set up aliasService expectations for password resolution
413413
EasyMock.expect(aliasServiceMock.getPasswordFromAliasForCluster("test-topology",
414-
RemoteAuthFilter.CONFIG_TRUSTSTORE_PASSWORD, false))
414+
RemoteAuthFilter.CONFIG_TRUSTSTORE_PASSWORD_ALIAS, false))
415415
.andReturn("trustpass".toCharArray())
416416
.anyTimes();
417417

0 commit comments

Comments
 (0)