Giter VIP home page Giter VIP logo

spring-pcf-akv's Introduction

Demo PCF SpringBoot app with Azure KeyVault Secrets

For many Azure services OSBA or Meta Azure Service Broker provide functionality to manage Azure resource lifecycle and credentials. For example it will create database (or connect to existing one) when service is created and provide user credentials to the application that binds to it.

Azure KeyVault and secrets lifecycle are typically managed by different groups responsible for security in the organization. In this case it might be sufficient to make use of cloud foundry user -provided service with specifies credentials required to retrieve secrets

User-provided service

As per Microsoft docs How to use the Spring Boot Starter for Azure Key Vault

  • Create or get info for service principal that is granted access to secrets in the Azure KeyVault ( see doc above on steps to create Vault,grant permissions etc)
  • Add secret to Vault with name my-very-secret
  • Create json file with CF service credential definition as below ( saved as akv-service.json in this project)
{
  "clientId":"<SP clientId>",
  "clientSecret":"<SP client Secret>",
  "vaultUri":"https://<vaultname>.vault.azure.net"
}
  • Create CF User Provided Service named azure-vault-service instance by running CF CUPS command
 cf cups azure-vault-service -p akv-service.json

Spring boot AKV starter

Now that we have credentials to establish connection to AKV available in CF service, we will use Spring Boot Starter use this credentials to get secrets from Azure Key Vault

  • Add Azure Key Vault starter to the project pom.xml
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-keyvault-secrets-spring-boot-starter</artifactId>
</dependency>
  • Add to the application settings in application.yaml to the cloud profile activated in CloudFoundry Service Principle and Vault details that will be read from CF User Provided Service.
---
spring.profiles: cloud

# running on PCF - read from VCAP service
azure:
  keyvault:
    uri: ${vcap.services.azure-vault-service.credentials.vaultUri}
    client-id: ${vcap.services.azure-vault-service.credentials.clientId}
    client-key: ${vcap.services.azure-vault-service.credentials.clientSecret}

Note: CF CUPS service data is available as flattened spring boot environment properties with following notation vcap.services.<name of service>.credentials.<name of key>

  • Now we can read Azure KeyVault secrets as any other Spring boot configuration properties ,( as Key Vault will be bound as custom Properties Datasource) Example in TestController.java, where @Value("${my-very-secret}") is read from Azure KeyVault.
@RestController
@RequestMapping("/v1")
public class TestController {

    @Value("${my-very-secret}")
    private String secretValue;

    @Value("${my-not-secret}")
    private String nosecretValue;

    @GetMapping("/values")
    public String[] Values() {
        return  new String[] { secretValue, nosecretValue};
    }
}
  • Compile, package and push application with binding to CF CUPS service created above, here is our manifest.yml
---
applications:
- name: spring-akv
  path: target/akv-0.0.1-SNAPSHOT.jar
  buildpack: java_buildpack
  services:
   - azure-vault-service
mvn clean package
cf push

References

Meta Azure Service Broker AKV docs

Azure Spring Boot Starter for AKV

Azure OSBA

spring-pcf-akv's People

Contributors

lenisha avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.