-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/VaultSharp/V1/SecretsEngines/Database/ConnectionConfigModelJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using VaultSharp.V1.SecretsEngines.Database.Models; | ||
using VaultSharp.V1.SecretsEngines.Database.Models.PostgreSQL; | ||
|
||
namespace VaultSharp.V1.SecretsEngines.Database | ||
{ | ||
/// <summary> | ||
/// Converts the <see cref="ConnectionConfigModel" /> object from JSON. | ||
/// </summary> | ||
internal class ConnectionConfigModelJsonConverter : JsonConverter<ConnectionConfigModel> | ||
{ | ||
public override void Write(Utf8JsonWriter writer, ConnectionConfigModel value, JsonSerializerOptions serializer) | ||
{ | ||
// VERY IMPORTANT | ||
// This will only work if the converter is registered programmatically and not via attributes | ||
// Via attributes, the code goes into a stack overflow exception. | ||
JsonSerializer.Serialize(writer, value); | ||
} | ||
|
||
public override ConnectionConfigModel Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) | ||
{ | ||
throw new JsonException(); | ||
} | ||
|
||
using (var jsonDocument = JsonDocument.ParseValue(ref reader)) | ||
{ | ||
if (!jsonDocument.RootElement.TryGetProperty("plugin_name", | ||
out var pluginNameProperty)) | ||
{ | ||
throw new JsonException(); | ||
} | ||
|
||
string pluginName = pluginNameProperty.GetString(); | ||
var jsonString = jsonDocument.RootElement.GetRawText(); | ||
|
||
if (pluginName.Equals("postgresql-database-plugin")) | ||
{ | ||
return JsonSerializer.Deserialize<PostgreSQLConnectionConfigModel>(jsonString); | ||
} | ||
|
||
return JsonSerializer.Deserialize<ConnectionConfigModel>(jsonString); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/VaultSharp/V1/SecretsEngines/Database/Models/ConnectionConfigModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters