forked from TrueCloudLab/certificates
Remove provisioner intermediate type.
This commit is contained in:
parent
1671ab2590
commit
507fd01062
5 changed files with 45 additions and 100 deletions
|
@ -31,7 +31,7 @@ type Authority interface {
|
||||||
Root(shasum string) (*x509.Certificate, error)
|
Root(shasum string) (*x509.Certificate, error)
|
||||||
Sign(cr *x509.CertificateRequest, signOpts authority.SignOptions, extraOpts ...provisioner.SignOption) (*x509.Certificate, *x509.Certificate, error)
|
Sign(cr *x509.CertificateRequest, signOpts authority.SignOptions, extraOpts ...provisioner.SignOption) (*x509.Certificate, *x509.Certificate, error)
|
||||||
Renew(peer *x509.Certificate) (*x509.Certificate, *x509.Certificate, error)
|
Renew(peer *x509.Certificate) (*x509.Certificate, *x509.Certificate, error)
|
||||||
GetProvisioners(cursor string, limit int) ([]*provisioner.Provisioner, string, error)
|
GetProvisioners(cursor string, limit int) (provisioner.List, string, error)
|
||||||
GetEncryptedKey(kid string) (string, error)
|
GetEncryptedKey(kid string) (string, error)
|
||||||
GetRoots() (federation []*x509.Certificate, err error)
|
GetRoots() (federation []*x509.Certificate, err error)
|
||||||
GetFederation() ([]*x509.Certificate, error)
|
GetFederation() ([]*x509.Certificate, error)
|
||||||
|
@ -162,8 +162,8 @@ type SignRequest struct {
|
||||||
// ProvisionersResponse is the response object that returns the list of
|
// ProvisionersResponse is the response object that returns the list of
|
||||||
// provisioners.
|
// provisioners.
|
||||||
type ProvisionersResponse struct {
|
type ProvisionersResponse struct {
|
||||||
Provisioners []*provisioner.Provisioner `json:"provisioners"`
|
Provisioners provisioner.List `json:"provisioners"`
|
||||||
NextCursor string `json:"nextCursor"`
|
NextCursor string `json:"nextCursor"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProvisionerKeyResponse is the response object that returns the encryptoed key
|
// ProvisionerKeyResponse is the response object that returns the encryptoed key
|
||||||
|
|
|
@ -52,10 +52,10 @@ type Config struct {
|
||||||
|
|
||||||
// AuthConfig represents the configuration options for the authority.
|
// AuthConfig represents the configuration options for the authority.
|
||||||
type AuthConfig struct {
|
type AuthConfig struct {
|
||||||
Provisioners []*provisioner.Provisioner `json:"provisioners"`
|
Provisioners provisioner.List `json:"provisioners"`
|
||||||
Template *x509util.ASN1DN `json:"template,omitempty"`
|
Template *x509util.ASN1DN `json:"template,omitempty"`
|
||||||
Claims *provisioner.Claims `json:"claims,omitempty"`
|
Claims *provisioner.Claims `json:"claims,omitempty"`
|
||||||
DisableIssuedAtCheck bool `json:"disableIssuedAtCheck,omitempty"`
|
DisableIssuedAtCheck bool `json:"disableIssuedAtCheck,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate validates the authority configuration.
|
// Validate validates the authority configuration.
|
||||||
|
|
|
@ -23,7 +23,7 @@ const DefaultProvisionersLimit = 20
|
||||||
const DefaultProvisionersMax = 100
|
const DefaultProvisionersMax = 100
|
||||||
|
|
||||||
type uidProvisioner struct {
|
type uidProvisioner struct {
|
||||||
provisioner *Provisioner
|
provisioner Interface
|
||||||
uid string
|
uid string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,12 +52,12 @@ func NewCollection(audiences []string) *Collection {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load a provisioner by the ID.
|
// Load a provisioner by the ID.
|
||||||
func (c *Collection) Load(id string) (*Provisioner, bool) {
|
func (c *Collection) Load(id string) (Interface, bool) {
|
||||||
return loadProvisioner(c.byID, id)
|
return loadProvisioner(c.byID, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadByToken parses the token claims and loads the provisioner associated.
|
// LoadByToken parses the token claims and loads the provisioner associated.
|
||||||
func (c *Collection) LoadByToken(token *jose.JSONWebToken, claims *jose.Claims) (*Provisioner, bool) {
|
func (c *Collection) LoadByToken(token *jose.JSONWebToken, claims *jose.Claims) (Interface, bool) {
|
||||||
// match with server audiences
|
// match with server audiences
|
||||||
if matchesAudience(claims.Audience, c.audiences) {
|
if matchesAudience(claims.Audience, c.audiences) {
|
||||||
// If matches with stored audiences it will be a JWT token (default), and
|
// If matches with stored audiences it will be a JWT token (default), and
|
||||||
|
@ -82,7 +82,7 @@ func (c *Collection) LoadByToken(token *jose.JSONWebToken, claims *jose.Claims)
|
||||||
|
|
||||||
// LoadByCertificate lookds for the provisioner extension and extracts the
|
// LoadByCertificate lookds for the provisioner extension and extracts the
|
||||||
// proper id to load the provisioner.
|
// proper id to load the provisioner.
|
||||||
func (c *Collection) LoadByCertificate(cert *x509.Certificate) (*Provisioner, bool) {
|
func (c *Collection) LoadByCertificate(cert *x509.Certificate) (Interface, bool) {
|
||||||
for _, e := range cert.Extensions {
|
for _, e := range cert.Extensions {
|
||||||
if e.Id.Equal(stepOIDProvisioner) {
|
if e.Id.Equal(stepOIDProvisioner) {
|
||||||
var provisioner stepProvisionerASN1
|
var provisioner stepProvisionerASN1
|
||||||
|
@ -111,7 +111,7 @@ func (c *Collection) LoadEncryptedKey(keyID string) (string, bool) {
|
||||||
|
|
||||||
// Store adds a provisioner to the collection, it makes sure two provisioner
|
// Store adds a provisioner to the collection, it makes sure two provisioner
|
||||||
// does not have the same ID.
|
// does not have the same ID.
|
||||||
func (c *Collection) Store(p *Provisioner) error {
|
func (c *Collection) Store(p Interface) error {
|
||||||
// Store provisioner always in byID. ID must be unique.
|
// Store provisioner always in byID. ID must be unique.
|
||||||
if _, loaded := c.byID.LoadOrStore(p.GetID(), p); loaded == true {
|
if _, loaded := c.byID.LoadOrStore(p.GetID(), p); loaded == true {
|
||||||
return errors.New("cannot add multiple provisioners with the same id")
|
return errors.New("cannot add multiple provisioners with the same id")
|
||||||
|
@ -142,7 +142,7 @@ func (c *Collection) Store(p *Provisioner) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find implements pagination on a list of sorted provisioners.
|
// Find implements pagination on a list of sorted provisioners.
|
||||||
func (c *Collection) Find(cursor string, limit int) ([]*Provisioner, string) {
|
func (c *Collection) Find(cursor string, limit int) (List, string) {
|
||||||
switch {
|
switch {
|
||||||
case limit <= 0:
|
case limit <= 0:
|
||||||
limit = DefaultProvisionersLimit
|
limit = DefaultProvisionersLimit
|
||||||
|
@ -154,7 +154,7 @@ func (c *Collection) Find(cursor string, limit int) ([]*Provisioner, string) {
|
||||||
cursor = fmt.Sprintf("%040s", cursor)
|
cursor = fmt.Sprintf("%040s", cursor)
|
||||||
i := sort.Search(n, func(i int) bool { return c.sorted[i].uid >= cursor })
|
i := sort.Search(n, func(i int) bool { return c.sorted[i].uid >= cursor })
|
||||||
|
|
||||||
slice := []*Provisioner{}
|
slice := List{}
|
||||||
for ; i < n && len(slice) < limit; i++ {
|
for ; i < n && len(slice) < limit; i++ {
|
||||||
slice = append(slice, c.sorted[i].provisioner)
|
slice = append(slice, c.sorted[i].provisioner)
|
||||||
}
|
}
|
||||||
|
@ -165,12 +165,12 @@ func (c *Collection) Find(cursor string, limit int) ([]*Provisioner, string) {
|
||||||
return slice, ""
|
return slice, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadProvisioner(m *sync.Map, key string) (*Provisioner, bool) {
|
func loadProvisioner(m *sync.Map, key string) (Interface, bool) {
|
||||||
i, ok := m.Load(key)
|
i, ok := m.Load(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
p, ok := i.(*Provisioner)
|
p, ok := i.(Interface)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
@ -179,7 +179,7 @@ func loadProvisioner(m *sync.Map, key string) (*Provisioner, bool) {
|
||||||
|
|
||||||
// provisionerSum returns the SHA1 of the provisioners ID. From this we will
|
// provisionerSum returns the SHA1 of the provisioners ID. From this we will
|
||||||
// create the unique and sorted id.
|
// create the unique and sorted id.
|
||||||
func provisionerSum(p *Provisioner) ([]byte, error) {
|
func provisionerSum(p Interface) ([]byte, error) {
|
||||||
sum := sha1.Sum([]byte(p.GetID()))
|
sum := sha1.Sum([]byte(p.GetID()))
|
||||||
return sum[:], nil
|
return sum[:], nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,91 +44,36 @@ type provisioner struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provisioner implements the provisioner.Interface on a base provisioner. It
|
// List represents a list of provisioners.
|
||||||
// also implements custom marshalers and unmarshalers so different provisioners
|
type List []Interface
|
||||||
// can be represented in a configuration type.
|
|
||||||
type Provisioner struct {
|
|
||||||
base Interface
|
|
||||||
}
|
|
||||||
|
|
||||||
// New creates a new provisioner from the base provisioner.
|
// UnmarshalJSON implements json.Unmarshaler and allows to unmarshal a list of a
|
||||||
func New(base Interface) *Provisioner {
|
// interfaces into the right type.
|
||||||
return &Provisioner{
|
func (l *List) UnmarshalJSON(data []byte) error {
|
||||||
base: base,
|
ps := []json.RawMessage{}
|
||||||
}
|
if err := json.Unmarshal(data, &ps); err != nil {
|
||||||
}
|
return errors.Wrap(err, "error unmarshaling provisioner list")
|
||||||
|
|
||||||
// Base returns the base type of the provisioner.
|
|
||||||
func (p *Provisioner) Base() Interface {
|
|
||||||
return p.base
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetID returns the base provisioner unique ID. This identifier is used as the
|
|
||||||
// key in a provisioner.Collection.
|
|
||||||
func (p *Provisioner) GetID() string {
|
|
||||||
return p.base.GetID()
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetEncryptedKey returns the base provisioner encrypted key if it's defined.
|
|
||||||
func (p *Provisioner) GetEncryptedKey() (string, string, bool) {
|
|
||||||
return p.base.GetEncryptedKey()
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetName returns the name of the provisioner
|
|
||||||
func (p *Provisioner) GetName() string {
|
|
||||||
return p.base.GetName()
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetType return the provisioners type.
|
|
||||||
func (p *Provisioner) GetType() Type {
|
|
||||||
return p.base.GetType()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init initializes the base provisioner with the given claims.
|
|
||||||
func (p *Provisioner) Init(c Config) error {
|
|
||||||
return p.base.Init(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Authorize validates the given token on the base provisioner returning a list
|
|
||||||
// of options to validate the signing request.
|
|
||||||
func (p *Provisioner) Authorize(token string) ([]SignOption, error) {
|
|
||||||
return p.base.Authorize(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AuthorizeRenewal checks if the base provisioner authorizes the renewal.
|
|
||||||
func (p *Provisioner) AuthorizeRenewal(cert *x509.Certificate) error {
|
|
||||||
return p.base.AuthorizeRenewal(cert)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AuthorizeRevoke checks on the base provisioner if the given token has revoke
|
|
||||||
// access.
|
|
||||||
func (p *Provisioner) AuthorizeRevoke(token string) error {
|
|
||||||
return p.base.AuthorizeRevoke(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalJSON implements the json.Marshaler interface on the Provisioner type.
|
|
||||||
func (p *Provisioner) MarshalJSON() ([]byte, error) {
|
|
||||||
return json.Marshal(p.base)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON implements the json.Unmarshaler interface on the Provisioner
|
|
||||||
// type.
|
|
||||||
func (p *Provisioner) UnmarshalJSON(data []byte) error {
|
|
||||||
var typ provisioner
|
|
||||||
if err := json.Unmarshal(data, &typ); err != nil {
|
|
||||||
return errors.Errorf("error unmarshaling provisioner")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch strings.ToLower(typ.Type) {
|
for _, data := range ps {
|
||||||
case "jwk":
|
var typ provisioner
|
||||||
p.base = &JWK{}
|
if err := json.Unmarshal(data, &typ); err != nil {
|
||||||
case "oidc":
|
return errors.Errorf("error unmarshaling provisioner")
|
||||||
p.base = &OIDC{}
|
}
|
||||||
default:
|
var p Interface
|
||||||
return errors.Errorf("provisioner type %s not supported", typ.Type)
|
switch strings.ToLower(typ.Type) {
|
||||||
}
|
case "jwk":
|
||||||
if err := json.Unmarshal(data, &p.base); err != nil {
|
p = &JWK{}
|
||||||
return errors.Errorf("error unmarshaling provisioner")
|
case "oidc":
|
||||||
|
p = &OIDC{}
|
||||||
|
default:
|
||||||
|
return errors.Errorf("provisioner type %s not supported", typ.Type)
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(data, p); err != nil {
|
||||||
|
return errors.Errorf("error unmarshaling provisioner")
|
||||||
|
}
|
||||||
|
*l = append(*l, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ func (a *Authority) GetEncryptedKey(kid string) (string, error) {
|
||||||
|
|
||||||
// GetProvisioners returns a map listing each provisioner and the JWK Key Set
|
// GetProvisioners returns a map listing each provisioner and the JWK Key Set
|
||||||
// with their public keys.
|
// with their public keys.
|
||||||
func (a *Authority) GetProvisioners(cursor string, limit int) ([]*provisioner.Provisioner, string, error) {
|
func (a *Authority) GetProvisioners(cursor string, limit int) (provisioner.List, string, error) {
|
||||||
provisioners, nextCursor := a.provisioners.Find(cursor, limit)
|
provisioners, nextCursor := a.provisioners.Find(cursor, limit)
|
||||||
return provisioners, nextCursor, nil
|
return provisioners, nextCursor, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue