From 469e57b2a585eeb34a06e30b8977a9a8c7365c9b Mon Sep 17 00:00:00 2001 From: Bruno Bressi Date: Mon, 16 Oct 2023 09:33:13 +0200 Subject: [PATCH] chore: spelling corrections in api --- api/api.go | 1 + api/auth.go | 55 +++++++++++++++++++++++++-------------------------- api/server.go | 14 ++++++------- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/api/api.go b/api/api.go index dcb7b22..d0955ba 100644 --- a/api/api.go +++ b/api/api.go @@ -55,6 +55,7 @@ import ( "google.golang.org/protobuf/encoding/protojson" ) +// StartApi starts the API server of the canary func StartApi(data data.Database, metrics metric.Metrics, config *Configuration, log *zap.SugaredLogger) error { a := &Api{ data: data, diff --git a/api/auth.go b/api/auth.go index 49a862a..02c99b9 100644 --- a/api/auth.go +++ b/api/auth.go @@ -30,7 +30,7 @@ import ( connect "github.com/bufbuild/connect-go" ) -// http auth handler +// NewAuthHandler returns a handler for HTTP authorization func (a *Api) NewAuthHandler(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { splitToken := strings.Split(r.Header.Get("Authorization"), "Bearer") @@ -58,37 +58,36 @@ func (a *Api) NewAuthHandler(h http.Handler) http.Handler { }) } -// grpc auth interceptor +// NewAuthInterceptor returns grpc auth interceptor to handle authorization func (a *Api) NewAuthInterceptor() connect.UnaryInterceptorFunc { interceptor := func(next connect.UnaryFunc) connect.UnaryFunc { - return connect.UnaryFunc( - func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) { - splitToken := strings.Split(req.Header().Get("Authorization"), "Bearer") - // check if token is set - if len(splitToken) != 2 { - a.log.Warnw("Request", "host", req.Header().Get("X-Forwarded-Host"), "auth", "failed", "reason", "no bearer token") - return nil, connect.NewError( - connect.CodeUnauthenticated, - errors.New("no token provided"), - ) - } - - // get token - authToken := strings.TrimSpace(splitToken[1]) - - // check if token is correct - for _, t := range a.config.Tokens { - if authToken == t { - a.log.Infow("Request", "host", req.Header().Get("X-Forwarded-Host"), "auth", "succeded") - return next(ctx, req) - } - } - a.log.Warnw("Request", "host", req.Header().Get("X-Forwarded-Host"), "auth", "failed", "reason", "invalid token") + return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) { + splitToken := strings.Split(req.Header().Get("Authorization"), "Bearer") + // check if token is set + if len(splitToken) != 2 { + a.log.Warnw("Request", "host", req.Header().Get("X-Forwarded-Host"), "auth", "failed", "reason", "no bearer token") return nil, connect.NewError( connect.CodeUnauthenticated, - errors.New("auth failed"), + errors.New("no token provided"), ) - }) + } + + // get token + authToken := strings.TrimSpace(splitToken[1]) + + // check if token is correct + for _, t := range a.config.Tokens { + if authToken == t { + a.log.Infow("Request", "host", req.Header().Get("X-Forwarded-Host"), "auth", "succeded") + return next(ctx, req) + } + } + a.log.Warnw("Request", "host", req.Header().Get("X-Forwarded-Host"), "auth", "failed", "reason", "invalid token") + return nil, connect.NewError( + connect.CodeUnauthenticated, + errors.New("auth failed"), + ) + } } - return connect.UnaryInterceptorFunc(interceptor) + return interceptor } diff --git a/api/server.go b/api/server.go index 677db0f..10f5dc3 100644 --- a/api/server.go +++ b/api/server.go @@ -56,11 +56,11 @@ type Configuration struct { CaCert []byte } -// List all measured samples -func (b *Api) ListSamples(ctx context.Context, req *connect.Request[apiv1.ListSampleRequest]) (*connect.Response[apiv1.ListSampleResponse], error) { +// ListSamples lists all measured samples of the canary +func (a *Api) ListSamples(ctx context.Context, req *connect.Request[apiv1.ListSampleRequest]) (*connect.Response[apiv1.ListSampleResponse], error) { samples := []*apiv1.Sample{} - for _, sample := range b.data.GetSampleList() { + for _, sample := range a.data.GetSampleList() { samples = append(samples, &apiv1.Sample{ From: sample.From, To: sample.To, @@ -75,11 +75,11 @@ func (b *Api) ListSamples(ctx context.Context, req *connect.Request[apiv1.ListSa }), nil } -// List all known nodes in mesh -func (b *Api) ListNodes(ctx context.Context, req *connect.Request[apiv1.ListNodesRequest]) (*connect.Response[apiv1.ListNodesResponse], error) { - nodes := []string{b.config.NodeName} +// ListNodes lists all known nodes in mesh +func (a *Api) ListNodes(ctx context.Context, req *connect.Request[apiv1.ListNodesRequest]) (*connect.Response[apiv1.ListNodesResponse], error) { + nodes := []string{a.config.NodeName} - for _, node := range b.data.GetNodeList() { + for _, node := range a.data.GetNodeList() { nodes = append(nodes, node.Name) }