Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed IDP keystore management #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public SharedConfiguration(JKSKeyManager keyManager) {
this.keyManager = keyManager;
}

public SharedConfiguration(JKSKeyManager keyManager, String password) {
this.keyManager = keyManager;
this.keystorePassword = password;
}

public abstract void reset();

public void setEntityId(String newEntityId, boolean addTokenToStore) {
Expand Down
11 changes: 9 additions & 2 deletions mujina-idp/src/main/java/mujina/api/IdpConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public IdpConfiguration(JKSKeyManager keyManager,
@Value("${idp.entity_id}") String defaultEntityId,
@Value("${idp.private_key}") String idpPrivateKey,
@Value("${idp.certificate}") String idpCertificate,
@Value("${idp.passphrase}") String keyStorePassword,
@Value("${idp.auth_method}") String authMethod) {
super(keyManager);
super(keyManager, keyStorePassword);
this.defaultEntityId = defaultEntityId;
this.idpPrivateKey = idpPrivateKey;
this.idpCertificate = idpCertificate;
Expand All @@ -45,7 +46,8 @@ public IdpConfiguration(JKSKeyManager keyManager,

@Override
public void reset() {
setEntityId(defaultEntityId);
// addTokenToStore can be false as key store is reset shortly after anyway
setEntityId(defaultEntityId, false);
resetAttributes();
resetKeyStore(defaultEntityId, idpPrivateKey, idpCertificate);
resetUsers();
Expand Down Expand Up @@ -74,6 +76,11 @@ private void resetAttributes() {
putAttribute("urn:mace:dir:attribute-def:eduPersonPrincipalName", "[email protected]");
}

@Override
public void setEntityId(String entityId) {
super.setEntityId(entityId, true);
}

private void putAttribute(String key, String... values) {
this.attributes.put(key, Arrays.asList(values));
}
Expand Down
94 changes: 93 additions & 1 deletion mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
import java.security.cert.CertificateException;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

@Configuration
@EnableWebSecurity
Expand Down Expand Up @@ -94,7 +98,7 @@
@Value("${idp.passphrase}") String idpPassphrase) throws InvalidKeySpecException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, XMLStreamException {
KeyStore keyStore = KeyStoreLocator.createKeyStore(idpPassphrase);
KeyStoreLocator.addPrivateKey(keyStore, idpEntityId, idpPrivateKey, idpCertificate, idpPassphrase);
return new JKSKeyManager(keyStore, Collections.singletonMap(idpEntityId, idpPassphrase), idpEntityId);
return new JKSKeyManager(keyStore, new FakePasswordMap(idpPassphrase), idpEntityId);
}

@Bean
Expand Down Expand Up @@ -156,4 +160,92 @@
}
}

static class FakePasswordMap implements Map<String, String> {

private final String value;

FakePasswordMap(String value) {
this.value = value;
}

@Override
public int size() {
return 1;

Check warning on line 173 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L173

Added line #L173 was not covered by tests
}

@Override
public boolean isEmpty() {
return false;

Check warning on line 178 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L178

Added line #L178 was not covered by tests
}

@Override
public boolean containsKey(Object o) {
return true;
}

@Override
public boolean containsValue(Object o) {
return Objects.equals(o, value);

Check warning on line 188 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L188

Added line #L188 was not covered by tests
}

@Override
public String get(Object o) {
return value;
}

@Override
public String put(String s, String s2) {
throw readOnly();

Check warning on line 198 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L198

Added line #L198 was not covered by tests
}

private IllegalStateException readOnly() {
return new IllegalStateException("Map is read-only");

Check warning on line 202 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L202

Added line #L202 was not covered by tests
}

@Override
public String remove(Object o) {
throw readOnly();

Check warning on line 207 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L207

Added line #L207 was not covered by tests
}

@Override
public void putAll(Map<? extends String, ? extends String> map) {
throw readOnly();

Check warning on line 212 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L212

Added line #L212 was not covered by tests
}

@Override
public void clear() {
throw readOnly();

Check warning on line 217 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L217

Added line #L217 was not covered by tests
}

@Override
public Set<String> keySet() {
return Collections.singleton("");

Check warning on line 222 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L222

Added line #L222 was not covered by tests
}

@Override
public Collection<String> values() {
return Collections.singleton(value);

Check warning on line 227 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L227

Added line #L227 was not covered by tests
}

@Override
public Set<Entry<String, String>> entrySet() {
return Collections.singleton(new Map.Entry<String, String>() {

Check warning on line 232 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L232

Added line #L232 was not covered by tests
@Override
public String getKey() {
return "";

Check warning on line 235 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L235

Added line #L235 was not covered by tests
}

@Override
public String getValue() {
return value;

Check warning on line 240 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L240

Added line #L240 was not covered by tests
}

@Override
public String setValue(String s) {
throw readOnly();

Check warning on line 245 in mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java

View check run for this annotation

Codecov / codecov/patch

mujina-idp/src/main/java/mujina/idp/WebSecurityConfigurer.java#L245

Added line #L245 was not covered by tests
}
});
}
}

}
2 changes: 1 addition & 1 deletion mujina-idp/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ idp:
# Public certificate to verify the signature of the SAML response
certificate: MIIDEzCCAfugAwIBAgIJAKoK/heBjcOYMA0GCSqGSIb3DQEBBQUAMCAxHjAcBgNVBAoMFU9yZ2FuaXphdGlvbiwgQ049T0lEQzAeFw0xNTExMTExMDEyMTVaFw0yNTExMTAxMDEyMTVaMCAxHjAcBgNVBAoMFU9yZ2FuaXphdGlvbiwgQ049T0lEQzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANBGwJ/qpTQNiSgUglSE2UzEkUow+wS8r67etxoEhlzJZfgK/k5TfG1wICDqapHAxEVgUM10aBHRctNocA5wmlHtxdidhzRZroqHwpKy2BmsKX5Z2oK25RLpsyusB1KroemgA/CjUnI6rIL1xxFn3KyOFh1ZBLUQtKNQeMS7HFGgSDAp+sXuTFujz12LFDugX0T0KB5a1+0l8y0PEa0yGa1oi6seONx849ZHxM0PRvUunWkuTM+foZ0jZpFapXe02yWMqhc/2iYMieE/3GvOguJchJt6R+cut8VBb6ubKUIGK7pmoq/TB6DVXpvsHqsDJXechxcicu4pdKVDHSec850CAwEAAaNQME4wHQYDVR0OBBYEFK7RqjoodSYVXGTVEdLf3kJflP/sMB8GA1UdIwQYMBaAFK7RqjoodSYVXGTVEdLf3kJflP/sMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBADNZkxlFXh4F45muCbnQd+WmaXlGvb9tkUyAIxVL8AIu8J18F420vpnGpoUAE+Hy3evBmp2nkrFAgmr055fAjpHeZFgDZBAPCwYd3TNMDeSyMta3Ka+oS7GRFDePkMEm+kH4/rITNKUF1sOvWBTSowk9TudEDyFqgGntcdu/l/zRxvx33y3LMG5USD0x4X4IKjRrRN1BbcKgi8dq10C3jdqNancTuPoqT3WWzRvVtB/q34B7F74/6JzgEoOCEHufBMp4ZFu54P0yEGtWfTwTzuoZobrChVVBt4w/XZagrRtUCDNwRpHNbpjxYudbqLqpi1MQpV9oht/BpTHVJG2i0ro=
# Passphrase of the keystore
passphrase: secret
passphrase: changeIt
# The number of seconds before a lower time bound, or after an upper time bound, to consider still acceptable
clock_skew: 300
# Number of seconds after a message issue instant after which the message is considered expired
Expand Down
28 changes: 27 additions & 1 deletion mujina-idp/src/test/java/mujina/idp/MetadataControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mujina.idp;

import mujina.AbstractIntegrationTest;
import org.junit.AfterClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;

Expand All @@ -16,7 +17,22 @@ public class MetadataControllerTest extends AbstractIntegrationTest {
private String idpBaseUrl;

@Test
public void metadata() throws Exception {
public void metadata() {

checkMetadata();

given()
.header("content-type", "application/json")
.body("x")
.put("/api/entityid")
.then()
.statusCode(SC_OK);

checkMetadata();

}

private void checkMetadata() {
given()
.config(newConfig()
.xmlConfig(xmlConfig().declareNamespace("md", "urn:oasis:names:tc:SAML:2.0:metadata")))
Expand All @@ -29,6 +45,16 @@ public void metadata() throws Exception {
equalTo(idpBaseUrl + "/SingleSignOnService"));
}

@AfterClass
public static void reset() {
given()
.header("Content-type", "application/json")
.post("/api/reset")
.then()
.statusCode(SC_OK);
}


}