Skip to content

Commit

Permalink
initial APIs for MSSQL
Browse files Browse the repository at this point in the history
1. todo: check documentation & format
2. todo: add support for list roles..
3. todo: verify with local MS SQL db
  • Loading branch information
rajanadar committed May 7, 2016
1 parent 78f13f6 commit 33b66e9
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 0 deletions.
@@ -0,0 +1,52 @@
using Newtonsoft.Json;

namespace VaultSharp.Backends.Secret.Models.MicrosoftSql
{
/// <summary>
/// Represents the Microsoft Sql connection information.
/// </summary>
public class MicrosoftSqlConnectionInfo
{
/// <summary>
/// <para>[required]</para>
/// Gets or sets the connection DSN used to communicate with Sql Server.
/// </summary>
/// <value>
/// The MSSQL DSN
/// </value>
[JsonProperty("connection_string ")]
public string ConnectionString { get; set; }

/// <summary>
/// <para>[optional]</para>
/// Gets or sets the maximum number of open connections to the database.
/// Defaults to 2.
/// </summary>
/// <value>
/// The maximum open connections.
/// </value>
[JsonProperty("max_open_connections")]
public int MaximumOpenConnections { get; set; }

/// <summary>
/// <para>[optional]</para>
/// Gets or sets a flag which when set verifies the <see cref="ConnectionString"/> by
/// actually connecting to the database.
/// Defaults to true.
/// </summary>
/// <value>
/// A flag to verify the connection.
/// </value>
[JsonProperty("verify_connection")]
public bool VerifyConnection { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftSqlConnectionInfo" /> class.
/// </summary>
public MicrosoftSqlConnectionInfo()
{
MaximumOpenConnections = 2;
VerifyConnection = true;
}
}
}
@@ -0,0 +1,22 @@
using Newtonsoft.Json;

namespace VaultSharp.Backends.Secret.Models.MicrosoftSql
{
/// <summary>
/// Represents the Microsoft Sql role definition
/// </summary>
public class MicrosoftSqlRoleDefinition
{
/// <summary>
/// <para>[required]</para>
/// Gets or sets the SQL statements executed to create and configure the role.
/// Must be semi-colon separated.
/// The '{{name}}' and '{{password}}' values will be substituted.
/// </summary>
/// <value>
/// The SQL.
/// </value>
[JsonProperty("sql")]
public string Sql { get; set; }
}
}
Expand Up @@ -14,6 +14,8 @@ internal class SecretBackendDefaultMountPoints

public const string Generic = "secret";

public const string MicrosoftSql = "mssql";

public const string MySql = "mysql";

public const string PKI = "pki";
Expand Down
19 changes: 19 additions & 0 deletions src/VaultSharp/Backends/Secret/Models/SecretBackendType.cs
Expand Up @@ -42,6 +42,11 @@ public class SecretBackendType : IEquatable<SecretBackendType>
/// </summary>
private static readonly SecretBackendType GenericType = new SecretBackendType("generic");

/// <summary>
/// Microsoft SQL type
/// </summary>
private static readonly SecretBackendType MicrosoftSqlType = new SecretBackendType(SecretBackendDefaultMountPoints.MicrosoftSql);

/// <summary>
/// My SQL type
/// </summary>
Expand Down Expand Up @@ -156,6 +161,20 @@ public static SecretBackendType Generic
}
}

/// <summary>
/// Gets the Microsoft Sql type.
/// </summary>
/// <value>
/// Microsoft Sql.
/// </value>
public static SecretBackendType MicrosoftSql
{
get
{
return MicrosoftSqlType;
}
}

/// <summary>
/// Gets the MySql type.
/// </summary>
Expand Down
83 changes: 83 additions & 0 deletions src/VaultSharp/IVaultClient.cs
Expand Up @@ -8,6 +8,7 @@
using VaultSharp.Backends.Secret.Models.AWS;
using VaultSharp.Backends.Secret.Models.Cassandra;
using VaultSharp.Backends.Secret.Models.Consul;
using VaultSharp.Backends.Secret.Models.MicrosoftSql;
using VaultSharp.Backends.Secret.Models.MySql;
using VaultSharp.Backends.Secret.Models.PKI;
using VaultSharp.Backends.Secret.Models.PostgreSql;
Expand Down Expand Up @@ -900,6 +901,88 @@ public interface IVaultClient
/// </returns>
Task GenericDeleteSecretAsync(string locationPath, string genericBackendMountPoint = SecretBackendDefaultMountPoints.Generic);

/// <summary>
/// Configures the connection information used to communicate with Microsoft Sql.
/// This API is a root protected call.
/// </summary>
/// <param name="microsoftSqlConnectionInfo"><para>[required]</para>
/// The Microsoft Sql connection information.</param>
/// <param name="microsoftSqlBackendMountPoint"><para>[optional]</para>
/// The mount point for the Microsoft Sql backend. Defaults to <see cref="SecretBackendType.MicrosoftSql" />
/// Provide a value only if you have customized the Microsoft Sql mount point.</param>
/// <returns>
/// The task.
/// </returns>
Task MicrosoftSqlConfigureConnectionAsync(MicrosoftSqlConnectionInfo microsoftSqlConnectionInfo, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql);

/// <summary>
/// Configures the lease settings for generated credentials.
/// This API is a root protected call.
/// </summary>
/// <param name="credentialLeaseSettings"><para>[required]</para>
/// The credential lease settings.</param>
/// <param name="microsoftSqlBackendMountPoint"><para>[optional]</para>
/// The mount point for the MicrosoftSql backend. Defaults to <see cref="SecretBackendType.MicrosoftSql" />
/// Provide a value only if you have customized the MicrosoftSql mount point.</param>
/// <returns>
/// The task.
/// </returns>
Task MicrosoftSqlConfigureCredentialLeaseSettingsAsync(CredentialLeaseSettings credentialLeaseSettings, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql);

/// <summary>
/// Creates or updates a named MicrosoftSql role.
/// </summary>
/// <param name="microsoftSqlRoleName"><para>[required]</para>
/// Name of the MicrosoftSql role.</param>
/// <param name="microsoftSqlRoleDefinition"><para>[required]</para>
/// The MicrosoftSql role definition with the creation, rollback query and lease information.</param>
/// <param name="microsoftSqlBackendMountPoint"><para>[optional]</para>
/// The mount point for the MicrosoftSql backend. Defaults to <see cref="SecretBackendType.MicrosoftSql" />
/// Provide a value only if you have customized the MicrosoftSql mount point.</param>
/// <returns>
/// The task.
/// </returns>
Task MicrosoftSqlWriteNamedRoleAsync(string microsoftSqlRoleName, MicrosoftSqlRoleDefinition microsoftSqlRoleDefinition, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql);

/// <summary>
/// Queries a named MicrosoftSql role definition
/// </summary>
/// <param name="microsoftSqlRoleName"><para>[required]</para>
/// Name of the MicrosoftSql role.</param>
/// <param name="microsoftSqlBackendMountPoint"><para>[optional]</para>
/// The mount point for the MicrosoftSql backend. Defaults to <see cref="SecretBackendType.MicrosoftSql" />
/// Provide a value only if you have customized the MicrosoftSql mount point.</param>
/// <returns>
/// The secret with the MicrosoftSql role definition with the creation, rollback query and lease information.
/// </returns>
Task<Secret<MicrosoftSqlRoleDefinition>> MicrosoftSqlReadNamedRoleAsync(string microsoftSqlRoleName, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql);

/// <summary>
/// Deletes a named MicrosoftSql role definition
/// </summary>
/// <param name="microsoftSqlRoleName"><para>[required]</para>
/// Name of the MicrosoftSql role.</param>
/// <param name="microsoftSqlBackendMountPoint"><para>[optional]</para>
/// The mount point for the MicrosoftSql backend. Defaults to <see cref="SecretBackendType.MicrosoftSql" />
/// Provide a value only if you have customized the MicrosoftSql mount point.</param>
/// <returns>
/// The task.
/// </returns>
Task MicrosoftSqlDeleteNamedRoleAsync(string microsoftSqlRoleName, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql);

/// <summary>
/// Generates a new set of dynamic credentials based on the named role.
/// </summary>
/// <param name="microsoftSqlRoleName"><para>[required]</para>
/// Name of the MicrosoftSql role.</param>
/// <param name="microsoftSqlBackendMountPoint"><para>[optional]</para>
/// The mount point for the MicrosoftSql backend. Defaults to <see cref="SecretBackendType.MicrosoftSql" />
/// Provide a value only if you have customized the MicrosoftSql mount point.</param>
/// <returns>
/// The secret with the <see cref="UsernamePasswordCredentials" /> as the data.
/// </returns>
Task<Secret<UsernamePasswordCredentials>> MicrosoftSqlGenerateDynamicCredentialsAsync(string microsoftSqlRoleName, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql);

/// <summary>
/// Configures the connection information used to communicate with MySql.
/// This API is a root protected call.
Expand Down
51 changes: 51 additions & 0 deletions src/VaultSharp/VaultClient.cs
Expand Up @@ -14,6 +14,7 @@
using VaultSharp.Backends.Secret.Models.AWS;
using VaultSharp.Backends.Secret.Models.Cassandra;
using VaultSharp.Backends.Secret.Models.Consul;
using VaultSharp.Backends.Secret.Models.MicrosoftSql;
using VaultSharp.Backends.Secret.Models.MySql;
using VaultSharp.Backends.Secret.Models.PKI;
using VaultSharp.Backends.Secret.Models.PostgreSql;
Expand Down Expand Up @@ -737,6 +738,56 @@ public async Task GenericDeleteSecretAsync(string locationPath, string genericBa
await MakeVaultApiRequest(genericBackendMountPoint.Trim('/') + "/" + locationPath.Trim('/'), HttpMethod.Delete).ConfigureAwait(continueOnCapturedContext: _continueAsyncTasksOnCapturedContext);
}

public async Task MicrosoftSqlConfigureConnectionAsync(MicrosoftSqlConnectionInfo microsoftSqlConnectionInfo, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql)
{
Checker.NotNull(microsoftSqlConnectionInfo, "microsoftSqlConnectionInfo");
Checker.NotNull(microsoftSqlBackendMountPoint, "microsoftSqlBackendMountPoint");

await MakeVaultApiRequest(microsoftSqlBackendMountPoint.Trim('/') + "/config/connection", HttpMethod.Post, microsoftSqlConnectionInfo).ConfigureAwait(continueOnCapturedContext: _continueAsyncTasksOnCapturedContext);
}

public async Task MicrosoftSqlConfigureCredentialLeaseSettingsAsync(CredentialLeaseSettings credentialLeaseSettings, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql)
{
Checker.NotNull(microsoftSqlBackendMountPoint, "microsoftSqlBackendMountPoint");
Checker.NotNull(credentialLeaseSettings, "credentialLeaseSettings");

await MakeVaultApiRequest(microsoftSqlBackendMountPoint.Trim('/') + "/config/lease", HttpMethod.Post, credentialLeaseSettings).ConfigureAwait(continueOnCapturedContext: _continueAsyncTasksOnCapturedContext);
}

public async Task MicrosoftSqlWriteNamedRoleAsync(string microsoftSqlRoleName, MicrosoftSqlRoleDefinition microsoftSqlRoleDefinition, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql)
{
Checker.NotNull(microsoftSqlBackendMountPoint, "microsoftSqlBackendMountPoint");
Checker.NotNull(microsoftSqlRoleName, "microsoftSqlRoleName");

await MakeVaultApiRequest(microsoftSqlBackendMountPoint.Trim('/') + "/roles/" + microsoftSqlRoleName, HttpMethod.Post, microsoftSqlRoleDefinition).ConfigureAwait(continueOnCapturedContext: _continueAsyncTasksOnCapturedContext);
}

public async Task<Secret<MicrosoftSqlRoleDefinition>> MicrosoftSqlReadNamedRoleAsync(string microsoftSqlRoleName, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql)
{
Checker.NotNull(microsoftSqlBackendMountPoint, "microsoftSqlBackendMountPoint");
Checker.NotNull(microsoftSqlRoleName, "microsoftSqlRoleName");

var result = await MakeVaultApiRequest<Secret<MicrosoftSqlRoleDefinition>>(microsoftSqlBackendMountPoint.Trim('/') + "/roles/" + microsoftSqlRoleName, HttpMethod.Get).ConfigureAwait(continueOnCapturedContext: _continueAsyncTasksOnCapturedContext);
return result;
}

public async Task MicrosoftSqlDeleteNamedRoleAsync(string microsoftSqlRoleName, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql)
{
Checker.NotNull(microsoftSqlBackendMountPoint, "microsoftSqlBackendMountPoint");
Checker.NotNull(microsoftSqlRoleName, "microsoftSqlRoleName");

await MakeVaultApiRequest(microsoftSqlBackendMountPoint.Trim('/') + "/roles/" + microsoftSqlRoleName, HttpMethod.Delete).ConfigureAwait(continueOnCapturedContext: _continueAsyncTasksOnCapturedContext);
}

public async Task<Secret<UsernamePasswordCredentials>> MicrosoftSqlGenerateDynamicCredentialsAsync(string microsoftSqlRoleName, string microsoftSqlBackendMountPoint = SecretBackendDefaultMountPoints.MicrosoftSql)
{
Checker.NotNull(microsoftSqlBackendMountPoint, "microsoftSqlBackendMountPoint");
Checker.NotNull(microsoftSqlRoleName, "microsoftSqlRoleName");

var result = await MakeVaultApiRequest<Secret<UsernamePasswordCredentials>>(microsoftSqlBackendMountPoint.Trim('/') + "/creds/" + microsoftSqlRoleName, HttpMethod.Get).ConfigureAwait(continueOnCapturedContext: _continueAsyncTasksOnCapturedContext);
return result;
}

public async Task MySqlConfigureConnectionAsync(MySqlConnectionInfo mySqlConnectionInfo, string mySqlBackendMountPoint = SecretBackendDefaultMountPoints.MySql)
{
Checker.NotNull(mySqlBackendMountPoint, "mySqlBackendMountPoint");
Expand Down
2 changes: 2 additions & 0 deletions src/VaultSharp/VaultSharp.csproj
Expand Up @@ -63,6 +63,8 @@
<Compile Include="Backends\Secret\Models\Consul\ConsulRoleDefinition.cs" />
<Compile Include="Backends\Secret\Models\Consul\ConsulTokenType.cs" />
<Compile Include="Backends\Secret\Models\CredentialLeaseSettings.cs" />
<Compile Include="Backends\Secret\Models\MicrosoftSql\MicrosoftSqlConnectionInfo.cs" />
<Compile Include="Backends\Secret\Models\MicrosoftSql\MicrosoftSqlRoleDefinition.cs" />
<Compile Include="Backends\Secret\Models\MySql\MySqlRoleDefinition.cs" />
<Compile Include="Backends\Secret\Models\MySql\MySqlConnectionInfo.cs" />
<Compile Include="Backends\Secret\Models\PKI\CertificateEndpointOptions.cs" />
Expand Down

0 comments on commit 33b66e9

Please sign in to comment.