fix: generalizing when enabled like behavior as a validator (#32325)

closes: #32318

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
This commit is contained in:
Steven Hawkins 2024-08-22 04:51:22 -04:00 committed by GitHub
parent 5ac8ffa5b5
commit 4fba6b391e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 68 additions and 33 deletions

View file

@ -36,11 +36,7 @@ public final class BootstrapAdminPropertyMappers {
return new PropertyMapper[]{ return new PropertyMapper[]{
fromOption(BootstrapAdminOptions.USERNAME) fromOption(BootstrapAdminOptions.USERNAME)
.paramLabel("username") .paramLabel("username")
.validator((mapper, value) -> { .validateEnabled(BootstrapAdminPropertyMappers::isPasswordSet, PASSWORD_SET)
if (!isPasswordSet()) {
throw new PropertyException(mapper.getOption().getKey() + " available only when " + PASSWORD_SET);
}
})
.build(), .build(),
fromOption(BootstrapAdminOptions.PASSWORD) fromOption(BootstrapAdminOptions.PASSWORD)
.paramLabel("password") .paramLabel("password")
@ -52,11 +48,7 @@ public final class BootstrapAdminPropertyMappers {
.build(),*/ .build(),*/
fromOption(BootstrapAdminOptions.CLIENT_ID) fromOption(BootstrapAdminOptions.CLIENT_ID)
.paramLabel("client id") .paramLabel("client id")
.validator((mapper, value) -> { .validateEnabled(BootstrapAdminPropertyMappers::isClientSecretSet, CLIENT_SECRET_SET)
if (!isClientSecretSet()) {
throw new PropertyException(mapper.getOption().getKey() + " available only when " + CLIENT_SECRET_SET);
}
})
.build(), .build(),
fromOption(BootstrapAdminOptions.CLIENT_SECRET) fromOption(BootstrapAdminOptions.CLIENT_SECRET)
.paramLabel("client secret") .paramLabel("client secret")

View file

@ -56,6 +56,7 @@ public class PropertyMapper<T> {
null, null,
null, null,
false, false,
null,
null) { null) {
@Override @Override
public ConfigValue getConfigValue(String name, ConfigSourceInterceptorContext context) { public ConfigValue getConfigValue(String name, ConfigSourceInterceptorContext context) {
@ -74,10 +75,12 @@ public class PropertyMapper<T> {
private final String envVarFormat; private final String envVarFormat;
private final String cliFormat; private final String cliFormat;
private final BiConsumer<PropertyMapper<T>, ConfigValue> validator; private final BiConsumer<PropertyMapper<T>, ConfigValue> validator;
private final String description;
PropertyMapper(Option<T> option, String to, BooleanSupplier enabled, String enabledWhen, PropertyMapper(Option<T> option, String to, BooleanSupplier enabled, String enabledWhen,
BiFunction<Optional<String>, ConfigSourceInterceptorContext, Optional<String>> mapper, BiFunction<Optional<String>, ConfigSourceInterceptorContext, Optional<String>> mapper,
String mapFrom, String paramLabel, boolean mask, BiConsumer<PropertyMapper<T>, ConfigValue> validator) { String mapFrom, String paramLabel, boolean mask, BiConsumer<PropertyMapper<T>, ConfigValue> validator,
String description) {
this.option = option; this.option = option;
this.to = to == null ? getFrom() : to; this.to = to == null ? getFrom() : to;
this.enabled = enabled; this.enabled = enabled;
@ -89,6 +92,7 @@ public class PropertyMapper<T> {
this.cliFormat = toCliFormat(option.getKey()); this.cliFormat = toCliFormat(option.getKey());
this.envVarFormat = toEnvVarFormat(getFrom()); this.envVarFormat = toEnvVarFormat(getFrom());
this.validator = validator; this.validator = validator;
this.description = description;
} }
private static Optional<String> defaultTransformer(Optional<String> value, ConfigSourceInterceptorContext context) { private static Optional<String> defaultTransformer(Optional<String> value, ConfigSourceInterceptorContext context) {
@ -190,7 +194,7 @@ public class PropertyMapper<T> {
} }
public String getDescription() { public String getDescription() {
return this.option.getDescription(); return this.description;
} }
/** /**
@ -294,9 +298,11 @@ public class PropertyMapper<T> {
private String enabledWhen = ""; private String enabledWhen = "";
private String paramLabel; private String paramLabel;
private BiConsumer<PropertyMapper<T>, ConfigValue> validator = (mapper, value) -> mapper.validateExpectedValues(value, mapper::validateSingleValue); private BiConsumer<PropertyMapper<T>, ConfigValue> validator = (mapper, value) -> mapper.validateExpectedValues(value, mapper::validateSingleValue);
private String description;
public Builder(Option<T> option) { public Builder(Option<T> option) {
this.option = option; this.option = option;
this.description = this.option.getDescription();
} }
public Builder<T> to(String to) { public Builder<T> to(String to) {
@ -344,12 +350,29 @@ public class PropertyMapper<T> {
this.validator = validator; this.validator = validator;
return this; return this;
} }
/**
* Similar to {@link #enabledWhen}, but uses the condition as a validator. This allows the option
* to appear in help.
* @param isEnabled
* @param enabledWhen
* @return
*/
public Builder<T> validateEnabled(BooleanSupplier isEnabled, String enabledWhen) {
this.validator = (mapper, value) -> {
if (!isEnabled.getAsBoolean()) {
throw new PropertyException(mapper.getOption().getKey() + " available only when " + enabledWhen);
}
};
this.description = String.format("%s Available only when %s.", this.description, enabledWhen);
return this;
}
public PropertyMapper<T> build() { public PropertyMapper<T> build() {
if (paramLabel == null && Boolean.class.equals(option.getType())) { if (paramLabel == null && Boolean.class.equals(option.getType())) {
paramLabel = Boolean.TRUE + "|" + Boolean.FALSE; paramLabel = Boolean.TRUE + "|" + Boolean.FALSE;
} }
return new PropertyMapper<T>(option, to, isEnabled, enabledWhen, mapper, mapFrom, paramLabel, isMasked, validator); return new PropertyMapper<T>(option, to, isEnabled, enabledWhen, mapper, mapFrom, paramLabel, isMasked, validator, description);
} }
} }

View file

@ -164,7 +164,8 @@ Bootstrap Admin:
--bootstrap-admin-client-id <client id> --bootstrap-admin-client-id <client id>
Client id for the temporary bootstrap admin service account. Used only when Client id for the temporary bootstrap admin service account. Used only when
the master realm is created. Default: temp-admin. the master realm is created. Available only when bootstrap admin client
secret is set. Default: temp-admin.
--bootstrap-admin-client-secret <client secret> --bootstrap-admin-client-secret <client secret>
Client secret for the temporary bootstrap admin service account. Used only Client secret for the temporary bootstrap admin service account. Used only
when the master realm is created. Use a non-CLI configuration option for when the master realm is created. Use a non-CLI configuration option for
@ -174,4 +175,5 @@ Bootstrap Admin:
created. Use a non-CLI configuration option for this option if possible. created. Use a non-CLI configuration option for this option if possible.
--bootstrap-admin-username <username> --bootstrap-admin-username <username>
Temporary bootstrap admin username. Used only when the master realm is Temporary bootstrap admin username. Used only when the master realm is
created. Default: temp-admin. created. Available only when bootstrap admin password is set. Default:
temp-admin.

View file

@ -268,7 +268,8 @@ Bootstrap Admin:
--bootstrap-admin-client-id <client id> --bootstrap-admin-client-id <client id>
Client id for the temporary bootstrap admin service account. Used only when Client id for the temporary bootstrap admin service account. Used only when
the master realm is created. Default: temp-admin. the master realm is created. Available only when bootstrap admin client
secret is set. Default: temp-admin.
--bootstrap-admin-client-secret <client secret> --bootstrap-admin-client-secret <client secret>
Client secret for the temporary bootstrap admin service account. Used only Client secret for the temporary bootstrap admin service account. Used only
when the master realm is created. Use a non-CLI configuration option for when the master realm is created. Use a non-CLI configuration option for
@ -278,4 +279,5 @@ Bootstrap Admin:
created. Use a non-CLI configuration option for this option if possible. created. Use a non-CLI configuration option for this option if possible.
--bootstrap-admin-username <username> --bootstrap-admin-username <username>
Temporary bootstrap admin username. Used only when the master realm is Temporary bootstrap admin username. Used only when the master realm is
created. Default: temp-admin. created. Available only when bootstrap admin password is set. Default:
temp-admin.

View file

@ -158,7 +158,8 @@ Bootstrap Admin:
--bootstrap-admin-client-id <client id> --bootstrap-admin-client-id <client id>
Client id for the temporary bootstrap admin service account. Used only when Client id for the temporary bootstrap admin service account. Used only when
the master realm is created. Default: temp-admin. the master realm is created. Available only when bootstrap admin client
secret is set. Default: temp-admin.
--bootstrap-admin-client-secret <client secret> --bootstrap-admin-client-secret <client secret>
Client secret for the temporary bootstrap admin service account. Used only Client secret for the temporary bootstrap admin service account. Used only
when the master realm is created. Use a non-CLI configuration option for when the master realm is created. Use a non-CLI configuration option for
@ -168,4 +169,5 @@ Bootstrap Admin:
created. Use a non-CLI configuration option for this option if possible. created. Use a non-CLI configuration option for this option if possible.
--bootstrap-admin-username <username> --bootstrap-admin-username <username>
Temporary bootstrap admin username. Used only when the master realm is Temporary bootstrap admin username. Used only when the master realm is
created. Default: temp-admin. created. Available only when bootstrap admin password is set. Default:
temp-admin.

View file

@ -262,7 +262,8 @@ Bootstrap Admin:
--bootstrap-admin-client-id <client id> --bootstrap-admin-client-id <client id>
Client id for the temporary bootstrap admin service account. Used only when Client id for the temporary bootstrap admin service account. Used only when
the master realm is created. Default: temp-admin. the master realm is created. Available only when bootstrap admin client
secret is set. Default: temp-admin.
--bootstrap-admin-client-secret <client secret> --bootstrap-admin-client-secret <client secret>
Client secret for the temporary bootstrap admin service account. Used only Client secret for the temporary bootstrap admin service account. Used only
when the master realm is created. Use a non-CLI configuration option for when the master realm is created. Use a non-CLI configuration option for
@ -272,4 +273,5 @@ Bootstrap Admin:
created. Use a non-CLI configuration option for this option if possible. created. Use a non-CLI configuration option for this option if possible.
--bootstrap-admin-username <username> --bootstrap-admin-username <username>
Temporary bootstrap admin username. Used only when the master realm is Temporary bootstrap admin username. Used only when the master realm is
created. Default: temp-admin. created. Available only when bootstrap admin password is set. Default:
temp-admin.

View file

@ -298,7 +298,8 @@ Bootstrap Admin:
--bootstrap-admin-client-id <client id> --bootstrap-admin-client-id <client id>
Client id for the temporary bootstrap admin service account. Used only when Client id for the temporary bootstrap admin service account. Used only when
the master realm is created. Default: temp-admin. the master realm is created. Available only when bootstrap admin client
secret is set. Default: temp-admin.
--bootstrap-admin-client-secret <client secret> --bootstrap-admin-client-secret <client secret>
Client secret for the temporary bootstrap admin service account. Used only Client secret for the temporary bootstrap admin service account. Used only
when the master realm is created. Use a non-CLI configuration option for when the master realm is created. Use a non-CLI configuration option for
@ -308,7 +309,8 @@ Bootstrap Admin:
created. Use a non-CLI configuration option for this option if possible. created. Use a non-CLI configuration option for this option if possible.
--bootstrap-admin-username <username> --bootstrap-admin-username <username>
Temporary bootstrap admin username. Used only when the master realm is Temporary bootstrap admin username. Used only when the master realm is
created. Default: temp-admin. created. Available only when bootstrap admin password is set. Default:
temp-admin.
Do NOT start the server using this command when deploying to production. Do NOT start the server using this command when deploying to production.

View file

@ -474,7 +474,8 @@ Bootstrap Admin:
--bootstrap-admin-client-id <client id> --bootstrap-admin-client-id <client id>
Client id for the temporary bootstrap admin service account. Used only when Client id for the temporary bootstrap admin service account. Used only when
the master realm is created. Default: temp-admin. the master realm is created. Available only when bootstrap admin client
secret is set. Default: temp-admin.
--bootstrap-admin-client-secret <client secret> --bootstrap-admin-client-secret <client secret>
Client secret for the temporary bootstrap admin service account. Used only Client secret for the temporary bootstrap admin service account. Used only
when the master realm is created. Use a non-CLI configuration option for when the master realm is created. Use a non-CLI configuration option for
@ -484,7 +485,8 @@ Bootstrap Admin:
created. Use a non-CLI configuration option for this option if possible. created. Use a non-CLI configuration option for this option if possible.
--bootstrap-admin-username <username> --bootstrap-admin-username <username>
Temporary bootstrap admin username. Used only when the master realm is Temporary bootstrap admin username. Used only when the master realm is
created. Default: temp-admin. created. Available only when bootstrap admin password is set. Default:
temp-admin.
Do NOT start the server using this command when deploying to production. Do NOT start the server using this command when deploying to production.

View file

@ -299,7 +299,8 @@ Bootstrap Admin:
--bootstrap-admin-client-id <client id> --bootstrap-admin-client-id <client id>
Client id for the temporary bootstrap admin service account. Used only when Client id for the temporary bootstrap admin service account. Used only when
the master realm is created. Default: temp-admin. the master realm is created. Available only when bootstrap admin client
secret is set. Default: temp-admin.
--bootstrap-admin-client-secret <client secret> --bootstrap-admin-client-secret <client secret>
Client secret for the temporary bootstrap admin service account. Used only Client secret for the temporary bootstrap admin service account. Used only
when the master realm is created. Use a non-CLI configuration option for when the master realm is created. Use a non-CLI configuration option for
@ -309,7 +310,8 @@ Bootstrap Admin:
created. Use a non-CLI configuration option for this option if possible. created. Use a non-CLI configuration option for this option if possible.
--bootstrap-admin-username <username> --bootstrap-admin-username <username>
Temporary bootstrap admin username. Used only when the master realm is Temporary bootstrap admin username. Used only when the master realm is
created. Default: temp-admin. created. Available only when bootstrap admin password is set. Default:
temp-admin.
By default, this command tries to update the server configuration by running a By default, this command tries to update the server configuration by running a
'build' before starting the server. You can disable this behavior by using the 'build' before starting the server. You can disable this behavior by using the

View file

@ -475,7 +475,8 @@ Bootstrap Admin:
--bootstrap-admin-client-id <client id> --bootstrap-admin-client-id <client id>
Client id for the temporary bootstrap admin service account. Used only when Client id for the temporary bootstrap admin service account. Used only when
the master realm is created. Default: temp-admin. the master realm is created. Available only when bootstrap admin client
secret is set. Default: temp-admin.
--bootstrap-admin-client-secret <client secret> --bootstrap-admin-client-secret <client secret>
Client secret for the temporary bootstrap admin service account. Used only Client secret for the temporary bootstrap admin service account. Used only
when the master realm is created. Use a non-CLI configuration option for when the master realm is created. Use a non-CLI configuration option for
@ -485,7 +486,8 @@ Bootstrap Admin:
created. Use a non-CLI configuration option for this option if possible. created. Use a non-CLI configuration option for this option if possible.
--bootstrap-admin-username <username> --bootstrap-admin-username <username>
Temporary bootstrap admin username. Used only when the master realm is Temporary bootstrap admin username. Used only when the master realm is
created. Default: temp-admin. created. Available only when bootstrap admin password is set. Default:
temp-admin.
By default, this command tries to update the server configuration by running a By default, this command tries to update the server configuration by running a
'build' before starting the server. You can disable this behavior by using the 'build' before starting the server. You can disable this behavior by using the

View file

@ -241,7 +241,8 @@ Bootstrap Admin:
--bootstrap-admin-client-id <client id> --bootstrap-admin-client-id <client id>
Client id for the temporary bootstrap admin service account. Used only when Client id for the temporary bootstrap admin service account. Used only when
the master realm is created. Default: temp-admin. the master realm is created. Available only when bootstrap admin client
secret is set. Default: temp-admin.
--bootstrap-admin-client-secret <client secret> --bootstrap-admin-client-secret <client secret>
Client secret for the temporary bootstrap admin service account. Used only Client secret for the temporary bootstrap admin service account. Used only
when the master realm is created. Use a non-CLI configuration option for when the master realm is created. Use a non-CLI configuration option for
@ -251,7 +252,8 @@ Bootstrap Admin:
created. Use a non-CLI configuration option for this option if possible. created. Use a non-CLI configuration option for this option if possible.
--bootstrap-admin-username <username> --bootstrap-admin-username <username>
Temporary bootstrap admin username. Used only when the master realm is Temporary bootstrap admin username. Used only when the master realm is
created. Default: temp-admin. created. Available only when bootstrap admin password is set. Default:
temp-admin.
By default, this command tries to update the server configuration by running a By default, this command tries to update the server configuration by running a
'build' before starting the server. You can disable this behavior by using the 'build' before starting the server. You can disable this behavior by using the

View file

@ -407,7 +407,8 @@ Bootstrap Admin:
--bootstrap-admin-client-id <client id> --bootstrap-admin-client-id <client id>
Client id for the temporary bootstrap admin service account. Used only when Client id for the temporary bootstrap admin service account. Used only when
the master realm is created. Default: temp-admin. the master realm is created. Available only when bootstrap admin client
secret is set. Default: temp-admin.
--bootstrap-admin-client-secret <client secret> --bootstrap-admin-client-secret <client secret>
Client secret for the temporary bootstrap admin service account. Used only Client secret for the temporary bootstrap admin service account. Used only
when the master realm is created. Use a non-CLI configuration option for when the master realm is created. Use a non-CLI configuration option for
@ -417,7 +418,8 @@ Bootstrap Admin:
created. Use a non-CLI configuration option for this option if possible. created. Use a non-CLI configuration option for this option if possible.
--bootstrap-admin-username <username> --bootstrap-admin-username <username>
Temporary bootstrap admin username. Used only when the master realm is Temporary bootstrap admin username. Used only when the master realm is
created. Default: temp-admin. created. Available only when bootstrap admin password is set. Default:
temp-admin.
By default, this command tries to update the server configuration by running a By default, this command tries to update the server configuration by running a
'build' before starting the server. You can disable this behavior by using the 'build' before starting the server. You can disable this behavior by using the