Retrieve a secret in the application
Table of contents
Previously, you have created an Azure Key Vault and added the Dapr component. Now, you will use the secret in the application. This bonus assignment is about using Azure Key Vault as a secret store for the FineCollectionService
to get the license key of the fine calculator.
Pre-requisite
If the setup of the Azure Key Vault is not done yet, please follow the instructions in Setup Azure Key Vault as a secret store.
Step 1: Create a secret in the Azure Key Vault for the license key
-
Open a terminal window.
-
Create a secret in the Azure Key Vault for the license key:
az keyvault secret set --vault-name $KEY_VAULT --name license-key --value HX783-5PN1G-CRJ4A-K2L7V
Step 2: Use the secret in the application FineCollectionService
-
Open the file
FineCollectionService/src/main/java/dapr/fines/fines/DaprFineCalulator.java
in your code editor, and inspect it.It implements the
FineCalculator
interface, which is used by theFineCollectionService
to calculate the fine for a car. TheFineCalculator
interface has a methodcalculateFine
that takes theexcessSpeed
as input and returns the amount of the fine as output. If the excess speed is too high, it return-1
.The object
FineFines
that computes the fine requires a license Key. The license key is used to validate the license of the fine calculator. ThisDaprFineCalculator
is getting the license key from the secret store when theFineCalculator
bean is created in the classFineCollectionConfiguration
. The license key is stored in the secret store with the namelicense-key
.public class DaprFineCalculator implements FineCalculator { private final String fineCalculatorLicenseKey; private final FineFines fineFines; public DaprFineCalculator(final DaprClient daprClient) { if (daprClient == null) { throw new IllegalArgumentException("daprClient"); } final Map<String, String> licenseKeySecret = daprClient.getSecret("secretstore", "license-key").block(); if (licenseKeySecret == null || licenseKeySecret.isEmpty()) { throw new RuntimeException("'license-key' is not part of the secret store."); } this.fineCalculatorLicenseKey = licenseKeySecret.get("license-key"); this.fineFines = new FineFines(); } @Override public int calculateFine(final int excessSpeed) { return fineFines.calculateFine(this.fineCalculatorLicenseKey, excessSpeed); } }
-
Open the file
FineCollectionService/src/main/java/dapr/fines/FineCollectionConfiguration.java
in your code editor. - Comment out the following lines as the license key is now retrieved from the secret store instead of the environment variable:
@Value("${finefines.license-key}") private String fineCalculatorLicenseKey;
- Comment out the following @Bean method that creates the bean
FineCalculator
:@Bean public FineCalculator fineCalculator() { return new DefaultFineCalculator(fineCalculatorLicenseKey); }
- Uncomment the following @Bean method that creates the bean
FineCalculator
:// @Bean // public FineCalculator fineCalculator(final DaprClient daprClient) { // return new DaprFineCalculator(daprClient); // }
This method requires the
DaprClient
as input. - Uncomment the following @Bean method that creates the bean
DaprClient
:// @Bean // public DaprClient daprClient() { // return new DaprClientBuilder().build(); // }
-
Check all your code-changes are correct by building the code. Execute the following command in the terminal window:
mvn package
Step 3: Test the application
You’re going to start all the services now.
-
Make sure no services from previous tests are running (close the command-shell windows).
-
Open the terminal window and make sure the current folder is
VehicleRegistrationService
. -
Enter the following command to run the VehicleRegistrationService:
mvn spring-boot:run
-
Open a new terminal window and change the current folder to
FineCollectionService
. -
Enter the following command to run the FineCollectionService with a Dapr sidecar:
- Ensure you have run
dapr init
command prior to running the below command
dapr run --app-id finecollectionservice --app-port 6001 --dapr-http-port 3601 --dapr-grpc-port 60001 --resources-path ../dapr/components mvn spring-boot:run
- Ensure you have run
-
Open a new terminal window and change the current folder to
TrafficControlService
. -
Enter the following command to run the TrafficControlService:
mvn spring-boot:run
-
Open a new terminal window and change the current folder to
Simulation
. -
Start the simulation:
mvn spring-boot:run
You should see the same logs as Assignment 1. Obviously, the behavior of the application is exactly the same as before.
Reference a secret in a component Deploy to ACA