2018-11-05 20:24:39 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
2019-03-26 16:08:56 +00:00
|
|
|
"bytes"
|
2018-11-05 20:24:39 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-test/deep"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/wrapping"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-11-05 20:24:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOpenAPI_Regex(t *testing.T) {
|
|
|
|
t.Run("Path fields", func(t *testing.T) {
|
|
|
|
input := `/foo/bar/{inner}/baz/{outer}`
|
|
|
|
|
|
|
|
matches := pathFieldsRe.FindAllStringSubmatch(input, -1)
|
|
|
|
|
|
|
|
exp1 := "inner"
|
|
|
|
exp2 := "outer"
|
|
|
|
if matches[0][1] != exp1 || matches[1][1] != exp2 {
|
|
|
|
t.Fatalf("Capture error. Expected %s and %s, got %v", exp1, exp2, matches)
|
|
|
|
}
|
|
|
|
|
|
|
|
input = `/foo/bar/inner/baz/outer`
|
|
|
|
matches = pathFieldsRe.FindAllStringSubmatch(input, -1)
|
|
|
|
|
|
|
|
if matches != nil {
|
|
|
|
t.Fatalf("Expected nil match (%s), got %+v", input, matches)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Filtering", func(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
regex *regexp.Regexp
|
|
|
|
output string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: `abcde`,
|
|
|
|
regex: wsRe,
|
|
|
|
output: "abcde",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: ` a b cd e `,
|
|
|
|
regex: wsRe,
|
|
|
|
output: "abcde",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
result := test.regex.ReplaceAllString(test.input, "")
|
|
|
|
if result != test.output {
|
|
|
|
t.Fatalf("Clean Regex error (%s). Expected %s, got %s", test.input, test.output, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenAPI_ExpandPattern(t *testing.T) {
|
|
|
|
tests := []struct {
|
2019-05-03 22:12:24 +00:00
|
|
|
inPattern string
|
|
|
|
outPathlets []string
|
2018-11-05 20:24:39 +00:00
|
|
|
}{
|
2023-01-31 21:27:39 +00:00
|
|
|
// A simple string without regexp metacharacters passes through as is
|
2018-11-05 20:24:39 +00:00
|
|
|
{"rekey/backup", []string{"rekey/backup"}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// A trailing regexp anchor metacharacter is removed
|
2018-11-05 20:24:39 +00:00
|
|
|
{"rekey/backup$", []string{"rekey/backup"}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// As is a leading one
|
|
|
|
{"^rekey/backup", []string{"rekey/backup"}},
|
|
|
|
// Named capture groups become OpenAPI parameters
|
2018-11-05 20:24:39 +00:00
|
|
|
{"auth/(?P<path>.+?)/tune$", []string{"auth/{path}/tune"}},
|
|
|
|
{"auth/(?P<path>.+?)/tune/(?P<more>.*?)$", []string{"auth/{path}/tune/{more}"}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// Even if the capture group contains very complex regexp structure inside it
|
|
|
|
{"something/(?P<something>(a|b(c|d))|e+|f{1,3}[ghi-k]?.*)", []string{"something/{something}"}},
|
|
|
|
// A question-mark results in a result without and with the optional path part
|
2018-11-05 20:24:39 +00:00
|
|
|
{"tools/hash(/(?P<urlalgorithm>.+))?", []string{
|
|
|
|
"tools/hash",
|
|
|
|
"tools/hash/{urlalgorithm}",
|
|
|
|
}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// Multiple question-marks evaluate each possible combination
|
2018-11-05 20:24:39 +00:00
|
|
|
{"(leases/)?renew(/(?P<url_lease_id>.+))?", []string{
|
|
|
|
"leases/renew",
|
|
|
|
"leases/renew/{url_lease_id}",
|
|
|
|
"renew",
|
|
|
|
"renew/{url_lease_id}",
|
|
|
|
}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// GenericNameRegex is one particular way of writing a named capture group, so behaves the same
|
2018-11-05 20:24:39 +00:00
|
|
|
{`config/ui/headers/` + GenericNameRegex("header"), []string{"config/ui/headers/{header}"}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// The question-mark behaviour is still works when the question-mark is directly applied to a named capture group
|
2018-11-05 20:24:39 +00:00
|
|
|
{`leases/lookup/(?P<prefix>.+?)?`, []string{
|
|
|
|
"leases/lookup/",
|
|
|
|
"leases/lookup/{prefix}",
|
|
|
|
}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// Optional trailing slashes at the end of the path get stripped - even if appearing deep inside an alternation
|
2018-11-05 20:24:39 +00:00
|
|
|
{`(raw/?$|raw/(?P<path>.+))`, []string{
|
|
|
|
"raw",
|
|
|
|
"raw/{path}",
|
|
|
|
}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// OptionalParamRegex is also another way of writing a named capture group, that is optional
|
2018-11-05 20:24:39 +00:00
|
|
|
{"lookup" + OptionalParamRegex("urltoken"), []string{
|
|
|
|
"lookup",
|
|
|
|
"lookup/{urltoken}",
|
|
|
|
}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// Optional trailign slashes at the end of the path get stripped in simpler cases too
|
2018-11-05 20:24:39 +00:00
|
|
|
{"roles/?$", []string{
|
|
|
|
"roles",
|
|
|
|
}},
|
|
|
|
{"roles/?", []string{
|
|
|
|
"roles",
|
|
|
|
}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// Non-optional trailing slashes remain... although don't do this, it breaks HelpOperation!
|
|
|
|
// (Existing real examples of this pattern being fixed via https://github.com/hashicorp/vault/pull/18571)
|
2018-11-05 20:24:39 +00:00
|
|
|
{"accessors/$", []string{
|
|
|
|
"accessors/",
|
|
|
|
}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// GenericNameRegex and OptionalParamRegex still work when concatenated
|
2018-11-05 20:24:39 +00:00
|
|
|
{"verify/" + GenericNameRegex("name") + OptionalParamRegex("urlalgorithm"), []string{
|
|
|
|
"verify/{name}",
|
|
|
|
"verify/{name}/{urlalgorithm}",
|
|
|
|
}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// Named capture groups that specify enum-like parameters work as expected
|
2018-11-06 23:04:30 +00:00
|
|
|
{"^plugins/catalog/(?P<type>auth|database|secret)/(?P<name>.+)$", []string{
|
|
|
|
"plugins/catalog/{type}/{name}",
|
|
|
|
}},
|
|
|
|
{"^plugins/catalog/(?P<type>auth|database|secret)/?$", []string{
|
|
|
|
"plugins/catalog/{type}",
|
|
|
|
}},
|
2023-01-31 21:27:39 +00:00
|
|
|
// Alternations between various literal path segments work
|
2021-12-22 23:36:47 +00:00
|
|
|
{"(pathOne|pathTwo)/", []string{"pathOne/", "pathTwo/"}},
|
|
|
|
{"(pathOne|pathTwo)/" + GenericNameRegex("name"), []string{"pathOne/{name}", "pathTwo/{name}"}},
|
2022-01-27 18:06:34 +00:00
|
|
|
{
|
|
|
|
"(pathOne|path-2|Path_3)/" + GenericNameRegex("name"),
|
|
|
|
[]string{"Path_3/{name}", "path-2/{name}", "pathOne/{name}"},
|
|
|
|
},
|
2023-01-31 21:27:39 +00:00
|
|
|
// They still work when combined with GenericNameWithAtRegex
|
|
|
|
{"(creds|sts)/" + GenericNameWithAtRegex("name"), []string{
|
|
|
|
"creds/{name}",
|
|
|
|
"sts/{name}",
|
|
|
|
}},
|
|
|
|
// And when they're somewhere other than the start of the pattern
|
|
|
|
{"keys/generate/(internal|exported|kms)", []string{
|
|
|
|
"keys/generate/exported",
|
|
|
|
"keys/generate/internal",
|
|
|
|
"keys/generate/kms",
|
|
|
|
}},
|
|
|
|
// If a plugin author makes their list operation support both singular and plural forms, the OpenAPI notices
|
|
|
|
{"rolesets?/?", []string{"roleset", "rolesets"}},
|
|
|
|
// Complex nested alternation and question-marks are correctly interpreted
|
|
|
|
{"crl(/pem|/delta(/pem)?)?", []string{"crl", "crl/delta", "crl/delta/pem", "crl/pem"}},
|
2018-11-05 20:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2023-01-31 21:27:39 +00:00
|
|
|
out, err := expandPattern(test.inPattern)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-05 20:24:39 +00:00
|
|
|
sort.Strings(out)
|
2019-05-03 22:12:24 +00:00
|
|
|
if !reflect.DeepEqual(out, test.outPathlets) {
|
|
|
|
t.Fatalf("Test %d: Expected %v got %v", i, test.outPathlets, out)
|
2018-11-05 20:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 21:27:39 +00:00
|
|
|
func TestOpenAPI_ExpandPattern_ReturnsError(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
inPattern string
|
|
|
|
outError error
|
|
|
|
}{
|
|
|
|
// None of these regexp constructs are allowed outside of named capture groups
|
|
|
|
{"[a-z]", errUnsupportableRegexpOperationForOpenAPI},
|
|
|
|
{".", errUnsupportableRegexpOperationForOpenAPI},
|
|
|
|
{"a+", errUnsupportableRegexpOperationForOpenAPI},
|
|
|
|
{"a*", errUnsupportableRegexpOperationForOpenAPI},
|
|
|
|
// So this pattern, which is a combination of two of the above isn't either - this pattern occurs in the KV
|
|
|
|
// secrets engine for its catch-all error handler, which provides a helpful hint to people treating a KV v2 as
|
|
|
|
// a KV v1.
|
|
|
|
{".*", errUnsupportableRegexpOperationForOpenAPI},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
_, err := expandPattern(test.inPattern)
|
|
|
|
if err != test.outError {
|
|
|
|
t.Fatalf("Test %d: Expected %q got %q", i, test.outError, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 20:24:39 +00:00
|
|
|
func TestOpenAPI_SplitFields(t *testing.T) {
|
|
|
|
fields := map[string]*FieldSchema{
|
|
|
|
"a": {Description: "path"},
|
|
|
|
"b": {Description: "body"},
|
|
|
|
"c": {Description: "body"},
|
|
|
|
"d": {Description: "body"},
|
|
|
|
"e": {Description: "path"},
|
|
|
|
}
|
|
|
|
|
|
|
|
pathFields, bodyFields := splitFields(fields, "some/{a}/path/{e}")
|
|
|
|
|
|
|
|
lp := len(pathFields)
|
|
|
|
lb := len(bodyFields)
|
|
|
|
l := len(fields)
|
|
|
|
if lp+lb != l {
|
|
|
|
t.Fatalf("split length error: %d + %d != %d", lp, lb, l)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, field := range pathFields {
|
|
|
|
if field.Description != "path" {
|
|
|
|
t.Fatalf("expected field %s to be in 'path', found in %s", name, field.Description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for name, field := range bodyFields {
|
|
|
|
if field.Description != "body" {
|
|
|
|
t.Fatalf("expected field %s to be in 'body', found in %s", name, field.Description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenAPI_SpecialPaths(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
pattern string
|
|
|
|
rootPaths []string
|
|
|
|
root bool
|
|
|
|
unauthPaths []string
|
|
|
|
unauth bool
|
|
|
|
}{
|
|
|
|
{"foo", []string{}, false, []string{"foo"}, true},
|
|
|
|
{"foo", []string{"foo"}, true, []string{"bar"}, false},
|
|
|
|
{"foo/bar", []string{"foo"}, false, []string{"foo/*"}, true},
|
|
|
|
{"foo/bar", []string{"foo/*"}, true, []string{"foo"}, false},
|
|
|
|
{"foo/", []string{"foo/*"}, true, []string{"a", "b", "foo/"}, true},
|
|
|
|
{"foo", []string{"foo*"}, true, []string{"a", "fo*"}, true},
|
|
|
|
{"foo/bar", []string{"a", "b", "foo/*"}, true, []string{"foo/baz/*"}, false},
|
|
|
|
}
|
|
|
|
for i, test := range tests {
|
2022-12-07 18:29:51 +00:00
|
|
|
doc := NewOASDocument("version")
|
2018-11-05 20:24:39 +00:00
|
|
|
path := Path{
|
|
|
|
Pattern: test.pattern,
|
|
|
|
}
|
|
|
|
sp := &logical.Paths{
|
|
|
|
Root: test.rootPaths,
|
|
|
|
Unauthenticated: test.unauthPaths,
|
|
|
|
}
|
2023-01-18 04:07:11 +00:00
|
|
|
err := documentPath(&path, sp, "kv", logical.TypeLogical, doc)
|
2019-05-03 22:12:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-05 20:24:39 +00:00
|
|
|
result := test.root
|
|
|
|
if doc.Paths["/"+test.pattern].Sudo != result {
|
|
|
|
t.Fatalf("Test (root) %d: Expected %v got %v", i, test.root, result)
|
|
|
|
}
|
|
|
|
result = test.unauth
|
|
|
|
if doc.Paths["/"+test.pattern].Unauthenticated != result {
|
|
|
|
t.Fatalf("Test (unauth) %d: Expected %v got %v", i, test.unauth, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenAPI_Paths(t *testing.T) {
|
|
|
|
origDepth := deep.MaxDepth
|
|
|
|
defer func() { deep.MaxDepth = origDepth }()
|
|
|
|
deep.MaxDepth = 20
|
|
|
|
|
|
|
|
t.Run("Legacy callbacks", func(t *testing.T) {
|
|
|
|
p := &Path{
|
|
|
|
Pattern: "lookup/" + GenericNameRegex("id"),
|
|
|
|
|
|
|
|
Fields: map[string]*FieldSchema{
|
2019-05-03 22:12:24 +00:00
|
|
|
"id": {
|
2018-11-05 20:24:39 +00:00
|
|
|
Type: TypeString,
|
|
|
|
Description: "My id parameter",
|
|
|
|
},
|
2019-05-03 22:12:24 +00:00
|
|
|
"token": {
|
2018-11-05 20:24:39 +00:00
|
|
|
Type: TypeString,
|
|
|
|
Description: "My token",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]OperationFunc{
|
|
|
|
logical.ReadOperation: nil,
|
|
|
|
logical.UpdateOperation: nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: "Synopsis",
|
|
|
|
HelpDescription: "Description",
|
|
|
|
}
|
|
|
|
|
|
|
|
sp := &logical.Paths{
|
|
|
|
Root: []string{},
|
|
|
|
Unauthenticated: []string{},
|
|
|
|
}
|
|
|
|
testPath(t, p, sp, expected("legacy"))
|
|
|
|
})
|
|
|
|
|
2022-01-18 17:21:44 +00:00
|
|
|
t.Run("Operations - All Operations", func(t *testing.T) {
|
2018-11-05 20:24:39 +00:00
|
|
|
p := &Path{
|
|
|
|
Pattern: "foo/" + GenericNameRegex("id"),
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"id": {
|
|
|
|
Type: TypeString,
|
|
|
|
Description: "id path parameter",
|
|
|
|
},
|
|
|
|
"flavors": {
|
|
|
|
Type: TypeCommaStringSlice,
|
|
|
|
Description: "the flavors",
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
Type: TypeNameString,
|
2019-02-12 23:36:13 +00:00
|
|
|
Default: "Larry",
|
2018-11-05 20:24:39 +00:00
|
|
|
Description: "the name",
|
|
|
|
},
|
2019-01-09 00:50:24 +00:00
|
|
|
"age": {
|
2019-06-21 15:08:08 +00:00
|
|
|
Type: TypeInt,
|
|
|
|
Description: "the age",
|
|
|
|
AllowedValues: []interface{}{1, 2, 3},
|
|
|
|
Required: true,
|
|
|
|
DisplayAttrs: &DisplayAttributes{
|
|
|
|
Name: "Age",
|
|
|
|
Sensitive: true,
|
|
|
|
Group: "Some Group",
|
|
|
|
Value: 7,
|
|
|
|
},
|
2019-01-09 00:50:24 +00:00
|
|
|
},
|
2018-11-05 20:24:39 +00:00
|
|
|
"x-abc-token": {
|
2019-01-09 00:50:24 +00:00
|
|
|
Type: TypeHeader,
|
|
|
|
Description: "a header value",
|
|
|
|
AllowedValues: []interface{}{"a", "b", "c"},
|
2018-11-05 20:24:39 +00:00
|
|
|
},
|
2022-04-22 22:37:12 +00:00
|
|
|
"maximum": {
|
|
|
|
Type: TypeInt64,
|
|
|
|
Description: "a maximum value",
|
|
|
|
},
|
2019-03-28 21:40:56 +00:00
|
|
|
"format": {
|
|
|
|
Type: TypeString,
|
|
|
|
Description: "a query param",
|
|
|
|
Query: true,
|
|
|
|
},
|
2018-11-05 20:24:39 +00:00
|
|
|
},
|
|
|
|
HelpSynopsis: "Synopsis",
|
|
|
|
HelpDescription: "Description",
|
|
|
|
Operations: map[logical.Operation]OperationHandler{
|
|
|
|
logical.ReadOperation: &PathOperation{
|
|
|
|
Summary: "My Summary",
|
|
|
|
Description: "My Description",
|
|
|
|
},
|
|
|
|
logical.UpdateOperation: &PathOperation{
|
|
|
|
Summary: "Update Summary",
|
|
|
|
Description: "Update Description",
|
|
|
|
},
|
|
|
|
logical.CreateOperation: &PathOperation{
|
|
|
|
Summary: "Create Summary",
|
|
|
|
Description: "Create Description",
|
|
|
|
},
|
|
|
|
logical.ListOperation: &PathOperation{
|
|
|
|
Summary: "List Summary",
|
|
|
|
Description: "List Description",
|
|
|
|
},
|
|
|
|
logical.DeleteOperation: &PathOperation{
|
|
|
|
Summary: "This shouldn't show up",
|
|
|
|
Unpublished: true,
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 15:08:08 +00:00
|
|
|
DisplayAttrs: &DisplayAttributes{
|
|
|
|
Navigation: true,
|
|
|
|
},
|
2018-11-05 20:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sp := &logical.Paths{
|
|
|
|
Root: []string{"foo*"},
|
|
|
|
}
|
|
|
|
testPath(t, p, sp, expected("operations"))
|
|
|
|
})
|
|
|
|
|
2022-01-18 17:21:44 +00:00
|
|
|
t.Run("Operations - List Only", func(t *testing.T) {
|
|
|
|
p := &Path{
|
|
|
|
Pattern: "foo/" + GenericNameRegex("id"),
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"id": {
|
|
|
|
Type: TypeString,
|
|
|
|
Description: "id path parameter",
|
|
|
|
},
|
|
|
|
"flavors": {
|
|
|
|
Type: TypeCommaStringSlice,
|
|
|
|
Description: "the flavors",
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
Type: TypeNameString,
|
|
|
|
Default: "Larry",
|
|
|
|
Description: "the name",
|
|
|
|
},
|
|
|
|
"age": {
|
|
|
|
Type: TypeInt,
|
|
|
|
Description: "the age",
|
|
|
|
AllowedValues: []interface{}{1, 2, 3},
|
|
|
|
Required: true,
|
|
|
|
DisplayAttrs: &DisplayAttributes{
|
|
|
|
Name: "Age",
|
|
|
|
Sensitive: true,
|
|
|
|
Group: "Some Group",
|
|
|
|
Value: 7,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"x-abc-token": {
|
|
|
|
Type: TypeHeader,
|
|
|
|
Description: "a header value",
|
|
|
|
AllowedValues: []interface{}{"a", "b", "c"},
|
|
|
|
},
|
|
|
|
"format": {
|
|
|
|
Type: TypeString,
|
|
|
|
Description: "a query param",
|
|
|
|
Query: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HelpSynopsis: "Synopsis",
|
|
|
|
HelpDescription: "Description",
|
|
|
|
Operations: map[logical.Operation]OperationHandler{
|
|
|
|
logical.ListOperation: &PathOperation{
|
|
|
|
Summary: "List Summary",
|
|
|
|
Description: "List Description",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DisplayAttrs: &DisplayAttributes{
|
|
|
|
Navigation: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sp := &logical.Paths{
|
|
|
|
Root: []string{"foo*"},
|
|
|
|
}
|
|
|
|
testPath(t, p, sp, expected("operations_list"))
|
|
|
|
})
|
|
|
|
|
2018-11-05 20:24:39 +00:00
|
|
|
t.Run("Responses", func(t *testing.T) {
|
|
|
|
p := &Path{
|
|
|
|
Pattern: "foo",
|
|
|
|
HelpSynopsis: "Synopsis",
|
|
|
|
HelpDescription: "Description",
|
|
|
|
Operations: map[logical.Operation]OperationHandler{
|
|
|
|
logical.ReadOperation: &PathOperation{
|
|
|
|
Summary: "My Summary",
|
|
|
|
Description: "My Description",
|
|
|
|
Responses: map[int][]Response{
|
|
|
|
202: {{
|
|
|
|
Description: "Amazing",
|
|
|
|
Example: &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"amount": 42,
|
|
|
|
},
|
|
|
|
},
|
2022-12-05 16:11:06 +00:00
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"field_a": {
|
|
|
|
Type: TypeString,
|
|
|
|
Description: "field_a description",
|
|
|
|
},
|
|
|
|
"field_b": {
|
|
|
|
Type: TypeBool,
|
|
|
|
Description: "field_b description",
|
|
|
|
},
|
|
|
|
},
|
2018-11-05 20:24:39 +00:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
logical.DeleteOperation: &PathOperation{
|
|
|
|
Summary: "Delete stuff",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sp := &logical.Paths{
|
|
|
|
Unauthenticated: []string{"x", "y", "foo"},
|
|
|
|
}
|
|
|
|
|
|
|
|
testPath(t, p, sp, expected("responses"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-04 19:46:54 +00:00
|
|
|
func TestOpenAPI_OperationID(t *testing.T) {
|
|
|
|
path1 := &Path{
|
|
|
|
Pattern: "foo/" + GenericNameRegex("id"),
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"id": {Type: TypeString},
|
|
|
|
},
|
|
|
|
Operations: map[logical.Operation]OperationHandler{
|
|
|
|
logical.ReadOperation: &PathOperation{},
|
|
|
|
logical.UpdateOperation: &PathOperation{},
|
|
|
|
logical.DeleteOperation: &PathOperation{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
path2 := &Path{
|
|
|
|
Pattern: "Foo/" + GenericNameRegex("id"),
|
|
|
|
Fields: map[string]*FieldSchema{
|
|
|
|
"id": {Type: TypeString},
|
|
|
|
},
|
|
|
|
Operations: map[logical.Operation]OperationHandler{
|
|
|
|
logical.ReadOperation: &PathOperation{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-10 23:39:53 +00:00
|
|
|
for _, context := range []string{"", "bar"} {
|
2022-12-07 18:29:51 +00:00
|
|
|
doc := NewOASDocument("version")
|
2023-01-18 04:07:11 +00:00
|
|
|
err := documentPath(path1, nil, "kv", logical.TypeLogical, doc)
|
2022-11-10 23:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-01-18 04:07:11 +00:00
|
|
|
err = documentPath(path2, nil, "kv", logical.TypeLogical, doc)
|
2022-11-10 23:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
doc.CreateOperationIDs(context)
|
2019-01-04 19:46:54 +00:00
|
|
|
|
2022-11-10 23:39:53 +00:00
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
op string
|
|
|
|
opID string
|
|
|
|
}{
|
|
|
|
{"/Foo/{id}", "get", "getFooId"},
|
|
|
|
{"/foo/{id}", "get", "getFooId_2"},
|
|
|
|
{"/foo/{id}", "post", "postFooId"},
|
|
|
|
{"/foo/{id}", "delete", "deleteFooId"},
|
|
|
|
}
|
2019-01-04 19:46:54 +00:00
|
|
|
|
2022-11-10 23:39:53 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
actual := getPathOp(doc.Paths[test.path], test.op).OperationID
|
|
|
|
expected := test.opID
|
|
|
|
if context != "" {
|
|
|
|
expected += "_" + context
|
|
|
|
}
|
2019-01-04 19:46:54 +00:00
|
|
|
|
2022-11-10 23:39:53 +00:00
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("expected %v, got %v", expected, actual)
|
|
|
|
}
|
2019-01-04 19:46:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 20:24:39 +00:00
|
|
|
func TestOpenAPI_CustomDecoder(t *testing.T) {
|
|
|
|
p := &Path{
|
|
|
|
Pattern: "foo",
|
|
|
|
HelpSynopsis: "Synopsis",
|
|
|
|
Operations: map[logical.Operation]OperationHandler{
|
|
|
|
logical.ReadOperation: &PathOperation{
|
|
|
|
Summary: "My Summary",
|
|
|
|
Responses: map[int][]Response{
|
|
|
|
100: {{
|
|
|
|
Description: "OK",
|
2019-03-26 16:08:56 +00:00
|
|
|
Example: &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"foo": 42,
|
|
|
|
},
|
|
|
|
},
|
2018-11-05 20:24:39 +00:00
|
|
|
}},
|
|
|
|
200: {{
|
|
|
|
Description: "Good",
|
2019-03-26 16:08:56 +00:00
|
|
|
Example: (*logical.Response)(nil),
|
2018-11-05 20:24:39 +00:00
|
|
|
}},
|
|
|
|
599: {{
|
|
|
|
Description: "Bad",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:29:51 +00:00
|
|
|
docOrig := NewOASDocument("version")
|
2023-01-18 04:07:11 +00:00
|
|
|
err := documentPath(p, nil, "kv", logical.TypeLogical, docOrig)
|
2019-05-03 22:12:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-05 20:24:39 +00:00
|
|
|
|
2019-03-26 16:08:56 +00:00
|
|
|
docJSON := mustJSONMarshal(t, docOrig)
|
2018-11-05 20:24:39 +00:00
|
|
|
|
|
|
|
var intermediate map[string]interface{}
|
|
|
|
if err := jsonutil.DecodeJSON(docJSON, &intermediate); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
docNew, err := NewOASDocumentFromMap(intermediate)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-26 16:08:56 +00:00
|
|
|
docNewJSON := mustJSONMarshal(t, docNew)
|
|
|
|
|
|
|
|
if diff := deep.Equal(docJSON, docNewJSON); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenAPI_CleanResponse(t *testing.T) {
|
|
|
|
// Verify that an all-null input results in empty JSON
|
|
|
|
orig := &logical.Response{}
|
|
|
|
|
|
|
|
cr := cleanResponse(orig)
|
|
|
|
|
|
|
|
newJSON := mustJSONMarshal(t, cr)
|
|
|
|
|
|
|
|
if !bytes.Equal(newJSON, []byte("{}")) {
|
|
|
|
t.Fatalf("expected {}, got: %q", newJSON)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that all non-null inputs results in JSON that matches the marshalling of
|
|
|
|
// logical.Response. This will fail if logical.Response changes without a corresponding
|
|
|
|
// change to cleanResponse()
|
|
|
|
orig = &logical.Response{
|
|
|
|
Secret: new(logical.Secret),
|
|
|
|
Auth: new(logical.Auth),
|
|
|
|
Data: map[string]interface{}{"foo": 42},
|
|
|
|
Redirect: "foo",
|
|
|
|
Warnings: []string{"foo"},
|
|
|
|
WrapInfo: &wrapping.ResponseWrapInfo{Token: "foo"},
|
|
|
|
Headers: map[string][]string{"foo": {"bar"}},
|
|
|
|
}
|
|
|
|
origJSON := mustJSONMarshal(t, orig)
|
|
|
|
|
|
|
|
cr = cleanResponse(orig)
|
|
|
|
|
|
|
|
cleanJSON := mustJSONMarshal(t, cr)
|
|
|
|
|
|
|
|
if diff := deep.Equal(origJSON, cleanJSON); diff != nil {
|
2018-11-05 20:24:39 +00:00
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPath(t *testing.T, path *Path, sp *logical.Paths, expectedJSON string) {
|
|
|
|
t.Helper()
|
|
|
|
|
2022-12-07 18:29:51 +00:00
|
|
|
doc := NewOASDocument("dummyversion")
|
2023-01-18 04:07:11 +00:00
|
|
|
if err := documentPath(path, sp, "kv", logical.TypeLogical, doc); err != nil {
|
2019-03-28 21:40:56 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-11-10 23:39:53 +00:00
|
|
|
doc.CreateOperationIDs("")
|
2018-11-05 20:24:39 +00:00
|
|
|
|
|
|
|
docJSON, err := json.MarshalIndent(doc, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare json by first decoding, then comparing with a deep equality check.
|
|
|
|
var expected, actual interface{}
|
|
|
|
if err := jsonutil.DecodeJSON(docJSON, &actual); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := jsonutil.DecodeJSON([]byte(expectedJSON), &expected); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := deep.Equal(actual, expected); diff != nil {
|
2021-04-08 16:43:39 +00:00
|
|
|
// fmt.Println(string(docJSON)) // uncomment to debug generated JSON (very helpful when fixing tests)
|
2018-11-05 20:24:39 +00:00
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 19:46:54 +00:00
|
|
|
func getPathOp(pi *OASPathItem, op string) *OASOperation {
|
|
|
|
switch op {
|
|
|
|
case "get":
|
|
|
|
return pi.Get
|
|
|
|
case "post":
|
|
|
|
return pi.Post
|
|
|
|
case "delete":
|
|
|
|
return pi.Delete
|
|
|
|
default:
|
|
|
|
panic("unexpected operation: " + op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 20:24:39 +00:00
|
|
|
func expected(name string) string {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join("testdata", name+".json"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:29:51 +00:00
|
|
|
content := strings.Replace(string(data), "<vault_version>", "dummyversion", 1)
|
2018-11-05 20:24:39 +00:00
|
|
|
|
|
|
|
return content
|
|
|
|
}
|
2019-03-26 16:08:56 +00:00
|
|
|
|
|
|
|
func mustJSONMarshal(t *testing.T, data interface{}) []byte {
|
|
|
|
j, err := json.MarshalIndent(data, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return j
|
|
|
|
}
|