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 the witness identities to be provided as flags #77

Merged
merged 2 commits into from
Jan 31, 2024
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
39 changes: 35 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net"
"net/http"
"os"
"strings"

"cloud.google.com/go/cloudsqlconn"
"github.com/golang/glog"
Expand All @@ -33,6 +34,7 @@ import (
"github.com/transparency-dev/distributor/config"
"golang.org/x/mod/sumdb/note"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v3"

_ "embed"

Expand All @@ -45,10 +47,12 @@ var (
useCloudSql = flag.Bool("use_cloud_sql", false, "Set to true to set up the DB connection using cloudsql connection. This will ignore mysql_uri and generate it from env variables.")
mysqlURI = flag.String("mysql_uri", "", "URI for MySQL DB")

witnessConfigFile = flag.String("witness_config_file", "", "Path to a file containing the public keys of allowed witnesses")
witnessConfigFile = flag.String("witness_config_file", "", "Path to a file containing the public keys of allowed witnesses. Mutually exclusive with witkey.")
witnessKeys witFlags
)

func main() {
flag.Var(&witnessKeys, "witkey", "Provide one or more witness keys directly as flags (can specify multiple times). Mutually exclusive with witness_config_file.")
flag.Parse()
ctx := context.Background()

Expand Down Expand Up @@ -153,16 +157,31 @@ func getLogsOrDie() map[string]config.LogInfo {
}

func getWitnessesOrDie() map[string]note.Verifier {
cfg := config.WitnessesYAML
if *witnessConfigFile != "" {
var cfg []byte
if witFile, witFlags := *witnessConfigFile != "", len(witnessKeys) > 0; witFile && !witFlags {
c, err := os.ReadFile(*witnessConfigFile)
if err != nil {
glog.Exitf("Failed to read witness_config_file (%q): %v", *witnessConfigFile, err)
}
glog.Infof("Witness list read from %v", *witnessConfigFile)
cfg = c
} else if !witFile && witFlags {
// This is a bit messy to turn flags into yaml and then parse them again, but the cost
// is small, and the benefit is that we guarantee the same parsing & instantiation logic.
witCfg := struct {
Witnesses []string `yaml:"Witnesses"`
}{}
witCfg.Witnesses = witnessKeys
var err error
cfg, err = yaml.Marshal(witCfg)
if err != nil {
glog.Exitf("Failed to marshal witness config: %v", err)
}
} else if !witFile && !witFlags {
glog.Info("Flags witness_config_file nor witkey are specified; default witness list will be used")
cfg = config.WitnessesYAML
} else {
glog.Info("Flag witness_config_file not specified; default witness list will be used")
glog.Exitf("Only one of witness_config_file and witkey can be specified")
}
w, err := config.ParseWitnessesConfig(cfg)
if err != nil {
Expand All @@ -173,5 +192,17 @@ func getWitnessesOrDie() map[string]note.Verifier {
for _, v := range w {
r[v.Name()] = v
}
glog.Infof("Configured with %d witness keys: %s", len(r), r)
return r
}

type witFlags []string

func (wf *witFlags) String() string {
return strings.Join(*wf, ",")
}

func (wf *witFlags) Set(w string) error {
*wf = append(*wf, w)
return nil
}
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func ParseWitnessesConfig(y []byte) (map[uint32]note.Verifier, error) {
witCfg := struct {
Witnesses []string `yaml:"Witnesses"`
}{}
if err := yaml.Unmarshal(WitnessesYAML, &witCfg); err != nil {
if err := yaml.Unmarshal(y, &witCfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal witness config: %v", err)
}
ws := make(map[uint32]note.Verifier)
Expand Down
Loading