diff options
author | Linus Nordberg <linus@nordberg.se> | 2022-05-24 23:33:38 +0200 |
---|---|---|
committer | Rasmus Dahlberg <rasmus@mullvad.net> | 2022-06-23 11:33:17 +0200 |
commit | 559bccccd40d028e412d9f11709ded0250ba6dcd (patch) | |
tree | 50f3193dbe70fec21357963c11e5f663013f4b4c /internal/utils/utils.go | |
parent | 4b20ef0c1732bcef633c0ed7104501898aa84e2c (diff) |
implement primary and secondary role, for replicationv0.5.0
Diffstat (limited to 'internal/utils/utils.go')
-rw-r--r-- | internal/utils/utils.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/internal/utils/utils.go b/internal/utils/utils.go new file mode 100644 index 0000000..a453107 --- /dev/null +++ b/internal/utils/utils.go @@ -0,0 +1,69 @@ +package utils + +import ( + "crypto" + "crypto/ed25519" + "encoding/hex" + "fmt" + "io/ioutil" + "os" + "strings" + + "git.sigsum.org/sigsum-go/pkg/log" + "git.sigsum.org/sigsum-go/pkg/types" +) + +// TODO: Move SetupLogging to sigsum-go/pkg/log + +func SetupLogging(logFile, logLevel string, logColor bool) error { + if len(logFile) != 0 { + f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return err + } + log.SetOutput(f) + } + + switch logLevel { + case "debug": + log.SetLevel(log.DebugLevel) + case "info": + log.SetLevel(log.InfoLevel) + case "warning": + log.SetLevel(log.WarningLevel) + case "error": + log.SetLevel(log.ErrorLevel) + default: + return fmt.Errorf("invalid logging level %s", logLevel) + } + + log.SetColor(logColor) + return nil +} + +func PubkeyFromHexString(pkhex string) (*types.PublicKey, error) { + pkbuf, err := hex.DecodeString(pkhex) + if err != nil { + return nil, fmt.Errorf("DecodeString: %v", err) + } + + var pk types.PublicKey + if n := copy(pk[:], pkbuf); n != types.PublicKeySize { + return nil, fmt.Errorf("invalid pubkey size: %v", n) + } + + return &pk, nil +} + +func NewLogIdentity(keyFile string) (crypto.Signer, string, error) { + buf, err := ioutil.ReadFile(keyFile) + if err != nil { + return nil, "", err + } + if buf, err = hex.DecodeString(strings.TrimSpace(string(buf))); err != nil { + return nil, "", fmt.Errorf("DecodeString: %v", err) + } + sk := crypto.Signer(ed25519.NewKeyFromSeed(buf)) + vk := sk.Public().(ed25519.PublicKey) + return sk, hex.EncodeToString([]byte(vk[:])), nil +} |