storj/internal/testidentity/gen_identities.go

139 lines
2.9 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"
2019-04-08 19:15:19 +01:00
"storj.io/storj/pkg/peertls"
"storj.io/storj/pkg/pkcrypto"
2019-04-08 19:15:19 +01:00
"storj.io/storj/pkg/storj"
2018-11-03 12:17:14 +00:00
)
func main() {
2019-04-08 19:15:19 +01:00
versionFlag := flag.Uint("version", 0, "determines which identity version to generate (0 generates the latest version)")
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.
2019-04-08 19:15:19 +01:00
package testidentity
2018-11-03 12:17:14 +00:00
`)
var (
signer *identity.FullCertificateAuthority
restChain []*x509.Certificate
err error
)
2019-04-08 19:15:19 +01:00
caOpts := identity.NewCAOptions{
VersionNumber: storj.IDVersionNumber(*versionFlag),
Difficulty: 12,
Concurrency: 4,
}
if *signed {
2019-04-08 19:15:19 +01:00
signer, err = identity.NewCA(context.Background(), caOpts)
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)
}
2019-04-08 19:15:19 +01:00
fmt.Fprintf(&buf,
"var pregeneratedV%dSigner = mustParseCertificateAuthorityPEM(%q, %q)\n",
*versionFlag, chain.Bytes(), keys.Bytes(),
)
}
if *signed {
2019-04-08 19:15:19 +01:00
fmt.Fprintf(&buf, "var pregeneratedV%dSignedIdentities = NewIdentities(", *versionFlag)
} else {
2019-04-08 19:15:19 +01:00
fmt.Fprintf(&buf, "var pregeneratedV%dIdentities = NewIdentities(", *versionFlag)
}
2018-11-03 12:17:14 +00:00
for k := 0; k < *count; k++ {
fmt.Println("Creating", k)
2019-04-08 19:15:19 +01:00
ca, err := identity.NewCA(context.Background(), caOpts)
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...)
2019-04-08 19:15:19 +01:00
err = peertls.WriteChain(&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)
}
}