偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Dapr 入門教程之密鑰存儲(chǔ)

云計(jì)算 云原生
Dapr 在 Kubernetes 模式下通過(guò) Helm 或 dapr init -k 部署的時(shí)候,啟用一個(gè)內(nèi)置的 Kubernetes Secret 存儲(chǔ),如果你使用另一個(gè) Secret 存儲(chǔ),你可以使用 disable-builtin-k8s-secret-store 設(shè)置禁用 Dapr Kubernetes Secret 存儲(chǔ)。

應(yīng)用程序通常通過(guò)使用專用的 Secret 存儲(chǔ)來(lái)存儲(chǔ)敏感信息,如密鑰和 Token,用于與數(shù)據(jù)庫(kù)、服務(wù)和外部系統(tǒng)進(jìn)行身份驗(yàn)證的 Secret 等。通常這需要涉及到設(shè)置一個(gè) Secret 存儲(chǔ),如 ??Azure Key Vault???、??Hashicorp Vault?? 等,并在那里存儲(chǔ)應(yīng)用程序級(jí)別的私密數(shù)據(jù)。為了訪問(wèn)這些 Secret 存儲(chǔ),應(yīng)用程序需要導(dǎo)入 Secret 存儲(chǔ)的 SDK,并使用它來(lái)訪問(wèn)私密數(shù)據(jù),這可能需要相當(dāng)數(shù)量的代碼,這些代碼與應(yīng)用程序的實(shí)際業(yè)務(wù)領(lǐng)域無(wú)關(guān),因此在可能使用不同供應(yīng)商特定的 Secret 存儲(chǔ)的多云場(chǎng)景中,這將成為更大的挑戰(zhàn)。

為了使開發(fā)者更容易使用應(yīng)用程序的私密數(shù)據(jù),Dapr 有一個(gè)專門的 Secret 構(gòu)建塊 API,允許開發(fā)者從 Secret 存儲(chǔ)中獲取私密數(shù)據(jù)。使用 Dapr 的 Secret 存儲(chǔ)構(gòu)建塊通常涉及以下內(nèi)容。

  • 為特定的 Secret 存儲(chǔ)解決方案設(shè)置一個(gè)組件。
  • 在應(yīng)用程序代碼中使用 Dapr 的 Secret API 來(lái)檢索私密數(shù)據(jù)。
  • (可選)在 Dapr 組件文件中引用 Secret。

默認(rèn)情況下,Dapr 在 Kubernetes 模式下通過(guò) Helm 或 dapr init -k 部署的時(shí)候,啟用一個(gè)內(nèi)置的 Kubernetes Secret 存儲(chǔ),如果你使用另一個(gè) Secret 存儲(chǔ),你可以使用 disable-builtin-k8s-secret-store 設(shè)置禁用 Dapr Kubernetes Secret 存儲(chǔ)。

應(yīng)用程序代碼可以調(diào)用 Secret 構(gòu)建塊 API 從 Dapr 支持的 Secret 存儲(chǔ)中檢索私密數(shù)據(jù),這些 Secret 存儲(chǔ)可以在你的代碼中使用。例如,下圖顯示了一個(gè)應(yīng)用程序從配置的云 Secret 存儲(chǔ)庫(kù)中的一個(gè)名為 vault 的 Secret 存儲(chǔ)庫(kù)中請(qǐng)求名為 mysecret 的私密數(shù)據(jù)。

圖片

Dapr Secret

應(yīng)用程序可以使用 secrets API 來(lái)訪問(wèn)來(lái)自 Kubernetes Secret 存儲(chǔ)的私密數(shù)據(jù)。在下面的例子中,應(yīng)用程序從 Kubernetes Secret 存儲(chǔ)中檢索相同的 mysecret。

圖片

Dapr Secret On K8s

本地環(huán)境使用 Secrets

同樣我們以 quickstarts 倉(cāng)庫(kù)進(jìn)行說(shuō)明。

git clone [-b <dapr_version_tag>] https://github.com/dapr/quickstarts.git
cd quickstarts

然后定位到 secretstore 目錄下面的 node 文件夾:

$ cd tutorials/secretstore/node

在 app.js 中是一個(gè)簡(jiǎn)單的 Express 應(yīng)用,它暴露了一些路由和處理程序,我們可以先查看下該文件中的如下內(nèi)容:

const daprPort = process.env.DAPR_HTTP_PORT || 3500;
const secretStoreName = process.env.SECRET_STORE;
const secretName = "mysecret";

其中 secretStoreName 從環(huán)境變量 SECRET_STORE 中讀取,,其為 Kubernetes 部署注入了值 kubernetes,對(duì)于本地開發(fā),環(huán)境變量必須設(shè)置為 localsecretstore 值。

然后我們看看 getsecret 處理程序代碼:

app.get("/getsecret", (_req, res) => {
const url = `${secretsUrl}/${secretStoreName}/${secretName}?metadata.namespace=default`;
console.log("Fetching URL: %s", url);
fetch(url)
.then((res) => res.json())
.then((json) => {
let secretBuffer = new Buffer(json["mysecret"]);
let encodedSecret = secretBuffer.toString("base64");
console.log("Base64 encoded secret is: %s", encodedSecret);
return res.send(encodedSecret);
});
});

該代碼從 secret store 中獲取名為 mysecret 的數(shù)據(jù),并顯示該數(shù)據(jù)的 Base64 編碼數(shù)據(jù)。

我們?cè)?nbsp;secrets.json 文件中添加一個(gè) mysecret 的 Secret 數(shù)據(jù):

{
"mysecret": "abcd"
}

同樣我們也需要添加一個(gè) Secret 對(duì)應(yīng)的 Component 組件,比如在本地自拓管模式,創(chuàng)建一個(gè)如下所示的配置文件:

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: localsecretstore
namespace: default
spec:
type: secretstores.local.file
version: v1
metadata:
- name: secretsFile
value: secrets.json
- name: nestedSeparator
value: ":"

上面的組件定義了一個(gè)本地 Secret 存儲(chǔ)庫(kù),其 Secret 文件路徑為 secrets.json 文件。

其中 Secret 存儲(chǔ) JSON 的路徑是與你調(diào)用 dapr run 的位置相關(guān)的。

然后我們需要將上面的 Secret Store 名稱設(shè)置為環(huán)境變量:

export SECRET_STORE="localsecretstore"
set SECRET_STORE=localsecretstore

接下來(lái)我們?yōu)?Node 應(yīng)用安裝依賴:

npm install

然后我們使用 Dapr 帶上本地的 secret store 組件運(yùn)行 Node 應(yīng)用:

$ dapr run --app-id nodeapp --components-path ./components --app-port 3000 --dapr-http-port 3500 node app.js
?? Starting Dapr with id nodeapp. HTTP Port: 3500. gRPC Port: 58744
INFO[0000] starting Dapr Runtime -- version 1.8.4 -- commit 18575823c74318c811d6cd6f57ffac76d5debe93 app_id=nodeapp instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4

INFO[0000] component loaded. name: localsecretstore, type: secretstores.local.file/v1 app_id=nodeapp instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
INFO[0000] all outstanding components processed app_id=nodeapp instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4

== APP == Node App listening on port 3000!
INFO[0000] application discovered on port 3000 app_id=nodeapp instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
WARN[0000] [DEPRECATION NOTICE] Adding a default content type to incoming service invocation requests is deprecated and will be removed in the future. See https://docs.dapr.io/operations/support/support-preview-features/ for more details. You can opt into the new behavior today by setting the configuration option `ServiceInvocation.NoDefaultContentType` to true. app_id=nodeapp instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
INFO[0000] application configuration loaded app_id=nodeapp instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
INFO[0000] actors: state store is not configured - this is okay for clients but services with hosted actors will fail to initialize! app_id=nodeapp instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
INFO[0000] actor runtime started. actor idle timeout: 1h0m0s. actor scan interval: 30s app_id=nodeapp instance=MBP2022.local scope=dapr.runtime.actor type=log ver=1.8.4
INFO[0000] dapr initialized. Status: Running. Init Elapsed 326.57000000000005ms app_id=nodeapp instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
INFO[0000] placement tables updated, version: 0 app_id=nodeapp instance=MBP2022.local scope=dapr.runtime.actor.internal.placement type=log ver=1.8.4
?? Updating metadata for app command: node app.js
? You're up and running! Both Dapr and your app logs will appear here.

啟動(dòng)后我們可以使用 dapr list 來(lái)查看應(yīng)用列表:

$ dapr list
APP ID HTTP PORT GRPC PORT APP PORT COMMAND AGE CREATED PID
nodeapp 3500 58744 3000 node app.js 11m 2022-09-27 15:13.46 5906

啟動(dòng)完成后我們可以直接訪問(wèn)應(yīng)用的 getsecret 接口:

$ curl -k http://localhost:3000/getsecret

正常輸出結(jié)果是 YWJjZA==,也就是上面的 abcd 做了 base64 編碼后的值。

然后觀察應(yīng)用的日志會(huì)出現(xiàn)類似于如下所示的內(nèi)容:

== APP == Fetching URL: http://localhost:3500/v1.0/secrets/localsecretstore/mysecret?metadata.namespace=default
== APP == Base64 encoded secret is: YWJjZA==

測(cè)試完成后可以使用 dapr stop 命令來(lái)停止應(yīng)用:

dapr stop --app-id nodeapp

Kubernetes 環(huán)境使用 Secrets

接下來(lái)我們來(lái)了解下在 Kubernetes 模式下 Dapr 是如何使用 Secrets store 的,當(dāng)然首先需要在 Kubernetes 集群中安裝 Dapr 控制平面。

Dapr 可以使用許多不同的 secret stores 來(lái)解析 secrets 數(shù)據(jù),比如 AWS Secret Manager、 Azure Key Vault、 GCP Secret Manager、 Kubernetes 等,我們這里可以直接使用 Kubernetes 的 Secret 對(duì)象進(jìn)行演示。

首先講 secrets 數(shù)據(jù)添加到 ./mysecret 文件,比如你的密碼是 abcd,則 ./mysecret 文件內(nèi)容應(yīng)該就是 abcd。

然后基于 ./mysecret 文件創(chuàng)建一個(gè) Kubernetes Secret 對(duì)象:

$ kubectl create secret generic mysecret --from-file ./mysecret

注意創(chuàng)建的 Secret 對(duì)象的名稱 mysecret,后面會(huì)使用到。

創(chuàng)建完成后我們可以查看下該對(duì)象中的數(shù)據(jù)是否符合預(yù)期:

$ kubectl get secret mysecret -o yaml
apiVersion: v1
data:
mysecret: YWJjZAo=
kind: Secret
metadata:
creationTimestamp: "2022-09-27T07:34:31Z"
name: mysecret
namespace: default
resourceVersion: "5133821"
uid: c9aa573c-5f71-439c-b482-748ac0fe3ae7
type: Opaque

接下來(lái)我們就可以部署 Node.js 應(yīng)用到 Kubernetes 集群中,對(duì)應(yīng)的資源清單文件如下所示:

kind: Service
apiVersion: v1
metadata:
name: nodeapp
labels:
app: node
spec:
selector:
app: node
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodeapp
labels:
app: node
spec:
selector:
matchLabels:
app: node
template:
metadata:
labels:
app: node
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "nodeapp"
dapr.io/app-port: "3000"
spec:
containers:
- name: node
image: ghcr.io/dapr/samples/secretstorenode:latest
env:
- name: SECRET_STORE
value: "kubernetes"
ports:
- containerPort: 3000
imagePullPolicy: Always

這里的核心重點(diǎn)是需要我們配置環(huán)境變量 SECRET_STORE,將其值設(shè)置為 kubernetes,這樣我們的應(yīng)用就知道應(yīng)該通過(guò) Kubernetes 獲取 Secret 數(shù)據(jù)了。直接部署該應(yīng)用即可:

$ kubectl apply -f deploy/node.yaml
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nodeapp-6cb5b689cf-vtn74 2/2 Running 0 92
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nodeapp LoadBalancer 10.101.216.73 192.168.0.50 80:32719/TCP 13d
nodeapp-dapr ClusterIP None <none> 80/TCP,50001/TCP,50002/TCP,9090/TCP 13d

部署完成后我們這里可以通過(guò) 192.168.0.50 這個(gè) EXTERNAL-IP 訪問(wèn)到應(yīng)用:

curl -k http://192.168.0.50/getsecret

正常上面的請(qǐng)求輸出結(jié)果為 YWJjZAo=,也可以查看 Node 應(yīng)用日志:

$ kubectl logs --selector=app=node -c node
Node App listening on port 3000!
Fetching URL: http://localhost:3500/v1.0/secrets/kubernetes/mysecret?metadata.namespace=default
Base64 encoded secret is: YWJjZAo=

從上面日志可以看出 Node 應(yīng)用程序正在向 dapr 發(fā)出請(qǐng)求,以便從 secret store 獲取 secret 數(shù)據(jù),注意其中的 mysecret 是上面創(chuàng)建的 Secret 對(duì)象名稱。

當(dāng)然如果你使用的是其他 secret store,比如 HashiCorp Vault 則需要?jiǎng)?chuàng)建一個(gè)對(duì)應(yīng)的 Component 組件了,類型為secretstores.hashicorp.vault,如下所示的資源清單:

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: vault
spec:
type: secretstores.hashicorp.vault
version: v1
metadata:
- name: vaultAddr
value: [vault_address]
- name: caCert
value: "[ca_cert]"
- name: caPath
value: "[path_to_ca_cert_file]"
- name: caPem
value : "[encoded_ca_cert_pem]"
- name: skipVerify
value : "[skip_tls_verification]"
- name: tlsServerName
value : "[tls_config_server_name]"
- name: vaultTokenMountPath
value : "[path_to_file_containing_token]"
- name: vaultToken
value : "[path_to_file_containing_token]"
- name: vaultKVPrefix
value : "[vault_prefix]"
- name: vaultKVUsePrefix
value: "[true/false]"
- name: enginePath
value: "secret"
- name: vaultValueType
value: "map"

對(duì)于其他 Dapr 支持的 secret store 的配置屬性可以參考官方文檔 https://docs.dapr.io/reference/components-reference/supported-secret-stores/ 了解相關(guān)信息。

責(zé)任編輯:姜華 來(lái)源: k8s技術(shù)圈
相關(guān)推薦

2022-09-21 21:50:18

Dapr消息隊(duì)列

2022-09-19 16:08:31

Dapr發(fā)布訂閱

2022-09-30 06:36:25

DaprFastHTTP

2010-08-02 09:36:22

Flex

2009-10-21 14:49:46

VB入門教程

2010-08-16 09:56:05

DivCSS

2011-09-02 14:29:20

jQuery Mobi主題

2009-10-21 18:09:12

VB入門教程

2014-12-31 10:54:44

DockerDocker Remo鏡像命令

2010-05-31 10:56:51

SVN服務(wù)器架設(shè)

2010-08-16 09:32:01

DivCSS

2010-08-16 10:10:11

DIV+CSS

2009-07-08 15:12:48

Java Servle

2014-05-26 15:35:55

Web組件Web Compone

2010-08-03 13:06:15

Flex Builde

2013-08-29 14:12:52

Storm分布式實(shí)時(shí)計(jì)算

2009-07-17 09:44:40

iBATIS教程

2011-09-02 10:59:10

jQuery Mobi

2013-06-24 13:38:34

HTML5 DataList

2018-03-22 14:59:13

Docker入門容器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)