-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathupgrades.go
194 lines (167 loc) · 5.11 KB
/
upgrades.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bufio"
"errors"
"fmt"
"io"
"io/fs"
"log"
"net"
"os"
"strings"
"time"
"github.com/spf13/viper"
)
func CheckUpgrades() {
v := viper.GetString("version")
if standaloneVersion == "" {
gitVersion := controlCommand("git-version", true)
if gitVersion != "" {
viper.Set("version", strings.TrimSpace(gitVersion))
_ = viper.WriteConfig()
}
} else if v != standaloneVersion {
viper.Set("version", standaloneVersion)
_ = viper.WriteConfig()
}
changed := CheckUpgrade_01_CeremonyHSM()
if changed {
time.Sleep(2 * time.Second)
log.Println("Applying updated configuration...")
controlCommand("apply", false)
time.Sleep(2 * time.Second)
log.Println("Updating CRL links if needed...")
controlCommand("check-crl", false)
time.Sleep(2 * time.Second)
log.Println("Restarting boulder containers...")
controlCommand("boulder-restart", false)
}
}
func readFileAsString(filename string) string {
read, err := os.ReadFile(filename)
if err != nil {
log.Printf("**** Could not read '%s': %s\n", filename, err.Error())
log.Println("**** ABORT MIGRATION ****")
time.Sleep(1 * time.Minute)
os.Exit(1)
}
return string(read)
}
func controlCommand(command string, ignoreError bool) string {
conn, err := net.Dial("tcp", "control:3030")
if err != nil {
if ignoreError {
return ""
}
log.Println("**** Failed to connect to control container!")
time.Sleep(1 * time.Minute)
os.Exit(1)
}
defer func() { _ = conn.Close() }()
_, _ = fmt.Fprint(conn, command+"\n")
reader := bufio.NewReader(conn)
message, err := io.ReadAll(reader)
if err != nil {
log.Printf("**** Failed to read response from control container: %s\n", err.Error())
time.Sleep(1 * time.Minute)
os.Exit(1)
}
if len(message) >= 4 {
tail := message[len(message)-4:]
if strings.Compare(string(tail), "\nok\n") == 0 {
msg := message[0 : len(message)-4]
log.Printf("**** Message from control server: '%s'", msg)
}
}
return string(message)
}
func copyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer func() { _ = sourceFile.Close() }()
destinationFile, err := os.Create(dst)
if err != nil {
return err
}
defer func() { _ = destinationFile.Close() }()
_, err = io.Copy(destinationFile, sourceFile)
if err != nil {
return err
}
err = destinationFile.Sync()
if err != nil {
return err
}
return nil
}
// Check if we should upgrade to using the Ceremony tool and store keys on SoftHSM (January 2025).
func CheckUpgrade_01_CeremonyHSM() bool {
baseDir := "/opt/labca/data/"
prevRootCert := baseDir + "root-ca.pem"
if _, err := os.Stat(prevRootCert); errors.Is(err, fs.ErrNotExist) {
baseDir = "/go/src/labca/data/"
prevRootCert = baseDir + "root-ca.pem"
if _, err := os.Stat(prevRootCert); errors.Is(err, fs.ErrNotExist) {
return false
}
}
log.Println("**** BEGIN MIGRATION: upgrade01 ****")
rootCertFile := fmt.Sprintf("%sroot-01-cert.pem", CERT_FILES_PATH)
if _, err := os.Stat(rootCertFile); !errors.Is(err, fs.ErrNotExist) {
log.Printf("**** File %s already exists!\n", rootCertFile)
log.Println("**** ABORT MIGRATION ****")
time.Sleep(1 * time.Minute)
os.Exit(1)
}
prevRootKey := baseDir + "root-ca.key"
if _, err := os.Stat(prevRootKey); errors.Is(err, fs.ErrNotExist) {
log.Println("**** Root key file not present on the system: cannot upgrade automatically!")
log.Println("**** Please do a fresh install of LabCA and import / upload the root certificate and key.")
log.Println("**** ABORT MIGRATION ****")
time.Sleep(1 * time.Minute)
os.Exit(1)
}
// Migrate root certificate and key
ci := &CertificateInfo{IsRoot: true}
ci.Initialize()
ci.IsRoot = true
ci.CreateType = "upload"
ci.Certificate = readFileAsString(prevRootCert)
ci.Key = readFileAsString(prevRootKey)
prevRootCRL := baseDir + "root-ca.crl"
if _, err := os.Stat(prevRootCRL); !errors.Is(err, fs.ErrNotExist) {
ci.CRL = readFileAsString(prevRootCRL)
_ = copyFile(prevRootCRL, strings.ReplaceAll(rootCertFile, "-cert.", "-crl."))
}
if err := ci.Create("root-01", false); err != nil {
log.Printf("**** Could not convert previous root certificate and key: %s\n", err.Error())
log.Println("**** ABORT MIGRATION ****")
time.Sleep(1 * time.Minute)
os.Exit(1)
}
// Migrate issuer certificate and key
ci = &CertificateInfo{IsRoot: false}
ci.Initialize()
ci.IsRoot = false
ci.CreateType = "upload"
prevIssuerCert := baseDir + "issuer/ca-int.pem"
ci.Certificate = readFileAsString(prevIssuerCert)
prevIssuerKey := baseDir + "issuer/ca-int.key"
ci.Key = readFileAsString(prevIssuerKey)
ci.CRL = ""
if err := ci.Create("issuer-01", false); err != nil {
log.Printf("**** Could not convert previous issuer certificate and key: %s\n", err.Error())
log.Println("**** ABORT MIGRATION ****")
time.Sleep(1 * time.Minute)
os.Exit(1)
}
_ = os.Rename(prevRootCert, prevRootCert+"_backup")
_ = os.Rename(prevRootKey, prevRootKey+"_backup")
_ = os.Rename(prevRootCRL, prevRootCRL+"_backup")
_ = os.Rename(prevIssuerCert, prevIssuerCert+"_backup")
_ = os.Rename(prevIssuerKey, prevIssuerKey+"_backup")
log.Println("**** END MIGRATION ****")
return true
}