-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Ability to enable/disable feature via single property #43542
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
Open
mabartos
wants to merge
2
commits into
keycloak:main
Choose a base branch
from
mabartos:KC-43541
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
common/src/main/java/org/keycloak/common/profile/SingleProfileConfigResolver.java
This file contains hidden or 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,36 @@ | ||
| package org.keycloak.common.profile; | ||
|
|
||
| import org.keycloak.common.Profile; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| // Features configuration based on the option 'feature-<name>' | ||
| public class SingleProfileConfigResolver implements ProfileConfigResolver { | ||
| private final Map<String, Boolean> features; | ||
|
|
||
| public SingleProfileConfigResolver(Map<String, Boolean> features) { | ||
| this.features = Optional.ofNullable(features).orElseGet(Collections::emptyMap); | ||
| } | ||
|
|
||
| @Override | ||
| public Profile.ProfileName getProfileName() { | ||
| Boolean state = features.get(Profile.ProfileName.PREVIEW.name().toLowerCase()); | ||
| if (state != null && state) { | ||
| return Profile.ProfileName.PREVIEW; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public FeatureConfig getFeatureConfig(String feature) { | ||
| Boolean state = features.get(feature); | ||
| if (state == null) { | ||
| return FeatureConfig.UNCONFIGURED; | ||
| } | ||
|
|
||
| return state ? FeatureConfig.ENABLED : FeatureConfig.DISABLED; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
61 changes: 59 additions & 2 deletions
61
quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/QuarkusProfileConfigResolver.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,17 +1,74 @@ | ||
| package org.keycloak.quarkus.runtime; | ||
|
|
||
| import org.keycloak.common.Profile; | ||
| import org.keycloak.common.profile.CommaSeparatedListProfileConfigResolver; | ||
| import org.keycloak.common.profile.ProfileConfigResolver; | ||
| import org.keycloak.common.profile.SingleProfileConfigResolver; | ||
| import org.keycloak.config.FeatureOptions; | ||
| import org.keycloak.quarkus.runtime.cli.PropertyException; | ||
| import org.keycloak.quarkus.runtime.configuration.Configuration; | ||
| import org.keycloak.quarkus.runtime.configuration.mappers.WildcardPropertyMapper; | ||
|
|
||
| public class QuarkusProfileConfigResolver extends CommaSeparatedListProfileConfigResolver { | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static org.keycloak.quarkus.runtime.configuration.MicroProfileConfigProvider.NS_KEYCLOAK_PREFIX; | ||
|
|
||
| public class QuarkusProfileConfigResolver implements ProfileConfigResolver { | ||
| private final CommaSeparatedListProfileConfigResolver commaSeparatedResolver; | ||
| private final SingleProfileConfigResolver singleResolver; | ||
|
|
||
| public QuarkusProfileConfigResolver() { | ||
| super(getConfig("kc.features"), getConfig("kc.features-disabled")); | ||
| this.commaSeparatedResolver = new CommaSeparatedListProfileConfigResolver(getConfig("kc.features"), getConfig("kc.features-disabled")); | ||
| this.singleResolver = new SingleProfileConfigResolver(getQuarkusFeatureState()); | ||
| } | ||
|
|
||
| static String getConfig(String key) { | ||
| return Configuration.getRawPersistedProperty(key) | ||
| .orElse(Configuration.getConfigValue(key).getValue()); | ||
| } | ||
|
|
||
| protected Map<String, Boolean> getQuarkusFeatureState() { | ||
| var map = new HashMap<String, Boolean>(); | ||
| var index = FeatureOptions.FEATURE.getKey().indexOf(WildcardPropertyMapper.WILDCARD_FROM_START); | ||
| var featureEnabledOptionPrefix = NS_KEYCLOAK_PREFIX + FeatureOptions.FEATURE.getKey().substring(0, index); | ||
|
|
||
| Configuration.getPropertyNames().forEach(property -> { | ||
| if (property.startsWith(NS_KEYCLOAK_PREFIX) && property.startsWith(featureEnabledOptionPrefix)) { | ||
| var feature = property.substring(featureEnabledOptionPrefix.length()); | ||
| var value = Configuration.getOptionalValue(property).orElseThrow( | ||
| () -> new PropertyException("Missing value for feature '%s'".formatted(feature))); | ||
|
|
||
| if (value.startsWith("v")) { | ||
| map.put(feature + ":" + value, true); | ||
| } else { | ||
| map.put(feature, switch (value) { | ||
| case "enabled" -> Boolean.TRUE; | ||
| case "disabled" -> Boolean.FALSE; | ||
| default -> null; | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return map; | ||
| } | ||
|
|
||
| @Override | ||
| public Profile.ProfileName getProfileName() { | ||
| var singleConfig = singleResolver.getProfileName(); | ||
| if (singleConfig != null) { | ||
| return singleConfig; | ||
| } | ||
| return commaSeparatedResolver.getProfileName(); | ||
| } | ||
|
|
||
| @Override | ||
| public FeatureConfig getFeatureConfig(String feature) { | ||
| var singleConfig = singleResolver.getFeatureConfig(feature); | ||
| if (singleConfig != FeatureConfig.UNCONFIGURED) { | ||
| return singleConfig; | ||
| } | ||
| return commaSeparatedResolver.getFeatureConfig(feature); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.