0
votes

I want to get secret from a KeyVault, but the KeyVaultClient.AuthenticationCallback not called.

I created unittest that is MSTest, I have this code:

 [TestInitialize]
    public void SetupTest()
    {
        CreateKeyvalut();
    }

    public async void CreateKeyvalut()
    {
        try
        {
            IKeyVaultClient keyVaultClient = GetKeyVaultClient(_clientId, _certificateThumbprint);

            var password = await GetSecretValueAsync(_secretIdentifier, keyVaultClient);
        }
        catch (Exception ex)
        {
            string errorMessage = $"[KeyVault] Error occurred when trying to connect Key Vault. Exception: {ex}";
            Trace.TraceWarning(errorMessage);

            throw;
        }
    }
    public static IKeyVaultClient GetKeyVaultClient(string clientId, string certificateThumbprint) {
        return new KeyVaultClient(AuthenticationCallback(clientId, certificateThumbprint));
    }

    public static KeyVaultClient.AuthenticationCallback AuthenticationCallback(string clientId, string certificateThumbprint)
    {
        return async (authority, resource, scope) =>
        {
            X509Certificate2 certificate = GetCertificate(certificateThumbprint);
            var context = new AuthenticationContext(authority);
            var clientCredentials = new ClientAssertionCertificate(clientId, certificate);
            AuthenticationResult result = await context.AcquireTokenAsync(resource, clientCredentials).ConfigureAwait(false);
            return result.AccessToken;
        };
    }

    public static async Task<string> GetSecretValueAsync(string secretIdentifier, IKeyVaultClient keyVaultClient)
    {
        var secretTask = await keyVaultClient.GetSecretAsync(secretIdentifier);
        return secretTask.Value;
    }

But it's never enter to the code inside the KeyVaultClient.AuthenticationCallback AuthenticationCallback.

What is the problem? The exception I am getting is:

The thread 0x492c has exited with code 0 (0x0). testhost.exe Warning: 0 : [KeyVault] Error occurred when trying to connect Key Vault. Exception: System.Threading.ThreadAbortException: Thread was being aborted. at Microsoft.Rest.RetryDelegatingHandler.d__15.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.KeyVault.KeyVaultCredential.d__13.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.KeyVault.KeyVaultClient.d__65.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.KeyVault.KeyVaultClientExtensions.d__13.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at <GetSecretValueAsync>d__24.MoveNext() in --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at d__18.MoveNext() in C:\MyProject\src\test\testValidation.cs:line 41

1
Can you try using public async Task SetupTest(), await the call to CreateKeyvalut() and also change the definition of the CreateKeyvalut function to public async Task CreateKeyvalut() ? Too long to explain here but your can refer to this post to understand async task and async void: stackoverflow.com/questions/12144077/…Thomas

1 Answers

1
votes

I have used exact same code and it is stepping in the AuthenticationCallBack function. Please check the screenshot.

enter image description here

Seems like Call back function throwing error because of the credential issue.

**at Microsoft.Azure.KeyVault.KeyVaultCredential.d__13.MoveNext()**

Please check the credential/certificate and try to debug your call back function.

Hope it helps.