Dapr 祕密輸入繫結可讓您在函式執行期間讀取作為輸入的祕密資料。
如需 Dapr 延伸模組的安裝和設定詳細資料,請參閱 Dapr 延伸模組概觀。
Example
您可以使用下列其中一種 C# 模式來建立 C# 函式:
| Execution model | Description |
|---|---|
| 隔離式背景工作模型 | 您的函數程式碼在個別的 .NET 背景工作處理序中執行。 搭配支援的 .NET 和 .NET Framework 版本使用。 若要深入瞭解,請參閱 隔離背景工作模型中執行 C# Azure Functions 的指南。 |
| In-process model | 您的函數程式碼執行的處理序與 Functions 主機處理序相同。 僅支援長期支援 (LTS) 的 .NET 版本。 若要深入瞭解,請參閱 使用 Azure Functions 開發 C# 類別庫函式。 |
[FunctionName("RetrieveSecret")]
public static void Run(
[DaprServiceInvocationTrigger] object args,
[DaprSecret("kubernetes", "my-secret", Metadata = "metadata.namespace=default")] IDictionary<string, string> secret,
ILogger log)
{
log.LogInformation("C# function processed a RetrieveSecret request from the Dapr Runtime.");
}
下列範例會使用 "RetrieveSecret" 繫結搭配 DaprSecretInput 來建立 DaprServiceInvocationTrigger 函式:
@FunctionName("RetrieveSecret")
public void run(
@DaprServiceInvocationTrigger(
methodName = "RetrieveSecret") Object args,
@DaprSecretInput(
secretStoreName = "kubernetes",
key = "my-secret",
metadata = "metadata.namespace=default")
Map<String, String> secret,
final ExecutionContext context)
在下列範例中,Dapr 祕密輸入繫結會與 Dapr 叫用觸發程序配對,該程序是由 app 物件所註冊:
const { app, trigger } = require('@azure/functions');
app.generic('RetrieveSecret', {
trigger: trigger.generic({
type: 'daprServiceInvocationTrigger',
name: "payload"
}),
extraInputs: [daprSecretInput],
handler: async (request, context) => {
context.log("Node function processed a RetrieveSecret request from the Dapr Runtime.");
const daprSecretInputValue = context.extraInputs.get(daprSecretInput);
// print the fetched secret value
for (var key in daprSecretInputValue) {
context.log(`Stored secret: Key=${key}, Value=${daprSecretInputValue[key]}`);
}
}
});
The following examples show Dapr triggers in a function.json file and PowerShell code that uses those bindings.
Here's the function.json file for daprServiceInvocationTrigger:
{
"bindings":
{
"type": "daprSecret",
"direction": "in",
"name": "secret",
"key": "my-secret",
"secretStoreName": "localsecretstore",
"metadata": "metadata.namespace=default"
}
}
For more information about function.json file properties, see the Configuration section.
In code:
using namespace System
using namespace Microsoft.Azure.WebJobs
using namespace Microsoft.Extensions.Logging
using namespace Microsoft.Azure.WebJobs.Extensions.Dapr
using namespace Newtonsoft.Json.Linq
param (
$payload, $secret
)
# PowerShell function processed a CreateNewOrder request from the Dapr Runtime.
Write-Host "PowerShell function processed a RetrieveSecretLocal request from the Dapr Runtime."
# Convert the object to a JSON-formatted string with ConvertTo-Json
$jsonString = $secret | ConvertTo-Json
Write-Host "$jsonString"
下列範例示範使用 v2 Python 程式設計模型的 Dapr 祕密輸入繫結。 若要在 Python 函式應用程式程式碼中搭配 daprSecret 使用 daprServiceInvocationTrigger 繫結:
import logging
import json
import azure.functions as func
app = func.FunctionApp()
@app.function_name(name="RetrieveSecret")
@app.dapr_service_invocation_trigger(arg_name="payload", method_name="RetrieveSecret")
@app.dapr_secret_input(arg_name="secret", secret_store_name="localsecretstore", key="my-secret", metadata="metadata.namespace=default")
def main(payload, secret: str) :
# Function should be invoked with this command: dapr invoke --app-id functionapp --method RetrieveSecret --data '{}'
logging.info('Python function processed a RetrieveSecret request from the Dapr Runtime.')
secret_dict = json.loads(secret)
for key in secret_dict:
logging.info("Stored secret: Key = " + key +
', Value = ' + secret_dict[key])
Attributes
In the in-process model, use the DaprSecret to define a Dapr secret input binding, which supports these parameters:
| Parameter | Description |
|---|---|
| SecretStoreName | 用來取得祕密的祕密存放區名稱。 |
| Key | 用來識別要取得之祕密名稱的金鑰。 |
| Metadata | Optional. 格式為 "key1=value1&key2=value2" 的中繼資料屬性陣列。 |
Annotations
DaprSecretInput 註釋可讓您允許函式存取祕密。
| Element | Description |
|---|---|
| secretStoreName | Dapr 祕密存放區的名稱。 |
| key | 祕密金鑰值。 |
| metadata | Optional. 中繼資料值。 |
Configuration
下表說明您在程式碼中設定的繫結設定屬性。
| Property | Description |
|---|---|
| key | 祕密金鑰值。 |
| secretStoreName | Name of the secret store as defined in the local-secret-store.yaml component file. |
| metadata | 中繼資料命名空間。 |
下表說明您在 function.json 檔案中設定的繫結設定屬性。
| function.json property | Description |
|---|---|
| key | 祕密金鑰值。 |
| secretStoreName | Name of the secret store as defined in the local-secret-store.yaml component file. |
| metadata | 中繼資料命名空間。 |
See the Example section for complete examples.
Usage
若要使用 Dapr 祕密輸入繫結,請先設定 Dapr 祕密存放區元件。 您可以在官方 Dapr 文件中深入了解要使用哪個元件,以及如何進行設定。
To use the daprSecret in Python v2, set up your project with the correct dependencies.
在
requirements.text檔案中新增以下這一行:azure-functions==1.18.0b3在終端機中,安裝 Python 程式庫。
pip install -r .\requirements.txt使用下列組態修改您的
local.setting.json檔案:"PYTHON_ISOLATE_WORKER_DEPENDENCIES":1
Next steps
深入了解 Dapr 祕密。(英文)