Skip to content

Commit

Permalink
fixes #133
Browse files Browse the repository at this point in the history
give role to cert auth
  • Loading branch information
rajanadar committed Apr 28, 2020
1 parent 0bb2b49 commit e4f5736
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.4.0001 (April 27, 2020)

**IMPROVEMENTS:**

* [GH-133] Add support for the optional ```CertificateRoleName``` while doing Cert based Auth.

## 1.4.0 (April 25, 2020)

**FEATURES:**
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ IVaultClient vaultClient = new VaultClient(vaultClientSettings);
var certificate = new X509Certificate2(your-p12-bytes, your-pass);

IAuthMethodInfo authMethod = new CertAuthMethodInfo(certificate);

// Optionally, you can also provide a Certificate Role Name during Auth.
// IAuthMethodInfo authMethod = new CertAuthMethodInfo(certificate, certificateRoleName);
var vaultClientSettings = new VaultClientSettings("https://MY_VAULT_SERVER:8200", authMethod);

IVaultClient vaultClient = new VaultClient(vaultClientSettings);
Expand Down
12 changes: 10 additions & 2 deletions src/VaultSharp/V1/AuthMethods/Cert/CertAuthMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ public class CertAuthMethodInfo : AbstractAuthMethodInfo
/// </value>
public X509Certificate2 ClientCertificate { get; }

/// <summary>
/// Optionally, you may specify a single certificate role to authenticate against.
/// </summary>
public string RoleName { get; }

/// <summary>
/// Initializes a new instance of the <see cref="CertAuthMethodInfo" /> class.
/// </summary>
/// <param name="clientCertificate">The client certificate.</param>
public CertAuthMethodInfo(X509Certificate2 clientCertificate) : this(AuthMethodType.Cert.Type, clientCertificate)
/// <param name="roleName">A single certificate role to authenticate against.</param>
public CertAuthMethodInfo(X509Certificate2 clientCertificate, string roleName = null) : this(AuthMethodType.Cert.Type, clientCertificate, roleName)
{
}

Expand All @@ -48,7 +54,8 @@ public CertAuthMethodInfo(X509Certificate2 clientCertificate) : this(AuthMethodT
/// </summary>
/// <param name="mountPoint">The mount point.</param>
/// <param name="clientCertificate">The client certificate.</param>
public CertAuthMethodInfo(string mountPoint, X509Certificate2 clientCertificate)
/// <param name="roleName">A single certificate role to authenticate against.</param>
public CertAuthMethodInfo(string mountPoint, X509Certificate2 clientCertificate, string roleName = null)
{
Checker.NotNull(mountPoint, "mountPoint");
Checker.NotNull(clientCertificate, "clientCertificate");
Expand All @@ -60,6 +67,7 @@ public CertAuthMethodInfo(string mountPoint, X509Certificate2 clientCertificate)

MountPoint = mountPoint;
ClientCertificate = clientCertificate;
RoleName = roleName;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -22,8 +21,14 @@ public CertAuthMethodLoginProvider(CertAuthMethodInfo certAuthMethodInfo, Polyma

public async Task<string> GetVaultTokenAsync()
{
// make an unauthenticated call to Vault, since this is the call to get the token. It shouldn't need a token.
var response = await _polymath.MakeVaultApiRequest<Secret<JToken>>(LoginResourcePath, HttpMethod.Post, unauthenticated: true).ConfigureAwait(_polymath.VaultClientSettings.ContinueAsyncTasksOnCapturedContext);
// Make an unauthenticated call to Vault, since this is the call to get the token.
// It shouldn't need a token.
var response = string.IsNullOrWhiteSpace(_certAuthMethodInfo.RoleName) ?

(await _polymath.MakeVaultApiRequest<Secret<JToken>>(LoginResourcePath, HttpMethod.Post, unauthenticated: true).ConfigureAwait(_polymath.VaultClientSettings.ContinueAsyncTasksOnCapturedContext)) :

(await _polymath.MakeVaultApiRequest<Secret<JToken>>(LoginResourcePath, HttpMethod.Post, new { name = _certAuthMethodInfo.RoleName }, unauthenticated: true).ConfigureAwait(_polymath.VaultClientSettings.ContinueAsyncTasksOnCapturedContext));

_certAuthMethodInfo.ReturnedLoginAuthInfo = response?.AuthInfo;

if (response?.AuthInfo != null && !string.IsNullOrWhiteSpace(response.AuthInfo.ClientToken))
Expand Down
6 changes: 3 additions & 3 deletions src/VaultSharp/VaultSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>VaultSharp.snk</AssemblyOriginatorKeyFile>
<Version>1.4.0</Version>
<Version>1.4.0001</Version>
<Authors>Raja Nadar</Authors>
<Copyright>Copyright © 2020 Raja Nadar. All rights reserved.</Copyright>
<PackageProjectUrl>https://github.com/rajanadar/VaultSharp</PackageProjectUrl>
Expand All @@ -20,8 +20,8 @@
* This library is built with .NET Standard 1.3 &amp; .NET 4.5 and hence is cross-platform across .NET Core 1.0, .NET 4.5 and more, Xamarin iOS, Android, Mac, UWP etc.</Description>
<RepositoryType>Github</RepositoryType>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.0</FileVersion>
<AssemblyVersion>1.4.0001.0</AssemblyVersion>
<FileVersion>1.4.0002.0</FileVersion>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageIcon>icon.png</PackageIcon>
</PropertyGroup>
Expand Down

0 comments on commit e4f5736

Please sign in to comment.