forked from apache/knox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a44fb7
commit c1e8a3c
Showing
15 changed files
with
261 additions
and
33 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
28 changes: 28 additions & 0 deletions
28
.../main/java/org/apache/knox/gateway/services/token/impl/TokenAuthorityServiceMessages.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,28 @@ | ||
/* | ||
* 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.knox.gateway.services.token.impl; | ||
|
||
import org.apache.knox.gateway.i18n.messages.Message; | ||
import org.apache.knox.gateway.i18n.messages.MessageLevel; | ||
import org.apache.knox.gateway.i18n.messages.Messages; | ||
|
||
@Messages(logger = "org.apache.knox.gateway.services.token.state") | ||
public interface TokenAuthorityServiceMessages { | ||
@Message(level = MessageLevel.ERROR, text = "There was an error getting kid, cause: {0}") | ||
void errorGettingKid(String message); | ||
} |
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import java.security.interfaces.RSAPublicKey; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Optional; | ||
|
||
import org.apache.knox.gateway.config.GatewayConfig; | ||
import org.apache.knox.gateway.services.ServiceLifecycleException; | ||
|
@@ -337,6 +338,7 @@ public void testTokenCreationCustomSigningKey() throws Exception { | |
ta.setAliasService(as); | ||
ta.setKeystoreService(ks); | ||
ta.init(config, new HashMap<>()); | ||
ta.start(); | ||
|
||
final JWTokenAttributes jwtAttributes = new JWTokenAttributesBuilder().setPrincipal(principal).setAudiences(Collections.emptyList()).setAlgorithm("RS256").setExpires(-1) | ||
.setSigningKeystoreName(customSigningKeyName).setSigningKeystoreAlias(customSigningKeyAlias).setSigningKeystorePassphrase(customSigningKeyPassphrase.toCharArray()).build(); | ||
|
@@ -584,4 +586,61 @@ public void testServiceInvalidKeyPassword() throws Exception { | |
|
||
EasyMock.verify(config, ms, as); | ||
} | ||
|
||
/** | ||
* Test getSigningCertKid() function | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void testGetSigningCertKid() throws Exception { | ||
Principal principal = EasyMock.createNiceMock(Principal.class); | ||
EasyMock.expect(principal.getName()).andReturn("[email protected]"); | ||
|
||
GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class); | ||
String basedir = System.getProperty("basedir"); | ||
if (basedir == null) { | ||
basedir = new File(".").getCanonicalPath(); | ||
} | ||
|
||
EasyMock.expect(config.getGatewaySecurityDir()).andReturn(basedir + "/target/test-classes").anyTimes(); | ||
EasyMock.expect(config.getGatewayKeystoreDir()).andReturn(basedir + "/target/test-classes/keystores").anyTimes(); | ||
EasyMock.expect(config.getSigningKeystoreName()).andReturn("server-keystore.jks").anyTimes(); | ||
EasyMock.expect(config.getSigningKeystorePath()).andReturn(basedir + "/target/test-classes/keystores/server-keystore.jks").anyTimes(); | ||
EasyMock.expect(config.getSigningKeystorePasswordAlias()).andReturn(GatewayConfig.DEFAULT_SIGNING_KEYSTORE_PASSWORD_ALIAS).anyTimes(); | ||
EasyMock.expect(config.getSigningKeyPassphraseAlias()).andReturn(GatewayConfig.DEFAULT_SIGNING_KEY_PASSPHRASE_ALIAS).anyTimes(); | ||
EasyMock.expect(config.getSigningKeystoreType()).andReturn("jks").anyTimes(); | ||
EasyMock.expect(config.getSigningKeyAlias()).andReturn("server").anyTimes(); | ||
EasyMock.expect(config.getCredentialStoreType()).andReturn(GatewayConfig.DEFAULT_CREDENTIAL_STORE_TYPE).anyTimes(); | ||
EasyMock.expect(config.getCredentialStoreAlgorithm()).andReturn(GatewayConfig.DEFAULT_CREDENTIAL_STORE_ALG).anyTimes(); | ||
|
||
MasterService ms = EasyMock.createNiceMock(MasterService.class); | ||
EasyMock.expect(ms.getMasterSecret()).andReturn("horton".toCharArray()); | ||
|
||
AliasService as = EasyMock.createNiceMock(AliasService.class); | ||
EasyMock.expect(as.getSigningKeyPassphrase()).andReturn("horton".toCharArray()).anyTimes(); | ||
|
||
EasyMock.replay(principal, config, ms, as); | ||
|
||
DefaultKeystoreService ks = new DefaultKeystoreService(); | ||
ks.setMasterService(ms); | ||
|
||
ks.init(config, new HashMap<>()); | ||
|
||
DefaultTokenAuthorityService ta = new DefaultTokenAuthorityService(); | ||
|
||
/* negative test */ | ||
/* expectation that that the exception is eaten up in case where there was an exception getting kid */ | ||
Optional<String> opt = ta.getCachedSigningKeyID(); | ||
assertFalse(opt.isPresent()); | ||
|
||
/* now test for cases where we expect to get kid */ | ||
ta.setAliasService(as); | ||
ta.setKeystoreService(ks); | ||
|
||
ta.init(config, new HashMap<>()); | ||
ta.start(); | ||
|
||
opt = ta.getCachedSigningKeyID(); | ||
assertTrue("Missing expected KID value", opt.isPresent()); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -360,14 +360,15 @@ protected TokenStateService createTokenStateService() throws Exception { | |
|
||
/* create a test JWT token */ | ||
protected JWT getJWTToken(final long expiry) { | ||
String[] claims = new String[4]; | ||
String[] claims = new String[6]; | ||
claims[0] = "KNOXSSO"; | ||
claims[1] = "[email protected]"; | ||
claims[2] = "https://login.example.com"; | ||
if(expiry > 0) { | ||
claims[3] = Long.toString(expiry); | ||
} | ||
|
||
claims[4] = "E0LDZulQ0XE_otJ5aoQtQu-RnXv8hU-M9U4dD7vDioA"; | ||
claims[5] = null; | ||
JWT token = new JWTToken("RS256", claims); | ||
// Sign the token | ||
JWSSigner signer = new RSASSASigner(privateKey); | ||
|
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
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 |
---|---|---|
|
@@ -127,11 +127,13 @@ public void testE2E() | |
} | ||
|
||
private JWT getTestToken(final String algorithm) { | ||
String[] claimArray = new String[4]; | ||
String[] claimArray = new String[6]; | ||
claimArray[0] = "KNOXSSO"; | ||
claimArray[1] = "[email protected]"; | ||
claimArray[2] = null; | ||
claimArray[3] = null; | ||
claimArray[4] = "E0LDZulQ0XE_otJ5aoQtQu-RnXv8hU-M9U4dD7vDioA"; | ||
claimArray[5] = null; | ||
|
||
final JWT token = new JWTToken(algorithm, claimArray, | ||
Collections.singletonList("aud"), false); | ||
|
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.