-
Notifications
You must be signed in to change notification settings - Fork 112
Sort signatures by keys IDs for deterministic signature order #155
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
trishankatdatadog
merged 10 commits into
theupdateframework:master
from
ethan-lowman-dd:ethan.lowman/sorted-signatures
Oct 18, 2021
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
641b890
Sort signatures by key ID
ethan-lowman-dd 7f8dfd0
Improve slice comparison
ethan-lowman-dd bc9bc15
Fix assert comment
ethan-lowman-dd b480753
Remove sortStrings test helper
ethan-lowman-dd bc71c8f
Add test for Signer sort
ethan-lowman-dd 88d9340
Sort keys before usage
ethan-lowman-dd a7cf9c6
Revert "Sort keys before usage"
ethan-lowman-dd 710115d
Rename to getSortedSigningKeys
ethan-lowman-dd 591dd73
Merge branch 'master' into ethan.lowman/sorted-signatures
ethan-lowman-dd 48c10d2
Check sort in a different way
ethan-lowman-dd 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.DS_Store | ||
cmd/tuf/tuf | ||
cmd/tuf-client/tuf-client |
8 changes: 4 additions & 4 deletions
8
client/testdata/go-tuf/consistent-snapshot-false/1/repository/2.root.json
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
8 changes: 4 additions & 4 deletions
8
client/testdata/go-tuf/consistent-snapshot-false/1/repository/root.json
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
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
8 changes: 4 additions & 4 deletions
8
client/testdata/go-tuf/consistent-snapshot-true/1/repository/2.root.json
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
8 changes: 4 additions & 4 deletions
8
client/testdata/go-tuf/consistent-snapshot-true/1/repository/root.json
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
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package signer | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/theupdateframework/go-tuf/pkg/keys" | ||
) | ||
|
||
// ByIDs implements sort.Interface for []keys.Signer based on | ||
// the sorted public IDs() for each Signer. This facilitates | ||
// deterministic order of signatures, which prevents tests | ||
// that use fixtures from being flaky. | ||
type ByIDs []keys.Signer | ||
|
||
func (b ByIDs) Len() int { | ||
return len(b) | ||
} | ||
|
||
func (b ByIDs) Swap(i, j int) { | ||
b[i], b[j] = b[j], b[i] | ||
} | ||
|
||
func (b ByIDs) Less(i, j int) bool { | ||
ids := b[i].PublicData().IDs() | ||
iIDs := make([]string, len(ids)) | ||
copy(iIDs, ids) | ||
sort.Strings(iIDs) | ||
|
||
ids = b[j].PublicData().IDs() | ||
jIDs := make([]string, len(ids)) | ||
copy(jIDs, ids) | ||
sort.Strings(jIDs) | ||
|
||
minLen := len(iIDs) | ||
trishankatdatadog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(jIDs) < minLen { | ||
minLen = len(jIDs) | ||
} | ||
|
||
// Compare iIDs[:minLen] to jIDs[:minLen] element-wise. | ||
for c := 0; c < minLen; c++ { | ||
if iIDs[c] == jIDs[c] { | ||
continue | ||
} | ||
return iIDs[c] < jIDs[c] | ||
} | ||
|
||
// iIDs[:minLen] is equal to jIDs[:minLen], so sort based on length. | ||
return len(iIDs) < len(jIDs) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package signer_test | ||
|
||
import ( | ||
"encoding/json" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/theupdateframework/go-tuf/data" | ||
"github.com/theupdateframework/go-tuf/internal/signer" | ||
"github.com/theupdateframework/go-tuf/pkg/keys" | ||
) | ||
|
||
type mockSigner struct { | ||
value json.RawMessage | ||
} | ||
|
||
func (s *mockSigner) MarshalPrivateKey() (*data.PrivateKey, error) { | ||
panic("not implemented") | ||
return nil, nil | ||
} | ||
|
||
func (s *mockSigner) UnmarshalPrivateKey(key *data.PrivateKey) error { | ||
panic("not implemented") | ||
return nil | ||
} | ||
|
||
func (s *mockSigner) PublicData() *data.PublicKey { | ||
return &data.PublicKey{ | ||
Type: "mock", | ||
Scheme: "mock", | ||
Algorithms: []string{"mock"}, | ||
Value: s.value, | ||
} | ||
} | ||
func (s *mockSigner) SignMessage(message []byte) ([]byte, error) { | ||
panic("not implemented") | ||
return nil, nil | ||
} | ||
|
||
func TestSignerSortByIDs(t *testing.T) { | ||
s1 := &mockSigner{ | ||
value: json.RawMessage(`{"mock": 1}`), | ||
} | ||
s2 := &mockSigner{ | ||
value: json.RawMessage(`{"mock": 2}`), | ||
} | ||
s3 := &mockSigner{ | ||
value: json.RawMessage(`{"mock": 3}`), | ||
} | ||
s4 := &mockSigner{ | ||
value: json.RawMessage(`{"mock": 4}`), | ||
} | ||
s5 := &mockSigner{ | ||
value: json.RawMessage(`{"mock": 5}`), | ||
} | ||
|
||
s := []keys.Signer{ | ||
s1, s2, s3, s4, s5, | ||
} | ||
|
||
sort.Sort(signer.ByIDs(s)) | ||
|
||
signerIDs := []string{} | ||
|
||
for i, signer := range s { | ||
ids := signer.PublicData().IDs() | ||
if len(ids) != 1 { | ||
t.Errorf("Signer %v IDs %v should have length 1", i, ids) | ||
} | ||
signerIDs = append(signerIDs, ids[0]) | ||
} | ||
|
||
if !sort.StringsAreSorted(signerIDs) { | ||
t.Errorf("Signers incorrectly sorted: %+v", signerIDs) | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.