Skip to content

Commit

Permalink
Add decryptor generate-config subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
schmir committed Sep 15, 2021
1 parent 3bda732 commit 69a5197
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 39 deletions.
51 changes: 51 additions & 0 deletions rolling-shutter/cmd/decryptor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"bytes"
"context"
"crypto/rand"
"log"

"github.com/jackc/pgx/v4/pgxpool"
Expand Down Expand Up @@ -37,6 +39,15 @@ var initDecryptorDBCmd = &cobra.Command{
},
}

var generateDecryptorConfigCmd = &cobra.Command{
Use: "generate-config",
Short: "Generate a decryptor configuration file",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return generateDecryptorConfig()
},
}

type DecryptorConfig struct {
ListenAddress multiaddr.Multiaddr
PeerMultiaddrs []multiaddr.Multiaddr
Expand All @@ -47,6 +58,10 @@ type DecryptorConfig struct {
func init() {
decryptorCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file")
decryptorCmd.AddCommand(initDecryptorDBCmd)
decryptorCmd.AddCommand(generateDecryptorConfigCmd)

generateDecryptorConfigCmd.PersistentFlags().StringVar(&outputFile, "output", "", "output file")
generateDecryptorConfigCmd.MarkPersistentFlagRequired("output")
}

func initDecryptorDB() error {
Expand Down Expand Up @@ -115,6 +130,42 @@ func readDecryptorConfig() (DecryptorConfig, error) {
return config, nil
}

var decryptorTemplate = medley.MustBuildTemplate(
"decryptor",
`# Shutter decryptor config for /p2p/{{ .P2PKey | P2PKeyPublic}}
# DatabaseURL looks like postgres://username:password@localhost:5432/database_name
# It it's empty, we use the standard PG* environment variables
DatabaseURL = "{{ .DatabaseURL }}"
# p2p configuration
ListenAddress = "{{ .ListenAddress }}"
PeerMultiaddrs = [{{ .PeerMultiaddrs | QuoteList}}]
# Secret Keys
P2PKey = "{{ .P2PKey | P2PKey}}"
`)

func generateDecryptorConfig() error {
p2pkey, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
return err
}

config := DecryptorConfig{
ListenAddress: mustMultiaddr("/ip4/127.0.0.1/tcp/2000"),
PeerMultiaddrs: nil,
DatabaseURL: "",
P2PKey: p2pkey,
}
buf := &bytes.Buffer{}
err = decryptorTemplate.Execute(buf, config)
if err != nil {
return err
}
return medley.SecureSpit(outputFile, buf.Bytes())
}

func decryptorMain() error {
ctx := context.Background()

Expand Down
40 changes: 1 addition & 39 deletions rolling-shutter/keyper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ import (
"crypto/ed25519"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"reflect"
"strings"
"text/template"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/mitchellh/mapstructure"
"github.com/multiformats/go-multiaddr"
"github.com/pkg/errors"
Expand Down Expand Up @@ -60,42 +57,7 @@ ValidatorSeed = "{{ .ValidatorKey.Seed | printf "%x" }}"
P2PKey = "{{ .P2PKey | P2PKey}}"
`

var tmpl *template.Template

func p2pKeyPublic(privkey p2pcrypto.PrivKey) string {
id, _ := peer.IDFromPublicKey(privkey.GetPublic())
return id.Pretty()
}

func p2pKey(privkey p2pcrypto.PrivKey) string {
d, _ := p2pcrypto.MarshalPrivateKey(privkey)
return p2pcrypto.ConfigEncodeKey(d)
}

func QuoteList(lst []multiaddr.Multiaddr) string {
var strlist []string
for _, x := range lst {
// We use json.Marshal here, not sure if it's the right thing to do, since we're
// writing TOML
d, _ := json.Marshal(x.String())
strlist = append(strlist, string(d))
}

return strings.Join(strlist, ", ")
}

func init() {
var err error
tmpl, err = template.New("keyper").Funcs(template.FuncMap{
"FromECDSA": crypto.FromECDSA,
"QuoteList": QuoteList,
"P2PKey": p2pKey,
"P2PKeyPublic": p2pKeyPublic,
}).Parse(configTemplate)
if err != nil {
panic(err)
}
}
var tmpl *template.Template = medley.MustBuildTemplate("keyper", configTemplate)

func stringToEd25519PrivateKey(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String || t != reflect.TypeOf(ed25519.PrivateKey{}) {
Expand Down
47 changes: 47 additions & 0 deletions rolling-shutter/medley/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package medley

import (
"encoding/json"
"strings"
"text/template"

"github.com/ethereum/go-ethereum/crypto"
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
multiaddr "github.com/multiformats/go-multiaddr"
)

func p2pKeyPublic(privkey p2pcrypto.PrivKey) string {
id, _ := peer.IDFromPublicKey(privkey.GetPublic())
return id.Pretty()
}

func p2pKey(privkey p2pcrypto.PrivKey) string {
d, _ := p2pcrypto.MarshalPrivateKey(privkey)
return p2pcrypto.ConfigEncodeKey(d)
}

func quoteList(lst []multiaddr.Multiaddr) string {
var strlist []string
for _, x := range lst {
// We use json.Marshal here, not sure if it's the right thing to do, since we're
// writing TOML
d, _ := json.Marshal(x.String())
strlist = append(strlist, string(d))
}

return strings.Join(strlist, ", ")
}

func MustBuildTemplate(name, content string) *template.Template {
t, err := template.New(name).Funcs(template.FuncMap{
"FromECDSA": crypto.FromECDSA,
"QuoteList": quoteList,
"P2PKey": p2pKey,
"P2PKeyPublic": p2pKeyPublic,
}).Parse(content)
if err != nil {
panic(err)
}
return t
}

0 comments on commit 69a5197

Please sign in to comment.