Skip to content

Commit

Permalink
fixed handler for devices with auths, added log (#196)
Browse files Browse the repository at this point in the history
* fixed handler for devices with auths, added log

* updated changelog
  • Loading branch information
kristinapathak committed Dec 8, 2021
1 parent abd48d9 commit 9f2c489
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [v0.6.2]
- Fixed device endpoint bug, added info log for partner IDs and trust. [#196](https://github.com/xmidt-org/talaria/pull/196)

## [v0.6.1]
- Added v2 compatible device endpoint for older devices connecting. [#195](https://github.com/xmidt-org/talaria/pull/195)

Expand Down Expand Up @@ -125,7 +128,8 @@ Switching to new build process
## [v0.1.1] Tue Mar 28 2017 Weston Schmidt - 0.1.1
- initial creation

[Unreleased]: https://github.com/xmidt-org/talaria/compare/v0.6.1...HEAD
[Unreleased]: https://github.com/xmidt-org/talaria/compare/v0.6.2...HEAD
[v0.6.2]: https://github.com/xmidt-org/talaria/compare/v0.6.1...v0.6.2
[v0.6.1]: https://github.com/xmidt-org/talaria/compare/v0.6.0...v0.6.1
[v0.6.0]: https://github.com/xmidt-org/talaria/compare/v0.5.13...v0.6.0
[v0.5.13]: https://github.com/xmidt-org/talaria/compare/v0.5.12...v0.5.13
Expand Down
19 changes: 16 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package main

import (
"context"
"net/http"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/justinas/alice"
"github.com/segmentio/ksuid"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/webpa-common/logging"
"github.com/xmidt-org/webpa-common/v2/device"
)

Expand All @@ -15,10 +20,11 @@ func init() {
// DeviceMetadataMiddleware is a device registration endpoint middleware
// which initializes the metadata a device carries throughout its
// connectivity lifecycle with the XMiDT cluster.
func DeviceMetadataMiddleware(delegate http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
func DeviceMetadataMiddleware(getLogger func(ctx context.Context) log.Logger) alice.Constructor {
return func(delegate http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := getLogger(ctx)
metadata := new(device.Metadata)
metadata.SetSessionID(ksuid.New().String())

Expand All @@ -38,8 +44,15 @@ func DeviceMetadataMiddleware(delegate http.Handler) http.Handler {
metadata.SetClaims(claimsMap)

}
if logger != nil {
level.Info(logger).Log(logging.MessageKey(), "got claims from auth token",
"partner-id", metadata.Claims()[device.PartnerIDClaimKey],
"trust", metadata.Claims()[device.TrustClaimKey],
)
}
}

delegate.ServeHTTP(w, r.WithContext(device.WithDeviceMetadata(ctx, metadata)))
})
}
}
6 changes: 3 additions & 3 deletions primaryHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,18 @@ func NewPrimaryHandler(logger log.Logger, manager device.Manager, v *viper.Viper
}

// the secured variant of the device connect handler - compatible with v2 and v3
apiHandler.Handle(
r.Handle(
fmt.Sprintf("%s/{version:%s|%s}/device", baseURI, v2, version),
deviceConnectChain.
Extend(versionCompatibleAuth).
Append(DeviceMetadataMiddleware).
Append(DeviceMetadataMiddleware(getLogger)).
Then(connectHandler),
).HeadersRegexp("Authorization", ".*")

r.Handle(
fmt.Sprintf("%s/{version:%s|%s}/device", baseURI, v2, version),
deviceConnectChain.
Append(DeviceMetadataMiddleware).
Append(DeviceMetadataMiddleware(getLogger)).
Then(connectHandler),
)

Expand Down

0 comments on commit 9f2c489

Please sign in to comment.