Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
L'associazione di input del segreto Dapr consente di leggere i dati dei segreti come input durante l'esecuzione della funzione.
Per informazioni sull'installazione e sulla configurazione dell'estensione Dapr, vedere panoramica dell'estensione Dapr.
Example
È possibile creare una funzione C# usando una delle modalità C# seguenti:
| Execution model | Description |
|---|---|
| Modello di lavoro isolato | Il codice della funzione viene eseguito in un processo di lavoro .NET separato. Usare con le versioni supportate di .NET e .NET Framework. Per altre informazioni, vedere Guida per l'esecuzione di Funzioni di Azure C# nel modello di lavoro isolato. |
| In-process model | Il codice della funzione viene eseguito nello stesso processo del processo host di Funzioni. Supporta solo versioni LTS (Long Term Support) di .NET. Per altre informazioni, vedere Sviluppare funzioni della libreria di classi C# usando Funzioni di Azure. |
[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.");
}
Nell'esempio seguente viene creata una funzione "RetrieveSecret" usando l'associazione DaprSecretInput con 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)
Nell'esempio seguente, l'associazione di input del segreto Dapr viene associata a un trigger Dapr invoke, registrato dall'oggetto 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"
L'esempio seguente illustra un'associazione di input dapr Secret, che usa il modello di programmazione Python v2. Per usare l'associazione daprSecret insieme a daprServiceInvocationTrigger nel codice dell'app per le funzioni Python:
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 | Nome dell'archivio segreto per ottenere il segreto. |
| Key | Chiave che identifica il nome del segreto da ottenere. |
| Metadata | Optional. Matrice di proprietà dei metadati nel formato "key1=value1&key2=value2". |
Annotations
L'annotazione DaprSecretInput consente di avere accesso alla funzione a un segreto.
| Element | Description |
|---|---|
| secretStoreName | Nome dell'archivio segreto Dapr. |
| key | Valore della chiave privata. |
| metadata | Optional. Valori dei metadati. |
Configuration
Nella tabella seguente vengono illustrate le proprietà di configurazione dell'associazione impostate nel codice.
| Property | Description |
|---|---|
| key | Valore della chiave privata. |
| secretStoreName | Name of the secret store as defined in the local-secret-store.yaml component file. |
| metadata | Spazio dei nomi dei metadati. |
Nella tabella seguente sono illustrate le proprietà di configurazione dell'associazione impostate nel file function.json.
| function.json property | Description |
|---|---|
| key | Valore della chiave privata. |
| secretStoreName | Name of the secret store as defined in the local-secret-store.yaml component file. |
| metadata | Spazio dei nomi dei metadati. |
See the Example section for complete examples.
Usage
Per usare l'associazione di input del segreto Dapr, iniziare configurando un componente dell'archivio segreti Dapr. Per altre informazioni sul componente da usare e su come configurarlo, vedere la documentazione ufficiale di Dapr.
To use the daprSecret in Python v2, set up your project with the correct dependencies.
Nel file
requirements.textaggiungere la riga seguente:azure-functions==1.18.0b3Nel terminale installare la libreria Python.
pip install -r .\requirements.txtModificare il file
local.setting.jsoncon la configurazione seguente:"PYTHON_ISOLATE_WORKER_DEPENDENCIES":1