-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix partnerID transferring to WRP message feature (#81)
* Add logic to fill out partnerID in wrp msg * Pass JWT partnerIDs to WRP message * bascule already provides the clientID * actually perform logging * update changelog * use defined variable * Add empty partnerID validator * update changelog * Update changelog to trigger next release
- Loading branch information
Showing
6 changed files
with
147 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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,34 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
"github.com/xmidt-org/bascule" | ||
) | ||
|
||
type allowedResources struct { | ||
AllowedPartners []string | ||
} | ||
|
||
type claims struct { | ||
AllowedResources allowedResources | ||
} | ||
|
||
var requirePartnerIDs bascule.ValidatorFunc = func(_ context.Context, token bascule.Token) error { | ||
var claims claims | ||
|
||
err := mapstructure.Decode(token.Attributes(), &claims) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Unexpected JWT claim format for partnerIDs: %v", err) | ||
} | ||
|
||
if len(claims.AllowedResources.AllowedPartners) < 1 { | ||
return errors.New("JWT must provide claims for partnerIDs") | ||
} | ||
|
||
return nil | ||
} |
This file contains 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,54 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xmidt-org/bascule" | ||
) | ||
|
||
func TestRequirePartnerIDs(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
attributes bascule.Attributes | ||
shouldPass bool | ||
}{ | ||
{ | ||
name: "partnerIDs", | ||
attributes: map[string]interface{}{ | ||
"allowedResources": map[string]interface{}{ | ||
"allowedPartners": []string{"partner0", "partner1"}, | ||
}}, | ||
shouldPass: true, | ||
}, | ||
|
||
{ | ||
name: "no partnerIDs", | ||
attributes: nil, | ||
}, | ||
{ | ||
name: "malformed partnerIDs field", | ||
attributes: map[string]interface{}{ | ||
"allowedResources": map[string]interface{}{ | ||
"allowedPartners": "partner0", | ||
}}, | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
token := bascule.NewToken("bearer", "client0", test.attributes) | ||
|
||
err := requirePartnerIDs(ctx, token) | ||
if test.shouldPass { | ||
assert.Nil(err) | ||
} else { | ||
assert.NotNil(err) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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 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 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,43 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/xmidt-org/bascule" | ||
"github.com/xmidt-org/webpa-common/logging" | ||
"github.com/xmidt-org/wrp-go/wrp" | ||
"testing" | ||
) | ||
|
||
func TestPopulateMessagePartners(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
attributes bascule.Attributes | ||
expectedPartnerIDs []string | ||
}{ | ||
{ | ||
name: "partnerIDs", | ||
attributes: map[string]interface{}{ | ||
"allowedResources": map[string]interface{}{ | ||
"allowedPartners": []string{"partner0", "partner1"}, | ||
}}, | ||
expectedPartnerIDs: []string{"partner0", "partner1"}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
auth := bascule.Authentication{ | ||
Token: bascule.NewToken("bearer", "client0", test.attributes), | ||
} | ||
|
||
ctx := bascule.WithAuthentication(context.Background(), auth) | ||
|
||
wrpMsg := new(wrp.Message) | ||
populateMessage(ctx, wrpMsg, logging.DefaultLogger()) | ||
assert.Equal(test.expectedPartnerIDs, wrpMsg.PartnerIDs) | ||
}) | ||
} | ||
} |