Add collection of files and authority template.

This commit is contained in:
Mariano Cano 2021-07-27 19:19:58 -07:00
parent c7f8516142
commit 0730a165fd
3 changed files with 59 additions and 35 deletions

View file

@ -75,6 +75,7 @@ type ASN1DN struct {
Locality string `json:"locality,omitempty"` Locality string `json:"locality,omitempty"`
Province string `json:"province,omitempty"` Province string `json:"province,omitempty"`
StreetAddress string `json:"streetAddress,omitempty"` StreetAddress string `json:"streetAddress,omitempty"`
SerialNumber string `json:"serialNumber,omitempty"`
CommonName string `json:"commonName,omitempty"` CommonName string `json:"commonName,omitempty"`
} }

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"path/filepath"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -20,26 +21,34 @@ func (a *Authority) Export() (c *config.Configuration, err error) {
} }
}() }()
files := make(map[string][]byte)
c = &config.Configuration{ c = &config.Configuration{
Root: mustReadFilesOrUris(a.config.Root), Version: "1.0",
FederatedRoots: mustReadFilesOrUris(a.config.FederatedRoots), Root: mustReadFilesOrUris(a.config.Root, files),
Intermediate: mustReadFileOrUri(a.config.IntermediateCert), FederatedRoots: mustReadFilesOrUris(a.config.FederatedRoots, files),
IntermediateKey: mustReadFileOrUri(a.config.IntermediateKey), Intermediate: mustReadFileOrUri(a.config.IntermediateCert, files),
IntermediateKey: mustReadFileOrUri(a.config.IntermediateKey, files),
Address: a.config.Address, Address: a.config.Address,
InsecureAddress: a.config.InsecureAddress, InsecureAddress: a.config.InsecureAddress,
DnsNames: a.config.DNSNames, DnsNames: a.config.DNSNames,
Db: mustMarshalToStruct(a.config.DB), Db: mustMarshalToStruct(a.config.DB),
Logger: mustMarshalToStruct(a.config.Logger), Logger: mustMarshalToStruct(a.config.Logger),
Monitoring: mustMarshalToStruct(a.config.Monitoring), Monitoring: mustMarshalToStruct(a.config.Monitoring),
Authority: &config.Authority{}, Authority: &config.Authority{
Password: mustPassword(a.config.Password), Id: a.config.AuthorityConfig.AuthorityID,
EnableAdmin: a.config.AuthorityConfig.EnableAdmin,
DisableIssuedAtCheck: a.config.AuthorityConfig.DisableIssuedAtCheck,
Backdate: a.config.AuthorityConfig.Backdate.String(),
},
Password: mustPassword(a.config.Password),
Files: files,
} }
// SSH // SSH
if v := a.config.SSH; v != nil { if v := a.config.SSH; v != nil {
c.Ssh = &config.SSH{ c.Ssh = &config.SSH{
HostKey: mustReadFileOrUri(v.HostKey), HostKey: mustReadFileOrUri(v.HostKey, files),
UserKey: mustReadFileOrUri(v.UserKey), UserKey: mustReadFileOrUri(v.UserKey, files),
AddUserPrincipal: v.AddUserPrincipal, AddUserPrincipal: v.AddUserPrincipal,
AddUserCommand: v.AddUserCommand, AddUserCommand: v.AddUserCommand,
} }
@ -80,8 +89,6 @@ func (a *Authority) Export() (c *config.Configuration, err error) {
} }
// Authority // Authority
c.Authority.Id = a.config.AuthorityConfig.AuthorityID
// cas options // cas options
if v := a.config.AuthorityConfig.Options; v != nil { if v := a.config.AuthorityConfig.Options; v != nil {
c.Authority.Type = 0 c.Authority.Type = 0
@ -96,13 +103,12 @@ func (a *Authority) Export() (c *config.Configuration, err error) {
c.Authority.CertificateIssuer = &config.CertificateIssuer{ c.Authority.CertificateIssuer = &config.CertificateIssuer{
Type: config.CertificateIssuer_Type(typ), Type: config.CertificateIssuer_Type(typ),
Provisioner: iss.Provisioner, Provisioner: iss.Provisioner,
Certificate: mustReadFileOrUri(iss.Certificate), Certificate: mustReadFileOrUri(iss.Certificate, files),
Key: mustReadFileOrUri(iss.Key), Key: mustReadFileOrUri(iss.Key, files),
Password: mustPassword(iss.Password), Password: mustPassword(iss.Password),
} }
} }
} }
// admins // admins
for { for {
list, cursor := a.admins.Find("", 100) list, cursor := a.admins.Find("", 100)
@ -111,7 +117,6 @@ func (a *Authority) Export() (c *config.Configuration, err error) {
break break
} }
} }
// provisioners // provisioners
for { for {
list, cursor := a.provisioners.Find("", 100) list, cursor := a.provisioners.Find("", 100)
@ -126,7 +131,21 @@ func (a *Authority) Export() (c *config.Configuration, err error) {
break break
} }
} }
// global claims
c.Authority.Claims = claimsToLinkedca(a.config.AuthorityConfig.Claims) c.Authority.Claims = claimsToLinkedca(a.config.AuthorityConfig.Claims)
// Distiguised names template
if v := a.config.AuthorityConfig.Template; v != nil {
c.Authority.Template = &config.DistinguishedName{
Country: v.Country,
Organization: v.Organization,
OrganizationalUnit: v.OrganizationalUnit,
Locality: v.Locality,
Province: v.Province,
StreetAddress: v.StreetAddress,
SerialNumber: v.SerialNumber,
CommonName: v.CommonName,
}
}
// TLS // TLS
if v := a.config.TLS; v != nil { if v := a.config.TLS; v != nil {
@ -155,18 +174,14 @@ func (a *Authority) Export() (c *config.Configuration, err error) {
if !ok { if !ok {
return nil, errors.Errorf("unsupported template type %s", t.Type) return nil, errors.Errorf("unsupported template type %s", t.Type)
} }
content := t.Content
if len(content) == 0 {
content = mustReadFileOrUri(t.TemplatePath)
}
c.Templates.Ssh.Hosts = append(c.Templates.Ssh.Hosts, &config.Template{ c.Templates.Ssh.Hosts = append(c.Templates.Ssh.Hosts, &config.Template{
Type: config.Template_Type(typ), Type: config.Template_Type(typ),
Name: t.Name, Name: t.Name,
Template: t.TemplatePath, Template: mustReadFileOrUri(t.TemplatePath, files),
Path: t.Path, Path: t.Path,
Comment: t.Comment, Comment: t.Comment,
Requires: t.RequiredData, Requires: t.RequiredData,
Content: content, Content: t.Content,
}) })
} }
for _, t := range v.SSH.User { for _, t := range v.SSH.User {
@ -174,18 +189,14 @@ func (a *Authority) Export() (c *config.Configuration, err error) {
if !ok { if !ok {
return nil, errors.Errorf("unsupported template type %s", t.Type) return nil, errors.Errorf("unsupported template type %s", t.Type)
} }
content := t.Content
if len(content) == 0 {
content = mustReadFileOrUri(t.TemplatePath)
}
c.Templates.Ssh.Users = append(c.Templates.Ssh.Users, &config.Template{ c.Templates.Ssh.Users = append(c.Templates.Ssh.Users, &config.Template{
Type: config.Template_Type(typ), Type: config.Template_Type(typ),
Name: t.Name, Name: t.Name,
Template: t.TemplatePath, Template: mustReadFileOrUri(t.TemplatePath, files),
Path: t.Path, Path: t.Path,
Comment: t.Comment, Comment: t.Comment,
Requires: t.RequiredData, Requires: t.RequiredData,
Content: content, Content: t.Content,
}) })
} }
} }
@ -212,11 +223,18 @@ func mustMarshalToStruct(v interface{}) *structpb.Struct {
return r return r
} }
func mustReadFileOrUri(fn string) []byte { func mustReadFileOrUri(fn string, m map[string][]byte) string {
if fn == "" { if fn == "" {
return nil return ""
} }
stepPath := filepath.ToSlash(step.StepPath())
if !strings.HasSuffix(stepPath, "/") {
stepPath += "/"
}
fn = strings.TrimPrefix(filepath.ToSlash(fn), stepPath)
ok, err := isFilename(fn) ok, err := isFilename(fn)
if err != nil { if err != nil {
panic(err) panic(err)
@ -226,15 +244,16 @@ func mustReadFileOrUri(fn string) []byte {
if err != nil { if err != nil {
panic(errors.Wrapf(err, "error reading %s", fn)) panic(errors.Wrapf(err, "error reading %s", fn))
} }
return b m[fn] = b
return fn
} }
return []byte(fn) return fn
} }
func mustReadFilesOrUris(fns []string) [][]byte { func mustReadFilesOrUris(fns []string, m map[string][]byte) []string {
var result [][]byte var result []string
for _, fn := range fns { for _, fn := range fns {
result = append(result, mustReadFileOrUri(fn)) result = append(result, mustReadFileOrUri(fn, m))
} }
return result return result
} }

View file

@ -36,7 +36,6 @@ func withDefaultASN1DN(def *config.ASN1DN) provisioner.CertificateModifierFunc {
if def == nil { if def == nil {
return errors.New("default ASN1DN template cannot be nil") return errors.New("default ASN1DN template cannot be nil")
} }
if len(crt.Subject.Country) == 0 && def.Country != "" { if len(crt.Subject.Country) == 0 && def.Country != "" {
crt.Subject.Country = append(crt.Subject.Country, def.Country) crt.Subject.Country = append(crt.Subject.Country, def.Country)
} }
@ -55,7 +54,12 @@ func withDefaultASN1DN(def *config.ASN1DN) provisioner.CertificateModifierFunc {
if len(crt.Subject.StreetAddress) == 0 && def.StreetAddress != "" { if len(crt.Subject.StreetAddress) == 0 && def.StreetAddress != "" {
crt.Subject.StreetAddress = append(crt.Subject.StreetAddress, def.StreetAddress) crt.Subject.StreetAddress = append(crt.Subject.StreetAddress, def.StreetAddress)
} }
if len(crt.Subject.SerialNumber) == 0 && def.SerialNumber != "" {
crt.Subject.SerialNumber = def.SerialNumber
}
if len(crt.Subject.CommonName) == 0 && def.CommonName != "" {
crt.Subject.CommonName = def.CommonName
}
return nil return nil
} }
} }