Spring Cloud Vault Profile Specific Bootstrap - java

I have a Spring Boot Application and I would like to Load configurations from Vault based on the Profile I am running. At present i have 2 profiles (dev, prod). My Dev Profile uses a H2 database where as the Prod Profile uses a Posgres DB. Running the dev Profile loads the correct config from Vault but running with the Prod Profile seems to be skipping it somehow and not looking in Vault.
bootstrap.yaml
spring:
application:
name: my-app
profiles:
active:
cloud:
vault:
host: localhost
port: 8200
scheme: http
uri: http://localhost:8200
connection-timeout: 5000
read-timeout: 15000
authentication: TOKEN
token: 00000000-0000-0000-0000-000000000000
kv:
enabled: true
backend: secret
default-context: my-app/dev
fail-fast: false
bootstrap-prod.yaml
spring:
application:
name: gateway
profiles:
active: prod
cloud:
vault:
host: localhost
port: 8200
scheme: http
uri: http://localhost:8200
connection-timeout: 5000
read-timeout: 15000
authentication: TOKEN
token: 00000000-0000-0000-0000-000000000000
kv:
enabled: true
backend: secret
default-context: my-app/dev
fail-fast: false
For example running the command gradle will load configuration from vault. But running gradle -Pprod fails to load the correct configuration from Vault.

try this syntax to activate your profile when using gradle:
./gradlew clean bootRun --args='--spring.profiles.active=prod'

Related

Configure SSH key with Config Server

I am doing a POC on Config Server wherein I will connect to BitBucket repo and if any checkin happen in that repo, my Config Service will pick those changes. In the below code if I am providing bitbucket credentials (hardcoding username and password), its able to connect to BitBucket and everything is working fine. But if I try to provide SSH key, its throwing below error:
application.yml
server.port: 8888
spring:
application.name: config-server
cloud.config.server.git.uri: ${url}
cloud:
config:
server:
git:
cloneOnStart: true
username: username
password: password
default-label: master
management:
endpoints:
web:
exposure:
include: refresh
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7001/eureka/
registryFetchIntervalSeconds: 1
instance:
leaseRenewalIntervalInSeconds: 1
Like I said the above configuration is working fine. When I replaced with the below configuration.
server.port: 8888
spring:
application.name: config-server
cloud.config.server.git.uri: ${URL}
cloud:
config:
server:
git:
#cloneOnStart: true
ignore-local-ssh-settings: true
hostKey: ${hostKEY}
hostKeyAlgorithm: ssh-rsa
private-key: ${SSH PRIVATE KEY}
management:
endpoints:
web:
exposure:
include: refresh
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7001/eureka/
registryFetchIntervalSeconds: 1
instance:
leaseRenewalIntervalInSeconds: 1
I am getting below error::
2023-02-10T12:26:39.276+05:30 INFO 24628 --- [ main] c.e.c.ConfigServerApplication : Started ConfigServerApplication in 2.949 seconds (process running for 3.646)
2023-02-10T12:26:41.041+05:30 WARN 24628 --- [2)-10.73.53.175] .c.s.e.MultipleJGitEnvironmentRepository : Error occured cloning to base directory.
org.eclipse.jgit.api.errors.TransportException: https://bitbucketdc-cluster07.jpmchase.net/scm/ccbrft/ccbrap-config.git: Authentication is required but no CredentialsProvider has been registered
I created SSH Key using command :: ssh-keygen -m PEM -t rsa -b 4096 -C "cigfid_dev#example.com
Any help is appreciated.

How to fetch configs for multiple microservices from a central config server in spring boot

I have a config server in github have a structure like this:
-ms_one
|_application.yml
|_application-dev.yml
-ms_two
|_application.yml
|_application-dev.yml
Repo url: My Github Repo
In the project I also follow the same structure:
-configServer
-ms_one
-ms_two
In the application.yml of configServer I have
spring:
application:
name: CONFIGSERVER
profiles:
active: dev
cloud:
config:
server:
git:
uri: <github repo url>
clone-on-start: true
default-label: main
search-paths: {application}
eureka:
client:
fetch-registry: true
register-with-eureka: true
logging:
level:
web: error
In the ms-one service I have in application.yml
spring:
profiles:
active: dev
application:
name: ms-one
config:
import: configserver:http://localhost:8003
Now, I want the config server to fetch the configuration from the folder with the same name as application name and apply the configuration. The problem is the correct configuration is not being fetched. Please help me doing this
I got the idea of setting spring.cloud.config.server.git.search-paths:{application} from the thread How to point spring-cloud-config server to a git folder inside a git repo

Profiles ordering in migration from Spring Boot 2.3 to 2.4

I’m migrating an application from Spring Boot 2.3 to Spring Boot 2.4, I have some problems with profiles order loading.
In my application, I have some profiles :
default (application-default.yml) : default values for the application. There is a lot of default values, the goal is not to complicate the file application.yml, so it's very important to have this file
quality (application-quality.yml) : quality profile
environment profile (for example application-Dev.yml) : profile of the current environment. It must override application.yml and application-default.yml values
database profile for the environment (for example application-databaseDev.yml) : profile for the database connection for the current environment
In Spring Boot 2.3, configuration is :
application.yml
spring:
profiles:
include:
- default
redis:
jedis:
pool:
max-active: 4
application-default.yml
spring:
profiles:
include:
- quality
myApplication:
vcs:
url: https://default.url
cache:
enabled: true
application-Dev.yml
spring:
profiles:
include:
- databaseDev
myApplication:
cache:
enabled: false
I'm starting application with parameter --spring.profiles.active=Dev
In this case, here is the order of the active profile The following profiles are active: default,quality,Dev,databaseDev
This is order I want. For exemple value for key myApplication.cache.enabled is false
I read the documentation to migrate to Spring Boot 2.4, so my configuration is now :
application.yml
spring:
profiles:
group:
"Dev": "default, quality, databaseDev"
redis:
jedis:
pool:
max-active: 4
application-default.yml
myApplication:
vcs:
url: https://default.url
cache:
enabled: true
application-Dev.yml
myApplication:
cache:
enabled: false
But now I have the active profile The following profiles are active: Dev,default,quality,databaseDev
In this case the value for key myApplication.cache.enabled is true
Now profile Dev doesn't override default profile as 2.3 but inversely default profile override Dev profile.
Is there a way to modify the configuration to work as 2.3 (without using use-legacy-processing: true)? Did I make a mistake somewhere?
I found a solution that works :
Rename the profile Dev to dev and create a group named Dev
Here is the result :
application.yml
spring:
config:
import: classpath:application-groups.yml
redis:
jedis:
pool:
max-active: 4
application-groups.yml
spring:
profiles:
group:
"base": "default, quality"
"Dev": "base, databaseDev"
application-default.yml
myApplication:
vcs:
url: https://default.url
cache:
enabled: true
application-dev.yml
myApplication:
cache:
enabled: false
Starting with parameter --spring.profiles.active=Dev
The following profiles are active: Dev,base,default,quality,databaseDev
What do you think about this solution ?

How to get multiple Consul KVs from different Consul context?

I have these KVs sources store on Consul:
config/books/<key>
config/common/<key>
And in my spring boot app application.yml, I have config it as following:
spring:
application:
name: sampleapp
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
prefix: config
defaultContext: books
I know that this configuration that I made has pointed the app to read from config/books .
But I can't figure out how can I retrieve the Consul KV config from both config/common and config/books.
It's not really flexible there, but it can be done if you rename your project and use defaultContext as well. Based on the documentation Spring Cloud Consul Config takes next paths:
config/testApp,dev/
config/testApp/
config/application,dev/
config/application/
where testApp - name of your app, application - default context
So with the following configuration it will work
spring:
application:
name: books
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
prefix: config
defaultContext: common
Logs after running this configuration:
Located property source: CompositePropertySource {name='consul', propertySources=[ConsulPropertySource {name='config/books/'}, ConsulPropertySource {name='config/common/'}]}

Spring Boot Admin does not detect service after restarting

I have an environment with different services. They all are deployed and managed by Docker images and Kubernetes. I also use spring-boot-admin in order to monitor them all and spring-cloud-kubernetes to discover all the services automatically.
This my properties file.
application.yml (In the SBA project)
server:
port: ${admin-server.port:8086}
tomcat:
remote-ip-header: x-forwarded-for
protocol-header: x-forwarded-proto
spring:
application:
name: admin-server
security:
user:
name: ${spring-security.admin.username:****}
password: ${spring-security.admin.password:****}
boot:
admin:
discovery:
ignored-services: admin-server
notify:
mail:
enabled: ${admin-mail.enabled:true}
to: ${admin-mail.recipients:******}
from: ${admin-mail.from:******}
template: classpath:/template/status-changed.html
ignore-changes: OFFLINE:UP, DOWN:UP
slack:
webhook-url: ${admin-slack.webhook:*******}
ignore-changes: OFFLINE:UP, DOWN:UP
enabled: true
mail:
test-connection: false
host: smtpjc.*****
port: 25
properties:
mail:
smtp:
connectiontimeout: 5000
timeout: 3000
writetimeout: 5000
debug: ${admin-mail.debug:true}
It works perfectly whenever I restart the SBA project, it discovers every service.
My problem comes when I restart a single project, it is shown as OFFLINE in the SBA and it does not change its status.
What am I missing?
I have found the issue and fixed it like this:
spring.cloud.kubernetes.reload.enabled: true
I should have added this line to the config file.
I've found out, that I had to use the fabric8 kubernetes-client.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-fabric8-all</artifactId>
</dependency>
I'm using these current versions:
<spring-cloud.version>2020.0.2</spring-cloud.version>
<spring-cloud-kubernetes.version>2.0.2</spring-cloud-kubernetes.version>
<spring-boot-admin.version>2.4.1</spring-boot-admin.version>
Otherwise I follow the instructions of https://piotrminkowski.com/2020/02/18/spring-boot-admin-on-kubernetes/

Categories