Skip to content
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

Allow sharding config to be written in yaml or json #974

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/rekor-server/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func init() {
rootCmd.PersistentFlags().String("trillian_log_server.address", "127.0.0.1", "Trillian log server address")
rootCmd.PersistentFlags().Uint16("trillian_log_server.port", 8090, "Trillian log server port")
rootCmd.PersistentFlags().Uint("trillian_log_server.tlog_id", 0, "Trillian tree id")
rootCmd.PersistentFlags().String("trillian_log_server.sharding_config", "", "path to config file for inactive shards")
rootCmd.PersistentFlags().String("trillian_log_server.sharding_config", "", "path to config file for inactive shards, in JSON or YAML")

hostname, err := os.Hostname()
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/sharding/ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package sharding
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -82,6 +83,10 @@ func logRangesFromPath(path string) (Ranges, error) {
return Ranges{}, nil
}
if err := yaml.Unmarshal(contents, &ranges); err != nil {
// Try to use JSON
if jerr := json.Unmarshal(contents, &ranges); jerr == nil {
return ranges, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't assign this jerr to err, there can be a situation where we have a nil error, but a zero-value range.

}
return Ranges{}, err
}
return ranges, nil
Expand Down
26 changes: 26 additions & 0 deletions pkg/sharding/ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ func TestLogRangesFromPath(t *testing.T) {
}
}

func TestLogRangesFromPathJSON(t *testing.T) {
contents := `[{"treeID": 0001, "treeLength": 3, "encodedPublicKey":"c2hhcmRpbmcK"}, {"treeID": 0002, "treeLength": 4}]`
file := filepath.Join(t.TempDir(), "sharding-config")
if err := ioutil.WriteFile(file, []byte(contents), 0644); err != nil {
t.Fatal(err)
}
expected := Ranges{
{
TreeID: 1,
TreeLength: 3,
EncodedPublicKey: "c2hhcmRpbmcK",
}, {
TreeID: 2,
TreeLength: 4,
},
}

got, err := logRangesFromPath(file)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, got) {
t.Fatalf("expected %v got %v", expected, got)
}
}

func TestLogRanges_ResolveVirtualIndex(t *testing.T) {
lrs := LogRanges{
inactive: []LogRange{
Expand Down