1919
2020import com .google .common .cache .Cache ;
2121import com .google .common .cache .CacheBuilder ;
22+ import org .apache .commons .lang3 .StringUtils ;
2223import org .apache .knox .gateway .RemoteAuthMessages ;
2324import org .apache .knox .gateway .audit .api .Action ;
2425import 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 ;
0 commit comments