2021-07-27 02:01:56 +00:00
|
|
|
package authority
|
|
|
|
|
2021-07-28 01:29:29 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/url"
|
2021-11-12 23:46:34 +00:00
|
|
|
"os"
|
2021-07-28 02:19:58 +00:00
|
|
|
"path/filepath"
|
2021-07-28 01:29:29 +00:00
|
|
|
"strings"
|
2021-07-27 02:01:56 +00:00
|
|
|
|
2021-07-28 01:29:29 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-08-05 03:15:04 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2021-10-18 20:56:24 +00:00
|
|
|
"go.step.sm/cli-utils/step"
|
2021-08-05 03:15:04 +00:00
|
|
|
"go.step.sm/linkedca"
|
2021-07-28 01:29:29 +00:00
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
|
|
)
|
2021-07-27 02:01:56 +00:00
|
|
|
|
2021-07-28 22:22:21 +00:00
|
|
|
// Export creates a linkedca configuration form the current ca.json and loaded
|
|
|
|
// authorities.
|
|
|
|
//
|
|
|
|
// Note that export will not export neither the pki password nor the certificate
|
|
|
|
// issuer password.
|
2021-08-05 03:15:04 +00:00
|
|
|
func (a *Authority) Export() (c *linkedca.Configuration, err error) {
|
2021-07-28 01:29:29 +00:00
|
|
|
// Recover from panics
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = r.(error)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-07-28 02:19:58 +00:00
|
|
|
files := make(map[string][]byte)
|
2021-07-28 22:22:21 +00:00
|
|
|
|
|
|
|
// The exported configuration should not include the password in it.
|
2021-08-05 03:15:04 +00:00
|
|
|
c = &linkedca.Configuration{
|
2021-07-28 02:19:58 +00:00
|
|
|
Version: "1.0",
|
2021-08-06 21:29:54 +00:00
|
|
|
Root: mustReadFilesOrURIs(a.config.Root, files),
|
|
|
|
FederatedRoots: mustReadFilesOrURIs(a.config.FederatedRoots, files),
|
|
|
|
Intermediate: mustReadFileOrURI(a.config.IntermediateCert, files),
|
|
|
|
IntermediateKey: mustReadFileOrURI(a.config.IntermediateKey, files),
|
2021-07-28 01:29:29 +00:00
|
|
|
Address: a.config.Address,
|
|
|
|
InsecureAddress: a.config.InsecureAddress,
|
|
|
|
DnsNames: a.config.DNSNames,
|
|
|
|
Db: mustMarshalToStruct(a.config.DB),
|
|
|
|
Logger: mustMarshalToStruct(a.config.Logger),
|
|
|
|
Monitoring: mustMarshalToStruct(a.config.Monitoring),
|
2021-08-05 03:15:04 +00:00
|
|
|
Authority: &linkedca.Authority{
|
2021-07-28 02:19:58 +00:00
|
|
|
Id: a.config.AuthorityConfig.AuthorityID,
|
|
|
|
EnableAdmin: a.config.AuthorityConfig.EnableAdmin,
|
|
|
|
DisableIssuedAtCheck: a.config.AuthorityConfig.DisableIssuedAtCheck,
|
2021-08-05 03:15:04 +00:00
|
|
|
Backdate: mustDuration(a.config.AuthorityConfig.Backdate),
|
2021-08-11 00:14:17 +00:00
|
|
|
DeploymentType: a.config.AuthorityConfig.DeploymentType,
|
2021-07-28 02:19:58 +00:00
|
|
|
},
|
2021-07-28 22:22:21 +00:00
|
|
|
Files: files,
|
2021-07-28 01:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SSH
|
|
|
|
if v := a.config.SSH; v != nil {
|
2021-08-05 03:15:04 +00:00
|
|
|
c.Ssh = &linkedca.SSH{
|
2021-08-06 21:29:54 +00:00
|
|
|
HostKey: mustReadFileOrURI(v.HostKey, files),
|
|
|
|
UserKey: mustReadFileOrURI(v.UserKey, files),
|
2021-07-28 01:29:29 +00:00
|
|
|
AddUserPrincipal: v.AddUserPrincipal,
|
|
|
|
AddUserCommand: v.AddUserCommand,
|
|
|
|
}
|
|
|
|
for _, k := range v.Keys {
|
2021-08-05 03:15:04 +00:00
|
|
|
typ, ok := linkedca.SSHPublicKey_Type_value[strings.ToUpper(k.Type)]
|
2021-07-28 01:29:29 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("unsupported ssh key type %s", k.Type)
|
|
|
|
}
|
2021-08-05 03:15:04 +00:00
|
|
|
c.Ssh.Keys = append(c.Ssh.Keys, &linkedca.SSHPublicKey{
|
|
|
|
Type: linkedca.SSHPublicKey_Type(typ),
|
2021-07-28 01:29:29 +00:00
|
|
|
Federated: k.Federated,
|
|
|
|
Key: mustMarshalToStruct(k),
|
|
|
|
})
|
|
|
|
}
|
2021-07-28 02:22:29 +00:00
|
|
|
if b := v.Bastion; b != nil {
|
2021-08-05 03:15:04 +00:00
|
|
|
c.Ssh.Bastion = &linkedca.Bastion{
|
2021-07-28 02:22:29 +00:00
|
|
|
Hostname: b.Hostname,
|
|
|
|
User: b.User,
|
|
|
|
Port: b.Port,
|
|
|
|
Command: b.Command,
|
|
|
|
Flags: b.Flags,
|
|
|
|
}
|
|
|
|
}
|
2021-07-28 01:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// KMS
|
|
|
|
if v := a.config.KMS; v != nil {
|
|
|
|
var typ int32
|
|
|
|
var ok bool
|
|
|
|
if v.Type == "" {
|
2021-08-05 03:15:04 +00:00
|
|
|
typ = int32(linkedca.KMS_SOFTKMS)
|
2021-07-28 01:29:29 +00:00
|
|
|
} else {
|
2021-08-05 03:15:04 +00:00
|
|
|
typ, ok = linkedca.KMS_Type_value[strings.ToUpper(v.Type)]
|
2021-07-28 01:29:29 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("unsupported kms type %s", v.Type)
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 03:15:04 +00:00
|
|
|
c.Kms = &linkedca.KMS{
|
|
|
|
Type: linkedca.KMS_Type(typ),
|
2021-07-28 01:29:29 +00:00
|
|
|
CredentialsFile: v.CredentialsFile,
|
|
|
|
Uri: v.URI,
|
|
|
|
Pin: v.Pin,
|
|
|
|
ManagementKey: v.ManagementKey,
|
|
|
|
Region: v.Region,
|
|
|
|
Profile: v.Profile,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authority
|
|
|
|
// cas options
|
|
|
|
if v := a.config.AuthorityConfig.Options; v != nil {
|
|
|
|
c.Authority.Type = 0
|
|
|
|
c.Authority.CertificateAuthority = v.CertificateAuthority
|
|
|
|
c.Authority.CertificateAuthorityFingerprint = v.CertificateAuthorityFingerprint
|
|
|
|
c.Authority.CredentialsFile = v.CredentialsFile
|
|
|
|
if iss := v.CertificateIssuer; iss != nil {
|
2021-08-05 03:15:04 +00:00
|
|
|
typ, ok := linkedca.CertificateIssuer_Type_value[strings.ToUpper(iss.Type)]
|
2021-07-28 01:29:29 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("unknown certificate issuer type %s", iss.Type)
|
|
|
|
}
|
2021-08-23 22:18:54 +00:00
|
|
|
// The exported certificate issuer should not include the password.
|
2021-08-05 03:15:04 +00:00
|
|
|
c.Authority.CertificateIssuer = &linkedca.CertificateIssuer{
|
|
|
|
Type: linkedca.CertificateIssuer_Type(typ),
|
2021-07-28 01:29:29 +00:00
|
|
|
Provisioner: iss.Provisioner,
|
2021-08-06 21:29:54 +00:00
|
|
|
Certificate: mustReadFileOrURI(iss.Certificate, files),
|
|
|
|
Key: mustReadFileOrURI(iss.Key, files),
|
2021-07-28 01:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// admins
|
2021-07-27 02:01:56 +00:00
|
|
|
for {
|
|
|
|
list, cursor := a.admins.Find("", 100)
|
2021-07-28 01:29:29 +00:00
|
|
|
c.Authority.Admins = append(c.Authority.Admins, list...)
|
2021-07-27 02:01:56 +00:00
|
|
|
if cursor == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-07-28 01:29:29 +00:00
|
|
|
// provisioners
|
2021-07-27 02:01:56 +00:00
|
|
|
for {
|
|
|
|
list, cursor := a.provisioners.Find("", 100)
|
|
|
|
for _, p := range list {
|
|
|
|
lp, err := ProvisionerToLinkedca(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-28 01:29:29 +00:00
|
|
|
c.Authority.Provisioners = append(c.Authority.Provisioners, lp)
|
2021-07-27 02:01:56 +00:00
|
|
|
}
|
|
|
|
if cursor == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-07-28 02:19:58 +00:00
|
|
|
// global claims
|
2021-07-28 01:29:29 +00:00
|
|
|
c.Authority.Claims = claimsToLinkedca(a.config.AuthorityConfig.Claims)
|
2021-08-23 22:18:54 +00:00
|
|
|
// Distinguished names template
|
2021-07-28 02:19:58 +00:00
|
|
|
if v := a.config.AuthorityConfig.Template; v != nil {
|
2021-08-05 03:15:04 +00:00
|
|
|
c.Authority.Template = &linkedca.DistinguishedName{
|
2021-07-28 02:19:58 +00:00
|
|
|
Country: v.Country,
|
|
|
|
Organization: v.Organization,
|
|
|
|
OrganizationalUnit: v.OrganizationalUnit,
|
|
|
|
Locality: v.Locality,
|
|
|
|
Province: v.Province,
|
|
|
|
StreetAddress: v.StreetAddress,
|
|
|
|
SerialNumber: v.SerialNumber,
|
|
|
|
CommonName: v.CommonName,
|
|
|
|
}
|
|
|
|
}
|
2021-07-27 02:01:56 +00:00
|
|
|
|
2021-07-28 01:29:29 +00:00
|
|
|
// TLS
|
|
|
|
if v := a.config.TLS; v != nil {
|
2021-08-05 03:15:04 +00:00
|
|
|
c.Tls = &linkedca.TLS{
|
2021-07-28 01:29:29 +00:00
|
|
|
MinVersion: v.MinVersion.String(),
|
|
|
|
MaxVersion: v.MaxVersion.String(),
|
|
|
|
Renegotiation: v.Renegotiation,
|
|
|
|
}
|
|
|
|
for _, cs := range v.CipherSuites.Value() {
|
2021-08-05 03:15:04 +00:00
|
|
|
c.Tls.CipherSuites = append(c.Tls.CipherSuites, linkedca.TLS_CiperSuite(cs))
|
2021-07-28 01:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-27 02:01:56 +00:00
|
|
|
|
2021-07-28 01:29:29 +00:00
|
|
|
// Templates
|
|
|
|
if v := a.config.Templates; v != nil {
|
2021-08-05 03:15:04 +00:00
|
|
|
c.Templates = &linkedca.ConfigTemplates{
|
|
|
|
Ssh: &linkedca.SSHConfigTemplate{},
|
2021-07-28 01:29:29 +00:00
|
|
|
Data: mustMarshalToStruct(v.Data),
|
|
|
|
}
|
|
|
|
// Remove automatically loaded vars
|
|
|
|
if c.Templates.Data != nil && c.Templates.Data.Fields != nil {
|
|
|
|
delete(c.Templates.Data.Fields, "Step")
|
|
|
|
}
|
|
|
|
for _, t := range v.SSH.Host {
|
2021-08-05 03:15:04 +00:00
|
|
|
typ, ok := linkedca.ConfigTemplate_Type_value[strings.ToUpper(string(t.Type))]
|
2021-07-28 01:29:29 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("unsupported template type %s", t.Type)
|
|
|
|
}
|
2021-08-05 03:15:04 +00:00
|
|
|
c.Templates.Ssh.Hosts = append(c.Templates.Ssh.Hosts, &linkedca.ConfigTemplate{
|
|
|
|
Type: linkedca.ConfigTemplate_Type(typ),
|
2021-07-28 01:29:29 +00:00
|
|
|
Name: t.Name,
|
2021-08-06 21:29:54 +00:00
|
|
|
Template: mustReadFileOrURI(t.TemplatePath, files),
|
2021-07-28 01:29:29 +00:00
|
|
|
Path: t.Path,
|
|
|
|
Comment: t.Comment,
|
|
|
|
Requires: t.RequiredData,
|
2021-07-28 02:19:58 +00:00
|
|
|
Content: t.Content,
|
2021-07-28 01:29:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, t := range v.SSH.User {
|
2021-08-05 03:15:04 +00:00
|
|
|
typ, ok := linkedca.ConfigTemplate_Type_value[strings.ToUpper(string(t.Type))]
|
2021-07-28 01:29:29 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("unsupported template type %s", t.Type)
|
|
|
|
}
|
2021-08-05 03:15:04 +00:00
|
|
|
c.Templates.Ssh.Users = append(c.Templates.Ssh.Users, &linkedca.ConfigTemplate{
|
|
|
|
Type: linkedca.ConfigTemplate_Type(typ),
|
2021-07-28 01:29:29 +00:00
|
|
|
Name: t.Name,
|
2021-08-06 21:29:54 +00:00
|
|
|
Template: mustReadFileOrURI(t.TemplatePath, files),
|
2021-07-28 01:29:29 +00:00
|
|
|
Path: t.Path,
|
|
|
|
Comment: t.Comment,
|
|
|
|
Requires: t.RequiredData,
|
2021-07-28 02:19:58 +00:00
|
|
|
Content: t.Content,
|
2021-07-28 01:29:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2021-08-05 03:15:04 +00:00
|
|
|
func mustDuration(d *provisioner.Duration) string {
|
|
|
|
if d == nil || d.Duration == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return d.String()
|
|
|
|
}
|
|
|
|
|
2021-07-28 01:29:29 +00:00
|
|
|
func mustMarshalToStruct(v interface{}) *structpb.Struct {
|
|
|
|
b, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrapf(err, "error marshaling %T", v))
|
|
|
|
}
|
|
|
|
var r *structpb.Struct
|
|
|
|
if err := json.Unmarshal(b, &r); err != nil {
|
|
|
|
panic(errors.Wrapf(err, "error unmarshaling %T", v))
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:29:54 +00:00
|
|
|
func mustReadFileOrURI(fn string, m map[string][]byte) string {
|
2021-07-28 01:29:29 +00:00
|
|
|
if fn == "" {
|
2021-07-28 02:19:58 +00:00
|
|
|
return ""
|
2021-07-28 01:29:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:56:24 +00:00
|
|
|
stepPath := filepath.ToSlash(step.Path())
|
2021-07-28 02:19:58 +00:00
|
|
|
if !strings.HasSuffix(stepPath, "/") {
|
|
|
|
stepPath += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn = strings.TrimPrefix(filepath.ToSlash(fn), stepPath)
|
|
|
|
|
2021-07-28 01:29:29 +00:00
|
|
|
ok, err := isFilename(fn)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if ok {
|
2021-11-17 19:40:01 +00:00
|
|
|
b, err := os.ReadFile(step.Abs(fn))
|
2021-07-28 01:29:29 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrapf(err, "error reading %s", fn))
|
|
|
|
}
|
2021-07-28 02:19:58 +00:00
|
|
|
m[fn] = b
|
|
|
|
return fn
|
2021-07-28 01:29:29 +00:00
|
|
|
}
|
2021-07-28 02:19:58 +00:00
|
|
|
return fn
|
2021-07-28 01:29:29 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 21:29:54 +00:00
|
|
|
func mustReadFilesOrURIs(fns []string, m map[string][]byte) []string {
|
2021-07-28 02:19:58 +00:00
|
|
|
var result []string
|
2021-07-28 01:29:29 +00:00
|
|
|
for _, fn := range fns {
|
2021-08-06 21:29:54 +00:00
|
|
|
result = append(result, mustReadFileOrURI(fn, m))
|
2021-07-28 01:29:29 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func isFilename(fn string) (bool, error) {
|
|
|
|
u, err := url.Parse(fn)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrapf(err, "error parsing %s", fn)
|
|
|
|
}
|
|
|
|
return u.Scheme == "" || u.Scheme == "file", nil
|
2021-07-27 02:01:56 +00:00
|
|
|
}
|