Skip to content
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
230 changes: 230 additions & 0 deletions src/VaultSharp/Core/AotRequestModels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace VaultSharp.Core
{
internal sealed class TokenRequest
{
[JsonPropertyName("token")] public string Token { get; set; }
}

internal sealed class TokenClientIdRequest
{
[JsonPropertyName("token")] public string Token { get; set; }
[JsonPropertyName("client_id")] public string ClientId { get; set; }
}

internal sealed class InputRequest
{
[JsonPropertyName("input")] public string Input { get; set; }
}

internal sealed class PathTokenRequest
{
[JsonPropertyName("path")] public string Path { get; set; }
[JsonPropertyName("token")] public string Token { get; set; }
}

internal sealed class PathAccessorRequest
{
[JsonPropertyName("path")] public string Path { get; set; }
[JsonPropertyName("accessor")] public string Accessor { get; set; }
}

internal sealed class PathRequest
{
[JsonPropertyName("path")] public string Path { get; set; }
}

internal sealed class OtpPgpKeyRequest
{
[JsonPropertyName("otp")] public string Otp { get; set; }
[JsonPropertyName("pgpKey")] public string PgpKey { get; set; }
}

internal sealed class KeyNonceRequest
{
[JsonPropertyName("key")] public string Key { get; set; }
[JsonPropertyName("nonce")] public string Nonce { get; set; }
}

internal sealed class ValueRequest
{
[JsonPropertyName("value")] public string Value { get; set; }
}

internal sealed class RekeyInitRequest
{
[JsonPropertyName("secret_shares")] public int SecretShares { get; set; }
[JsonPropertyName("secret_threshold")] public int SecretThreshold { get; set; }
[JsonPropertyName("pgp_keys")] public string[] PgpKeys { get; set; }
[JsonPropertyName("backup")] public bool Backup { get; set; }
}

internal sealed class KeyResetRequest
{
[JsonPropertyName("key")] public string Key { get; set; }
[JsonPropertyName("reset")] public bool Reset { get; set; }
}

internal sealed class MaxTtlRequest
{
[JsonPropertyName("max_ttl")] public string MaxTtl { get; set; }
}

internal sealed class AccessorRequest
{
[JsonPropertyName("accessor")] public string Accessor { get; set; }
}

internal sealed class CodeRequest
{
[JsonPropertyName("code")] public string Code { get; set; }
}

internal sealed class IpUsernameRequest
{
[JsonPropertyName("ip")] public string Ip { get; set; }
[JsonPropertyName("username")] public string Username { get; set; }
}

internal sealed class SerialNumberRequest
{
[JsonPropertyName("serial_number")] public string SerialNumber { get; set; }
}

internal sealed class CasRequest
{
[JsonPropertyName("cas")] public int Cas { get; set; }
}

internal sealed class VersionsRequest
{
[JsonPropertyName("versions")] public System.Collections.Generic.IList<int> Versions { get; set; }
}

internal sealed class FormatRequest
{
[JsonPropertyName("format")] public string Format { get; set; }
}

internal sealed class TtlRequest
{
[JsonPropertyName("ttl")] public long? Ttl { get; set; }
}

internal sealed class ServiceAccountNamesRequest
{
[JsonPropertyName("service_account_names")] public List<string> ServiceAccountNames { get; set; }
}

internal sealed class IncrementRequest
{
[JsonPropertyName("increment")] public string Increment { get; set; }
}

internal sealed class PoliciesRequest
{
[JsonPropertyName("policies")] public string Policies { get; set; }
}

internal sealed class PoliciesGroupsRequest
{
[JsonPropertyName("policies")] public string Policies { get; set; }
[JsonPropertyName("groups")] public string Groups { get; set; }
}

internal sealed class NameRequest
{
[JsonPropertyName("name")] public string Name { get; set; }
}

internal sealed class SecretIdRequest
{
[JsonPropertyName("secret_id")] public string SecretId { get; set; }
}

internal sealed class SecretIdAccessorRequest
{
[JsonPropertyName("secret_id_accessor")] public string SecretIdAccessor { get; set; }
}

internal sealed class SecretIdNumUsesRequest
{
[JsonPropertyName("secret_id_num_uses")] public long SecretIdNumUses { get; set; }
}

internal sealed class SecretIdTtlRequest
{
[JsonPropertyName("secret_id_ttl")] public long SecretIdTtl { get; set; }
}

internal sealed class TokenTtlRequest
{
[JsonPropertyName("token_ttl")] public long TokenTtl { get; set; }
}

internal sealed class TokenMaxTtlRequest
{
[JsonPropertyName("token_max_ttl")] public long TokenMaxTtl { get; set; }
}

internal sealed class BindSecretIdRequest
{
[JsonPropertyName("bind_secret_id")] public bool BindSecretId { get; set; }
}

internal sealed class SecretIdBoundCidrsRequest
{
[JsonPropertyName("secret_id_bound_cidrs")] public List<string> SecretIdBoundCidrs { get; set; }
}

internal sealed class TokenBoundCidrsRequest
{
[JsonPropertyName("token_bound_cidrs")] public List<string> TokenBoundCidrs { get; set; }
}

internal sealed class TokenPeriodRequest
{
[JsonPropertyName("token_period")] public long TokenPeriod { get; set; }
}

internal sealed class HmacRequest
{
[JsonPropertyName("hmac")] public bool Hmac { get; set; }
}

internal sealed class LeaseIdRequest
{
[JsonPropertyName("lease_id")] public string LeaseId { get; set; }
}

internal sealed class LeaseRenewRequest
{
[JsonPropertyName("lease_id")] public string LeaseId { get; set; }
[JsonPropertyName("increment")] public int Increment { get; set; }
}

internal sealed class LevelRequest
{
[JsonPropertyName("level")] public string Level { get; set; }
}

internal sealed class RulesRequest
{
[JsonPropertyName("rules")] public string Rules { get; set; }
}

internal sealed class PolicyTextRequest
{
[JsonPropertyName("policy")] public string Policy { get; set; }
}

internal sealed class CloudFoundryLoginRequest
{
[JsonPropertyName("role")] public string Role { get; set; }
[JsonPropertyName("cf_instance_cert")] public string CfInstanceCert { get; set; }
[JsonPropertyName("signing_time")] public string SigningTime { get; set; }
[JsonPropertyName("signature")] public string Signature { get; set; }
}
}
33 changes: 30 additions & 3 deletions src/VaultSharp/Core/Polymath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
using VaultSharp.V1.Commons;
using System.Text.Json.Nodes;
using System.Text.Json;
#if NET8_0_OR_GREATER
using System.Text.Json.Serialization.Metadata;
#endif

namespace VaultSharp.Core
{
Expand All @@ -28,15 +31,26 @@ internal class Polymath
private readonly HttpClient _httpClient;
private Lazy<Task<string>> _lazyVaultToken;
private readonly IAuthMethodLoginProvider _authMethodLoginProvider;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public HttpMethod ListHttpMethod { get; } = new HttpMethod("LIST");

public VaultClientSettings VaultClientSettings { get; }
internal JsonSerializerOptions JsonSerializerOptions => _jsonSerializerOptions;

public Polymath(VaultClientSettings vaultClientSettings)
{
VaultClientSettings = vaultClientSettings;

_jsonSerializerOptions = VaultClientSettings.JsonSerializerOptions ?? new JsonSerializerOptions();

#if NET8_0_OR_GREATER
if (!_jsonSerializerOptions.TypeInfoResolverChain.Contains(VaultSharpJsonContext.Default))
{
_jsonSerializerOptions.TypeInfoResolverChain.Insert(0, VaultSharpJsonContext.Default);
}
#endif

#if NET45
var handler = new WebRequestHandler();

Expand Down Expand Up @@ -200,7 +214,11 @@ protected async Task<TResponse> MakeRequestAsync<TResponse>(string resourcePath,
{
var requestUri = new Uri(_httpClient.BaseAddress, new Uri(resourcePath, UriKind.Relative));

string requestJson = requestData != null ? JsonSerializer.Serialize(requestData) : null;
#if NET8_0_OR_GREATER
string requestJson = requestData != null ? JsonSerializer.Serialize(requestData, _jsonSerializerOptions.GetTypeInfo(requestData.GetType())) : null;
#else
string requestJson = requestData != null ? JsonSerializer.Serialize(requestData, requestData.GetType(), _jsonSerializerOptions) : null;
#endif

var requestContent = requestJson != null
? new StringContent(requestJson, Encoding.UTF8)
Expand Down Expand Up @@ -237,7 +255,11 @@ protected async Task<TResponse> MakeRequestAsync<TResponse>(string resourcePath,
httpRequestMessage = new HttpRequestMessage(httpMethod, requestUri)
{
Content = requestData != null
? new StringContent(JsonSerializer.Serialize(requestData), Encoding.UTF8, "application/merge-patch+json")
#if NET8_0_OR_GREATER
? new StringContent(JsonSerializer.Serialize(requestData, _jsonSerializerOptions.GetTypeInfo(requestData.GetType())), Encoding.UTF8, "application/merge-patch+json")
#else
? new StringContent(JsonSerializer.Serialize(requestData, requestData.GetType(), _jsonSerializerOptions), Encoding.UTF8, "application/merge-patch+json")
#endif
: null
};

Expand Down Expand Up @@ -275,7 +297,12 @@ protected async Task<TResponse> MakeRequestAsync<TResponse>(string resourcePath,
{
if (!string.IsNullOrWhiteSpace(responseText))
{
var response = rawResponse ? (responseText as TResponse) : JsonSerializer.Deserialize<TResponse>(responseText);

#if NET8_0_OR_GREATER
var response = rawResponse ? (responseText as TResponse) : (TResponse)JsonSerializer.Deserialize(responseText, _jsonSerializerOptions.GetTypeInfo(typeof(TResponse)));
#else
var response = rawResponse ? (responseText as TResponse) : JsonSerializer.Deserialize<TResponse>(responseText, _jsonSerializerOptions);
#endif
return response;
}

Expand Down
24 changes: 18 additions & 6 deletions src/VaultSharp/Core/VaultApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace VaultSharp.Core
{
Expand Down Expand Up @@ -67,16 +66,29 @@ public VaultApiException(HttpStatusCode httpStatusCode, string message) : base(m

try
{
var structured = JsonSerializer.Deserialize<Dictionary<string, IEnumerable<string>>>(message);
using (var document = JsonDocument.Parse(message))
{
var root = document.RootElement;

if (structured.ContainsKey("errors"))
if (root.TryGetProperty("errors", out var errorsElement) && errorsElement.ValueKind == JsonValueKind.Array)
{
ApiErrors = structured["errors"];
var errors = new List<string>();
foreach (var item in errorsElement.EnumerateArray())
{
errors.Add(item.GetString());
}
ApiErrors = errors;
}

if (structured.ContainsKey("warnings"))
if (root.TryGetProperty("warnings", out var warningsElement) && warningsElement.ValueKind == JsonValueKind.Array)
{
ApiWarnings = structured["warnings"];
var warnings = new List<string>();
foreach (var item in warningsElement.EnumerateArray())
{
warnings.Add(item.GetString());
}
ApiWarnings = warnings;
}
}
}
catch
Expand Down
Loading