spec

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2023 License: Apache-2.0 Imports: 32 Imported by: 6

Documentation

Index

Constants

View Source
const (
	NoConflict = iota
	PreferType1
	PreferType2
	ConflictUnresolved
)
View Source
const (
	BasicAuthSecuritySchemeKey  = "BasicAuth"
	APIKeyAuthSecuritySchemeKey = "ApiKeyAuth"
	OAuth2SecuritySchemeKey     = "OAuth2"
	BearerAuthSecuritySchemeKey = "BearerAuth"

	BearerAuthPrefix = "Bearer "
	BasicAuthPrefix  = "Basic "

	AccessTokenParamKey = "access_token"
)

Variables

View Source
var (
	ErrUnknownSpecVersion     = errors.New("unknown spec version")
	ErrUnsupportedSpecVersion = errors.New("unsupported spec version")
)

Sentinel errors for spec version issues.

View Source
var APIKeyNames = map[string]bool{
	"key":     true,
	"api_key": true,
}

APIKeyNames is set of names of headers or query params defining API keys. This should be runtime configurable, of course. Note: keys should be lowercase.

Functions

func AddOperationToPathItem

func AddOperationToPathItem(item *oapi_spec.PathItem, method string, operation *oapi_spec.Operation)

func CloneOperation

func CloneOperation(op *spec.Operation) (*spec.Operation, error)

func ConvertHeadersToMap

func ConvertHeadersToMap(headers []*Header) map[string]string

func CopyPathItemWithNewOperation

func CopyPathItemWithNewOperation(item *oapi_spec.PathItem, method string, operation *oapi_spec.Operation) *oapi_spec.PathItem

func GetOperationFromPathItem

func GetOperationFromPathItem(item *oapi_spec.PathItem, method string) *oapi_spec.Operation

func GetPathAndQuery

func GetPathAndQuery(fullPath string) (path, query string)

example: for "/example-path?param=value" returns "/example-path", "param=value"

func LoadAndValidateRawJSONSpecV3 added in v0.1.0

func LoadAndValidateRawJSONSpecV3(spec []byte) (*oapi_spec.T, error)

func LoadAndValidateRawJSONSpecV3FromV2 added in v0.1.0

func LoadAndValidateRawJSONSpecV3FromV2(spec []byte) (*oapi_spec.T, error)

func MergePathItems

func MergePathItems(dst, src *oapi_spec.PathItem) *oapi_spec.PathItem

func NewAPIKeySecuritySchemeInHeader added in v0.1.0

func NewAPIKeySecuritySchemeInHeader(name string) *spec.SecurityScheme

func NewAPIKeySecuritySchemeInQuery added in v0.1.0

func NewAPIKeySecuritySchemeInQuery(name string) *spec.SecurityScheme

func NewBasicAuthSecurityScheme added in v0.1.0

func NewBasicAuthSecurityScheme() *spec.SecurityScheme

func NewOAuth2SecurityScheme added in v0.1.0

func NewOAuth2SecurityScheme(scopes []string) *spec.SecurityScheme

Types

type APIDiff

type APIDiff struct {
	Type             DiffType
	Path             string
	OriginalPathItem *oapi_spec.PathItem
	ModifiedPathItem *oapi_spec.PathItem
	InteractionID    uuid.UUID
	SpecID           uuid.UUID
}

type ApprovedSpec

type ApprovedSpec struct {
	PathItems       map[string]*oapi_spec.PathItem
	SecuritySchemes oapi_spec.SecuritySchemes
	SpecVersion     OASVersion
}

func (*ApprovedSpec) Clone

func (a *ApprovedSpec) Clone() (*ApprovedSpec, error)

func (*ApprovedSpec) GetPathItem

func (a *ApprovedSpec) GetPathItem(path string) *oapi_spec.PathItem

func (*ApprovedSpec) GetSpecVersion added in v0.1.0

func (a *ApprovedSpec) GetSpecVersion() OASVersion

type ApprovedSpecReview

type ApprovedSpecReview struct {
	PathItemsReview []*ApprovedSpecReviewPathItem
	PathToPathItem  map[string]*oapi_spec.PathItem
}

type ApprovedSpecReviewPathItem

type ApprovedSpecReviewPathItem struct {
	ReviewPathItem
	PathUUID string
}

type Common

type Common struct {
	TruncatedBody bool      `json:"TruncatedBody,omitempty"`
	Body          []byte    `json:"body,omitempty"`
	Headers       []*Header `json:"headers"`
	Version       string    `json:"version,omitempty"`
}

type DiffParams

type DiffParams struct {
	// contains filtered or unexported fields
}

type DiffType

type DiffType string
const (
	DiffTypeNoDiff      DiffType = "NO_DIFF"
	DiffTypeZombieDiff  DiffType = "ZOMBIE_DIFF"
	DiffTypeShadowDiff  DiffType = "SHADOW_DIFF"
	DiffTypeGeneralDiff DiffType = "GENERAL_DIFF"
)

type HTTPInteractionData

type HTTPInteractionData struct {
	ReqBody, RespBody       string
	ReqHeaders, RespHeaders map[string]string
	QueryParams             url.Values
	// contains filtered or unexported fields
}
type Header struct {
	Key   string `json:"key,omitempty"`
	Value string `json:"value,omitempty"`
}

type LearningParametrizedPaths

type LearningParametrizedPaths struct {
	// map parameterized paths into a list of paths included in it.
	// e.g: /api/{param1} -> /api/1, /api/2
	// non parameterized path will map to itself
	Paths map[string]map[string]bool
}

type LearningSpec

type LearningSpec struct {
	// map parameterized path into path item
	PathItems       map[string]*openapi3.PathItem
	SecuritySchemes openapi3.SecuritySchemes
}

func (*LearningSpec) AddPathItem

func (l *LearningSpec) AddPathItem(path string, pathItem *openapi3.PathItem)

func (*LearningSpec) GetPathItem

func (l *LearningSpec) GetPathItem(path string) *openapi3.PathItem

type OASVersion added in v0.1.0

type OASVersion int64
const (
	Unknown OASVersion = iota
	OASv2
	OASv3
)

func GetJSONSpecVersion added in v0.1.0

func GetJSONSpecVersion(jsonSpec []byte) (OASVersion, error)

func LoadAndValidateRawJSONSpec added in v0.1.0

func LoadAndValidateRawJSONSpec(spec []byte) (*openapi3.T, OASVersion, error)

func (OASVersion) String added in v0.1.0

func (o OASVersion) String() string

type OAuth2Claims

type OAuth2Claims struct {
	Scope string `json:"scope"`
	jwt.RegisteredClaims
}

type OperationGenerator

type OperationGenerator struct {
	ResponseHeadersToIgnore map[string]struct{}
	RequestHeadersToIgnore  map[string]struct{}
}

func CreateTestNewOperationGenerator

func CreateTestNewOperationGenerator() *OperationGenerator

func NewOperationGenerator

func NewOperationGenerator(config OperationGeneratorConfig) *OperationGenerator

func (*OperationGenerator) GenerateSpecOperation

func (o *OperationGenerator) GenerateSpecOperation(data *HTTPInteractionData, securitySchemes spec.SecuritySchemes) (*spec.Operation, error)

Note: SecuritySchemes might be updated.

type OperationGeneratorConfig

type OperationGeneratorConfig struct {
	ResponseHeadersToIgnore []string
	RequestHeadersToIgnore  []string
}

type PathParam

type PathParam struct {
	*spec.Parameter
}

type ProvidedSpec

type ProvidedSpec struct {
	Doc                 *openapi3.T
	OriginalSpecVersion OASVersion
}

func (*ProvidedSpec) GetBasePath added in v0.1.0

func (p *ProvidedSpec) GetBasePath() string

func (*ProvidedSpec) GetPathItem

func (p *ProvidedSpec) GetPathItem(path string) *openapi3.PathItem

func (*ProvidedSpec) GetSpecVersion added in v0.1.0

func (p *ProvidedSpec) GetSpecVersion() OASVersion

type Request

type Request struct {
	Common *Common `json:"common,omitempty"`
	Host   string  `json:"host,omitempty"`
	Method string  `json:"method,omitempty"`
	Path   string  `json:"path,omitempty"`
}

type Response

type Response struct {
	Common     *Common `json:"common,omitempty"`
	StatusCode string  `json:"statusCode,omitempty"`
}

type ReviewPathItem

type ReviewPathItem struct {
	// ParameterizedPath represents the parameterized path grouping Paths
	ParameterizedPath string
	// Paths group of paths ParametrizedPath is representing
	Paths map[string]bool
}

type Spec

type Spec struct {
	SpecInfo

	OpGenerator *OperationGenerator
	// contains filtered or unexported fields
}

func CreateDefaultSpec

func CreateDefaultSpec(host string, port string, config OperationGeneratorConfig) *Spec

func (*Spec) ApplyApprovedReview

func (s *Spec) ApplyApprovedReview(approvedReviews *ApprovedSpecReview, version OASVersion) error

func (*Spec) CreateSuggestedReview

func (s *Spec) CreateSuggestedReview() *SuggestedSpecReview

CreateSuggestedReview group all paths that have suspect parameter (with a certain template), into one path which is parameterized, and then add this path params to the spec.

func (*Spec) DiffTelemetry

func (s *Spec) DiffTelemetry(telemetry *Telemetry, specSource SpecSource) (*APIDiff, error)

func (*Spec) GenerateOASJson

func (s *Spec) GenerateOASJson(version OASVersion) ([]byte, error)

func (*Spec) GenerateOASYaml

func (s *Spec) GenerateOASYaml(version OASVersion) ([]byte, error)

func (*Spec) GetPathID added in v0.2.0

func (s *Spec) GetPathID(path string, specSource SpecSource) (string, error)

func (*Spec) HasApprovedSpec

func (s *Spec) HasApprovedSpec() bool

func (*Spec) HasProvidedSpec

func (s *Spec) HasProvidedSpec() bool

func (*Spec) LearnTelemetry

func (s *Spec) LearnTelemetry(telemetry *Telemetry) error

func (*Spec) LoadProvidedSpec

func (s *Spec) LoadProvidedSpec(providedSpec []byte, pathToPathID map[string]string) error

func (*Spec) SpecInfoClone

func (s *Spec) SpecInfoClone() (*Spec, error)

func (*Spec) UnsetApprovedSpec

func (s *Spec) UnsetApprovedSpec()

func (*Spec) UnsetProvidedSpec

func (s *Spec) UnsetProvidedSpec()

type SpecInfo

type SpecInfo struct {
	// Host of the spec
	Host string

	Port string
	// Spec ID
	ID uuid.UUID
	// Provided Spec
	ProvidedSpec *ProvidedSpec
	// Merged & approved state (can be generated into spec YAML)
	ApprovedSpec *ApprovedSpec
	// Upon learning, this will be updated (not the ApprovedSpec field)
	LearningSpec *LearningSpec

	ApprovedPathTrie pathtrie.PathTrie
	ProvidedPathTrie pathtrie.PathTrie
}

type SpecSource added in v0.2.0

type SpecSource string
const (
	SpecSourceReconstructed SpecSource = "RECONSTRUCTED"
	SpecSourceProvided      SpecSource = "PROVIDED"
)

type SuggestedSpecReview

type SuggestedSpecReview struct {
	PathItemsReview []*SuggestedSpecReviewPathItem
	PathToPathItem  map[string]*oapi_spec.PathItem
}

type SuggestedSpecReviewPathItem

type SuggestedSpecReviewPathItem struct {
	ReviewPathItem
}

type Telemetry

type Telemetry struct {
	DestinationAddress   string    `json:"destinationAddress,omitempty"`
	DestinationNamespace string    `json:"destinationNamespace,omitempty"`
	Request              *Request  `json:"request,omitempty"`
	RequestID            string    `json:"requestID,omitempty"`
	Response             *Response `json:"response,omitempty"`
	Scheme               string    `json:"scheme,omitempty"`
	SourceAddress        string    `json:"sourceAddress,omitempty"`
}

type TestOperation

type TestOperation struct {
	Op *oapi_spec.Operation
}

func NewOperation

func NewOperation(t *testing.T, data *HTTPInteractionData) *TestOperation

func (*TestOperation) Deprecated

func (op *TestOperation) Deprecated() *TestOperation

func (*TestOperation) WithParameter added in v0.1.0

func (op *TestOperation) WithParameter(param *oapi_spec.Parameter) *TestOperation

func (*TestOperation) WithRequestBody added in v0.1.0

func (op *TestOperation) WithRequestBody(requestBody *oapi_spec.RequestBody) *TestOperation

func (*TestOperation) WithResponse added in v0.1.0

func (op *TestOperation) WithResponse(status int, response *oapi_spec.Response) *TestOperation

func (*TestOperation) WithSecurityRequirement added in v0.1.0

func (op *TestOperation) WithSecurityRequirement(securityRequirement oapi_spec.SecurityRequirement) *TestOperation

type TestPathItem

type TestPathItem struct {
	PathItem oapi_spec.PathItem
}

func NewTestPathItem

func NewTestPathItem() *TestPathItem

func (*TestPathItem) WithOperation

func (t *TestPathItem) WithOperation(method string, op *oapi_spec.Operation) *TestPathItem

func (*TestPathItem) WithPathParams

func (t *TestPathItem) WithPathParams(name string, schema *oapi_spec.Schema) *TestPathItem

type TestResponse added in v0.1.0

type TestResponse struct {
	*oapi_spec.Response
}

func (*TestResponse) WithHeader added in v0.1.0

func (r *TestResponse) WithHeader(name string, schema *oapi_spec.Schema) *TestResponse

func (*TestResponse) WithJSONSchema added in v0.1.0

func (r *TestResponse) WithJSONSchema(schema *oapi_spec.Schema) *TestResponse

type TestResponses added in v0.1.0

type TestResponses struct {
	oapi_spec.Responses
}

func (*TestResponses) WithResponse added in v0.1.0

func (r *TestResponses) WithResponse(code string, response *oapi_spec.Response) *TestResponses

type TestSpec

type TestSpec struct {
	Doc *oapi_spec.T
}

func (*TestSpec) WithPathItem

func (t *TestSpec) WithPathItem(path string, pathItem *oapi_spec.PathItem) *TestSpec

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL