-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCredentialUtils.cs
More file actions
49 lines (46 loc) · 1.95 KB
/
CredentialUtils.cs
File metadata and controls
49 lines (46 loc) · 1.95 KB
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
using System.Security.Cryptography.X509Certificates;
using Couchbase.AnalyticsClient.HTTP;
using Couchbase.Grpc.Protocol.Columnar;
namespace Couchbase.Analytics.Performer.Internal.Utils;
internal static class CertificateUtils
{
internal static X509Certificate2 CreateCertificateFromStrings(string certPem, string keyPem)
{
var certPath = Path.GetTempFileName();
var keyPath = Path.GetTempFileName();
var pfxPath = Path.GetTempFileName();
try
{
File.WriteAllText(certPath, certPem);
File.WriteAllText(keyPath, keyPem);
using var pemCert = X509Certificate2.CreateFromPemFile(certPath, keyPath);
var pfxBytes = pemCert.Export(X509ContentType.Pfx);
File.WriteAllBytes(pfxPath, pfxBytes);
return X509CertificateLoader.LoadPkcs12FromFile(pfxPath, password: null);
}
finally
{
File.Delete(certPath);
File.Delete(keyPath);
File.Delete(pfxPath);
}
}
internal static ICredential ToCore(this ClusterNewInstanceRequest.Types.Credential protoCredential)
{
return protoCredential.TypeCase switch
{
ClusterNewInstanceRequest.Types.Credential.TypeOneofCase.UsernameAndPassword =>
Credential.Create(
protoCredential.UsernameAndPassword.Username,
protoCredential.UsernameAndPassword.Password),
ClusterNewInstanceRequest.Types.Credential.TypeOneofCase.JwtAuth =>
JwtCredential.Create(protoCredential.JwtAuth.Jwt),
ClusterNewInstanceRequest.Types.Credential.TypeOneofCase.CertificateAuth =>
CertificateCredential.Create(
CreateCertificateFromStrings(
protoCredential.CertificateAuth.Cert,
protoCredential.CertificateAuth.Key)),
_ => throw new ArgumentOutOfRangeException()
};
}
}