secret Posts

Injecting Openshift Secret and Reading it as an Environment Variables in Spring Boot

In this writing, im planning to create a simple Spring Boot application but with a dynamic configuration that is going to be fetched from environment variables. Usually we are using this for securing some sensitive values such as Database credentials or endpoints.

For this scenario, im trying to make password variables as parameterized inside Spring Boot’s Binds it with environment variables with the name of OPENSHIFT_APP_PASSWORD.


And call it from our controller,

package com.edw.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

public class IndexController {

    private String serverPassword;

    public Map helloWorld() {
        return new HashMap() {{
            put("hello", "world");
            put("password", serverPassword);

Dont forget setting up maven’s configuration,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""


            <name>Red Hat Early Access Repository</name>
            <name>Red Hat GA Repository</name>

            <name>Red Hat Early Access Repository</name>
            <name>Red Hat GA Repository</name>





After we commit all the code into github, we can pull them from Openshift with a simple oc command.

$ oc new-app

We can create a variable as a Secret by using below oc command

$ oc create secret generic mypassword --from-literal=OPENSHIFT_APP_PASSWORD=whatever

And inject it into our application,

$ oc set env --from=secret/mypassword dc/spring-boot-and-ocp-secret

Expose our app’s endpoint,

$ oc expose service spring-boot-and-ocp-secret

And do a curl to see that variable “password” has been filled with “whatever” which comes from our OCP Secret.

$ curl -kv http://ocp-endpoint/

* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< Content-Type: application/json

Code for this can be found on below link