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

Base ConnectionConfigModel Not Deserializing #363

Closed treyhendon closed 1 month ago

treyhendon commented 1 month ago

https://github.com/rajanadar/VaultSharp/blob/4f906a91681a2f1d265065d1c43c1f726602afe4/src/VaultSharp/V1/SecretsEngines/Database/ConnectionConfigModelJsonConverter.cs#L109

PostgreSQLConnectionConfigModel is properly being deserialized, but base properties from ConnectionConfigModel (i.e. connection_url, username, password) are null in the returned Secret<ConnectionConfigModel>.

treyhendon commented 1 month ago

I'm debugging through locally to try to find a solution. Posting here in case you know a quick fix. Thanks!

rajanadar commented 1 month ago

Are all base properties null?

The System.Text.Json custom converters seem to be problematic if used via attributes. (vs programmatically) I'll also debug further. https://github.com/dotnet/runtime/issues/46372

treyhendon commented 1 month ago

Any base property that didn't have a default in the class appears null, yes. I do see the value in the jsonString from the API response.

treyhendon commented 1 month ago

I followed your comment in the ConnectionConfigModel class and saw that .net issue about STJ. I was testing different forms of attribute references to see if I could force the base model to deserialize first, but so far no luck.

treyhendon commented 1 month ago

As an example, here's what I get in jsonString:

{"allowed_roles":["api-sample","api-sample-migration"],"connection_details":{"connection_url":"postgresql://{{username}}:{{password}}@myserver.svc.cluster.local:5432/sample?sslmode=require","username":"myuser"},"password_policy":"","plugin_name":"postgresql-database-plugin","plugin_version":"","root_credentials_rotate_statements":[]}

I made a temp obj variable to capture the deserialzed value: image

rajanadar commented 1 month ago

thanks , that is very helpful. I think it is not a deser issue. The structure of the connection object between the write and read seems to be different. The read structure is as follows: https://developer.hashicorp.com/vault/api-docs/secret/databases#sample-response

Thats the problem. I'll release a fix with the right model tomorrow.

treyhendon commented 1 month ago

Oh! I think I see it. The JSON structure is slightly different than expected.

{
    "allowed_roles": [
        "api-sample",
        "api-sample-migration"
    ],
    "connection_details": {
        "connection_url": "postgresql://{{username}}:{{password}}@myserver.svc.cluster.local:5432/sample?sslmode=require",
        "username": "myuser"
    },
    "password_policy": "",
    "plugin_name": "postgresql-database-plugin",
    "plugin_version": "",
    "root_credentials_rotate_statements": []
}

Looks like ConnectionDetails would hold ConnectionUrl, Username, Password, etc

rajanadar commented 1 month ago

@treyhendon I prepared a local fix. Which version of .NET are you using? i'll give you a DLL for temp testing here, before releasing a nuget

treyhendon commented 1 month ago

I'm using .net 8. Thanks!

rajanadar commented 1 month ago

please use this dll and let me know. VaultSharp-1.17.5.1-For-Test-.NET 8.zip

treyhendon commented 1 month ago

I actually just pulled your master branch and was able to successfully test! I'll download the dll and try that as well. Thanks!

rajanadar commented 1 month ago

Ah cool. I forget that folks can easily build from the project as well.

treyhendon commented 1 month ago

I can also confirm the .net 8 dll is a success. Thanks again for such a quick response!

rajanadar commented 1 month ago

Cool, good to hear. I'll publish the official nuget in a day. It's my bad, I overlooked the read Api response model.

rajanadar commented 1 month ago

@treyhendon , here it is: https://www.nuget.org/packages/VaultSharp/1.17.5.1