storj/internal/testplanet/identities.go

95 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
package testplanet
import (
"errors"
2019-01-30 20:47:21 +00:00
"storj.io/storj/pkg/identity"
2018-11-03 12:17:14 +00:00
)
//go:generate go run gen_identities.go -count 150 -out identities_table.go
//go:generate go run gen_identities.go -signed -count 150 -out signed_identities_table.go
2018-11-03 12:17:14 +00:00
// Identities is a pregenerated full identity table.
type Identities struct {
2019-01-30 20:47:21 +00:00
list []*identity.FullIdentity
2018-11-03 12:17:14 +00:00
next int
}
// NewIdentities creates a new table from provided identities.
2019-01-30 20:47:21 +00:00
func NewIdentities(list ...*identity.FullIdentity) *Identities {
2018-11-03 12:17:14 +00:00
return &Identities{
list: list,
next: 0,
}
}
2018-12-17 15:09:52 +00:00
// PregeneratedIdentity returns a pregenerated identity from a list
2019-01-30 20:47:21 +00:00
func PregeneratedIdentity(index int) (*identity.FullIdentity, error) {
2018-12-17 15:09:52 +00:00
if pregeneratedIdentities.next >= len(pregeneratedIdentities.list) {
return nil, errors.New("out of pregenerated identities")
}
return pregeneratedIdentities.list[index], nil
}
// PregeneratedSignedIdentity returns a signed pregenerated identity from a list
func PregeneratedSignedIdentity(index int) (*identity.FullIdentity, error) {
if pregeneratedIdentities.next >= len(pregeneratedSignedIdentities.list) {
return nil, errors.New("out of signed pregenerated identities")
}
return pregeneratedSignedIdentities.list[index], nil
}
2018-12-17 15:09:52 +00:00
// NewPregeneratedIdentities retruns a new table from provided identities.
func NewPregeneratedIdentities() *Identities {
return pregeneratedIdentities.Clone()
}
// NewPregeneratedSignedIdentities retruns a new table from provided signed identities.
func NewPregeneratedSignedIdentities() *Identities {
return pregeneratedSignedIdentities.Clone()
}
// NewPregeneratedSigner returns the signer for all pregenerated, signed identities
func NewPregeneratedSigner() *identity.FullCertificateAuthority {
return pregeneratedSigner
}
2018-11-03 12:17:14 +00:00
// Clone creates a shallow clone of the table.
func (identities *Identities) Clone() *Identities {
return NewIdentities(identities.list...)
}
// NewIdentity gets a new identity from the list.
2019-01-30 20:47:21 +00:00
func (identities *Identities) NewIdentity() (*identity.FullIdentity, error) {
2018-11-03 12:17:14 +00:00
if identities.next >= len(identities.list) {
return nil, errors.New("out of pregenerated identities")
}
id := identities.list[identities.next]
identities.next++
return id, nil
}
// mustParseIdentityPEM parses pem encoded identity chain and key strings.
func mustParseIdentityPEM(chain, key string) *identity.FullIdentity {
2018-11-03 12:17:14 +00:00
// TODO: add whitelist handling somehow
2019-01-30 20:47:21 +00:00
fi, err := identity.FullIdentityFromPEM([]byte(chain), []byte(key))
2018-11-03 12:17:14 +00:00
if err != nil {
panic(err)
}
return fi
}
// mustParseCertificateAuthorityPEM parses pem encoded certificate authority chain and key strings.
func mustParseCertificateAuthorityPEM(chain, key string) *identity.FullCertificateAuthority {
// TODO: add whitelist handling somehow
fi, err := identity.FullCertificateAuthorityFromPEM([]byte(chain), []byte(key))
if err != nil {
panic(err)
}
return fi
}