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:

  1. {
  2. "my-secret" : "I'm Batman"
  3. }

Step 2: Create a secret store Dapr component

Create a new directory named my-components to hold the new component file:

  1. mkdir my-components

Inside this directory create a new file localSecretStore.yaml with the following contents:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: my-secret-store
  5. namespace: default
  6. spec:
  7. type: secretstores.local.file
  8. version: v1
  9. metadata:
  10. - name: secretsFile
  11. value: <PATH TO SECRETS FILE>/mysecrets.json
  12. - name: nestedSeparator
  13. 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:

  1. 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:

  1. curl http://localhost:3500/v1.0/secrets/my-secret-store/my-secret
  1. 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.

  1. {"my-secret":"I'm Batman"}

Next step: Explore Dapr quickstarts >>

Last modified February 18, 2022: Update setup-jetstream.md (#2200) (428d8c2)