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
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
package testidentity
|
2018-11-03 12:17:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
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/storj"
|
2018-11-03 12:17:14 +00:00
|
|
|
)
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
//go:generate go run gen_identities.go -version 1 -count 150 -out V1_identities_table.go
|
|
|
|
//go:generate go run gen_identities.go -signed -version 1 -count 150 -out V1_signed_identities_table.go
|
|
|
|
|
|
|
|
var (
|
|
|
|
// IdentityVersions holds pregenerated identities for each/ identity version.
|
|
|
|
IdentityVersions = VersionedIdentitiesMap{
|
|
|
|
storj.V1: pregeneratedV1Identities,
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignedIdentityVersions holds pregenerated, signed identities for each.
|
|
|
|
// identity version
|
|
|
|
SignedIdentityVersions = VersionedIdentitiesMap{
|
|
|
|
storj.V1: pregeneratedV1SignedIdentities,
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignerVersions holds certificate authorities for each identity version.
|
|
|
|
SignerVersions = VersionedCertificateAuthorityMap{
|
|
|
|
storj.V1: pregeneratedV1Signer,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// VersionedIdentitiesMap maps a `storj.IDVersionNumber` to a set of
|
|
|
|
// pregenerated identities with the corresponding version.
|
|
|
|
type VersionedIdentitiesMap map[storj.IDVersionNumber]*Identities
|
|
|
|
|
|
|
|
// VersionedCertificateAuthorityMap maps a `storj.IDVersionNumber` to a set of
|
|
|
|
// pregenerated certificate authorities used for signing the corresponding
|
|
|
|
// version of signed identities.
|
|
|
|
type VersionedCertificateAuthorityMap map[storj.IDVersionNumber]*identity.FullCertificateAuthority
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 09:17:35 +00:00
|
|
|
// MustPregeneratedIdentity returns a pregenerated identity or panics
|
2019-04-08 19:15:19 +01:00
|
|
|
func MustPregeneratedIdentity(index int, version storj.IDVersion) *identity.FullIdentity {
|
|
|
|
identity, err := PregeneratedIdentity(index, version)
|
2019-03-06 09:17:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return identity
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustPregeneratedSignedIdentity returns a pregenerated identity or panics
|
2019-04-08 19:15:19 +01:00
|
|
|
func MustPregeneratedSignedIdentity(index int, version storj.IDVersion) *identity.FullIdentity {
|
|
|
|
identity, err := PregeneratedSignedIdentity(index, version)
|
2019-03-06 09:17:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return identity
|
|
|
|
}
|
|
|
|
|
2018-12-17 15:09:52 +00:00
|
|
|
// PregeneratedIdentity returns a pregenerated identity from a list
|
2019-04-08 19:15:19 +01:00
|
|
|
func PregeneratedIdentity(index int, version storj.IDVersion) (*identity.FullIdentity, error) {
|
|
|
|
pregeneratedIdentities := IdentityVersions[version.Number]
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-25 07:38:03 +00:00
|
|
|
// PregeneratedSignedIdentity returns a signed pregenerated identity from a list
|
2019-04-08 19:15:19 +01:00
|
|
|
func PregeneratedSignedIdentity(index int, version storj.IDVersion) (*identity.FullIdentity, error) {
|
|
|
|
pregeneratedSignedIdentities := SignedIdentityVersions[version.Number]
|
|
|
|
|
|
|
|
if pregeneratedSignedIdentities.next >= len(pregeneratedSignedIdentities.list) {
|
2019-02-25 07:38:03 +00:00
|
|
|
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.
|
2019-04-08 19:15:19 +01:00
|
|
|
func NewPregeneratedIdentities(version storj.IDVersion) *Identities {
|
|
|
|
return IdentityVersions[version.Number].Clone()
|
2018-12-17 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 07:38:03 +00:00
|
|
|
// NewPregeneratedSignedIdentities retruns a new table from provided signed identities.
|
2019-04-08 19:15:19 +01:00
|
|
|
func NewPregeneratedSignedIdentities(version storj.IDVersion) *Identities {
|
|
|
|
return SignedIdentityVersions[version.Number].Clone()
|
2019-02-25 07:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPregeneratedSigner returns the signer for all pregenerated, signed identities
|
2019-04-08 19:15:19 +01:00
|
|
|
func NewPregeneratedSigner(version storj.IDVersion) *identity.FullCertificateAuthority {
|
|
|
|
//return pregeneratedV1Signer
|
|
|
|
return SignerVersions[version.Number]
|
2019-02-25 07:38:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-25 07:38:03 +00:00
|
|
|
// 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
|
|
|
|
}
|
2019-02-25 07:38:03 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|