Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyClient incorrectly sends explicit null value #6259

Open
chlowell opened this issue Nov 22, 2024 · 0 comments
Open

KeyClient incorrectly sends explicit null value #6259

chlowell opened this issue Nov 22, 2024 · 0 comments
Assignees
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library. KeyVault

Comments

@chlowell
Copy link
Member

In the REST API spec, the "attributes" object of "KeyItem" isn't marked x-nullable, so a client shouldn't send an explicit null value for this key. KeyClient does, however, which gets an error response from Managed HSM.

repro

#include <azure/identity/azure_cli_credential.hpp>
#include <azure/keyvault/keys/key_client.hpp>
#include <azure/core/http/policies/policy.hpp>
#include <azure/core/http/http.hpp>
#include <iostream>
#include <memory>

class LoggingPolicy : public Azure::Core::Http::Policies::HttpPolicy
{
public:
    std::unique_ptr<Azure::Core::Http::RawResponse> Send(
        Azure::Core::Http::Request &request,
        Azure::Core::Http::Policies::NextHttpPolicy nextHttpPolicy,
        Azure::Core::Context const &context) const override
    {
        std::cout << request.GetMethod().ToString() << " " << request.GetUrl().GetPath() << std::endl;
        if (request.GetBodyStream())
        {
            auto bodyStream = request.GetBodyStream();
            std::vector<uint8_t> body(bodyStream->Length());
            bodyStream->Read(body.data(), body.size(), context);
            bodyStream->Rewind();
            std::cout << "\tBody: " << std::string(body.begin(), body.end()) << std::endl;
        }
        return nextHttpPolicy.Send(request, context);
    }

    std::unique_ptr<Azure::Core::Http::Policies::HttpPolicy> Clone() const override
    {
        return std::make_unique<LoggingPolicy>(*this);
    }
};

int main(int argc, char *argv[])
{
    std::string url = argv[1];

    auto credential = std::make_shared<Azure::Identity::AzureCliCredential>();

    auto opts = Azure::Security::KeyVault::Keys::KeyClientOptions();
    opts.PerRetryPolicies.emplace_back(std::make_unique<LoggingPolicy>());
    Azure::Security::KeyVault::Keys::KeyClient client(url, credential, opts);

    Azure::Security::KeyVault::Keys::CreateRsaKeyOptions createRsaKeyOptions("test-key", true);
    createRsaKeyOptions.KeySize = 2048;
    // uncommenting this enables the request to succeed
    // createRsaKeyOptions.Enabled = true;
    try
    {
        auto key = client.CreateRsaKey(createRsaKeyOptions).Value;
        std::cout << "Created key " << key.Id() << std::endl;
    }
    catch (Azure::Core::RequestFailedException const &e)
    {
        std::cerr << "Error: " << e.Message << std::endl;
    }

    return 0;
}

output

$ ./repro <MHSM URL>
POST /keys/test-key/create?api-version=7.3
        Body: {"attributes":null,"kty":"RSA-HSM"}
Error: Object Attributes: A JSON object was expected (Activity ID: ...)
@chlowell chlowell added bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library. KeyVault labels Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library. KeyVault
Projects
Status: Untriaged
Development

No branches or pull requests

2 participants