storj/internal/testplanet/gen_identities.go
2019-01-30 22:47:21 +02:00

99 lines
1.8 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
// +build ignore
// gen_identities generates random identities table for testing
package main
import (
"bytes"
"context"
"encoding/pem"
"flag"
"fmt"
"go/format"
"os"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/peertls"
)
func main() {
count := flag.Int("count", 5, "number of identities to create")
out := flag.String("out", "identities_table.go", "generated file")
flag.Parse()
var buf bytes.Buffer
buf.WriteString(`
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
// Code generated by gen_identities. DO NOT EDIT.
package testplanet
var pregeneratedIdentities = NewIdentities(
`)
for k := 0; k < *count; k++ {
fmt.Println("Creating", k)
ca, err := identity.NewCA(context.Background(), identity.NewCAOptions{
Difficulty: 12,
Concurrency: 4,
})
if err != nil {
panic(err)
}
identity, err := ca.NewIdentity()
if err != nil {
panic(err)
}
var chain bytes.Buffer
err = peertls.WriteChain(&chain, identity.Leaf, ca.Cert)
if err != nil {
panic(err)
}
var keys bytes.Buffer
err = peertls.WriteKey(&keys, identity.Key)
if err != nil {
panic(err)
}
fmt.Fprintf(&buf, "mustParsePEM(%q, %q),\n", chain.Bytes(), keys.Bytes())
}
buf.WriteString(`)`)
formatted, err := format.Source(buf.Bytes())
if err != nil {
panic(err)
}
file, err := os.Create(*out)
if err != nil {
panic(err)
}
if _, err := file.Write(formatted); err != nil {
panic(err)
}
if err := file.Close(); err != nil {
panic(err)
}
}
func encodeBlocks(blocks ...*pem.Block) ([]byte, error) {
var buf bytes.Buffer
for _, block := range blocks {
if err := pem.Encode(&buf, block); err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}