mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-01 11:08:10 +00:00
OpenAPI: Separate ListOperation from ReadOperation (#21723)
* OpenAPI: Separate ListOperation from ReadOperation Historically, since Vault's ReadOperation and ListOperation both map to the HTTP GET method, their representation in the generated OpenAPI has been a bit confusing. This was partially mitigated some time ago, by making the `list` query parameter express whether it was required or optional - but only in a way useful to human readers - the human had to know, for example, that the schema of the response body would change depending on whether `list` was selected. Now that there is an effort underway to automatically generate API clients from the OpenAPI spec, we have a need to fix this more comprehensively. Fortunately, we do have a means to do so - since Vault has opinionated treatment of trailing slashes, linked to operations being list or not, we can use an added trailing slash on the URL path to separate list operations in the OpenAPI spec. This PR implements that, and then fixes an operation ID which becomes duplicated, with this change applied. See also hashicorp/vault-client-go#174, a bug which will be fixed by this work. * Set further DisplayAttrs in auth/github plugin To mask out more duplicate read/list functionality, now being separately generated to OpenAPI client libraries as a result of this change. * Apply requested changes to operation IDs I'm not totally convinced its worth the extra lines of code, but equally, I don't have strong feelings about it, so I'll just make the change. * Adjust logic to prevent any possibility of generating OpenAPI paths with doubled final slashes Even in the edge case of improper use of regex patterns and operations. * changelog * Fix TestSudoPaths to pass again... which snowballed a bit... Once I looked hard at it, I found it was missing several sudo paths, which led to additional bug fixing elsewhere. I might need to pull some parts of this change out into a separate PR for ease of review... * Fix other tests * More test fixing * Undo scope creep - back away from fixing sudo paths not shown as such in OpenAPI, at least within this PR Just add TODO comments for now.
This commit is contained in:
@@ -107,13 +107,12 @@ type OASLicense struct {
|
||||
}
|
||||
|
||||
type OASPathItem struct {
|
||||
Description string `json:"description,omitempty"`
|
||||
Parameters []OASParameter `json:"parameters,omitempty"`
|
||||
Sudo bool `json:"x-vault-sudo,omitempty" mapstructure:"x-vault-sudo"`
|
||||
Unauthenticated bool `json:"x-vault-unauthenticated,omitempty" mapstructure:"x-vault-unauthenticated"`
|
||||
CreateSupported bool `json:"x-vault-createSupported,omitempty" mapstructure:"x-vault-createSupported"`
|
||||
DisplayNavigation bool `json:"x-vault-displayNavigation,omitempty" mapstructure:"x-vault-displayNavigation"`
|
||||
DisplayAttrs *DisplayAttributes `json:"x-vault-displayAttrs,omitempty" mapstructure:"x-vault-displayAttrs"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Parameters []OASParameter `json:"parameters,omitempty"`
|
||||
Sudo bool `json:"x-vault-sudo,omitempty" mapstructure:"x-vault-sudo"`
|
||||
Unauthenticated bool `json:"x-vault-unauthenticated,omitempty" mapstructure:"x-vault-unauthenticated"`
|
||||
CreateSupported bool `json:"x-vault-createSupported,omitempty" mapstructure:"x-vault-createSupported"`
|
||||
DisplayAttrs *DisplayAttributes `json:"x-vault-displayAttrs,omitempty" mapstructure:"x-vault-displayAttrs"`
|
||||
|
||||
Get *OASOperation `json:"get,omitempty"`
|
||||
Post *OASOperation `json:"post,omitempty"`
|
||||
@@ -309,6 +308,7 @@ func documentPath(p *Path, backend *Backend, requestResponsePrefix string, doc *
|
||||
|
||||
// Process each supported operation by building up an Operation object
|
||||
// with descriptions, properties and examples from the framework.Path data.
|
||||
var listOperation *OASOperation
|
||||
for opType, opHandler := range operations {
|
||||
props := opHandler.Properties()
|
||||
if props.Unpublished || forceUnpublished {
|
||||
@@ -324,11 +324,6 @@ func documentPath(p *Path, backend *Backend, requestResponsePrefix string, doc *
|
||||
}
|
||||
}
|
||||
|
||||
// If both List and Read are defined, only process Read.
|
||||
if opType == logical.ListOperation && operations[logical.ReadOperation] != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
op := NewOASOperation()
|
||||
|
||||
operationID := constructOperationID(
|
||||
@@ -408,9 +403,10 @@ func documentPath(p *Path, backend *Backend, requestResponsePrefix string, doc *
|
||||
}
|
||||
}
|
||||
|
||||
// LIST is represented as GET with a `list` query parameter.
|
||||
// LIST is represented as GET with a `list` query parameter. Code later on in this function will assign
|
||||
// list operations to a path with an extra trailing slash, ensuring they do not collide with read
|
||||
// operations.
|
||||
if opType == logical.ListOperation {
|
||||
// Only accepts List (due to the above skipping of ListOperations that also have ReadOperations)
|
||||
op.Parameters = append(op.Parameters, OASParameter{
|
||||
Name: "list",
|
||||
Description: "Must be set to `true`",
|
||||
@@ -418,14 +414,6 @@ func documentPath(p *Path, backend *Backend, requestResponsePrefix string, doc *
|
||||
In: "query",
|
||||
Schema: &OASSchema{Type: "string", Enum: []interface{}{"true"}},
|
||||
})
|
||||
} else if opType == logical.ReadOperation && operations[logical.ListOperation] != nil {
|
||||
// Accepts both Read and List
|
||||
op.Parameters = append(op.Parameters, OASParameter{
|
||||
Name: "list",
|
||||
Description: "Return a list if `true`",
|
||||
In: "query",
|
||||
Schema: &OASSchema{Type: "string"},
|
||||
})
|
||||
}
|
||||
|
||||
// Add tags based on backend type
|
||||
@@ -521,18 +509,79 @@ func documentPath(p *Path, backend *Backend, requestResponsePrefix string, doc *
|
||||
switch opType {
|
||||
case logical.CreateOperation, logical.UpdateOperation:
|
||||
pi.Post = op
|
||||
case logical.ReadOperation, logical.ListOperation:
|
||||
case logical.ReadOperation:
|
||||
pi.Get = op
|
||||
case logical.DeleteOperation:
|
||||
pi.Delete = op
|
||||
case logical.ListOperation:
|
||||
listOperation = op
|
||||
}
|
||||
}
|
||||
|
||||
openAPIPath := "/" + path
|
||||
if doc.Paths[openAPIPath] != nil {
|
||||
backend.Logger().Warn("OpenAPI spec generation: multiple framework.Path instances generated the same path; last processed wins", "path", openAPIPath)
|
||||
// The conventions enforced by the Vault HTTP routing code make it impossible to match a path with a trailing
|
||||
// slash to anything other than a ListOperation. Catch mistakes in path definition, to enforce that if both of
|
||||
// the two following blocks of code (non-list, and list) write an OpenAPI path to the output document, then the
|
||||
// first one will definitely not have a trailing slash.
|
||||
originalPathHasTrailingSlash := strings.HasSuffix(path, "/")
|
||||
if originalPathHasTrailingSlash && (pi.Get != nil || pi.Post != nil || pi.Delete != nil) {
|
||||
backend.Logger().Warn(
|
||||
"OpenAPI spec generation: discarding impossible-to-invoke non-list operations from path with "+
|
||||
"required trailing slash; this is a bug in the backend code", "path", path)
|
||||
pi.Get = nil
|
||||
pi.Post = nil
|
||||
pi.Delete = nil
|
||||
}
|
||||
|
||||
// Write the regular, non-list, OpenAPI path to the OpenAPI document, UNLESS we generated a ListOperation, and
|
||||
// NO OTHER operation types. In that fairly common case (there are lots of list-only endpoints), we avoid
|
||||
// writing a redundant OpenAPI path for (e.g.) "auth/token/accessors" with no operations, only to then write
|
||||
// one for "auth/token/accessors/" immediately below.
|
||||
//
|
||||
// On the other hand, we do still write the OpenAPI path here if we generated ZERO operation types - this serves
|
||||
// to provide documentation to a human that an endpoint exists, even if it has no invokable OpenAPI operations.
|
||||
// Examples of this include kv-v2's ".*" endpoint (regex cannot be translated to OpenAPI parameters), and the
|
||||
// auth/oci/login endpoint (implements ResolveRoleOperation only, only callable from inside Vault).
|
||||
if listOperation == nil || pi.Get != nil || pi.Post != nil || pi.Delete != nil {
|
||||
openAPIPath := "/" + path
|
||||
if doc.Paths[openAPIPath] != nil {
|
||||
backend.Logger().Warn(
|
||||
"OpenAPI spec generation: multiple framework.Path instances generated the same path; "+
|
||||
"last processed wins", "path", openAPIPath)
|
||||
}
|
||||
doc.Paths[openAPIPath] = &pi
|
||||
}
|
||||
|
||||
// If there is a ListOperation, write it to a separate OpenAPI path in the document.
|
||||
if listOperation != nil {
|
||||
// Append a slash here to disambiguate from the path written immediately above.
|
||||
// However, if the path already contains a trailing slash, we want to avoid doubling it, and it is
|
||||
// guaranteed (through the interaction of logic in the last two blocks) that the block immediately above
|
||||
// will NOT have written a path to the OpenAPI document.
|
||||
if !originalPathHasTrailingSlash {
|
||||
path += "/"
|
||||
}
|
||||
|
||||
listPathItem := OASPathItem{
|
||||
Description: pi.Description,
|
||||
Parameters: pi.Parameters,
|
||||
DisplayAttrs: pi.DisplayAttrs,
|
||||
|
||||
// Since the path may now have an extra slash on the end, we need to recalculate the special path
|
||||
// matches, as the sudo or unauthenticated status may be changed as a result!
|
||||
Sudo: specialPathMatch(path, sudoPaths),
|
||||
Unauthenticated: specialPathMatch(path, unauthPaths),
|
||||
|
||||
Get: listOperation,
|
||||
}
|
||||
|
||||
openAPIPath := "/" + path
|
||||
if doc.Paths[openAPIPath] != nil {
|
||||
backend.Logger().Warn(
|
||||
"OpenAPI spec generation: multiple framework.Path instances generated the same path; "+
|
||||
"last processed wins", "path", openAPIPath)
|
||||
}
|
||||
doc.Paths[openAPIPath] = &listPathItem
|
||||
}
|
||||
doc.Paths[openAPIPath] = &pi
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
65
sdk/framework/testdata/operations.json
vendored
65
sdk/framework/testdata/operations.json
vendored
@@ -47,17 +47,7 @@
|
||||
"200": {
|
||||
"description": "OK"
|
||||
}
|
||||
},
|
||||
"parameters": [
|
||||
{
|
||||
"name": "list",
|
||||
"description": "Return a list if `true`",
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"post": {
|
||||
"operationId": "kv-write-foo-id",
|
||||
@@ -82,6 +72,59 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foo/{id}/": {
|
||||
"description": "Synopsis",
|
||||
"x-vault-sudo": true,
|
||||
"x-vault-displayAttrs": {
|
||||
"navigation": true
|
||||
},
|
||||
"parameters": [
|
||||
{
|
||||
"name": "format",
|
||||
"description": "a query param",
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "id",
|
||||
"description": "id path parameter",
|
||||
"in": "path",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
},
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"get": {
|
||||
"operationId": "kv-list-foo-id",
|
||||
"tags": [
|
||||
"secrets"
|
||||
],
|
||||
"summary": "List Summary",
|
||||
"description": "List Description",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK"
|
||||
}
|
||||
},
|
||||
"parameters": [
|
||||
{
|
||||
"name": "list",
|
||||
"description": "Must be set to `true`",
|
||||
"required": true,
|
||||
"in": "query",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"true"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
|
||||
2
sdk/framework/testdata/operations_list.json
vendored
2
sdk/framework/testdata/operations_list.json
vendored
@@ -10,7 +10,7 @@
|
||||
}
|
||||
},
|
||||
"paths": {
|
||||
"/foo/{id}": {
|
||||
"/foo/{id}/": {
|
||||
"description": "Synopsis",
|
||||
"x-vault-sudo": true,
|
||||
"x-vault-displayAttrs": {
|
||||
|
||||
Reference in New Issue
Block a user