CAConfig global variable and refactoring.
This commit is contained in:
34
certdb.go
34
certdb.go
@@ -8,9 +8,9 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// CAState represents the persisted CA state in JSON
|
||||
// _CAState represents the persisted CA state in JSON
|
||||
// (matches the structure of example_ca.json)
|
||||
type CAState struct {
|
||||
type _CAState struct {
|
||||
CreatedAt string `json:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
Serial int `json:"serial,omitempty"`
|
||||
@@ -26,13 +26,13 @@ type CertificateRecord struct {
|
||||
}
|
||||
|
||||
// LoadCAState loads the CA state from a JSON file
|
||||
func LoadCAState(filename string) (*CAState, error) {
|
||||
func LoadCAState(filename string) (*_CAState, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
var state CAState
|
||||
var state _CAState
|
||||
if err := json.NewDecoder(f).Decode(&state); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func LoadCAState(filename string) (*CAState, error) {
|
||||
}
|
||||
|
||||
// SaveCAState saves the CA state to a JSON file
|
||||
func SaveCAState(filename string, state *CAState) error {
|
||||
func SaveCAState(filename string, state *_CAState) error {
|
||||
state.UpdatedAt = time.Now().UTC().Format(time.RFC3339)
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
@@ -55,14 +55,14 @@ func SaveCAState(filename string, state *CAState) error {
|
||||
// UpdateCAStateAfterIssue updates the CA state JSON after issuing a certificate
|
||||
func UpdateCAStateAfterIssue(jsonFile, serialType, basename string, serialNumber any, validity time.Duration) error {
|
||||
var err error
|
||||
if GlobalCAState == nil {
|
||||
GlobalCAState, err = LoadCAState(jsonFile)
|
||||
if CAState == nil {
|
||||
CAState, err = LoadCAState(jsonFile)
|
||||
if err != nil {
|
||||
GlobalCAState = nil
|
||||
CAState = nil
|
||||
}
|
||||
}
|
||||
if GlobalCAState == nil {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: GlobalCAState is nil in UpdateCAStateAfterIssue. This indicates a programming error.\n")
|
||||
if CAState == nil {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: CAState is nil in UpdateCAStateAfterIssue. This indicates a programming error.\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
issued := time.Now().UTC().Format(time.RFC3339)
|
||||
@@ -70,8 +70,8 @@ func UpdateCAStateAfterIssue(jsonFile, serialType, basename string, serialNumber
|
||||
serialStr := ""
|
||||
switch serialType {
|
||||
case "sequential":
|
||||
serialStr = fmt.Sprintf("%d", GlobalCAState.Serial)
|
||||
GlobalCAState.Serial++
|
||||
serialStr = fmt.Sprintf("%d", CAState.Serial)
|
||||
CAState.Serial++
|
||||
case "random":
|
||||
serialStr = fmt.Sprintf("%x", serialNumber)
|
||||
default:
|
||||
@@ -81,10 +81,10 @@ func UpdateCAStateAfterIssue(jsonFile, serialType, basename string, serialNumber
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddCertificate appends a new CertificateRecord to the GlobalCAState
|
||||
// AddCertificate appends a new CertificateRecord to the CAState
|
||||
func AddCertificate(name, issued, expires, serial string, valid bool) {
|
||||
if GlobalCAState == nil {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: GlobalCAState is nil in AddCertificate. This indicates a programming error.\n")
|
||||
if CAState == nil {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: CAState is nil in AddCertificate. This indicates a programming error.\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
rec := CertificateRecord{
|
||||
@@ -94,5 +94,7 @@ func AddCertificate(name, issued, expires, serial string, valid bool) {
|
||||
Serial: serial,
|
||||
Valid: valid,
|
||||
}
|
||||
GlobalCAState.Certificates = append(GlobalCAState.Certificates, rec)
|
||||
CAState.Certificates = append(CAState.Certificates, rec)
|
||||
}
|
||||
|
||||
// No CAConfig references to update in this file
|
||||
|
Reference in New Issue
Block a user