How to prevent property bee from being set on a specific config file


doorway

We want to implement a feature that enables the user to choose a role in the system by sending the role that the user would like to have in the login request.

This feature is used for testing (it is "impossible" in customer systems to create test users or assign roles to existing users) and of course it shouldn't be deployed to production.

I would like to deploy my application to fail if the property feature.choose-roleis set to trueand spring active profileset to production.

When we use the springs config-server feature, I also want the application to stop working completely if the property is set to true at runtime.

My first attempt was to simply create this Config:

@Configuration
public class FeatureToggleGuardConfig {

    @Bean
    @RefreshScope
    @ConditionalOnProperty(value = "feature.choose-roles", havingValue = "true")
    @Profile("production")
    public Object preventDeploymentOfRoleChoosingFeatureOnProduction() {
        throw new RuntimeException("feature.choose-roles must not be true in production profile!");
    }
}

This works if the property is set to true when deploying, but from what I understand it will only attempt to refresh the bean if someone actually tries to use it - which will never happen.

Also - I don't think this will throw a runtime exception stopping the whole application when used.

in short:

I want to prevent my application from running (or keep running) if the property feature.choose-rolesis true and the active profile is "production" .

I don't want to change production code to do this ( if(feature is enables && profile is production)etc)

Will M

Maybe instead of having your profile driving some kind of blocker, let your profile drive a configuration bean that says whether to use the feature or not. Then, read the nonProd config from your properties and make the prod config always return false.

It's just like:

public interface ChooseRolesConfig {

    boolean allowChoosingRoles();
}

@Profile("!production")
public class NonProdChooseRolesConfig implements ChooseRolesConfig {

    @Value("${feature.choose-roles}")
    boolean chooseRoles;

    @Override
    public boolean allowChoosingRoles() {
        return chooseRoles;
    }
}

@Profile("production")
public class ProdChooseRolesConfig implements ChooseRolesConfig {

    @Override
    public boolean allowChoosingRoles() {
        return false;
    }
}

Then just autowire the ChooseRolesConfig object and call the method no matter how you change the function. Choose to use cloud-configured roles, always false for prod.

Disclaimer: I wrote this blindly, so it might be missing some notes or something, but hope you understand

Related


How to declare @property but prevent its ivar from being created?

Benzoto I have classes Athat provide a way to get and set objects of type Foo. In property terms, I usually declare in an interface: @property (nonatomic, strong) Foo * foo; This (in modern ObjC) generates both accessors and ivars _foofor storage. If you want

How to prevent a specific dynamic library from being loaded

Herman I want to use an ancient software (Unreal Tournament "Classic" from 1999, also known as UT99). The dynamic library libtxc_dxtn.sois loaded implicitly in search of optional S3 Texture Compression (S3TC) support. Unfortunately, when loading the library, t