storj/cmd/identity/keys.go
Egon Elbre bbf81f2479 Consolidate identity management to identity cli commands (#1083)
* Consolidate identity management:

Move identity cretaion/signing out of storagenode setup command.

* fixes

* linters

* Consolidate identity management:

Move identity cretaion/signing out of storagenode setup command.

* fixes

* sava backups before saving signed certs

* add "-prebuilt-test-cmds" test flag

* linters

* prepare cli tests for travis

* linter fixes

* more fixes

* linter gods

* sp/sdk/sim

* remove ca.difficulty

* remove unused difficulty

* return setup to its rightful place

* wip travis

* Revert "wip travis"

This reverts commit 56834849dcf066d3cc0a4f139033fc3f6d7188ca.

* typo in travis.yaml

* remove tests

* remove more

* make it only create one identity at a time for consistency

* add config-dir for consitency

* add identity creation to storj-sim

* add flags

* simplify

* fix nolint and compile

* prevent overwrite and pass difficulty, concurrency, and parent creds

* goimports
2019-01-18 11:36:58 +01:00

71 lines
1.7 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"crypto/ecdsa"
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync/atomic"
"github.com/spf13/cobra"
"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/process"
"storj.io/storj/pkg/storj"
)
var (
keysCmd = &cobra.Command{
Use: "keys",
Short: "Manage keys",
}
keyGenerateCmd = &cobra.Command{
Use: "generate",
Short: "generate lots of keys",
RunE: cmdKeyGenerate,
Annotations: map[string]string{"type": "setup"},
}
keyCfg struct {
MinDifficulty int `help:"minimum difficulty to output" default:"18"`
Concurrency int `help:"worker concurrency" default:"4"`
OutputDir string `help:"output directory to place keys" default:"."`
}
)
func init() {
rootCmd.AddCommand(keysCmd)
keysCmd.AddCommand(keyGenerateCmd)
cfgstruct.Bind(keyGenerateCmd.Flags(), &keyCfg)
}
func cmdKeyGenerate(cmd *cobra.Command, args []string) (err error) {
ctx := process.Ctx(cmd)
err = os.MkdirAll(keyCfg.OutputDir, 0700)
if err != nil {
return err
}
counter := new(uint32)
return identity.GenerateKeys(ctx, uint16(keyCfg.MinDifficulty), keyCfg.Concurrency,
func(k *ecdsa.PrivateKey, id storj.NodeID) (done bool, err error) {
data, err := x509.MarshalECPrivateKey(k)
if err != nil {
return false, err
}
difficulty, err := id.Difficulty()
if err != nil {
return false, err
}
filename := fmt.Sprintf("gen-%02d-%d.key", difficulty, atomic.AddUint32(counter, 1))
fmt.Println("writing", filename)
err = ioutil.WriteFile(filepath.Join(keyCfg.OutputDir, filename), data, 0600)
return false, err
})
}