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

feat: allow injection of master key via stdin #772

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions cmd/thor/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ var (
Hidden: true,
Usage: "directory for user global configurations",
}
masterKeyStdinFlag = cli.BoolFlag{
Name: "master-key-stdin",
Usage: "read master key from stdin",
Hidden: true,
}
dataDirFlag = cli.StringFlag{
Name: "data-dir",
Value: defaultDataDir(),
Expand Down
1 change: 1 addition & 0 deletions cmd/thor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func main() {
Flags: []cli.Flag{
networkFlag,
configDirFlag,
masterKeyStdinFlag,
dataDirFlag,
cacheFlag,
beneficiaryFlag,
Expand Down
33 changes: 28 additions & 5 deletions cmd/thor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package main

import (
"bufio"
"context"
"crypto/ecdsa"
"encoding/json"
Expand Down Expand Up @@ -405,15 +406,37 @@ func masterKeyPath(ctx *cli.Context) (string, error) {
return filepath.Join(configDir, "master.key"), nil
}

func loadNodeMaster(ctx *cli.Context) (*node.Master, error) {
path, err := masterKeyPath(ctx)
func loadNodeMasterFromStdin() (*ecdsa.PrivateKey, error) {
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return nil, err
}
key, err := loadOrGeneratePrivateKey(path)
if err != nil {
return nil, errors.Wrap(err, "load or generate master key")

return crypto.HexToECDSA(strings.TrimSpace(input))
}

func loadNodeMaster(ctx *cli.Context) (*node.Master, error) {
var key *ecdsa.PrivateKey
var err error

useStdin := ctx.Bool(masterKeyStdinFlag.Name)
if useStdin {
key, err = loadNodeMasterFromStdin()
if err != nil {
return nil, errors.Wrap(err, "read master key from stdin")
}
} else {
path, err := masterKeyPath(ctx)
if err != nil {
return nil, err
}
key, err = loadOrGeneratePrivateKey(path)
if err != nil {
return nil, errors.Wrap(err, "load or generate master key")
}
}

master := &node.Master{PrivateKey: key}
if master.Beneficiary, err = beneficiary(ctx); err != nil {
return nil, err
Expand Down
Loading