Define a component

Create a component definition file to interact with the secrets building block

When building an app, you’d most likely create your own component file definitions, depending on the building block and specific component that you’d like to use.

In this tutorial, you will create a component definition file to interact with the secrets building block API:

  • 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

  1. Create a new directory named my-components to hold the new secret and component file:

    1. mkdir my-components
  2. Navigate into this directory.

    1. cd my-components
  3. Dapr supports many types of secret stores, but for this tutorial, create a local JSON file named mysecrets.json with the following secret:

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

Step 2: Create a secret store Dapr component

  1. 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: ./mysecrets.json
    12. - name: nestedSeparator
    13. value: ":"

In the above file definition:

  • type: secretstores.local.file 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 secret store JSON path is relative to where you call dapr run.

Step 3: Run the Dapr sidecar

Launch a Dapr sidecar that will listen on port 3500 for a blank application named myapp:

PowerShell environment:

  1. dapr run --app-id myapp --dapr-http-port 3500 --resources-path ../

non-PowerShell environment:

  1. dapr run --app-id myapp --dapr-http-port 3500 --resources-path .

Tip

If an error message occurs, stating the app-id is already in use, you may need to stop any currently running Dapr sidecars. Stop the sidecar before running the next dapr run command by either:

  • Pressing Ctrl+C or Command+C.
  • Running the dapr stop command in the terminal.

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'

Output:

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

Next step: Set up a Pub/sub broker >>

Last modified March 21, 2024: Merge pull request #4082 from newbe36524/v1.13 (f4b0938)