指南:在组件中引用密钥

    组件可以在组件定义中为 部分引用密钥。

    为了引用密钥,您需要设置 auth.secretStore 字段以指定密钥存储的名称。

    在 Kubernetes 运行时,如果 auth.secretStore 为空,则假定使用Kubernetes 密钥存储。

    Go to this link to see all the secret stores supported by Dapr, along with information on how to configure and use them.

    引用密钥

    虽然您可以选择使用纯文本密钥,但不建议用于生产:

    1. apiVersion: dapr.io/v1alpha1
    2. kind: Component
    3. metadata:
    4. name: statestore
    5. namespace: default
    6. spec:
    7. type: state.redis
    8. version: v1
    9. metadata:
    10. - name: redisHost
    11. value: localhost:6379
    12. secretKeyRef:
    13. name: redis-secret
    14. key: redis-password
    15. auth:
    16. secretStore: <SECRET_STORE_NAME>

    SECRET_STORE_NAME is the name of the configured secret store component. 当在 Kubernetes 中运行并使用 Kubernetes 密钥存储时,字段 auth.SecretStore 默认为 并且可以留空。

    上面的组件定义让Dapr从定义的秘密存储中提取一个名为 redis-secret 的密钥,并将密钥的值分配给组件中的 redis-password 密钥中的 redisPassword 欄位。

    下面的示例向您展示如何创建 Kubernetes 密钥来保持 Event Hubs 绑定的连接字符串。

    1. 首先,创建Kubernetes密钥:

    2. 接下来,在您的绑定中引用该密钥:

      1. apiVersion: dapr.io/v1alpha1
      2. kind: Component
      3. metadata:
      4. name: eventhubs
      5. namespace: default
      6. spec:
      7. type: bindings.azure.eventhubs
      8. version: v1
      9. metadata:
      10. - name: connectionString
      11. secretKeyRef:
      12. name: eventhubs-secret
      13. key: connectionString

    访问密钥的范围

    Dapr 可以使用其配置限制对密钥存储中的密钥的访问。 Read How To: Use secret scoping and for more information. 这是推荐的使用 Dapr 限制访问密钥的方式。

    当在 Kubernetes 中运行时,Dapr 在安装过程中定义了默认的 Role 和 RoleBinding ,用于在 default 命名空间中从 Kubernetes 密钥存储中获取密钥。 对于启用了 Dapr 的应用程序,从default命名空间获取密钥,可以在组件中定义和引用一个密钥,如上例所示。

    如果您的 Dapr 启用的应用正在使用从非默认命名空间获取密钥的组件,在该命名空间应用以下资源:

    1. ---
    2. kind: Role
    3. name: secret-reader
    4. namespace: <NAMESPACE>
    5. rules:
    6. - apiGroups: [""]
    7. resources: ["secrets"]
    8. verbs: ["get", "list"]
    9. ---
    10. kind: RoleBinding
    11. apiVersion: rbac.authorization.k8s.io/v1
    12. metadata:
    13. name: dapr-secret-reader
    14. namespace: <NAMESPACE>
    15. subjects:
    16. - kind: ServiceAccount
    17. name: default
    18. roleRef:
    19. kind: Role
    20. name: secret-reader
    21. apiGroup: rbac.authorization.k8s.io

    这些资源给予了 Dapr 权限,从Kubernetes 密钥商店获取角色和 RoleBinding 定义的命名空间的密钥。

    Note

    在生产场景中,仅限Dapr访问某些秘密资源时,您可以使用 resourceNames 字段。 请参阅此 获取更多解释。

    相关链接