Skip to content
This repository was archived by the owner on Nov 29, 2023. It is now read-only.

Define Request From Local Cerificate #18

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Vault.NET is an .NET API client for the interacting with [Vault](https://www.vau

```csharp
using Vault;

var vaultClient = new VaultClient();
vaultClient.Token = "XXXXXX";
```
Expand All @@ -24,33 +23,28 @@ var data = new Dictionary<string, string>
{"zip", "zap"}
};
await vaultClient.Secret.Write("secret/foo", data);

var secret = await vaultClient.Secret.Read<Dictionary<string, string>>("secret/foo");
Console.WriteLine(secret.Data["zip"]);

// zap
```

### PKI

```csharp
using Vault.Models.Secret.Pki;

var testRole = new RolesRequest
{
AllowAnyDomain = true,
EnforceHostnames = false,
MaxTtl = "1h"
};
await vaultClient.Secret.Write("pki/roles/test", testRole);

var certRequest = new IssueRequest
{
CommonName = "Test Cert"
};
var cert = await vaultClient.Secret.Write<IssueRequest, IssueResponse>("pki/issue/test", certRequest);
Console.WriteLine(secret.Data.Certificate);

// -----BEGIN CERTIFICATE-----
// MII...
```
Expand All @@ -59,9 +53,7 @@ Console.WriteLine(secret.Data.Certificate);

```csharp
using Vault.Models.Auth.UserPass;

await vaultClient.Sys.EnableAuth("userpass", "userpass", "Userpass Mount");

var usersRequest = new UsersRequest
{
Password = "password",
Expand All @@ -70,16 +62,13 @@ var usersRequest = new UsersRequest
MaxTtl = "2h"
};
await vaultClient.Auth.Write("userpass/users/username", usersRequest);

var loginRequest = new LoginRequest
{
Password = "password"
};
var loginResponse = await vaultClient.Auth.Write<LoginRequest, NoData>("userpass/login/username", loginRequest);

// Set client token to authenticated token
vaultClient.Token = loginResponse.Auth.ClientToken;

// Proceed with authenticated requests
```

Expand Down
1 change: 1 addition & 0 deletions src/Vault/Vault.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System.Web" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.WebRequest" />
</ItemGroup>

</Project>
47 changes: 46 additions & 1 deletion src/Vault/VaultHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -11,7 +14,49 @@ namespace Vault
{
public class VaultHttpClient : IVaultHttpClient
{
private static readonly HttpClient HttpClient = new HttpClient();
private static HttpClient HttpClientInitialization()
{
HttpClient httpClient = null;

#if NET45
if (!string.IsNullOrEmpty(Vault.VaultOptions.Default.CertPath))
{
WebRequestHandler requestHandler = new WebRequestHandler();
requestHandler.ClientCertificateOptions = ClientCertificateOption.Manual;
requestHandler.ClientCertificates.Add(new X509Certificate2(Vault.VaultOptions.Default.CertPath));
httpClient = new HttpClient(requestHandler);
}
else
httpClient = new HttpClient();
#else
if (!string.IsNullOrEmpty(Vault.VaultOptions.Default.CertPath))
{
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (request, cert, chain, errors) =>
{
const SslPolicyErrors unforgivableErrors =
SslPolicyErrors.RemoteCertificateNotAvailable |
SslPolicyErrors.RemoteCertificateNameMismatch;

if ((errors & unforgivableErrors) != 0)
{
return false;
}

X509Certificate2 remoteRoot = chain.ChainElements[chain.ChainElements.Count - 1].Certificate;
return new X509Certificate2(Vault.VaultOptions.Default.CertPath).RawData.SequenceEqual(remoteRoot.RawData);
};
httpClient = new HttpClient(handler);
}
else
{
httpClient = new HttpClient();
}
#endif
return httpClient;
}

private static readonly HttpClient HttpClient = HttpClientInitialization();

public VaultHttpClient()
{
Expand Down
1 change: 1 addition & 0 deletions src/Vault/VaultOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class VaultOptions : IOptions<VaultOptions>

public string Address { get; set; } = "https://localhost:8200";
public string Token { get; set; }
public string CertPath { get; set; }

VaultOptions IOptions<VaultOptions>.Value => this;
}
Expand Down