Dapr シークレット入力バインドを使うと、関数の実行中にシークレット データを入力として読み取ることができます。
Dapr 拡張機能のセットアップと構成について詳しくは、Dapr 拡張機能の概要に関する記事をご覧ください。
Example
A C# 関数は、次の C# モードのいずれかを使用して作成できます。
| Execution model | Description |
|---|---|
| 分離ワーカー モデル | 関数コードは、別の .NET ワーカー プロセスで実行されます。 .NET と .NET Framework のサポートされているバージョンで使います。 詳細については、 分離ワーカー モデルで C# Azure Functions を実行するためのガイドを参照してください。 |
| In-process model | 関数コードは、Functions ホスト プロセスと同じプロセスで実行されます。 .NET の長期サポート (LTS) バージョンのみをサポートします。 詳細については、「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 シークレット入力バインドは、app オブジェクトによって登録される Dapr 呼び出しトリガーとペアになっています。
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