rajanadar / VaultSharp

A comprehensive cross-platform .NET Library for HashiCorp's Vault, a secret management tool
http://rajanadar.github.io/VaultSharp
Apache License 2.0
493 stars 134 forks source link

VaultSharp.V1.SecretsEngines.Database: JsonException when deserializing a numeric rotation_period #312

Closed RatserX closed 1 year ago

RatserX commented 1 year ago

Describe the bug According to the Hashicorp documentation, the rotation_period can be stored as a string or an int. However, System.Text.Json (contrary to Newtonsoft.Json) does not accept non-string values when deserializing to properties of type string (in this case, VaultSharp.V1.SecretsEngines.Database.RotationPeriod).

Since our rotation_period is stored as a number, we're being hit with a JsonException whenever we try to obtain our credentials using GetStaticCredentialsAsync on the latest version of the library.

Database Secrets Engine (rotation_period): https://developer.hashicorp.com/vault/api-docs/secret/databases#rotation_period System.Text.Json migration (non-string values): https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/migrate-from-newtonsoft?pivots=dotnet-7-0#non-string-values-for-string-properties

VaultSharp Version 1.13.0

Exception Details/Stack Trace/Error Message System.Text.Json.JsonException: 'The JSON value could not be converted to System.String. Path: $.rotation_period | LineNumber: 0 | BytePositionInLine: 255.' InvalidOperationException: Cannot get the value of a token type 'Number' as a string.

Any additional info I'm thinking a solution would be to create a custom converter that handles converting non-string values to string (similar to how Newtonsoft.Json does it) and then register the JsonConverter on the RotationPeriod property. Idea taken from: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to?pivots=dotnet-7-0#registration-sample---jsonconverter-on-a-property

rajanadar commented 1 year ago

hi @RatserX ,

i published the fix as a beta package: https://www.nuget.org/packages/VaultSharp/1.13.0.1-beta1 Can you try and let me know if any issues? If none, I'll publish the proper version

RatserX commented 1 year ago

Hi @rajanadar , After doing some tests in our Dev environment I can confirm the beta package fixes the main issue. So far, I haven't seen any other issues happening so I think this is good to go.

rajanadar commented 1 year ago

Very cool. I'll publish it out in a day.

rajanadar commented 1 year ago

@RatserX , official version is published: https://www.nuget.org/packages/VaultSharp/1.13.0.1

RatserX commented 1 year ago

Thank you so much for the help!