The documentation you are viewing is for Dapr v1.5 which is an older version of Dapr. For up-to-date documentation, see the latest version.
操作方法:在组件中引用秘密
概述
组件可以在组件定义中为 spec.metadata
部分引用秘密。
为了引用秘密,您需要设置 auth.secretStore
字段以指定秘密存储的名称。
在 Kubernetes 运行时,如果 auth.secretStore
为空,则假定使用 Kubernetes 秘密存储。
支持的秘密存储
转到此链接 ,以查看 Dapr 支持的所有秘密存储,以及有关如何配置和使用它们的信息。
引用秘密
虽然您可以选择使用明文秘密(如 MyPassword),如下面的 yaml 所示,用于 redisPassword
的 value
,但不建议将其用于生产:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
namespace: default
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: MyPassword
相反,在您应该在秘密存储中创建秘密,并在组件定义中引用它。 下面显示了两种情况 - “秘密包含嵌入的密钥"和"秘密是字符串”。
" 秘密包含嵌入的密钥"的情况适用于在秘密中嵌入一个密钥,即秘密不是整个连接字符串。 这显示在以下组件定义 yaml 中。
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
namespace: default
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
secretKeyRef:
name: redis-secret
key: redis-password
auth:
secretStore: <SECRET_STORE_NAME>
SECRET_STORE_NAME
是已配置的秘密存储组件的名称。 当在 Kubernetes 中运行并使用 Kubernetes 秘密存储时,字段 auth.SecretStore
默认为 kubernetes
并且可以留空。
上面的组件定义告诉 Dapr 从定义的 secretStore
中提取名为 redis-secret
的秘密,并将在秘密中嵌入的 redis-password
值分配给组件中的 redisPassword
字段。 这种情况的一个用法是,当代码构造连接字符串时,例如,将 URL、Secret 以及其他必要信息组合到字符串中。
另一方面,当秘密中没有嵌入密钥时,以下"秘密是字符串"情况就适用。 相反,这个秘密只是一个字符串。 因此,在 secretKeyRef
部分中,秘密的 name
和 key
将完全相同。 当Secret本身是一个完整的连接字符串,没有需要提取其值的嵌入的键时,就是这种情况。 通常,连接字符串由连接信息、某种允许连接的机密以及可能的其他信息组成,不需要单独的"秘密”。 这种情况如下面的组件定义 yaml 所示。
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: servicec-inputq-azkvsecret-asbqueue
spec:
type: bindings.azure.servicebusqueues
version: v1
metadata:
-name: connectionString
secretKeyRef:
name: asbNsConnString
key: asbNsConnString
-name: queueName
value: servicec-inputq
auth:
secretStore: <SECRET_STORE_NAME>
上面"秘密是字符串"情况的 yaml 告诉 Dapr 从定义的 secretStore
中提取名为 asbNsConnstring
的连接字符串,并将该值分配给组件中的 connectionString
字段,因为"秘密"中没有嵌入 secretStore
的键,因为它是纯字符串。 这要求秘密的 name
和秘密的 key
相同。
示例
引用 Kubernetes 密钥
下面的示例演示如何创建 Kubernetes 秘密来保存 Event Hubs 绑定的连接字符串。
-
首先,创建 Kubernetes 秘密:
kubectl create secret generic eventhubs-secret --from-literal=connectionString=*********
-
接下来,在您的绑定中引用该 Secret:
apiVersion: dapr.io/v1alpha1 kind: Component metadata: name: eventhubs namespace: default spec: type: bindings.azure.eventhubs version: v1 metadata: - name: connectionString secretKeyRef: name: eventhubs-secret key: connectionString
-
最后,将组件应用到 Kubernetes 集群:
kubectl apply -f ./eventhubs.yaml
限制秘密访问范围
Dapr 可以使用其配置限制对秘密存储中的秘密的访问。 有关详细信息,请阅读 操作方法:使用秘密范围 和 操作方法:限制可从秘密存储读取的秘密。 这是使用 Dapr 限制对秘密的访问的推荐方法。
Kubernetes 权限
默认命名空间
当在 Kubernetes 中运行时,Dapr 在安装过程中定义了默认的 Role 和 RoleBinding ,用于在 default
命名空间中从 Kubernetes 秘密存储中获取秘密。 对于启用了 Dapr 的应用程序,从 default
命名空间获取秘密,可以在组件中定义和引用秘密,如上面的示例所示。
非默认命名空间
如果您的 Dapr 启用的应用正在使用从非默认命名空间获取秘密的组件,在该命名空间应用以下资源:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: secret-reader
namespace: <NAMESPACE>
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "list"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: dapr-secret-reader
namespace: <NAMESPACE>
subjects:
- kind: ServiceAccount
name: default
roleRef:
kind: Role
name: secret-reader
apiGroup: rbac.authorization.k8s.io
这些资源授予 Dapr 从 Role 和 RoleBinding 中定义的命名空间的 Kubernetes 秘密存储中获取秘密的权限。
相关链接
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.