How to access Elastic Beanstalk env var in springboot application - java

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

Related

property expressions not working in quarkus native

I have the following property in my application.properties
// application.properties
LOCAL.JWT_PUBLIC_KEY=${JWT_PUBLIC_KEY:KEY_JWT}
And I'm injecting that property using the microprofile config anotation ConfigProperty
// OnPremiseSecrets.java
public JwtConfig(#ConfigProperty(name = "LOCAL.JWT_PUBLIC_KEY") String jwtPublicKey)
it works perfectly while running the program in JVM with a compiled jar quarkusDev
but it doesn't work in a native graalvm compilation, in the logs it return KEY_JWT as jwtPublicKey value which is the default.
I tried reading enviernment variables directry using
System.getenv
and it returns the right value, the environment variable is configured

Quarkus environment variables issue with Test containers

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.

is it possible to refer environmental variable as values for properties in the kafkaProducer.properties and KafkaConsumer.properties files of Kafka

Im have a producer and consumer java code and Im trying to upgrade it to connect with the Kafka which is secured with SSL. I'm in a situation that the ssl related passwords should be given only via environmental variables.
So is it possible to directly refer to the values refered by Environmental variables in the KafkaProducer.properties and KafkaConsumer.properties files
For Example:
I declare an environmental variable in linux system SSL_KEY_PASSWORD=password
And inside the KafkaProducer/Consumer properties, I declare as,
''' ssl.key.password=${SSL_KEY_PASSWORD} '''
Sample KAFKA Consumer/Producer property file config may look like,
# For SSL
security.protocol=SSL
ssl.truststore.location=/var/private/ssl/client.truststore.jks
ssl.truststore.password=${TRUSTSTORE_PASS_ENV_VARIABLE}
# For SSL auth
ssl.keystore.location=/var/private/ssl/client.keystore.jks
ssl.keystore.password=${KEYSTORE_PASS_ENV_VARIABLE}
ssl.key.password=${KEY_PASS_ENV_VARIABLE}
No, they cannot.
Unclear how you are using the files or Kafka clients. If from the shell commands, you should create a bash wrapper around the command you're running that uses sed or other templating scripts that generates the files before running the final command.
If you are actually writing Java code, then build the Properties from the environment there, and not using files.
Don't think properties file values are interpolated but probably you can test it once. Alternatively you can also remove these lines from property file and do it from code something like below...
final Properties properties = new Properties();
final FileInputStream input = new FileInputStream(yourExistingFile);
properties.load(input);
properties.put("ssl.key.password",System.getenv("SSL_KEY_PASSWORD"));//this is additional property

Spring Boot App does not recognize environment variables in application.yml file

there. I'm new to Java Spring Boot and I'm trying to set environment variables in application.yml.
I've added dotenv maven dependency:
<!-- https://mvnrepository.com/artifact/io.github.cdimascio/java-dotenv -->
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>java-dotenv</artifactId>
<version>5.1.3</version>
</dependency>
I've set variables in the .env file:
SPRING_DATABASE_URL = jdbc://db_url
SPRING_DATABASE_USERNAME = username
SPRING_DATABASE_PASSWORD = password
And in my application.yml:
spring:
datasource:
url: ${SPRING_DATABASE_URL}
username: ${env.SPRING_DATABASE_USERNAME}
password: ${env.SPRING_DATABASE_PASSWORD}
While running application I'm getting jdbc error:
java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, ${SPRING_DATABASE_URL}
I've tried some solutions like:
export SPRING_DATABASE_URL = jdbc://db_url
or in application.yml:
url: ${env.SPRING_DATABASE_URL}
or
url: ${env.SPRING.DATABASE.URL}
or
url: ${SPRING.DATABASE.URL}
Am I doing something wrong or missing? I appreciate your help, thank you.
I recently ran in a similar issue and wanted to set environment variables via .env with application.yml - here is what I found out:
First, as you mentioned you have to add the java-dotenv dependency to pom.xml:
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>2.2.0</version>
</dependency>
Then create your .env file in the root of your project (where pom.xml is located) and write your environment variables like e.g. ENV_PORT=8081.
Before you can use this environment variable, you have to "bind" the content of the .env file with Spring Boot when you start the app to make it globally available. According to this thread, this can be achieved by simply altering your Main entry point of spring (where you init the framework) like so:
#SpringBootApplication
public class MySpringApplication {
public static void main(String[] args) {
Map<String, Object> env = Dotenv.load()
.entries()
.stream()
.collect(
Collectors.toMap(DotenvEntry::getKey, DotenvEntry::getValue));
new SpringApplicationBuilder(MySpringApplication.class)
.environment(new StandardEnvironment() {
#Override
protected void customizePropertySources(MutablePropertySources propertySources) {
super.customizePropertySources(propertySources);
propertySources.addLast(new MapPropertySource("dotenvProperties", env));
}
}).run(args);
}
}
That's it, now you can reference to your environment variables in application.yml like so:
server:
port: ${ENV_PORT}
Hope this helps! If you are interested, here is also a full working example where I am using this approach.
java-dotenv you use has been renamed to kotlin-dotenv. If you look at its documentation, you'll see how it's used:
With Java
import io.github.cdimascio.dotenv.Dotenv;
Dotenv dotenv = Dotenv.load();
dotenv.get("MY_ENV_VAR1")
That's not what spring boot does - spring boot will not use any dotenv to fetch environment variables, so naturally the plugin won't work for your use case so .env files will not work.
If you start your app defining the environment variable when starting it app, it will work. To see how environment variables are passed using vscode, see this thread.

How to set tomcat.runtime.environment.version on the web server?

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

Categories