locked
Azure data factory v2 custom activities. Get a connection string from a linkedService with Azure Key Vault RRS feed

  • Question

  • I a custom activity with a linkedService in ReferenceObjects property. I get the linkedService from linkedService.json but its connection string is from a KeyVault not from string value. How can i get the connectionString to open a SqlConnection from the custom activitiy.

    Example:

    My pipeline:

    {
        "name": "myPipeline",
        "properties": {
            "activities": [
                {
                    "name": "myCustomActivity",
                    "type": "Custom",
                    "policy": {
                        "timeout": "02:00:00",
                        "retry": 3,
                        "retryIntervalInSeconds": 30,
                        "secureOutput": false,
                        "secureInput": false
                    },
                    "typeProperties": {
                        "command": "test.exe",
                        "resourceLinkedService": {
                            "referenceName": "blobLS",
                            "type": "LinkedServiceReference"
                        },
                        "folderPath": "testContainer/Test",
                        "referenceObjects": {
                            "linkedServices": [
                                {
                                    "referenceName": "AzureSqlLinkedService",
                                    "type": "LinkedServiceReference"
                                }
                            ]
                        }
                    },
                    "linkedServiceName": {
                        "referenceName": "BatchLS",
                        "type": "LinkedServiceReference"
                    }
                }
            ]
        }
    }

    My reference linkedService:

    {
        "name": "AzureSqlLinkedService",
        "properties": {
            "type": "AzureSqlDatabase",
            "typeProperties": {
                "connectionString": {
                    "type": "AzureKeyVaultSecret",
                    "store": {
                        "referenceName": "keyVaultLinkedService",
                        "type": "LinkedServiceReference"
                    },
                    "secretName": "mySecretName"
                }
            }
        }
    }

    and my custom activity:

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Linq;
    using System.IO;
    using System.Data.SqlClient;

    namespace MyCustomActivities
    {
        class Program
        {
            static void Main(string[] args)
            {
                dynamic linkedServices = JsonConvert.DeserializeObject(File.ReadAllText("linkedServices.json"));
                string connectionString = ((dynamic)(((JArray)linkedServices).Where(
                                linkedService =>
                                ((dynamic)linkedService).name.ToString().Equals("odsStagingLS"))).First()).properties.typeProperties.connectionString;
                //Here i have the json code but in value it isn't the connectionString because it use Key Vault.
                //How i get the conectionString to to the follow
                SqlConnection connection = new SqlConnection(connectionString);
            }
        }
    }


    • Edited by jyeros Thursday, November 1, 2018 10:39 PM
    Thursday, November 1, 2018 10:39 PM

All replies

  • Hi,

    Consider the connection string is stored as the secret in the AKV,  you need to create another function/class to get the secret. You could download this Azure key vault c# sample to take a look. This thread also gives you some information. Thanks.

    • Proposed as answer by Jason_J (Azure) Wednesday, November 7, 2018 5:23 PM
    Friday, November 2, 2018 1:32 AM
  • Thanks. The information was so useful. But i couldn't run the custom activity because authentication problems. I resolve with this link get-a-secret-from-azure-key-vault. I try to use the library

    Microsoft.Azure.Services.AppAuthentication

    but it doesn't work for me.

    Thursday, November 8, 2018 2:55 PM