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

Add HTTP PATCH support to KV #12687

Merged
merged 35 commits into from Oct 13, 2021
Merged

Add HTTP PATCH support to KV #12687

merged 35 commits into from Oct 13, 2021

Conversation

ccapurso
Copy link
Contributor

@ccapurso ccapurso commented Sep 30, 2021

This PR introduces support for HTTP PATCH into Vault. The ACL layer has been modified to include a patch capability. Despite the fact that a patch operation could be interpreted as a type of update, the update capability will continue to function as it does today.

A PatchOperation has been added to the existing logical.Operation set. Vault's logical request handling will map HTTP PATCH requests to a PatchOperation. Currently, Vault will only support JSON merge patch requests which must have a Content-Type header with a MIME type of application/merge-patch+json. If an incorrect MIME type has been provided, Vault will respond with 415 Unsupported Media Type. PATCH requests must be made to existing resources. A 404 Not Found response will be returned to the requester in the case that a logical.Request handler returns a nil response without an error.

To ensure consistent handling of JSON merge patch requests, a global framework.HandlePatchOperation function has been provided which accepts request data, the existing resource data, and a preprocessor func (framework.PatchPreprocessorFunc). The preprocessor is used to ensure the shape of the request data matches the shape of the resource data. The framework.HandlePatchOperation function uses a library called json-patch in order to perform the patch operation. The library expects that both the patch document and the existing resource are provided as marshaled JSON in the form of byte arrays. The patch abstraction will be responsible for the following:

  1. Ensure that provided resource is not nil, otherwise return an error
  2. Construct input map by iterating through fields in framework.FieldDataa using GetOk method so that unexpected fields are ignored and data types are handled properly
  3. If a preprocessing func has been provided, call it with the constructed map from step 2.
  4. Separately call json.Marshal provided with maps of resource and input constructed in step 2.
  5. Call MergePatch from the json-patch library with marshaled JSON byte arrays created in step 4.
  6. Return output from 5. to calling request handler

A JSONMergePatch function will be added to the Logical API in the Vault Go client. It will set the Content-Type header value to application/merge-patch+json.

The existing vault kv patchcommand performs a read, local update, and update in order to achieve a patch operation. The command has been modified to initially attempt an HTTP PATCH using the JSONMergePatch function with a fallback to current behavior in the case of a 403 response. The -cas and -method flags have also been added to the command.

The -method flag supports the values of patch and rw. The patch method will explicitly run the HTTP PATCH logic. The rw method will run the existing read-then-write flow.

The -cas flag will be used for the check-and-set version if provided for HTTP PATCH. The -cas flag will be ignored for the read-then-write flow. Instead, the version value from the secret returned by the read will specify the cas field used in the subsequent write.

Documentation PR: #12689

Fixes #1468
Fixes #7437
Fixes #12330

@vercel vercel bot temporarily deployed to Preview – vault-storybook September 30, 2021 18:14 Inactive
@vercel vercel bot temporarily deployed to Preview – vault September 30, 2021 18:14 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook September 30, 2021 18:16 Inactive
@vercel vercel bot temporarily deployed to Preview – vault September 30, 2021 18:16 Inactive
@ccapurso ccapurso added this to the 1.9 milestone Oct 1, 2021
@vercel vercel bot temporarily deployed to Preview – vault October 1, 2021 19:13 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook October 1, 2021 19:13 Inactive
@vercel vercel bot temporarily deployed to Preview – vault October 7, 2021 19:02 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook October 7, 2021 19:02 Inactive
@vercel vercel bot temporarily deployed to Preview – vault October 7, 2021 19:27 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook October 7, 2021 19:27 Inactive
@vercel vercel bot temporarily deployed to Preview – vault October 7, 2021 19:43 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook October 7, 2021 19:43 Inactive
@vercel vercel bot temporarily deployed to Preview – vault October 7, 2021 21:23 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook October 7, 2021 21:23 Inactive
@vercel vercel bot temporarily deployed to Preview – vault October 7, 2021 21:25 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook October 7, 2021 21:25 Inactive
Copy link
Contributor

@pmmukh pmmukh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one question, but lgtm!

command/kv_test.go Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview – vault October 8, 2021 17:05 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook October 8, 2021 17:05 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants