-
Notifications
You must be signed in to change notification settings - Fork 10
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
Improve version checking system #506
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,39 +1,14 @@ | ||||||||||||||||||||||||||||||||||||||||||||
package authn | ||||||||||||||||||||||||||||||||||||||||||||
package authn_test | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||||||||||||||||||||||
"bytes" | ||||||||||||||||||||||||||||||||||||||||||||
"github.com/Masterminds/semver/v3" | ||||||||||||||||||||||||||||||||||||||||||||
"github.com/stretchr/testify/require" | ||||||||||||||||||||||||||||||||||||||||||||
"github.com/xmtp/xmtpd/pkg/authn" | ||||||||||||||||||||||||||||||||||||||||||||
"github.com/xmtp/xmtpd/pkg/registry" | ||||||||||||||||||||||||||||||||||||||||||||
"github.com/xmtp/xmtpd/pkg/testutils" | ||||||||||||||||||||||||||||||||||||||||||||
"os/exec" | ||||||||||||||||||||||||||||||||||||||||||||
"strings" | ||||||||||||||||||||||||||||||||||||||||||||
"testing" | ||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
func getLatestTag(t *testing.T) string { | ||||||||||||||||||||||||||||||||||||||||||||
// Prepare the command | ||||||||||||||||||||||||||||||||||||||||||||
cmd := exec.Command("git", "describe", "--tags", "--abbrev=0") | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Capture the output | ||||||||||||||||||||||||||||||||||||||||||||
var out bytes.Buffer | ||||||||||||||||||||||||||||||||||||||||||||
cmd.Stdout = &out | ||||||||||||||||||||||||||||||||||||||||||||
cmd.Stderr = &out | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Run the command | ||||||||||||||||||||||||||||||||||||||||||||
err := cmd.Run() | ||||||||||||||||||||||||||||||||||||||||||||
require.NoError(t, err, out.String()) | ||||||||||||||||||||||||||||||||||||||||||||
return strings.TrimSpace(out.String()) | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
func getLatestVersion(t *testing.T) semver.Version { | ||||||||||||||||||||||||||||||||||||||||||||
tag := getLatestTag(t) | ||||||||||||||||||||||||||||||||||||||||||||
v, err := semver.NewVersion(tag) | ||||||||||||||||||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
return *v | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
func newVersionNoError(t *testing.T, version string, pre string, meta string) semver.Version { | ||||||||||||||||||||||||||||||||||||||||||||
v, err := semver.NewVersion(version) | ||||||||||||||||||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -47,7 +22,7 @@ func newVersionNoError(t *testing.T, version string, pre string, meta string) se | |||||||||||||||||||||||||||||||||||||||||||
return vmeta | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
func TestClaimsVerifierNoVersion(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||
func TestClaimsNoVersion(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||
signerPrivateKey := testutils.RandomPrivateKey(t) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
tests := []struct { | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -60,9 +35,17 @@ func TestClaimsVerifierNoVersion(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
for _, tt := range tests { | ||||||||||||||||||||||||||||||||||||||||||||
t.Run(tt.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||
tokenFactory := NewTokenFactory(signerPrivateKey, uint32(SIGNER_NODE_ID), tt.version) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
verifier, nodeRegistry := buildVerifier(t, uint32(VERIFIER_NODE_ID)) | ||||||||||||||||||||||||||||||||||||||||||||
tokenFactory := authn.NewTokenFactory( | ||||||||||||||||||||||||||||||||||||||||||||
signerPrivateKey, | ||||||||||||||||||||||||||||||||||||||||||||
uint32(SIGNER_NODE_ID), | ||||||||||||||||||||||||||||||||||||||||||||
tt.version, | ||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
verifier, nodeRegistry := buildVerifier( | ||||||||||||||||||||||||||||||||||||||||||||
t, | ||||||||||||||||||||||||||||||||||||||||||||
uint32(VERIFIER_NODE_ID), | ||||||||||||||||||||||||||||||||||||||||||||
testutils.GetLatestVersion(t), | ||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||
nodeRegistry.EXPECT().GetNode(uint32(SIGNER_NODE_ID)).Return(®istry.Node{ | ||||||||||||||||||||||||||||||||||||||||||||
SigningKey: &signerPrivateKey.PublicKey, | ||||||||||||||||||||||||||||||||||||||||||||
NodeID: uint32(SIGNER_NODE_ID), | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -80,10 +63,14 @@ func TestClaimsVerifierNoVersion(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
func TestClaimsVerifier(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||
func TestClaimsVariousVersions(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||
signerPrivateKey := testutils.RandomPrivateKey(t) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
currentVersion := getLatestVersion(t) | ||||||||||||||||||||||||||||||||||||||||||||
currentVersion := *testutils.GetLatestVersion(t) | ||||||||||||||||||||||||||||||||||||||||||||
version013, err := semver.NewVersion("0.1.3") | ||||||||||||||||||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||||||||||||||||||
version014, err := semver.NewVersion("0.1.4") | ||||||||||||||||||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
tests := []struct { | ||||||||||||||||||||||||||||||||||||||||||||
name string | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -94,7 +81,6 @@ func TestClaimsVerifier(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||
{"next-patch-version", currentVersion.IncPatch(), false}, | ||||||||||||||||||||||||||||||||||||||||||||
{"next-minor-version", currentVersion.IncMinor(), true}, | ||||||||||||||||||||||||||||||||||||||||||||
{"next-major-version", currentVersion.IncMajor(), true}, | ||||||||||||||||||||||||||||||||||||||||||||
{"last-supported-version", newVersionNoError(t, currentVersion.String(), "", ""), false}, | ||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||
"with-prerelease-version", | ||||||||||||||||||||||||||||||||||||||||||||
newVersionNoError(t, currentVersion.String(), "17-gdeadbeef", ""), | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -105,13 +91,87 @@ func TestClaimsVerifier(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||
newVersionNoError(t, currentVersion.String(), "", "branch-dev"), | ||||||||||||||||||||||||||||||||||||||||||||
false, | ||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||
{"known-0.1.3", *version013, true}, | ||||||||||||||||||||||||||||||||||||||||||||
{"known-0.1.4", *version014, true}, | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
for _, tt := range tests { | ||||||||||||||||||||||||||||||||||||||||||||
t.Run(tt.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||
tokenFactory := authn.NewTokenFactory( | ||||||||||||||||||||||||||||||||||||||||||||
signerPrivateKey, | ||||||||||||||||||||||||||||||||||||||||||||
uint32(SIGNER_NODE_ID), | ||||||||||||||||||||||||||||||||||||||||||||
&tt.version, | ||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
verifier, nodeRegistry := buildVerifier( | ||||||||||||||||||||||||||||||||||||||||||||
t, | ||||||||||||||||||||||||||||||||||||||||||||
uint32(VERIFIER_NODE_ID), | ||||||||||||||||||||||||||||||||||||||||||||
testutils.GetLatestVersion(t), | ||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||
nodeRegistry.EXPECT().GetNode(uint32(SIGNER_NODE_ID)).Return(®istry.Node{ | ||||||||||||||||||||||||||||||||||||||||||||
SigningKey: &signerPrivateKey.PublicKey, | ||||||||||||||||||||||||||||||||||||||||||||
NodeID: uint32(SIGNER_NODE_ID), | ||||||||||||||||||||||||||||||||||||||||||||
}, nil) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
token, err := tokenFactory.CreateToken(uint32(VERIFIER_NODE_ID)) | ||||||||||||||||||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||||||||||||||||||
_, verificationError := verifier.Verify(token.SignedString) | ||||||||||||||||||||||||||||||||||||||||||||
if tt.wantErr { | ||||||||||||||||||||||||||||||||||||||||||||
require.Error(t, verificationError) | ||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||
require.NoError(t, verificationError) | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
func TestClaimsValidator(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||
signerPrivateKey := testutils.RandomPrivateKey(t) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
currentVersion := *testutils.GetLatestVersion(t) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
tests := []struct { | ||||||||||||||||||||||||||||||||||||||||||||
name string | ||||||||||||||||||||||||||||||||||||||||||||
version semver.Version | ||||||||||||||||||||||||||||||||||||||||||||
serverVersion semver.Version | ||||||||||||||||||||||||||||||||||||||||||||
wantErr bool | ||||||||||||||||||||||||||||||||||||||||||||
}{ | ||||||||||||||||||||||||||||||||||||||||||||
{"current-version", currentVersion, currentVersion, false}, | ||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||
"with-prerelease-version", | ||||||||||||||||||||||||||||||||||||||||||||
currentVersion, | ||||||||||||||||||||||||||||||||||||||||||||
newVersionNoError(t, currentVersion.String(), "17-gdeadbeef", ""), | ||||||||||||||||||||||||||||||||||||||||||||
false, | ||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||
"with-metadata-version", | ||||||||||||||||||||||||||||||||||||||||||||
currentVersion, | ||||||||||||||||||||||||||||||||||||||||||||
newVersionNoError(t, currentVersion.String(), "", "branch-dev"), | ||||||||||||||||||||||||||||||||||||||||||||
false, | ||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||
"future-major-rejects-us", | ||||||||||||||||||||||||||||||||||||||||||||
currentVersion, | ||||||||||||||||||||||||||||||||||||||||||||
currentVersion.IncMajor(), | ||||||||||||||||||||||||||||||||||||||||||||
true, | ||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||
"future-patch-accepts-us", | ||||||||||||||||||||||||||||||||||||||||||||
currentVersion, | ||||||||||||||||||||||||||||||||||||||||||||
currentVersion.IncPatch(), | ||||||||||||||||||||||||||||||||||||||||||||
true, | ||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+159
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix incorrect test expectation. The test case "future-patch-accepts-us" expects an error ( Apply this fix: - "wantErr": true,
+ "wantErr": false, 📝 Committable suggestion
Suggested change
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
for _, tt := range tests { | ||||||||||||||||||||||||||||||||||||||||||||
t.Run(tt.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||
tokenFactory := NewTokenFactory(signerPrivateKey, uint32(SIGNER_NODE_ID), &tt.version) | ||||||||||||||||||||||||||||||||||||||||||||
tokenFactory := authn.NewTokenFactory( | ||||||||||||||||||||||||||||||||||||||||||||
signerPrivateKey, | ||||||||||||||||||||||||||||||||||||||||||||
uint32(SIGNER_NODE_ID), | ||||||||||||||||||||||||||||||||||||||||||||
&tt.version, | ||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
verifier, nodeRegistry := buildVerifier(t, uint32(VERIFIER_NODE_ID)) | ||||||||||||||||||||||||||||||||||||||||||||
verifier, nodeRegistry := buildVerifier(t, uint32(VERIFIER_NODE_ID), &tt.serverVersion) | ||||||||||||||||||||||||||||||||||||||||||||
nodeRegistry.EXPECT().GetNode(uint32(SIGNER_NODE_ID)).Return(®istry.Node{ | ||||||||||||||||||||||||||||||||||||||||||||
SigningKey: &signerPrivateKey.PublicKey, | ||||||||||||||||||||||||||||||||||||||||||||
NodeID: uint32(SIGNER_NODE_ID), | ||||||||||||||||||||||||||||||||||||||||||||
|
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.
Probably in the future we might want to move this constraint on-chain to the XMTP Node Registry.