Skip to content

Commit

Permalink
Fix SAML config: convert SamlConfig toLower (#2158)
Browse files Browse the repository at this point in the history
* Convert SamlConfig toLower

* sonar findings

* review

* sorry, yes
  • Loading branch information
strehle committed Jan 20, 2023
1 parent e8b6efd commit ffaf945
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static java.util.Collections.EMPTY_MAP;
Expand Down Expand Up @@ -97,7 +98,7 @@ public void afterPropertiesSet() throws InvalidIdentityZoneDetailsException {
samlKeys = ofNullable(samlKeys).orElse(EMPTY_MAP);
for (Map.Entry<String, Map<String,String>> entry : samlKeys.entrySet()) {
SamlKey samlKey = new SamlKey(entry.getValue().get("key"), entry.getValue().get("passphrase"), entry.getValue().get("certificate"));
definition.getSamlConfig().addKey(entry.getKey(), samlKey);
definition.getSamlConfig().addKey(ofNullable(entry.getKey()).orElseThrow(() -> new InvalidIdentityZoneDetailsException("SAML key id must not be null.", null)).toLowerCase(Locale.ROOT), samlKey);
}
definition.getSamlConfig().setActiveKeyId(this.activeKeyId);

Expand Down Expand Up @@ -165,7 +166,7 @@ public IdentityZoneConfigurationBootstrap setSamlKeys(Map<String, Map<String, St
}

public IdentityZoneConfigurationBootstrap setActiveKeyId(String activeKeyId) {
this.activeKeyId = activeKeyId;
this.activeKeyId = activeKeyId != null ? activeKeyId.toLowerCase(Locale.ROOT) : null;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public void test_multiple_keys() throws InvalidIdentityZoneDetailsException {
key1.put("key", SamlTestUtils.PROVIDER_PRIVATE_KEY);
key1.put("passphrase", SamlTestUtils.PROVIDER_PRIVATE_KEY_PASSWORD);
key1.put("certificate", SamlTestUtils.PROVIDER_CERTIFICATE);
keys.put("key1", key1);
bootstrap.setActiveKeyId("key1");
keys.put("Key1", key1);
bootstrap.setActiveKeyId("KEY1");
bootstrap.setSamlKeys(keys);
bootstrap.afterPropertiesSet();
IdentityZone uaa = provisioning.retrieve(IdentityZone.getUaaZoneId());
Expand All @@ -139,6 +139,22 @@ public void test_multiple_keys() throws InvalidIdentityZoneDetailsException {
assertEquals(SamlTestUtils.PROVIDER_CERTIFICATE, config.getKeys().get("key1").getCertificate());
}

@Test
void test_keyId_null_exception() {
bootstrap.setSamlSpPrivateKey(SamlTestUtils.PROVIDER_PRIVATE_KEY);
bootstrap.setSamlSpCertificate(SamlTestUtils.PROVIDER_CERTIFICATE);
bootstrap.setSamlSpPrivateKeyPassphrase(SamlTestUtils.PROVIDER_PRIVATE_KEY_PASSWORD);
Map<String, Map<String, String>> keys = new HashMap<>();
Map<String, String> key1 = new HashMap<>();
key1.put("key", SamlTestUtils.PROVIDER_PRIVATE_KEY);
key1.put("passphrase", SamlTestUtils.PROVIDER_PRIVATE_KEY_PASSWORD);
key1.put("certificate", SamlTestUtils.PROVIDER_CERTIFICATE);
keys.put(null, key1);
bootstrap.setActiveKeyId(null);
bootstrap.setSamlKeys(keys);
assertThrows(InvalidIdentityZoneDetailsException.class, () -> bootstrap.afterPropertiesSet());
}

@Test
public void testDefaultSamlKeys() throws Exception {
bootstrap.setSamlSpPrivateKey(SamlTestUtils.PROVIDER_PRIVATE_KEY);
Expand Down

0 comments on commit ffaf945

Please sign in to comment.