mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-31 02:28:09 +00:00 
			
		
		
		
	Add mount path into the default generated openapi.json spec (UI) (#17926)
This commit is contained in:
		 Anton Averchenkov
					Anton Averchenkov
				
			
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			 GitHub
						GitHub
					
				
			
						parent
						
							256fca684b
						
					
				
				
					commit
					db8efac708
				
			| @@ -208,16 +208,16 @@ var ( | ||||
| 	altRootsRe       = regexp.MustCompile(`^\(([\w\-_]+(?:\|[\w\-_]+)+)\)(/.*)$`) // Pattern starting with alts, e.g. "(root1|root2)/(?P<name>regex)" | ||||
| 	cleanCharsRe     = regexp.MustCompile("[()^$?]")                              // Set of regex characters that will be stripped during cleaning | ||||
| 	cleanSuffixRe    = regexp.MustCompile(`/\?\$?$`)                              // Path suffix patterns that will be stripped during cleaning | ||||
| 	nonWordRe        = regexp.MustCompile(`[^\w]+`)                               // Match a sequence of non-word characters | ||||
| 	nonWordRe        = regexp.MustCompile(`[^a-zA-Z0-9]+`)                        // Match a sequence of non-word characters | ||||
| 	pathFieldsRe     = regexp.MustCompile(`{(\w+)}`)                              // Capture OpenAPI-style named parameters, e.g. "lookup/{urltoken}", | ||||
| 	reqdRe           = regexp.MustCompile(`\(?\?P<(\w+)>[^)]*\)?`)                // Capture required parameters, e.g. "(?P<name>regex)" | ||||
| 	wsRe             = regexp.MustCompile(`\s+`)                                  // Match whitespace, to be compressed during cleaning | ||||
| ) | ||||
|  | ||||
| // documentPaths parses all paths in a framework.Backend into OpenAPI paths. | ||||
| func documentPaths(backend *Backend, requestResponsePrefix string, genericMountPaths bool, doc *OASDocument) error { | ||||
| func documentPaths(backend *Backend, requestResponsePrefix string, doc *OASDocument) error { | ||||
| 	for _, p := range backend.Paths { | ||||
| 		if err := documentPath(p, backend.SpecialPaths(), requestResponsePrefix, genericMountPaths, backend.BackendType, doc); err != nil { | ||||
| 		if err := documentPath(p, backend.SpecialPaths(), requestResponsePrefix, backend.BackendType, doc); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| @@ -226,7 +226,7 @@ func documentPaths(backend *Backend, requestResponsePrefix string, genericMountP | ||||
| } | ||||
|  | ||||
| // documentPath parses a framework.Path into one or more OpenAPI paths. | ||||
| func documentPath(p *Path, specialPaths *logical.Paths, requestResponsePrefix string, genericMountPaths bool, backendType logical.BackendType, doc *OASDocument) error { | ||||
| func documentPath(p *Path, specialPaths *logical.Paths, requestResponsePrefix string, backendType logical.BackendType, doc *OASDocument) error { | ||||
| 	var sudoPaths []string | ||||
| 	var unauthPaths []string | ||||
|  | ||||
| @@ -265,16 +265,21 @@ func documentPath(p *Path, specialPaths *logical.Paths, requestResponsePrefix st | ||||
| 		// Body fields will be added to individual operations. | ||||
| 		pathFields, bodyFields := splitFields(p.Fields, path) | ||||
|  | ||||
| 		if genericMountPaths && requestResponsePrefix != "system" && requestResponsePrefix != "identity" { | ||||
| 			// Add mount path as a parameter | ||||
| 		defaultMountPath := requestResponsePrefix | ||||
| 		if requestResponsePrefix == "kv" { | ||||
| 			defaultMountPath = "secret" | ||||
| 		} | ||||
|  | ||||
| 		if defaultMountPath != "system" && defaultMountPath != "identity" { | ||||
| 			p := OASParameter{ | ||||
| 				Name:        "mountPath", | ||||
| 				Description: "Path that the backend was mounted at", | ||||
| 				Name:        fmt.Sprintf("%s_mount_path", defaultMountPath), | ||||
| 				Description: "Path where the backend was mounted; the endpoint path will be offset by the mount path", | ||||
| 				In:          "path", | ||||
| 				Schema: &OASSchema{ | ||||
| 					Type: "string", | ||||
| 					Type:    "string", | ||||
| 					Default: defaultMountPath, | ||||
| 				}, | ||||
| 				Required: true, | ||||
| 				Required: false, | ||||
| 			} | ||||
|  | ||||
| 			pi.Parameters = append(pi.Parameters, p) | ||||
| @@ -780,6 +785,9 @@ func cleanResponse(resp *logical.Response) *cleanedResponse { | ||||
| // | ||||
| // An optional user-provided suffix ("context") may also be appended. | ||||
| func (d *OASDocument) CreateOperationIDs(context string) { | ||||
| 	// title caser | ||||
| 	title := cases.Title(language.English) | ||||
|  | ||||
| 	opIDCount := make(map[string]int) | ||||
| 	var paths []string | ||||
|  | ||||
| @@ -806,9 +814,12 @@ func (d *OASDocument) CreateOperationIDs(context string) { | ||||
| 				continue | ||||
| 			} | ||||
|  | ||||
| 			// Discard "_mount_path" from any {thing_mount_path} parameters | ||||
| 			path = strings.Replace(path, "_mount_path", "", 1) | ||||
|  | ||||
| 			// Space-split on non-words, title case everything, recombine | ||||
| 			opID := nonWordRe.ReplaceAllString(strings.ToLower(path), " ") | ||||
| 			opID = strings.Title(opID) | ||||
| 			opID = title.String(opID) | ||||
| 			opID = method + strings.ReplaceAll(opID, " ", "") | ||||
|  | ||||
| 			// deduplicate operationIds. This is a safeguard, since generated IDs should | ||||
|   | ||||
		Reference in New Issue
	
	Block a user