storj/internal/testplanet/gen_identities.go

136 lines
2.7 KiB
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2018-11-03 12:17:14 +00:00
// See LICENSE for copying information
// +build ignore
// gen_identities generates random identities table for testing
package main
import (
"bytes"
"context"
"crypto/x509"
2018-11-03 12:17:14 +00:00
"flag"
"fmt"
"go/format"
"os"
2019-01-30 20:47:21 +00:00
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/pkcrypto"
2018-11-03 12:17:14 +00:00
)
func main() {
signed := flag.Bool("signed", false, "if true, generate a signer and sign all identities")
2018-11-03 12:17:14 +00:00
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(`
2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2018-11-03 12:17:14 +00:00
// See LICENSE for copying information
// Code generated by gen_identities. DO NOT EDIT.
package testplanet
`)
var (
signer *identity.FullCertificateAuthority
restChain []*x509.Certificate
err error
)
if *signed {
signer, err = identity.NewCA(context.Background(), identity.NewCAOptions{
Difficulty: 12,
Concurrency: 4,
})
if err != nil {
panic(err)
}
restChain = []*x509.Certificate{signer.Cert}
var chain bytes.Buffer
err = pkcrypto.WriteCertPEM(&chain, signer.Cert)
if err != nil {
panic(err)
}
var keys bytes.Buffer
err = pkcrypto.WritePrivateKeyPEM(&keys, signer.Key)
if err != nil {
panic(err)
}
fmt.Fprintf(&buf, "var pregeneratedSigner = mustParseCertificateAuthorityPEM(%q, %q)", chain.Bytes(), keys.Bytes())
}
if *signed {
buf.WriteString(`
var pregeneratedSignedIdentities = NewIdentities(
`)
} else {
buf.WriteString(`
var pregeneratedIdentities = NewIdentities(
`)
}
2018-11-03 12:17:14 +00:00
for k := 0; k < *count; k++ {
fmt.Println("Creating", k)
2019-01-30 20:47:21 +00:00
ca, err := identity.NewCA(context.Background(), identity.NewCAOptions{
Difficulty: 12,
Concurrency: 4,
})
2018-11-03 12:17:14 +00:00
if err != nil {
panic(err)
}
if *signed {
ca.Cert, err = signer.Sign(ca.Cert)
if err != nil {
panic(err)
}
ca.RestChain = restChain
}
ident, err := ca.NewIdentity()
2018-11-03 12:17:14 +00:00
if err != nil {
panic(err)
}
var chain bytes.Buffer
certs := append([]*x509.Certificate{ident.Leaf, ca.Cert}, ca.RestChain...)
err = pkcrypto.WriteCertPEM(&chain, certs...)
2018-11-03 12:17:14 +00:00
if err != nil {
panic(err)
}
var keys bytes.Buffer
err = pkcrypto.WritePrivateKeyPEM(&keys, ident.Key)
2018-11-03 12:17:14 +00:00
if err != nil {
panic(err)
}
fmt.Fprintf(&buf, "mustParseIdentityPEM(%q, %q),\n", chain.Bytes(), keys.Bytes())
2018-11-03 12:17:14 +00:00
}
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)
}
}