From 4135e32ea43cdc8955a139ea9c172d3a50ddd624 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 25 Sep 2023 07:41:47 +0300 Subject: [PATCH] chore: commit generated files --- .../test_security/oas_client_gen.go | 107 ++++++++++++++ .../test_security/oas_handlers_gen.go | 133 ++++++++++++++++++ .../oas_response_decoders_gen.go | 9 ++ .../oas_response_encoders_gen.go | 7 + .../test_security/oas_router_gen.go | 40 ++++++ .../test_security/oas_schemas_gen.go | 3 + .../test_security/oas_security_gen.go | 20 +++ .../test_security/oas_server_gen.go | 4 + .../test_security/oas_unimplemented_gen.go | 7 + 9 files changed, 330 insertions(+) diff --git a/internal/integration/test_security/oas_client_gen.go b/internal/integration/test_security/oas_client_gen.go index 6d25b9edc..561f28620 100644 --- a/internal/integration/test_security/oas_client_gen.go +++ b/internal/integration/test_security/oas_client_gen.go @@ -23,6 +23,10 @@ import ( // Invoker invokes operations described by OpenAPI v3 specification. type Invoker interface { + // CustomSecurity invokes customSecurity operation. + // + // GET /customSecurity + CustomSecurity(ctx context.Context) error // DisjointSecurity invokes disjointSecurity operation. // // GET /disjointSecurity @@ -87,6 +91,109 @@ func (c *Client) requestURL(ctx context.Context) *url.URL { return u } +// CustomSecurity invokes customSecurity operation. +// +// GET /customSecurity +func (c *Client) CustomSecurity(ctx context.Context) error { + _, err := c.sendCustomSecurity(ctx) + return err +} + +func (c *Client) sendCustomSecurity(ctx context.Context) (res *CustomSecurityOK, err error) { + otelAttrs := []attribute.KeyValue{ + otelogen.OperationID("customSecurity"), + semconv.HTTPMethodKey.String("GET"), + semconv.HTTPRouteKey.String("/customSecurity"), + } + + // Run stopwatch. + startTime := time.Now() + defer func() { + // Use floating point division here for higher precision (instead of Millisecond method). + elapsedDuration := time.Since(startTime) + c.duration.Record(ctx, float64(float64(elapsedDuration)/float64(time.Millisecond)), metric.WithAttributes(otelAttrs...)) + }() + + // Increment request counter. + c.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + + // Start a span for this request. + ctx, span := c.cfg.Tracer.Start(ctx, "CustomSecurity", + trace.WithAttributes(otelAttrs...), + clientSpanKind, + ) + // Track stage for error reporting. + var stage string + defer func() { + if err != nil { + span.RecordError(err) + span.SetStatus(codes.Error, stage) + c.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + } + span.End() + }() + + stage = "BuildURL" + u := uri.Clone(c.requestURL(ctx)) + var pathParts [1]string + pathParts[0] = "/customSecurity" + uri.AddPathParts(u, pathParts[:]...) + + stage = "EncodeRequest" + r, err := ht.NewRequest(ctx, "GET", u) + if err != nil { + return res, errors.Wrap(err, "create request") + } + + { + type bitset = [1]uint8 + var satisfied bitset + { + stage = "Security:Custom" + switch err := c.securityCustom(ctx, "CustomSecurity", r); { + case err == nil: // if NO error + satisfied[0] |= 1 << 0 + case errors.Is(err, ogenerrors.ErrSkipClientSecurity): + // Skip this security. + default: + return res, errors.Wrap(err, "security \"Custom\"") + } + } + + if ok := func() bool { + nextRequirement: + for _, requirement := range []bitset{ + {0b00000001}, + } { + for i, mask := range requirement { + if satisfied[i]&mask != mask { + continue nextRequirement + } + } + return true + } + return false + }(); !ok { + return res, ogenerrors.ErrSecurityRequirementIsNotSatisfied + } + } + + stage = "SendRequest" + resp, err := c.cfg.Client.Do(r) + if err != nil { + return res, errors.Wrap(err, "do request") + } + defer resp.Body.Close() + + stage = "DecodeResponse" + result, err := decodeCustomSecurityResponse(resp) + if err != nil { + return res, errors.Wrap(err, "decode response") + } + + return result, nil +} + // DisjointSecurity invokes disjointSecurity operation. // // GET /disjointSecurity diff --git a/internal/integration/test_security/oas_handlers_gen.go b/internal/integration/test_security/oas_handlers_gen.go index e61cd9bab..e46bdd57d 100644 --- a/internal/integration/test_security/oas_handlers_gen.go +++ b/internal/integration/test_security/oas_handlers_gen.go @@ -20,6 +20,139 @@ import ( "github.com/ogen-go/ogen/otelogen" ) +// handleCustomSecurityRequest handles customSecurity operation. +// +// GET /customSecurity +func (s *Server) handleCustomSecurityRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) { + otelAttrs := []attribute.KeyValue{ + otelogen.OperationID("customSecurity"), + semconv.HTTPMethodKey.String("GET"), + semconv.HTTPRouteKey.String("/customSecurity"), + } + + // Start a span for this request. + ctx, span := s.cfg.Tracer.Start(r.Context(), "CustomSecurity", + trace.WithAttributes(otelAttrs...), + serverSpanKind, + ) + defer span.End() + + // Run stopwatch. + startTime := time.Now() + defer func() { + elapsedDuration := time.Since(startTime) + // Use floating point division here for higher precision (instead of Millisecond method). + s.duration.Record(ctx, float64(float64(elapsedDuration)/float64(time.Millisecond)), metric.WithAttributes(otelAttrs...)) + }() + + // Increment request counter. + s.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + + var ( + recordError = func(stage string, err error) { + span.RecordError(err) + span.SetStatus(codes.Error, stage) + s.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) + } + err error + opErrContext = ogenerrors.OperationContext{ + Name: "CustomSecurity", + ID: "customSecurity", + } + ) + { + type bitset = [1]uint8 + var satisfied bitset + { + sctx, ok, err := s.securityCustom(ctx, "CustomSecurity", r) + if err != nil { + err = &ogenerrors.SecurityError{ + OperationContext: opErrContext, + Security: "Custom", + Err: err, + } + recordError("Security:Custom", err) + s.cfg.ErrorHandler(ctx, w, r, err) + return + } + if ok { + satisfied[0] |= 1 << 0 + ctx = sctx + } + } + + if ok := func() bool { + nextRequirement: + for _, requirement := range []bitset{ + {0b00000001}, + } { + for i, mask := range requirement { + if satisfied[i]&mask != mask { + continue nextRequirement + } + } + return true + } + return false + }(); !ok { + err = &ogenerrors.SecurityError{ + OperationContext: opErrContext, + Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied, + } + recordError("Security", err) + s.cfg.ErrorHandler(ctx, w, r, err) + return + } + } + + var response *CustomSecurityOK + if m := s.cfg.Middleware; m != nil { + mreq := middleware.Request{ + Context: ctx, + OperationName: "CustomSecurity", + OperationSummary: "", + OperationID: "customSecurity", + Body: nil, + Params: middleware.Parameters{}, + Raw: r, + } + + type ( + Request = struct{} + Params = struct{} + Response = *CustomSecurityOK + ) + response, err = middleware.HookMiddleware[ + Request, + Params, + Response, + ]( + m, + mreq, + nil, + func(ctx context.Context, request Request, params Params) (response Response, err error) { + err = s.h.CustomSecurity(ctx) + return response, err + }, + ) + } else { + err = s.h.CustomSecurity(ctx) + } + if err != nil { + recordError("Internal", err) + s.cfg.ErrorHandler(ctx, w, r, err) + return + } + + if err := encodeCustomSecurityResponse(response, w, span); err != nil { + recordError("EncodeResponse", err) + if !errors.Is(err, ht.ErrInternalServerErrorResponse) { + s.cfg.ErrorHandler(ctx, w, r, err) + } + return + } +} + // handleDisjointSecurityRequest handles disjointSecurity operation. // // GET /disjointSecurity diff --git a/internal/integration/test_security/oas_response_decoders_gen.go b/internal/integration/test_security/oas_response_decoders_gen.go index 440bf4b0e..4da9041cb 100644 --- a/internal/integration/test_security/oas_response_decoders_gen.go +++ b/internal/integration/test_security/oas_response_decoders_gen.go @@ -8,6 +8,15 @@ import ( "github.com/ogen-go/ogen/validate" ) +func decodeCustomSecurityResponse(resp *http.Response) (res *CustomSecurityOK, _ error) { + switch resp.StatusCode { + case 200: + // Code 200. + return &CustomSecurityOK{}, nil + } + return res, validate.UnexpectedStatusCode(resp.StatusCode) +} + func decodeDisjointSecurityResponse(resp *http.Response) (res *DisjointSecurityOK, _ error) { switch resp.StatusCode { case 200: diff --git a/internal/integration/test_security/oas_response_encoders_gen.go b/internal/integration/test_security/oas_response_encoders_gen.go index 547bd716f..369e0ca78 100644 --- a/internal/integration/test_security/oas_response_encoders_gen.go +++ b/internal/integration/test_security/oas_response_encoders_gen.go @@ -9,6 +9,13 @@ import ( "go.opentelemetry.io/otel/trace" ) +func encodeCustomSecurityResponse(response *CustomSecurityOK, w http.ResponseWriter, span trace.Span) error { + w.WriteHeader(200) + span.SetStatus(codes.Ok, http.StatusText(200)) + + return nil +} + func encodeDisjointSecurityResponse(response *DisjointSecurityOK, w http.ResponseWriter, span trace.Span) error { w.WriteHeader(200) span.SetStatus(codes.Ok, http.StatusText(200)) diff --git a/internal/integration/test_security/oas_router_gen.go b/internal/integration/test_security/oas_router_gen.go index 45326b85d..6049d712a 100644 --- a/internal/integration/test_security/oas_router_gen.go +++ b/internal/integration/test_security/oas_router_gen.go @@ -59,6 +59,24 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { break } switch elem[0] { + case 'c': // Prefix: "customSecurity" + if l := len("customSecurity"); len(elem) >= l && elem[0:l] == "customSecurity" { + elem = elem[l:] + } else { + break + } + + if len(elem) == 0 { + // Leaf node. + switch r.Method { + case "GET": + s.handleCustomSecurityRequest([0]string{}, elemIsEscaped, w, r) + default: + s.notAllowed(w, r, "GET") + } + + return + } case 'd': // Prefix: "disjointSecurity" if l := len("disjointSecurity"); len(elem) >= l && elem[0:l] == "disjointSecurity" { elem = elem[l:] @@ -205,6 +223,28 @@ func (s *Server) FindPath(method string, u *url.URL) (r Route, _ bool) { break } switch elem[0] { + case 'c': // Prefix: "customSecurity" + if l := len("customSecurity"); len(elem) >= l && elem[0:l] == "customSecurity" { + elem = elem[l:] + } else { + break + } + + if len(elem) == 0 { + switch method { + case "GET": + // Leaf: CustomSecurity + r.name = "CustomSecurity" + r.summary = "" + r.operationID = "customSecurity" + r.pathPattern = "/customSecurity" + r.args = args + r.count = 0 + return r, true + default: + return + } + } case 'd': // Prefix: "disjointSecurity" if l := len("disjointSecurity"); len(elem) >= l && elem[0:l] == "disjointSecurity" { elem = elem[l:] diff --git a/internal/integration/test_security/oas_schemas_gen.go b/internal/integration/test_security/oas_schemas_gen.go index 808a1f528..fb9df6c00 100644 --- a/internal/integration/test_security/oas_schemas_gen.go +++ b/internal/integration/test_security/oas_schemas_gen.go @@ -55,6 +55,9 @@ func (s *CookieKey) SetAPIKey(val string) { s.APIKey = val } +// CustomSecurityOK is response for CustomSecurity operation. +type CustomSecurityOK struct{} + // DisjointSecurityOK is response for DisjointSecurity operation. type DisjointSecurityOK struct{} diff --git a/internal/integration/test_security/oas_security_gen.go b/internal/integration/test_security/oas_security_gen.go index da70a1412..f8992df99 100644 --- a/internal/integration/test_security/oas_security_gen.go +++ b/internal/integration/test_security/oas_security_gen.go @@ -20,6 +20,8 @@ type SecurityHandler interface { HandleBearerToken(ctx context.Context, operationName string, t BearerToken) (context.Context, error) // HandleCookieKey handles cookieKey security. HandleCookieKey(ctx context.Context, operationName string, t CookieKey) (context.Context, error) + // HandleCustom handles custom security. + HandleCustom(ctx context.Context, operationName string, req *http.Request) (context.Context, error) // HandleHeaderKey handles headerKey security. HandleHeaderKey(ctx context.Context, operationName string, t HeaderKey) (context.Context, error) // HandleQueryKey handles queryKey security. @@ -96,6 +98,16 @@ func (s *Server) securityCookieKey(ctx context.Context, operationName string, re } return rctx, true, err } +func (s *Server) securityCustom(ctx context.Context, operationName string, req *http.Request) (context.Context, bool, error) { + t := req + rctx, err := s.sec.HandleCustom(ctx, operationName, t) + if errors.Is(err, ogenerrors.ErrSkipServerSecurity) { + return nil, false, nil + } else if err != nil { + return nil, false, err + } + return rctx, true, err +} func (s *Server) securityHeaderKey(ctx context.Context, operationName string, req *http.Request) (context.Context, bool, error) { var t HeaderKey const parameterName = "X-Api-Key" @@ -138,6 +150,8 @@ type SecuritySource interface { BearerToken(ctx context.Context, operationName string) (BearerToken, error) // CookieKey provides cookieKey security value. CookieKey(ctx context.Context, operationName string) (CookieKey, error) + // Custom provides custom security value. + Custom(ctx context.Context, operationName string, req *http.Request) error // HeaderKey provides headerKey security value. HeaderKey(ctx context.Context, operationName string) (HeaderKey, error) // QueryKey provides queryKey security value. @@ -171,6 +185,12 @@ func (s *Client) securityCookieKey(ctx context.Context, operationName string, re }) return nil } +func (s *Client) securityCustom(ctx context.Context, operationName string, req *http.Request) error { + if err := s.sec.Custom(ctx, operationName, req); err != nil { + return errors.Wrap(err, "security source \"Custom\"") + } + return nil +} func (s *Client) securityHeaderKey(ctx context.Context, operationName string, req *http.Request) error { t, err := s.sec.HeaderKey(ctx, operationName) if err != nil { diff --git a/internal/integration/test_security/oas_server_gen.go b/internal/integration/test_security/oas_server_gen.go index dc189325a..02974740c 100644 --- a/internal/integration/test_security/oas_server_gen.go +++ b/internal/integration/test_security/oas_server_gen.go @@ -8,6 +8,10 @@ import ( // Handler handles operations described by OpenAPI v3 specification. type Handler interface { + // CustomSecurity implements customSecurity operation. + // + // GET /customSecurity + CustomSecurity(ctx context.Context) error // DisjointSecurity implements disjointSecurity operation. // // GET /disjointSecurity diff --git a/internal/integration/test_security/oas_unimplemented_gen.go b/internal/integration/test_security/oas_unimplemented_gen.go index 0336bbba9..06dc18c8b 100644 --- a/internal/integration/test_security/oas_unimplemented_gen.go +++ b/internal/integration/test_security/oas_unimplemented_gen.go @@ -13,6 +13,13 @@ type UnimplementedHandler struct{} var _ Handler = UnimplementedHandler{} +// CustomSecurity implements customSecurity operation. +// +// GET /customSecurity +func (UnimplementedHandler) CustomSecurity(ctx context.Context) error { + return ht.ErrNotImplemented +} + // DisjointSecurity implements disjointSecurity operation. // // GET /disjointSecurity