prysm-pulse/cmd/beacon-chain/jwt/jwt.go
Raul Jordan d077483577
Add V3 Suffix to All Prysm Packages (#11083)
* v3 import renamings

* tidy

* fmt

* rev

* Update beacon-chain/core/epoch/precompute/reward_penalty_test.go

* Update beacon-chain/core/helpers/validators_test.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/iface/BUILD.bazel

* Update beacon-chain/db/kv/kv.go

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/sync/initial-sync/service.go

* fix deps

* fix bad replacements

* fix bad replacements

* change back

* gohashtree version

* fix deps

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-08-16 12:20:13 +00:00

72 lines
1.9 KiB
Go

package jwt
import (
"errors"
"path/filepath"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/prysmaticlabs/prysm/v3/cmd"
"github.com/prysmaticlabs/prysm/v3/crypto/rand"
"github.com/prysmaticlabs/prysm/v3/io/file"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
const (
secretFileName = "jwt.hex"
)
var Commands = &cli.Command{
Name: "generate-auth-secret",
Usage: "creates a random, 32 byte hex string in a plaintext file to be used for authenticating JSON-RPC requests. If no --output-file flag is defined, the file will be created in the current working directory",
Description: `creates a random, 32 byte hex string in a plaintext file to be used for authenticating JSON-RPC requests. If no --output-file flag is defined, the file will be created in the current working directory`,
Flags: cmd.WrapFlags([]cli.Flag{
cmd.JwtOutputFileFlag,
}),
Action: generateAuthSecretInFile,
}
func generateAuthSecretInFile(c *cli.Context) error {
fileName := secretFileName
specifiedFilePath := c.String(cmd.JwtOutputFileFlag.Name)
if len(specifiedFilePath) > 0 {
fileName = specifiedFilePath
}
var err error
fileName, err = file.ExpandPath(fileName)
if err != nil {
return err
}
fileDir := filepath.Dir(fileName)
exists, err := file.HasDir(fileDir)
if err != nil {
return err
}
if !exists {
if err := file.MkdirAll(fileDir); err != nil {
return err
}
}
secret, err := generateRandomHexString()
if err != nil {
return err
}
if err := file.WriteFile(fileName, []byte(secret)); err != nil {
return err
}
logrus.Infof("Successfully wrote JSON-RPC authentication secret to file %s", fileName)
return nil
}
func generateRandomHexString() (string, error) {
secret := make([]byte, 32)
randGen := rand.NewGenerator()
n, err := randGen.Read(secret)
if err != nil {
return "", err
} else if n <= 0 {
return "", errors.New("rand: unexpected length")
}
return hexutil.Encode(secret), nil
}