Skip to content

Commit

Permalink
chore: spelling corrections in api
Browse files Browse the repository at this point in the history
  • Loading branch information
puffitos committed Oct 16, 2023
1 parent 5da36b0 commit 469e57b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
55 changes: 27 additions & 28 deletions api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}

Expand Down

0 comments on commit 469e57b

Please sign in to comment.