Define a component
In the previous step you called the Dapr HTTP API to store and retrieve a state from a Redis backed state store. Dapr knew to use the Redis instance that was configured locally on your machine through default component definition files that were created when Dapr was initialized.
When building an app, you most likely would create your own component file definitions depending on the building block and specific component that you’d like to use.
As an example of how to define custom components for your application, you will now create a component definition file to interact with the secrets building block.
In this guide you will:
- Create a local JSON secret store
- Register the secret store with Dapr using a component definition file
- Obtain the secret using the Dapr HTTP API
Step 1: Create a JSON secret store
While Dapr supports many types of secret stores, the easiest way to get started is a local JSON file with your secret (note this secret store is meant for development purposes and is not recommended for production use cases as it is not secured).
Begin by saving the following JSON contents into a file named mysecrets.json
:
{
"my-secret" : "I'm Batman"
}
Step 2: Create a secret store Dapr component
Create a new directory named my-components
to hold the new component file:
mkdir my-components
Inside this directory create a new file localSecretStore.yaml
with the following contents:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: my-secret-store
namespace: default
spec:
type: secretstores.local.file
version: v1
metadata:
- name: secretsFile
value: <PATH TO SECRETS FILE>/mysecrets.json
- name: nestedSeparator
value: ":"
You can see that the above file definition has a type: secretstores.local.file
which tells Dapr to use the local file component as a secret store. The metadata fields provide component specific information needed to work with this component (in this case, the path to the secret store JSON is relative to where you call dapr run
from.)
Step 3: Run the Dapr sidecar
Run the following command to launch a Dapr sidecar that will listen on port 3500 for a blank application named myapp:
dapr run --app-id myapp --dapr-http-port 3500 --components-path ./my-components
If you encounter a error message stating the app ID is already in use, it may be that the sidecar you ran in the previous step is still running. Make sure you stop the sidecar before running the above command (e.g. using “Control-C”).
Step 4: Get a secret
In a separate terminal run:
curl http://localhost:3500/v1.0/secrets/my-secret-store/my-secret
Invoke-RestMethod -Uri 'http://localhost:3500/v1.0/secrets/my-secret-store/my-secret'
You should see output with the secret you stored in the JSON file.
{"my-secret":"I'm Batman"}
Next step: Explore Dapr quickstarts >>
Last modified September 20, 2021 : Merge pull request #1800 from greenie-msft/gRPC_proxying_video (36dff3c)