From ba4cc2ff89ed000d8a528ca659c1eb7b24e78776 Mon Sep 17 00:00:00 2001 From: "felix.gateru" Date: Fri, 20 Oct 2023 06:25:56 +0300 Subject: [PATCH] Clean up Signed-off-by: felix.gateru --- coap/handler.go | 245 ----------------------------------- docker/nginx/nginx-key.conf | 2 +- docker/nginx/nginx-x509.conf | 2 +- 3 files changed, 2 insertions(+), 247 deletions(-) delete mode 100644 coap/handler.go diff --git a/coap/handler.go b/coap/handler.go deleted file mode 100644 index 57da644fd8c..00000000000 --- a/coap/handler.go +++ /dev/null @@ -1,245 +0,0 @@ -// Copyright (c) Mainflux -// SPDX-License-Identifier: Apache-2.0 - -package coap - -import ( - "context" - "fmt" - "net/url" - "regexp" - "strings" - "time" - - "github.com/mainflux/mainflux/logger" - "github.com/mainflux/mainflux/pkg/errors" - "github.com/mainflux/mainflux/pkg/messaging" - "github.com/mainflux/mainflux/things/policies" - "github.com/mainflux/mproxy/pkg/session" -) - -var _ session.Handler = (*handler)(nil) - -const protocol = "coap" - -// Log message formats. -const ( - LogInfoSubscribed = "subscribed with client_id %s to topics %s" - LogInfoUnsubscribed = "unsubscribed client_id %s from topics %s" - LogInfoConnected = "connected with client_id %s" - LogInfoDisconnected = "disconnected client_id %s and username %s" - LogInfoPublished = "published with client_id %s to the topic %s" -) - -// Error wrappers for handler errors. -var ( - ErrMalformedSubtopic = errors.New("malformed subtopic") - ErrClientNotInitialized = errors.New("client is not initialized") - ErrMalformedTopic = errors.New("malformed topic") - ErrMissingClientID = errors.New("client_id not found") - ErrMissingTopicPub = errors.New("failed to publish due to missing topic") - ErrMissingTopicSub = errors.New("failed to subscribe due to missing topic") - ErrFailedConnect = errors.New("failed to connect") - ErrFailedSubscribe = errors.New("failed to subscribe") - ErrFailedUnsubscribe = errors.New("failed to unsubscribe") - ErrFailedPublish = errors.New("failed to publish") - ErrFailedDisconnect = errors.New("failed to disconnect") - ErrFailedPublishDisconnectEvent = errors.New("failed to publish disconnect event") - ErrFailedParseSubtopic = errors.New("failed to parse subtopic") - ErrFailedPublishConnectEvent = errors.New("failed to publish connect event") - ErrFailedPublishToMsgBroker = errors.New("failed to publish to mainflux message broker") - authQuery = "auth" -) - -var channelRegExp = regexp.MustCompile(`^\/?channels\/([\w\-]+)\/messages(\/[^?]*)?(\?.*)?$`) - -// Event implements events.Event interface. -type handler struct { - pubsub messaging.PubSub - auth policies.AuthServiceClient - logger logger.Logger -} - -// NewHandler creates new Handler entity. -func NewHandler(pubsub messaging.PubSub, logger logger.Logger, auth policies.AuthServiceClient) session.Handler { - return &handler{ - logger: logger, - pubsub: pubsub, - auth: auth, - } -} - -// AuthConnect is called on device connection, -// prior forwarding to the coap server. -func (h *handler) AuthConnect(ctx context.Context) error { - return nil -} - -// AuthPublish is called on device publish, -// prior forwarding to the coap server. -func (h *handler) AuthPublish(ctx context.Context, topic *string, payload *[]byte) error { - if topic == nil { - return ErrMissingTopicPub - } - s, ok := session.FromContext(ctx) - if !ok { - return ErrClientNotInitialized - } - - return h.authAccess(ctx, string(s.Password), *topic, policies.WriteAction) -} - -// AuthSubscribe is called on device publish, -// prior forwarding to the coap server. -func (h *handler) AuthSubscribe(ctx context.Context, topics *[]string) error { - s, ok := session.FromContext(ctx) - if !ok { - return ErrClientNotInitialized - } - if topics == nil || *topics == nil { - return ErrMissingTopicSub - } - - for _, v := range *topics { - if err := h.authAccess(ctx, string(s.Password), v, policies.ReadAction); err != nil { - return err - } - } - - return nil -} - -// Connect - after client successfully connected. -func (h *handler) Connect(ctx context.Context) error { - return nil -} - -// Publish - after client successfully published. -func (h *handler) Publish(ctx context.Context, topic *string, payload *[]byte) error { - s, ok := session.FromContext(ctx) - if !ok { - return errors.Wrap(ErrFailedPublish, ErrClientNotInitialized) - } - h.logger.Info(fmt.Sprintf(LogInfoPublished, s.ID, *topic)) - // Topics are in the format: - // channels//messages//.../ct/ - - channelParts := channelRegExp.FindStringSubmatch(*topic) - if len(channelParts) < 2 { - return errors.Wrap(ErrFailedPublish, ErrMalformedTopic) - } - - chanID := channelParts[1] - subtopic := channelParts[2] - - subtopic, err := parseSubtopic(subtopic) - if err != nil { - return errors.Wrap(ErrFailedParseSubtopic, err) - } - - msg := messaging.Message{ - Protocol: protocol, - Channel: chanID, - Subtopic: subtopic, - Publisher: s.Username, - Payload: *payload, - Created: time.Now().UnixNano(), - } - - if err := h.pubsub.Publish(ctx, msg.Channel, &msg); err != nil { - return errors.Wrap(ErrFailedPublishToMsgBroker, err) - } - return nil -} - -// Subscribe - after client successfully subscribed. -func (h *handler) Subscribe(ctx context.Context, topics *[]string) error { - s, ok := session.FromContext(ctx) - if !ok { - return errors.Wrap(ErrFailedSubscribe, ErrClientNotInitialized) - } - h.logger.Info(fmt.Sprintf(LogInfoSubscribed, s.ID, strings.Join(*topics, ","))) - return nil -} - -// Unsubscribe - after client unsubscribed. -func (h *handler) Unsubscribe(ctx context.Context, topics *[]string) error { - return nil -} - -// Disconnect - connection with broker or client lost. -func (h *handler) Disconnect(ctx context.Context) error { - return nil -} - -func (h *handler) authAccess(ctx context.Context, password, topic, action string) error { - password, err := parseKey(password) - if err != nil { - return err - } - // Topics are in the format: - // channels//messages//.../ct/ - if !channelRegExp.Match([]byte(topic)) { - return ErrMalformedTopic - } - - channelParts := channelRegExp.FindStringSubmatch(topic) - if len(channelParts) < 1 { - return ErrMalformedTopic - } - - chanID := channelParts[1] - - ar := &policies.AuthorizeReq{ - Subject: password, - Object: chanID, - Action: action, - EntityType: policies.ThingEntityType, - } - res, err := h.auth.Authorize(ctx, ar) - if err != nil { - return err - } - if !res.GetAuthorized() { - return errors.ErrAuthorization - } - - return nil -} - -func parseSubtopic(subtopic string) (string, error) { - if subtopic == "" { - return subtopic, nil - } - - subtopic, err := url.QueryUnescape(subtopic) - if err != nil { - return "", ErrMalformedSubtopic - } - subtopic = strings.ReplaceAll(subtopic, "/", ".") - - elems := strings.Split(subtopic, ".") - filteredElems := []string{} - for _, elem := range elems { - if elem == "" { - continue - } - - if len(elem) > 1 && (strings.Contains(elem, "*") || strings.Contains(elem, ">")) { - return "", ErrMalformedSubtopic - } - - filteredElems = append(filteredElems, elem) - } - - subtopic = strings.Join(filteredElems, ".") - return subtopic, nil -} - -func parseKey(password string) (string, error) { - vars := strings.Split(password, "=") - if len(vars) != 2 || vars[0] != authQuery { - return "", errors.ErrAuthorization - } - return vars[1], nil -} diff --git a/docker/nginx/nginx-key.conf b/docker/nginx/nginx-key.conf index c23e9a994c6..e60b6661e1e 100644 --- a/docker/nginx/nginx-key.conf +++ b/docker/nginx/nginx-key.conf @@ -161,7 +161,7 @@ stream { } } -# COAP +# CoAP stream { include snippets/stream_access_log.conf; diff --git a/docker/nginx/nginx-x509.conf b/docker/nginx/nginx-x509.conf index fd48e2462c5..be17e293f64 100644 --- a/docker/nginx/nginx-x509.conf +++ b/docker/nginx/nginx-x509.conf @@ -151,7 +151,7 @@ stream { } } -#COAP +#CoAP stream { include snippets/stream_access_log.conf;