Fixes for bugs related to rendering certificate templates from defaults and variables.
This commit is contained in:
44
ca.go
44
ca.go
@@ -412,8 +412,9 @@ func InitCA() error {
|
|||||||
NotBefore: now,
|
NotBefore: now,
|
||||||
NotAfter: now.Add(validity),
|
NotAfter: now.Add(validity),
|
||||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
|
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
|
||||||
BasicConstraintsValid: true,
|
BasicConstraintsValid: true, // This is a CA certificate
|
||||||
IsCA: true,
|
IsCA: true, // This is a CA certificate
|
||||||
|
MaxPathLenZero: true, // Allow issuing end-entity certificates
|
||||||
}
|
}
|
||||||
// Add email if present
|
// Add email if present
|
||||||
if caConfig.Email != "" {
|
if caConfig.Email != "" {
|
||||||
@@ -512,11 +513,13 @@ func issueSingleCertificate(def CertificateDefinition) error {
|
|||||||
expires := dateIssued.Add(validityDur)
|
expires := dateIssued.Add(validityDur)
|
||||||
|
|
||||||
certTmpl := x509.Certificate{
|
certTmpl := x509.Certificate{
|
||||||
SerialNumber: serialNumber,
|
SerialNumber: serialNumber,
|
||||||
Subject: subjectPKIX,
|
Subject: subjectPKIX,
|
||||||
NotBefore: dateIssued,
|
NotBefore: dateIssued,
|
||||||
NotAfter: expires,
|
NotAfter: expires,
|
||||||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
|
||||||
|
IsCA: false,
|
||||||
|
BasicConstraintsValid: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, s := range def.SAN {
|
for _, s := range def.SAN {
|
||||||
@@ -571,11 +574,11 @@ func issueSingleCertificate(def CertificateDefinition) error {
|
|||||||
if verbose {
|
if verbose {
|
||||||
fmt.Printf(`
|
fmt.Printf(`
|
||||||
Certificate:
|
Certificate:
|
||||||
Name: %s
|
Name: %s
|
||||||
Subject: %s
|
Subject: %s
|
||||||
Type: %s
|
Type: %s
|
||||||
Validity: %s
|
Validity: %s
|
||||||
SAN: %v
|
SAN: %v
|
||||||
`,
|
`,
|
||||||
def.Name,
|
def.Name,
|
||||||
def.Subject,
|
def.Subject,
|
||||||
@@ -584,6 +587,7 @@ Certificate:
|
|||||||
def.SAN,
|
def.SAN,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
caState.UpdateCAStateAfterIssue(
|
caState.UpdateCAStateAfterIssue(
|
||||||
caConfig.SerialType,
|
caConfig.SerialType,
|
||||||
def.Name,
|
def.Name,
|
||||||
@@ -624,26 +628,26 @@ func ProvisionCertificates(filePath string, overwrite bool, dryRun bool, verbose
|
|||||||
|
|
||||||
// Loop through all certificate definitions
|
// Loop through all certificate definitions
|
||||||
// to render templates and fill missing fields from defaults
|
// to render templates and fill missing fields from defaults
|
||||||
for _, def := range certDefs.Certificates {
|
for i := range certDefs.Certificates {
|
||||||
// Fill missing fields from defaults, if provided
|
// Fill missing fields from defaults, if provided
|
||||||
def.FillDefaultValues(certDefs.Defaults)
|
certDefs.Certificates[i].FillDefaultValues(certDefs.Defaults)
|
||||||
// Render templates in the definition using the variables map
|
// Render templates in the definition using the variables map
|
||||||
// with added definition name.
|
// with added definition name.
|
||||||
variables := certDefs.Variables
|
variables := certDefs.Variables
|
||||||
if variables == nil {
|
if variables == nil {
|
||||||
variables = make(map[string]string)
|
variables = make(map[string]string)
|
||||||
}
|
}
|
||||||
variables["Name"] = def.Name
|
variables["Name"] = certDefs.Certificates[i].Name
|
||||||
err = def.RenderTemplates(variables)
|
err = certDefs.Certificates[i].RenderTemplates(variables)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to render templates for certificate %s: %v", def.Name, err)
|
return fmt.Errorf("failed to render templates for certificate %s: %v", certDefs.Certificates[i].Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n := len(certDefs.Certificates)
|
n := len(certDefs.Certificates)
|
||||||
// No errors so far, now we can issue certificates
|
// No errors so far, now we can issue certificates
|
||||||
for i, def := range certDefs.Certificates {
|
for i := range certDefs.Certificates {
|
||||||
fmt.Printf("[%d/%d] Issuing %s... ", i+1, n, def.Name)
|
fmt.Printf("[%d/%d] Issuing %s... ", i+1, n, certDefs.Certificates[i].Name)
|
||||||
|
|
||||||
if dryRun {
|
if dryRun {
|
||||||
fmt.Printf("(dry run)\n")
|
fmt.Printf("(dry run)\n")
|
||||||
@@ -651,7 +655,7 @@ func ProvisionCertificates(filePath string, overwrite bool, dryRun bool, verbose
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err = issueSingleCertificate(def)
|
err = issueSingleCertificate(certDefs.Certificates[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error: %v\n", err)
|
fmt.Printf("error: %v\n", err)
|
||||||
errors++
|
errors++
|
||||||
|
Reference in New Issue
Block a user