Environment variable access AWS - java

Why can my application not access environment variables that I have set? I deployed a WAR to AWS and set environment variables via ssh, but am unable to read them in. This process works in my dev environment, so not sure why it won't work in production. Any ideas? I am on a windows box locally and a linux box in production, but I don't think that should matter.

The environment variables should be set so that the user your app's process is running as can see them.
You say you are deploying a WAR - so my guess is that it's on tomcat - you could add your environment variables to the setenv.sh script in the tomcat/bin directory.
Sample setenv.sh contents with a few options for tomcat and an environment variable:
export CATALINA_OPTS="-Xmx1024M -XX:PermSize=512M -Duser.timezone=UTC -DgeoIPFile='/some/path/to/GeoIP.dat' -DxafileDir='/tmp/xafile' -DuploadDir='/path/to/uploads/dir'"
export MYENVVAR="12345"

Related

mvn package not recognizing OS environment variables

I'm trying to deploy an API do AWS EC2.
And this API has environment variables inside application.properties, such as $DB_HOST, $DB_USER and $DB_PASS, and then I set them at
vim .bashrc
source .bashrc
All the process occurs alright for the deployment, but when I run
sudo mvn package
It doesn't recognize the environment variables setted from the OS (linux) and build fail.
I can't really find any similar solution here.
Also tried putting them inside the /˜profile with
nano /˜profile
source /˜profile
Anyone has any ideas? :)
When you use sudo, it doesn't use your environment variables but those of the root user (or in case you use sudo -u <user>, that user).
I suggest not using sudo without specifying a user. That means that the code is running as root, which is a big security risk. I'd create a new user dedicated for your application. You can configure that user's environment variables as well.

How to make sure that environment variable placeholders are substituted in a Spring Boot application running in Apache Tomcat?

I have a Spring Boot application, which runs in an Apache Tomcat server. In application.yaml I have, among others, following entries:
mail:
pop3Host: ${MAIL_HOSTNAME}
inboxFolder: ${MAIL_INBOX}
hostName: ${MAIL_HOSTNAME}
port: ${MAIL_PORT}
userName: ${MAIL_USERNAME}
password: ${MAIL_PASSWORD}
The application is deployed to Tomcat from within IntelliJ Idea so I can debug it.
I start Tomcat using the following command:
export JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8090,server=y,suspend=n"
export JAVA_OPTS=" -DMAIL_HOSTNAME='smtp.provider.com' -DMAIL_INBOX='MAIL_INBOX' -DMAIL_PORT='587' -DMAIL_USERNAME='username' -DMAIL_PASSWORD='XXXXXXXX'"
export CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8090,server=y,suspend=n"
./catalina.sh jpda start
However, after I
start Tomcat using the above script,
deploy the Spring Boot application from IntelliJ Idea, and
make sure that the code where those values are used is executed,
I get the exception indicating that the placeholders have not been substituted.
How can I fix it, i. e. make sure that I can specify some information (like user name and password) in application.yaml via environment variables (so that I don't include the actual credentials in application.yaml)?
export JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8090,server=y,suspend=n"
export JAVA_OPTS=" -DMAIL_HOSTNAME='smtp.provider.com' -DMAIL_INBOX='MAIL_INBOX' -DMAIL_PORT='587' -DMAIL_USERNAME='username' -DMAIL_PASSWORD='XXXXXXXX'"
export CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8090,server=y,suspend=n"
./catalina.sh jpda start
Add export MAIL_HOSTNAME= etc. to the above lines, or create a setenv.sh file with such lines (in the same directory as catalina.sh file).
Using setenv.sh is documented in RUNNING.txt file of Apache Tomcat.
It is not possible to pass JVM arguments to a process running in a remote machine from an IDE. The Spring Boot params will be loaded from the JVM params when the process is started in the remote machine. I am sorry to say that it won't work.
An alternate solution i suggest is, use spring config server to create a separate profile for remote debugging config ( like we will have for Dev, QA environments etc ). When you try to debug the application, please restart the remote application using jenkins job ( I am assuming you don't have remote acccess to the box where the app is running) by passing the profile name in the jenkins job so that the values you wanted will be picked up. Please let me know if you need more details.

Where to set java options in jruby on rails?

We are working on jruby on rails app. Want to set JRUBY_OPTS or JAVA_OPTS.
I can easily use the export to set them on my machine, but I'd like these options to persist with the application.
Our application runs on tomcat in production can set options there in the config file.
Putting export JAVA_OPTS in the initializer file didn't work, seems like the options are set after application starts
you can not "persist" such options within the app.
as you exported locally you might export JAVA_OPTS before Tomcat starts - its a JVM process that will read and set those opts for the VM, your app being deployed there has no control over most settings possible via JAVA_OPTS (memory, stack-size etc)

Java environment variables under Windows 2012R2

We are trying to run Apache-Tomcat which would start / stop as a service. However, when trying to set it up, we get error messages as for unset variables, making it impossible to set it as a service. Looking at the file, and given that this is a .BAT file, as provided by the distribution, is there an easy way to set the JAVA-HOME and other required variables for Java to be set globally for the box ? Our main challenge is to get Apache-Tomcat copied / setup through a script, and the environment variables globally (and permanently) set so that when the server is rebooted, or the service started/stopped/restarted.
The JAVA_HOME environment variable is only used when running the service.bat file. Once the service has been created, it has embedded the value, so changing a global JAVA_HOME environment variable will make no difference.
When you want to use a different JAVA_HOME value, you have to deregister the service and reregister with the new value.
This is regardless of Windows version. Also remember to run service.bat "as admin".
There is a good reason why a registered Tomcat instance is not using global environment values. As an example, we're running 6 different Tomcat instances on our UAT server, each with a different combination of Tomcat version and Java version. We couldn't do that if all the instances were using a single global JAVA_HOME or CATALINA_HOME value.

How can I change Session Persistence in Tomcat 7.X?

I am using Tomcat 7.0 and I found a reference of how to change session savings into the database. But I don't get how to change
org.apache.catalina.session.StandardSession.ACTIVITY_CHECK to true
The link that I am referencing is http://www.intelligrape.com/blog/2010/07/21/tomcat-6-session-persistence-through-jdbcstore/
I am using localhost Tomcat 7.X downloaded as zip, MySQL 5.5, STS 3.1(Incuding Eclipse)
Any figuring out?
This is a system property ( http://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html ) so to set these you have to specify the -D option. In your case "-Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true".
But where? Well, System Properties are given to the Java process when it starts. In case of Tomcat you can use a environment variable called $JAVA_OPTS which is given to the Java process on startup. So
export JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true"
and then run Tomcat. For this to persist make sure that the user running catalina.sh has this environment variable always set.
add below line in catalina.bat just after where you found other JAVA OPTS already being set.
set "JAVA_OPTS=%JAVA_OPTS% -Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true"
e.g.
rem Register custom URL handlers
rem Do this here so custom URL handles (specifically 'war:...') can be used in the security policy
set "JAVA_OPTS=%JAVA_OPTS% -Djava.protocol.handler.pkgs=org.apache.catalina.webresources"
set "JAVA_OPTS=%JAVA_OPTS% -Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true"

Categories