How to specify vault endpoint In springboot vault configuration without application name - spring

In spring boot I wanted to read the properties from vault but I wanted to read vaulr secret token and the full path of the configuration endpoint from environment variable.
But if I give spring.cloud.vault.uri it is not working and for fetching from environmental variable I am using ${VAULT_TOKEN} but this also, not working.
spring.cloud.vault.uri=http://127.0.0.1:8200/secret/gs-vault-config/cloud/test
spring.cloud.vault.token=${VAULT_TOKEN}
I wanted to fetch both spring.cloud.vault.uri and spring.cloud.vault.token from environmental variable.

It's not a good idea.
Usually the file bootstrap.properties/bootstrap.yaml is placed separately from the project (project in /src, bootstrap config file in /config). It allows us to use it without rebuilding the project on parameter changes, and to use a dynamic configuration.
We already have a file with dynamic content, you want to create a separate dynamic config file with values for base dynamic config file! Tautology!

Related

Spring cloud config fails to pick configuration from local file system

I am trying to setup spring cloud config using local file system. However, I couldn't get it working.
Below is my application.properties file:
spring.application.name=spring-cloud-config-servers
server.port=8888
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=classpath:/config
Inside limits-service.properties, I have the following:
limits-service.minimum=4
limits-service.maximum=400
Once i run the server, i get the following. Please help in fixing it.
It might be a simple mistake of naming?
For example, your spring application name is "limit-service" while your property files are named "limits-service" and that might be why it is not reading them.
yes above one it is working, spring.applicatiom.name must be equals to the properties file(Lets say uppercase must follow uppercase of properties file as well )

Spring Cloud Config with HOCON files

I have configuration files which are in HOCON (.conf) format. I would like to use Spring Cloud Config to fetch them from a BitBucket repo, and send them to an application (which is NOT a Spring application) when it boots.
I am using the "Serving Plain Text" feature of Spring Cloud Config to fetch and serve a single HOCON file, which defines a particular environment. For example, the client application requests the development.conf environment configuration file, and by specifiying it's exact path in the BitBucket repo, say myApp/development/master/development.conf, the client is served that plaintext file.
However, as is usually desired, I would like base properties from the base.conf to be included in development.conf and have values overridden by the dev enviroment property values if there are common properties in both files.
Previously there was no config server, and so by simply writing the include "base" command at the top of the development.conf file, a HOCON parser (part of the TypeSafe library) in the client app requested the base.conf file (both files being in local directories), and did the overriding when necessary.
When including a Spring Config server, development.conf can be fetched in plain text as mentioned, but it is only (as before) at the client side that the application parses the include "base" command and then refetches the base.conf file. This makes the boot phase of the app very slow (probably) because fetching the additional files requires additional authorization on BitBucket.
Question: how to make it faster?
I have tried spring.cloud.config.server.git.cloneOnStart: true, but this has apparently no improvement (maybe I'm using it incorrectly?)
Should I consider converting the .conf format to .properties format, and using spring.profiles.include: base property at the top of the new developement.properties file?
Perhaps embed the Spring server in another Spring Boot application?
I like the property overriding and inclusion feature of Spring Cloud. I just don't know how to make it work with HOCON formatted files.

How to access environment variable externally in spring boot?

Is it possible to access environment variable of different application.properties from a single place. Actually we are building this software where we have different application.properties for different projects like user-asset. So is it possible to have all environment variable at one external place. If yes, how will it be accessed?
You would have an application.properties file that defines variables that would never change in what every situation you have.
application.properties
server.error.whitelabel.enabled=true #Just an example
Then you could have a separate application.properties with a different name such as application-active.properties. This file would add onto the base application.properties file.
application-active.properties
example.enviroment.variable=${I_AM_AN_ENVIROMENT_VARIABLE}
Then you could have a different application.properties file that has the same property name, in this case example.enviroment.variable.
application-dev.properties
example.enviroment.variable=${I_AM_A_DIFFERENT_ENVIRONMENT_VARIABLE}
Then in your code, you would just need to grab the example.enviroment.variable property depending on the current profile and it would grab the correct environment variable.
To specify what application.properites look at using profiles in spring-boot.
If its an environment variable, then I think multiple applications can access the same variable.
But if its inside the application.properties file, then I think its not possible.
Not only that, if you really feel that one application needs to access the application.properties of another, then I believe this is not a right way to proceed.
Rather, you should externalize the configurations (maybe by using a config server like spring-cloud-config) and share the common properties between the applications.

Environment specific properties from user home in springboot

I am working on a spring boot application in which i have to set Environment specific properties from user home folder.
i dig Google for the same & found we can put different properties file (dev, test, production) under resources and then we have to tell spring boot which environment we want to use using spring.profiles.active=dev OR prod.
however, my requirement is quite different. i will put a file in user home in my system & want to read properties form that file. how can i do that, need guidance.
Helping hands will be highly appreciated.
From the Spring Boot docs:
You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths).
As the docs go on to state, this must be specified on the command line or as an environment variable.
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
We explain that use case in a Devoxx presentation using EnvironmentPostProcessor, please refer to this section of the presentation for more details. You can also find the code sample online.
Well, it seems in your case you dont need environment variable. For production server your property file will be staying in and in staging machine it is also staying at same place. So where ever you deploy it will pick from . IMO you don't need to set environment, you just have to point property file to
Now to define this path you have 2 ways..
- You can put static path in your code
- You can set environment variable like Property_Path and read it in spring boot application..
However If you want to go one step ahead, you can use spring cloud configuration manager, by passing application+profile name to it, CM can fetch property file from directly from git or file system for you ...

Spring cloud config server - more than 1 properties file per app

Does anybody knows if it is possible to expose more than 1 property file per application in Spring Cloud config server?
For example I would like to have defined in my git repo properties for the same app, but in different files:
myapp-customer-services.yml
myapp-products-services.yml
and have all those properties defined inside the files, exposed under "myapp".
No that's not possible currently. I'm not sure it really makes much sense to be honest, since you can easily clearly delineate different sets of properties within a YAML file using separate documents.
Yes it is possible to expose more than 1 property file per application in Spring Cloud config server
You can access it in you client by using following properties
first specify profile which you want
example
myapp-customer.yml
myapp-products.yml
spring.profiles.active=customer,products
spring.cloud.config.name=myapp

Resources