-
Notifications
You must be signed in to change notification settings - Fork 1
/
ManagedIdentityTests.cs
147 lines (136 loc) · 6.93 KB
/
ManagedIdentityTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Identity;
using Azure.Security.KeyVault.Certificates;
using Azure.Security.KeyVault.Keys;
using Azure.Security.KeyVault.Keys.Cryptography;
using Azure.Security.KeyVault.Secrets;
using NUnit.Framework;
using src;
namespace test
{
[TestFixture]
public class ManagedIdentityTests
{
[Test]
public void TestEncryptAndDecryptShouldCancelOutEachOther()
{
// Set ENV vars to configure the token provider endpoint of the managed identity credential
Environment.SetEnvironmentVariable("IDENTITY_ENDPOINT", "http://localhost:8080/metadata/identity/oauth2/token");
Environment.SetEnvironmentVariable("IDENTITY_HEADER", "header");
//given
const string secretMessage = "a secret message";
TokenCredential credential = new DefaultAzureCredential(); // Will use Assumed Identity container
var options = new KeyClientOptions(KeyClientOptions.ServiceVersion.V7_3)
{
DisableChallengeResourceVerification = true
};
var keyClient = new KeyClient(new Uri("https://localhost:8443/"), credential, GetClientOptions(options));
const string keyName = "rsa-key";
var createRsaKeyOptions = new CreateRsaKeyOptions(keyName)
{
KeySize = 2048,
KeyOperations = { KeyOperation.Encrypt, KeyOperation.Decrypt, KeyOperation.WrapKey, KeyOperation.UnwrapKey }
};
keyClient.CreateKey(keyName, KeyType.Rsa, createRsaKeyOptions);
var cryptographyClientOptions = new CryptographyClientOptions(CryptographyClientOptions.ServiceVersion.V7_3);
var underTest = new AzureKeyVaultKeyRepository(keyClient, credential, keyName, GetClientOptions(cryptographyClientOptions));
//when
var encrypted = underTest.Encrypt(secretMessage);
var decrypted = underTest.Decrypt(encrypted);
//then
Assert.AreEqual(secretMessage, decrypted);
}
[Test]
public void TestGetConnectionDetailsShouldReturnValidData()
{
// Set ENV vars to configure the token provider endpoint of the managed identity credential
Environment.SetEnvironmentVariable("IDENTITY_ENDPOINT", "http://localhost:8080/metadata/identity/oauth2/token");
Environment.SetEnvironmentVariable("IDENTITY_HEADER", "header");
//given
const string serverNameSecret = "ServerName";
const string userNameSecret = "UserName";
const string passwordSecret = "Password";
const string serverName = "ServerName\\InstanceName";
const string userName = "admin";
const string password = "secret123";
TokenCredential credential = new DefaultAzureCredential(); // Will use Assumed Identity container
var options = new SecretClientOptions(SecretClientOptions.ServiceVersion.V7_3)
{
DisableChallengeResourceVerification = true
};
var secretClient = new SecretClient(new Uri("https://localhost:8443/"), credential, GetClientOptions(options));
secretClient.SetSecret(serverNameSecret, serverName);
secretClient.SetSecret(userNameSecret, userName);
secretClient.SetSecret(passwordSecret, password);
var underTest = new AzureKeyVaultSecretRepository(secretClient,
serverNameSecret, userNameSecret, passwordSecret);
//when
var actualServerName = underTest.GetServerName();
var actualUserName = underTest.GetUserName();
var actualPassword = underTest.GetPassword();
//then
Assert.AreEqual(serverName, actualServerName);
Assert.AreEqual(userName, actualUserName);
Assert.AreEqual(password, actualPassword);
}
[Test]
public void TestGetCertificateDetailsShouldReturnValidData()
{
// Set ENV vars to configure the token provider endpoint of the managed identity credential
Environment.SetEnvironmentVariable("IDENTITY_ENDPOINT", "http://localhost:8080/metadata/identity/oauth2/token");
Environment.SetEnvironmentVariable("IDENTITY_HEADER", "header");
//given
const string certificateName = "certificate";
const string subject = "CN=example.com";
TokenCredential credential = new DefaultAzureCredential(); // Will use Assumed Identity container
var secretClientOptions = new SecretClientOptions(SecretClientOptions.ServiceVersion.V7_3)
{
DisableChallengeResourceVerification = true
};
var certificateClientOptions = new CertificateClientOptions(CertificateClientOptions.ServiceVersion.V7_3)
{
DisableChallengeResourceVerification = true
};
var secretClient = new SecretClient(new Uri("https://localhost:8443/"), credential, GetClientOptions(secretClientOptions));
var certificateClient = new CertificateClient(new Uri("https://localhost:8443/"), credential, GetClientOptions(certificateClientOptions));
var certificatePolicy = new CertificatePolicy("Self", subject)
{
KeyType = CertificateKeyType.Ec,
KeyCurveName = CertificateKeyCurveName.P256,
Exportable = true,
ContentType = CertificateContentType.Pkcs12,
ValidityInMonths = 12
};
certificateClient.StartCreateCertificate(certificateName, certificatePolicy).WaitForCompletion();
var underTest = new AzureKeyVaultCertificateRepository(secretClient, certificateName);
//when
var actualCertificate = underTest.GetCertificate();
var actualPrivateKey = underTest.GetPrivateKey();
//then
Assert.AreEqual(subject, actualCertificate.Subject);
Assert.NotNull(actualPrivateKey);
}
private T GetClientOptions<T>(T options) where T : ClientOptions
{
DisableSslValidationOnClientOptions(options);
return options;
}
/// <summary>
/// Disables server certification callback.
/// <br/>
/// <b>WARNING: Do not use in production environments.</b>
/// </summary>
/// <param name="options"></param>
private void DisableSslValidationOnClientOptions(ClientOptions options)
{
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
options.Transport = new HttpClientTransport(clientHandler);
}
}
}