8000 Keep custom HTTPS truststore type when a PEM server certificate is configured by gaurav0107 · Pull Request #55501 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Draft
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 @@ -241,7 +241,7 @@ private void addKeyStoreMappers(List<PropertyMapper<?>> mappers) {
mappers.add(
fromOption(HttpOptions.HTTPS_KEY_STORE_TYPE)
.mapFrom(HttpOptions.HTTPS_KEY_STORE_TYPE, (name, value, ctx) ->
filterOtherStoreType(value, HttpOptions.HTTPS_KEY_STORE_FILE))
filterOtherStoreType(value, HttpOptions.HTTPS_KEY_STORE_FILE, StoreRole.KEY_STORE))
.to(TLS_PREFIX + "key-store.other.type")
.paramLabel("type")
.build()
Expand Down Expand Up @@ -287,7 +287,7 @@ private void addTrustStoreMappers(List<PropertyMapper<?>> mappers) {
mappers.add(
fromOption(HttpOptions.HTTPS_TRUST_STORE_TYPE)
.mapFrom(HttpOptions.HTTPS_TRUST_STORE_TYPE, (name, value, ctx) ->
filterOtherStoreType(value, HttpOptions.HTTPS_TRUST_STORE_FILE))
filterOtherStoreType(value, HttpOptions.HTTPS_TRUST_STORE_FILE, StoreRole.TRUST_STORE))
.to(TLS_PREFIX + "trust-store.other.type")
.paramLabel("type")
.build()
Expand Down Expand Up @@ -402,18 +402,19 @@ static StoreType detectStoreType(String explicitType, String filePath, StoreRole
return StoreType.PKCS12;
}

static String filterOtherStoreType(String value, Option<File> storeFileOption) {
return filterOtherStoreType(value, storeFileOption, HttpOptions.HTTPS_CERTIFICATE_FILE);
static String filterOtherStoreType(String value, Option<File> storeFileOption, StoreRole role) {
return filterOtherStoreType(value, storeFileOption, role, HttpOptions.HTTPS_CERTIFICATE_FILE);
}

static String filterOtherStoreType(String value, Option<File> storeFileOption, Option<File> pemCertFileOption) {
static String filterOtherStoreType(String value, Option<File> storeFileOption, StoreRole role,
Option<File> pemCertFileOption) {
if (value == null) {
return null;
}
if (detectStoreType(value, null, null) != StoreType.OTHER) {
return null;
}
if (getOptionalKcValue(pemCertFileOption.getKey()).isPresent()) {
if (role == StoreRole.KEY_STORE && getOptionalKcValue(pemCertFileOption.getKey()).isPresent()) {
return null;
}
// only set the type if the corresponding file is configured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ private void addMgmtKeyStoreMappers(List<PropertyMapper<?>> mappers) {
fromOption(ManagementOptions.HTTPS_MANAGEMENT_KEY_STORE_TYPE)
.isEnabled(ManagementPropertyMappers::isInheritedScheme, HTTP_MANAGEMENT_SCHEME_IS_INHERITED)
.mapFrom(ManagementOptions.HTTPS_MANAGEMENT_KEY_STORE_TYPE, (name, value, context) ->
HttpPropertyMappers.filterOtherStoreType(value, ManagementOptions.HTTPS_MANAGEMENT_KEY_STORE_FILE, ManagementOptions.HTTPS_MANAGEMENT_CERTIFICATE_FILE))
HttpPropertyMappers.filterOtherStoreType(value, ManagementOptions.HTTPS_MANAGEMENT_KEY_STORE_FILE,
HttpPropertyMappers.StoreRole.KEY_STORE, ManagementOptions.HTTPS_MANAGEMENT_CERTIFICATE_FILE))
.to(MGMT_TLS_PREFIX + "key-store.other.type")
.paramLabel("type")
.build()
Expand Down Expand Up @@ -271,7 +272,8 @@ private void addMgmtTrustStoreMappers(List<PropertyMapper<?>> mappers) {
fromOption(ManagementOptions.HTTPS_MANAGEMENT_TRUST_STORE_TYPE)
.isEnabled(ManagementPropertyMappers::isInheritedScheme, HTTP_MANAGEMENT_SCHEME_IS_INHERITED)
.mapFrom(ManagementOptions.HTTPS_MANAGEMENT_TRUST_STORE_TYPE, (name, value, context) ->
HttpPropertyMappers.filterOtherStoreType(value, ManagementOptions.HTTPS_MANAGEMENT_TRUST_STORE_FILE))
HttpPropertyMappers.filterOtherStoreType(value, ManagementOptions.HTTPS_MANAGEMENT_TRUST_STORE_FILE,
HttpPropertyMappers.StoreRole.TRUST_STORE))
.to(MGMT_TLS_PREFIX + "trust-store.other.type")
.paramLabel("type")
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ public void unrecognizedExtensionReturnsNull() {

@Test
public void filterOtherStoreTypeReturnsNullForKnownTypes() {
assertThat(HttpPropertyMappers.filterOtherStoreType("PKCS12", HttpOptions.HTTPS_KEY_STORE_FILE) == null, is(true));
assertThat(HttpPropertyMappers.filterOtherStoreType("P12", HttpOptions.HTTPS_KEY_STORE_FILE) == null, is(true));
assertThat(HttpPropertyMappers.filterOtherStoreType("JKS", HttpOptions.HTTPS_KEY_STORE_FILE) == null, is(true));
assertThat(HttpPropertyMappers.filterOtherStoreType("jks", HttpOptions.HTTPS_KEY_STORE_FILE) == null, is(true));
assertThat(HttpPropertyMappers.filterOtherStoreType("PKCS12", HttpOptions.HTTPS_KEY_STORE_FILE, HttpPropertyMappers.StoreRole.KEY_STORE) == null, is(true));
assertThat(HttpPropertyMappers.filterOtherStoreType("P12", HttpOptions.HTTPS_KEY_STORE_FILE, HttpPropertyMappers.StoreRole.KEY_STORE) == null, is(true));
assertThat(HttpPropertyMappers.filterOtherStoreType("JKS", HttpOptions.HTTPS_KEY_STORE_FILE, HttpPropertyMappers.StoreRole.KEY_STORE) == null, is(true));
assertThat(HttpPropertyMappers.filterOtherStoreType("jks", HttpOptions.HTTPS_KEY_STORE_FILE, HttpPropertyMappers.StoreRole.KEY_STORE) == null, is(true));
}

@Test
public void filterOtherStoreTypeKeepsExoticTypesWhenFileSet() {
createConfigFromCliArguments("--https-key-store-file=server.bcfks", "--https-key-store-type=BCFKS");
assertThat(HttpPropertyMappers.filterOtherStoreType("BCFKS", HttpOptions.HTTPS_KEY_STORE_FILE), is("BCFKS"));
assertThat(HttpPropertyMappers.filterOtherStoreType("BCFKS", HttpOptions.HTTPS_KEY_STORE_FILE, HttpPropertyMappers.StoreRole.KEY_STORE), is("BCFKS"));
}

@Test
public void filterOtherStoreTypeReturnsNullWithoutFile() {
assertThat(HttpPropertyMappers.filterOtherStoreType("BCFKS", HttpOptions.HTTPS_KEY_STORE_FILE) == null, is(true));
assertThat(HttpPropertyMappers.filterOtherStoreType("BCFKS", HttpOptions.HTTPS_KEY_STORE_FILE, HttpPropertyMappers.StoreRole.KEY_STORE) == null, is(true));
}

@Test
Expand Down Expand Up @@ -214,6 +214,33 @@ public void managementPemTakesPrecedenceOverInheritedOtherKeyStore() {
assertExternalConfigNull(ManagementPropertyMappers.MGMT_TLS_PREFIX + "key-store.other.password");
}

@Test
public void pemKeyStoreKeepsOtherTrustStoreType() {
createConfigFromCliArguments(
"--https-certificate-file=/cert.pem", "--https-certificate-key-file=/key.pem",
"--https-trust-store-file=trust.jceks", "--https-trust-store-type=JCEKS", "--https-trust-store-password=pass");
assertExternalConfig(Map.of(
HttpPropertyMappers.TLS_PREFIX + "key-store.pem.default.cert", "/cert.pem",
HttpPropertyMappers.TLS_PREFIX + "trust-store.other.path", "trust.jceks",
HttpPropertyMappers.TLS_PREFIX + "trust-store.other.password", "pass",
HttpPropertyMappers.TLS_PREFIX + "trust-store.other.type", "JCEKS"
));
}

@Test
public void managementPemKeyStoreKeepsInheritedOtherTrustStoreType() {
putEnvVar("KC_HEALTH_ENABLED", "true");
createConfigFromCliArguments(
"--https-certificate-file=/cert.pem", "--https-certificate-key-file=/key.pem",
"--https-trust-store-file=trust.jceks", "--https-trust-store-type=JCEKS", "--https-trust-store-password=pass");
assertExternalConfig(Map.of(
ManagementPropertyMappers.MGMT_TLS_PREFIX + "key-store.pem.default.cert", "/cert.pem",
ManagementPropertyMappers.MGMT_TLS_PREFIX + "trust-store.other.path", "trust.jceks",
ManagementPropertyMappers.MGMT_TLS_PREFIX + "trust-store.other.password", "pass",
ManagementPropertyMappers.MGMT_TLS_PREFIX + "trust-store.other.type", "JCEKS"
));
}

@Test
public void managementInheritsKeystoreTypeFromHttp() {
putEnvVar("KC_HEALTH_ENABLED", "true");
Expand Down
Loading
0