I have Java function:
public String getEnvironment(){
String env = System.getProperty("tomcat.runtime.environment.version");
if(env == null)
return "DEV";
return env;
}
and it is usually called this way:
string devEmail = configuration.getString(getEnvironment() + ".DEVELOPER_EMAIL");
Basically, getEnvironment will return strings (depending of environment) DEV, TEST or PROD and it will read from some configuration object proper information for current environment.
The part I don't understand is how is this set? How production server knows to return PROD, developemnt DEV, or test server to return TEST? Or, how do I set property "tomcat.runtime.environment.version" on the web server?
It's Apache Tomcat 6.0 web server.
you need to add it to catalina.sh on linux or catalina.bat on windows.
JAVA_OPTS="$JAVA_OPTS -Dtomcat.runtime.environment.version=PROD"
This is a Java System Property. It can be set at JVM launch time with the "define" or -D flag.
You could define the property by adding it to CATALINA_OPTS environment variable:
CATALINA_OPTS=-Dtomcat.runtime.environment.version=PROD
Related
We have a springboot/Tomcat server running on Elastic Beanstalk. We want to use the Env vars set in beanstalk in our springboot code. Currently we have something like
Private string getvar = System.getenv("ENV_VAR");
//and have also tried
Private string getvar = System.getProperty("ENV_VAR");
Locally this works just fine. When it's on aws, it can't find the variables. We have them set in our EB Instance -> Configuration -> Software -> Environment Variables:
Key = ENV_VAR
Value = valueWeExpect
and I confirmed they are set via cloudShell.
Does anyone know if we are missing a dependency or referencing the variables incorrectly? Is there anything we have to add?
I get my via
#Autowired
private Environment _env;
_env.getProperty("ENV_VAR")
Environment is org.springframework.core.env.Environment
Developed the integration tests using Test container. Have few fields as environment variables(Eg: passing it as quarkus.datasource.username=${SER_DB_USERNAME:postgres}) in application.properties file.
When setting environment field through test container
GenericContainer<?> someService = new GenericContainer<>(img)
.withEnv("SER_DB_USERNAME", DataLayer.DB_USERNAME)
This value is being successfully taken with test containers but
For the below environment variable,
app.security.enabled=${SER_SEC_ENABLE:true} defined in application.properties file
#IfBuildProperty(name = "app.security.enabled", stringValue = "true")
the environment variable is setting through cmd prompt using -DSER_SEC_ENABLED=true, but when trying to pass the same value in test containers, it's always null.
GenericContainer<?> someService = new GenericContainer<>(img)
.withEnv("SER_SEC_ENABLE", "true")
Without having more context of the project, I can at least observe, that app.security.enabled is a build property rather than a runtime property, so it might be evaluated at build time already. If you start the container with an already built image/application, it is very likely, that the environment variable has no effect.
Furthermore, setting a property on the JVM using the -D flag does not result in an environment variable, this is explicitly a system property on the JVM.
We run our micronaut integration tests in the cloud in a docker container
We're setting the MICRONAUT_ENVIRONMENTS=staging in the docker environment variables, to force our application to read the config values from application-staging.yaml.
However, micronaut is automatically adding "test" as an environment, and then read the config values from application-test.yaml.
From the docs (https://docs.micronaut.io/2.2.1/guide/index.html#propertySource), environment variables should have priority compared to deduced environments when loading the config
Is there any reason why micronaut is giving priority to the application-test.yaml values here?
The test environment is added when micronaut tests are running, even when setting up the environment variable MICRONAUT_ENVIRONMENTS
After a bit of digging, it seems the "test" environment is added before the DefaultEnvironment class is initialized, hence it's added even if micronaut.env.deduction is set to false
I have imported an existing Java EE application into my Eclipse IDE.
I found this piece of code under it
public static String decideEnv() {
String env = null;
env = (String) System.getProperties().get("TB_ENVIRONEMNT");
return env;
}
could anybody please let me know from where exactly it gets the value with this line
System.getProperties().get("TB_ENVIRONEMNT")
Do we need to set this value anywhere ??
Go to Run/Run Configurations, locate your run configuration (I guess you are using applicaiton server), choose it and add -DTB_ENVIRONEMNT=your value here to Arguments tab, text area "VM arguments"
There can be multiple ways to set the system properties, following ways are common
search in your code if it sets programatically (do file search )
When it loads the app to server, we can pass the system properties there too
Also see in your native OS's system properties
You can pass it from command line using -Dname=value switch:
java my-app -DTB_ENVIRONEMNT=dev
Working with OWASP's ESAPI, I found myself stuck at this particular line of code.
private static String customDirectory = System.getProperty("org.owasp.esapi.resources");
The code returns null as there is no such system property "org.owasp.esapi.resources" set on my computer. Is there any way to set this property on my computer permanently?
You need to pass it into your JVM as a command line property. Most application containers use the environment variable JAVA_OPTS as a "permanent" store of options that should be passed to the JVM. You can try to do something like this:
In *nix:
export JAVA_OPTS="-Dorg.owasp.esapi.resources=/path/to/esapi/configuration"
In windows:
set JAVA_OPTS="-Dorg.owasp.esapi.resources=C:\path\to\esapi\configuration"
You can add this to windows or linux as a startup command and it will always be set if you desire, or add it to your application's startup script for a more localized solution.
If you put the ESAPI.properties and Validation.properties inside the resources folder it will recognize automatically.
In case you need to specify s specific folder or sub-folders, one possibility is adding this property in your standalone.
<system-properties>
<property name="org.owasp.esapi.resources" value="C:/.../resources/esapi"/>
<system-properties>
However, in unit tests, you have to specify before the tests.
#BeforeClass
public static void before() {
if(System.getProperty("org.owasp.esapi.resources") != null) {
System.out.println(System.getProperty("org.owasp.esapi.resources"));
} else {
System.setProperty("org.owasp.esapi.resources", "src/main/resources");
}
}