-
Notifications
You must be signed in to change notification settings - Fork 673
SMQ-3137 - Proxy CoAP using mgate #2222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3354e85
feat: use mgate to proxy coap with dtls
felixgateru 771463a
refactor: update with updates to mgate
felixgateru c7e5578
refactor: add health check topic handling
felixgateru b3f8a0a
feat: mount coap dtls certs to container
felixgateru 5b257af
fix(Makefile): add coap_dtls_certs to make all target
felixgateru ff4ffec
refactor: update tracing middleware
felixgateru 1877e91
fix(adapter.go): fix typo
felixgateru 073bf56
refactor(adapter.go): remove channel authz on unsubscribe
felixgateru 330e6b9
chore: bump mgate dependency
felixgateru fcab001
refactor: address comments
felixgateru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| // Copyright (c) Abstract Machines | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package coap | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| mgate "github.com/absmach/mgate/pkg/coap" | ||
| "github.com/absmach/mgate/pkg/session" | ||
| grpcChannelsV1 "github.com/absmach/supermq/api/grpc/channels/v1" | ||
| grpcClientsV1 "github.com/absmach/supermq/api/grpc/clients/v1" | ||
| smqauthn "github.com/absmach/supermq/pkg/authn" | ||
| "github.com/absmach/supermq/pkg/connections" | ||
| "github.com/absmach/supermq/pkg/errors" | ||
| svcerr "github.com/absmach/supermq/pkg/errors/service" | ||
| "github.com/absmach/supermq/pkg/messaging" | ||
| "github.com/absmach/supermq/pkg/policies" | ||
| ) | ||
|
|
||
| var _ session.Handler = (*handler)(nil) | ||
|
|
||
| // Log message formats. | ||
| const ( | ||
| subscribedInfoFmt = "subscribed with client_id %s to topics %s" | ||
| publishedInfoFmt = "published with client_id %s to the topic %s" | ||
| ) | ||
|
|
||
| // Error wrappers for COAP errors. | ||
| var ( | ||
| errClientNotInitialized = errors.New("client is not initialized") | ||
| errMissingTopicPub = errors.New("failed to publish due to missing topic") | ||
| errMissingTopicSub = errors.New("failed to subscribe due to missing topic") | ||
| errFailedPublish = errors.New("failed to publish") | ||
| ) | ||
|
|
||
| type handler struct { | ||
| clients grpcClientsV1.ClientsServiceClient | ||
| channels grpcChannelsV1.ChannelsServiceClient | ||
| logger *slog.Logger | ||
| parser messaging.TopicParser | ||
| } | ||
|
|
||
| // NewHandler creates new Handler entity. | ||
| func NewHandler(logger *slog.Logger, clients grpcClientsV1.ClientsServiceClient, channels grpcChannelsV1.ChannelsServiceClient, parser messaging.TopicParser) session.Handler { | ||
| return &handler{ | ||
| logger: logger, | ||
| clients: clients, | ||
| channels: channels, | ||
| parser: parser, | ||
| } | ||
| } | ||
|
|
||
| // 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 | ||
| } | ||
|
|
||
| domainID, channelID, _, topicType, err := h.parser.ParsePublishTopic(ctx, *topic, true) | ||
| if err != nil { | ||
| return mgate.NewCOAPProxyError(http.StatusBadRequest, errors.Wrap(errFailedPublish, err)) | ||
| } | ||
|
|
||
| clientID, err := h.authAccess(ctx, string(s.Password), domainID, channelID, connections.Publish, topicType) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| s.Username = clientID | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // AuthSubscribe is called on device publish, | ||
| // prior forwarding to the COAP broker. | ||
| 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 _, topic := range *topics { | ||
| domainID, channelID, _, topicType, err := h.parser.ParseSubscribeTopic(ctx, topic, true) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if _, err := h.authAccess(ctx, string(s.Password), domainID, channelID, connections.Subscribe, topicType); 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 errClientNotInitialized | ||
| } | ||
|
|
||
| if len(*payload) == 0 { | ||
| h.logger.Warn("Empty payload, not publishing to broker", slog.String("client_id", s.Username)) | ||
| return nil | ||
| } | ||
|
|
||
| h.logger.Info(fmt.Sprintf(publishedInfoFmt, s.Username, *topic)) | ||
|
|
||
| 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 errClientNotInitialized | ||
| } | ||
| h.logger.Info(fmt.Sprintf(subscribedInfoFmt, s.Username, 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, secret, domainID, chanID string, msgType connections.ConnType, topicType messaging.TopicType) (string, error) { | ||
| authnRes, err := h.clients.Authenticate(ctx, &grpcClientsV1.AuthnReq{Token: smqauthn.AuthPack(smqauthn.DomainAuth, domainID, secret)}) | ||
| if err != nil { | ||
| return "", mgate.NewCOAPProxyError(http.StatusUnauthorized, svcerr.ErrAuthentication) | ||
| } | ||
| if !authnRes.Authenticated { | ||
| return "", mgate.NewCOAPProxyError(http.StatusUnauthorized, svcerr.ErrAuthentication) | ||
| } | ||
|
|
||
| if topicType == messaging.HealthType { | ||
| return authnRes.GetId(), nil | ||
| } | ||
|
|
||
| ar := &grpcChannelsV1.AuthzReq{ | ||
| Type: uint32(msgType), | ||
| ClientId: authnRes.GetId(), | ||
| ClientType: policies.ClientType, | ||
| ChannelId: chanID, | ||
| DomainId: domainID, | ||
| } | ||
| res, err := h.channels.Authorize(ctx, ar) | ||
| if err != nil { | ||
| return "", mgate.NewCOAPProxyError(http.StatusUnauthorized, errors.Wrap(svcerr.ErrAuthentication, err)) | ||
| } | ||
| if !res.GetAuthorized() { | ||
| return "", mgate.NewCOAPProxyError(http.StatusUnauthorized, svcerr.ErrAuthentication) | ||
| } | ||
|
|
||
| return authnRes.GetId(), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to remove the channel authorization while unsubscribing ?
supermq/coap/adapter.go
Lines 116 to 128 in 69b849c