-
Notifications
You must be signed in to change notification settings - Fork 28
Adds func for creating V2 beholder auth headers #1640
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package beholder_test | |
| import ( | ||
| "context" | ||
| "crypto/ed25519" | ||
| "encoding/binary" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "strings" | ||
|
|
@@ -34,6 +35,165 @@ func TestBuildAuthHeaders(t *testing.T) { | |
| assert.Equal(t, expectedHeaders, headers) | ||
| } | ||
|
|
||
| func TestNewAuthHeaderV2(t *testing.T) { | ||
| // Generate test key pair | ||
| pubKey, privKey, err := ed25519.GenerateKey(nil) | ||
| require.NoError(t, err) | ||
|
|
||
| t.Run("creates valid V2 auth headers", func(t *testing.T) { | ||
| mockSigner := &MockSigner{} | ||
|
|
||
| ts := time.Now() | ||
|
|
||
| // Create the expected message bytes (pubkey + timestamp) | ||
| expectedSignature := []byte("test-signature") | ||
| mockSigner. | ||
| On("Sign", t.Context(), hex.EncodeToString(pubKey), mock.Anything). | ||
| Return(expectedSignature, nil). | ||
| Once() | ||
|
|
||
| headers, err := beholder.NewAuthHeaderV2(t.Context(), pubKey, mockSigner, ts) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, headers) | ||
| require.Contains(t, headers, "X-Beholder-Node-Auth-Token") | ||
|
|
||
| authHeader := headers["X-Beholder-Node-Auth-Token"] | ||
| parts := strings.Split(authHeader, ":") | ||
| require.Len(t, parts, 4, "Auth header should have format version:pubkey_hex:timestamp:signature_hex") | ||
|
|
||
| assert.Equal(t, "2", parts[0], "Version should be 2") | ||
| assert.Equal(t, hex.EncodeToString(pubKey), parts[1], "Public key should match") | ||
| assert.Equal(t, fmt.Sprintf("%d", ts.UnixNano()), parts[2], "Timestamp should match") | ||
| assert.Equal(t, hex.EncodeToString(expectedSignature), parts[3], "Signature should match") | ||
|
|
||
| mockSigner.AssertExpectations(t) | ||
| }) | ||
| t.Run("returns error when signer fails", func(t *testing.T) { | ||
| mockSigner := &MockSigner{} | ||
| ts := time.Now() | ||
|
|
||
| expectedErr := fmt.Errorf("signing failed") | ||
| mockSigner. | ||
| On("Sign", t.Context(), hex.EncodeToString(pubKey), mock.Anything). | ||
| Return([]byte{}, expectedErr). | ||
| Once() | ||
|
|
||
| headers, err := beholder.NewAuthHeaderV2(t.Context(), pubKey, mockSigner, ts) | ||
| require.Error(t, err) | ||
| assert.Nil(t, headers) | ||
| assert.Contains(t, err.Error(), "beholder: failed to sign auth header") | ||
| assert.Contains(t, err.Error(), expectedErr.Error()) | ||
|
|
||
| mockSigner.AssertExpectations(t) | ||
| }) | ||
|
|
||
| t.Run("verifies signature with ed25519", func(t *testing.T) { | ||
| // Use a real signature for verification | ||
| mockSigner := &MockSigner{} | ||
| ts := time.Now() | ||
|
|
||
| // Calculate the message that should be signed | ||
| tsBytes := make([]byte, 8) | ||
| binary.BigEndian.PutUint64(tsBytes, uint64(ts.UnixNano())) | ||
| msgBytes := append(pubKey, tsBytes...) | ||
|
|
||
| // Sign with the actual private key | ||
| realSignature := ed25519.Sign(privKey, msgBytes) | ||
|
|
||
| mockSigner. | ||
| On("Sign", t.Context(), hex.EncodeToString(pubKey), mock.MatchedBy(func(data []byte) bool { | ||
| // Match if the data contains pubkey + timestamp | ||
| return len(data) == len(pubKey)+8 && string(data[:len(pubKey)]) == string(pubKey) | ||
| })). | ||
| Return(realSignature, nil). | ||
| Once() | ||
|
|
||
| headers, err := beholder.NewAuthHeaderV2(t.Context(), pubKey, mockSigner, ts) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, headers) | ||
|
|
||
| authHeader := headers["X-Beholder-Node-Auth-Token"] | ||
| parts := strings.Split(authHeader, ":") | ||
| require.Len(t, parts, 4) | ||
|
|
||
| signatureBytes, err := hex.DecodeString(parts[3]) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify the signature | ||
| valid := ed25519.Verify(pubKey, msgBytes, signatureBytes) | ||
|
Contributor
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. Should it verify signature against the header (parts[1], part[2]) not the
Contributor
Author
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.
|
||
| assert.True(t, valid, "Signature should be valid") | ||
|
|
||
| mockSigner.AssertExpectations(t) | ||
| }) | ||
|
|
||
| t.Run("handles context cancellation", func(t *testing.T) { | ||
| mockSigner := &MockSigner{} | ||
|
|
||
| ts := time.Now() | ||
|
|
||
| mockSigner. | ||
| On("Sign", t.Context(), hex.EncodeToString(pubKey), mock.Anything). | ||
| Return([]byte{}, context.Canceled). | ||
| Maybe() | ||
|
|
||
| headers, err := beholder.NewAuthHeaderV2(t.Context(), pubKey, mockSigner, ts) | ||
|
|
||
| // The function should propagate the context error | ||
| if err != nil { | ||
| assert.Contains(t, err.Error(), "beholder: failed to sign auth header") | ||
| } | ||
|
|
||
| // If mockSigner.Sign was called and returned error, headers should be nil | ||
| if err != nil { | ||
| assert.Nil(t, headers) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("uses correct keyID format", func(t *testing.T) { | ||
| mockSigner := &MockSigner{} | ||
| ts := time.Now() | ||
|
|
||
| var capturedKeyID string | ||
| mockSigner. | ||
| On("Sign", t.Context(), mock.Anything, mock.Anything). | ||
| Run(func(args mock.Arguments) { | ||
| capturedKeyID = args.Get(1).(string) | ||
| }). | ||
| Return([]byte("signature"), nil). | ||
| Once() | ||
|
|
||
| _, err := beholder.NewAuthHeaderV2(t.Context(), pubKey, mockSigner, ts) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify keyID is hex-encoded public key | ||
| assert.Equal(t, hex.EncodeToString(pubKey), capturedKeyID) | ||
|
|
||
| mockSigner.AssertExpectations(t) | ||
| }) | ||
|
|
||
| t.Run("different timestamps produce different headers", func(t *testing.T) { | ||
| mockSigner := &MockSigner{} | ||
|
|
||
| ts1 := time.Unix(1000, 0) | ||
| ts2 := time.Unix(2000, 0) | ||
|
|
||
| mockSigner. | ||
| On("Sign", t.Context(), hex.EncodeToString(pubKey), mock.Anything). | ||
| Return([]byte("signature"), nil) | ||
|
|
||
| headers1, err := beholder.NewAuthHeaderV2(t.Context(), pubKey, mockSigner, ts1) | ||
| require.NoError(t, err) | ||
|
|
||
| headers2, err := beholder.NewAuthHeaderV2(t.Context(), pubKey, mockSigner, ts2) | ||
| require.NoError(t, err) | ||
|
|
||
| // Headers should be different due to different timestamps | ||
| assert.NotEqual(t, headers1["X-Beholder-Node-Auth-Token"], headers2["X-Beholder-Node-Auth-Token"]) | ||
|
|
||
| mockSigner.AssertExpectations(t) | ||
| }) | ||
| } | ||
|
|
||
| func TestStaticAuthHeaderProvider(t *testing.T) { | ||
| // Create test headers | ||
| testHeaders := map[string]string{ | ||
|
|
||
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.
For timestamps before the Unix epoch (pre-1970), ts.UnixNano() is negative.
In practice we should not get those but for consistency its better to use same uint64 value for both signature and header