Skip to content

Commit

Permalink
Merge pull request #6346 from TheThingsNetwork/fix/pba-aud-from-address
Browse files Browse the repository at this point in the history
Fix Packet Broker audience from dial address
  • Loading branch information
johanstokking authored Jul 3, 2023
2 parents 373b2cf + 58cbd4e commit 79a095f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ For details about compatibility between different releases, see the **Commitment
- HTTP API routes for parsing QR codes for the QR Generator service. We exercise our right to break compatibility with third party HTTP clients since this is a bug.
- `/qr-code/end-devices/parse` is changed to `/qr-codes/end-devices/parse`.
- `/qr-code/end-devices/{format_id}/parse` is changed to `/qr-codes/end-devices/{format_id}/parse`.
- Fixed authenticating with Packet Broker when gRPC dialer schemes are used in the address.

## [3.26.1] - 2023-06-20

Expand Down
7 changes: 6 additions & 1 deletion pkg/packetbroker/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func WithAudienceFromAddresses(addresses ...string) TokenOption {
if addr == "" {
continue
}
// If the address is a URL with a scheme, check if it's a gRPC dialer scheme and remove it.
// gRPC dialer schemes in the target address look like passthrough:///host:port.
if u, err := url.Parse(addr); err == nil && u.Scheme != "" && strings.HasPrefix(addr, u.Scheme+":///") {
addr = addr[len(u.Scheme)+4:]
}
if h, _, err := net.SplitHostPort(addr); err == nil {
addr = h
}
Expand Down Expand Up @@ -110,7 +115,7 @@ func TokenSource(ctx context.Context, clientID, clientSecret string, opts ...Tok
return config.TokenSource(ctx)
}

// TokenNetworkClaims defines a Packet Broker network identifier.
// TokenNetworkClaim defines a Packet Broker network identifier.
type TokenNetworkClaim struct {
NetID uint32 `json:"nid"`
TenantID string `json:"tid"`
Expand Down
33 changes: 33 additions & 0 deletions pkg/packetbroker/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,39 @@ func TestToken(t *testing.T) {
return a.So(claims.PacketBroker.Cluster, should.BeFalse)
},
},
{
name: "SuccessWithDialerScheme",
clientID: "test",
clientSecret: "secret",
opts: []packetbroker.TokenOption{
packetbroker.WithScope(packetbroker.ScopeNetworks),
packetbroker.WithAudienceFromAddresses("passthrough:///iam.packetbroker.net:443"),
},
tokenRequestAssertion: func(a *assertions.Assertion, vars url.Values) bool {
return a.So(vars["scope"], should.Resemble, []string{"networks"}) &&
a.So(vars["audience"], should.Resemble, []string{"iam.packetbroker.net"})
},
tokenClaims: func() packetbroker.IAMTokenClaims {
return packetbroker.IAMTokenClaims{
Networks: []packetbroker.TokenNetworkClaim{
{
NetID: 0x000013,
TenantID: "ttn",
},
},
}
},
audience: "iam.packetbroker.net",
tokenAssertion: func(a *assertions.Assertion, token string) bool {
id, err := packetbroker.UnverifiedNetworkIdentifier(token)
return a.So(err, should.BeNil) &&
a.So(id.NetId, should.Equal, 0x000013) &&
a.So(id.TenantId, should.Equal, "ttn")
},
tokenClaimsAssertion: func(a *assertions.Assertion, claims packetbroker.TokenClaims) bool {
return a.So(claims.PacketBroker.Cluster, should.BeFalse)
},
},
{
name: "BadRequest",
clientID: "test",
Expand Down

0 comments on commit 79a095f

Please sign in to comment.