Skip to content

Commit 4004984

Browse files
committed
fix: revert - client - avoid loss of private key in case of timeout issue
1 parent 0cacc13 commit 4004984

1 file changed

Lines changed: 14 additions & 32 deletions

File tree

client/client.go

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -123,36 +123,6 @@ func applyCertFileChanges(acmeClient *restclient.Client, diff MapDiff, logger lo
123123
continue
124124
}
125125

126-
// Save private key BEFORE calling CreateCertificate to prevent loss on timeout
127-
// If the server times out but continues processing, we still have the key for reconciliation
128-
if GlobalConfig.Common.CertDeploy {
129-
err := utils.CreateNonExistingFolder(GlobalConfig.Common.CertDir+certData.Issuer, GlobalConfig.Common.CertDirPerm)
130-
if err != nil {
131-
hasErrors = true
132-
_ = level.Error(logger).Log("err", err, "domain", certData.Domain, "issuer", certData.Issuer, "owner", Owner)
133-
continue
134-
}
135-
err = createLocalPrivateKeyFile(keyFilePath, privateKey)
136-
if err != nil {
137-
hasErrors = true
138-
_ = level.Error(logger).Log("err", err, "domain", certData.Domain, "issuer", certData.Issuer, "owner", Owner)
139-
continue
140-
}
141-
_ = level.Info(logger).Log("msg", "local private key file created", "domain", certData.Domain, "issuer", certData.Issuer, "owner", Owner)
142-
}
143-
144-
// Backup private key to Vault BEFORE calling CreateCertificate
145-
if GlobalConfig.Common.CertBackup {
146-
data := CertBackup{Key: string(privateKey)}
147-
vaultSecretPath := fmt.Sprintf("%s/%s/%s/%s", GlobalConfig.Storage.Vault.CertPrefix, Owner, certData.Issuer, certData.Domain)
148-
err = vault.GlobalClient.PutSecretWithAppRole(vaultSecretPath, utils.StructToMapInterface(data))
149-
if err != nil {
150-
_ = level.Error(logger).Log("msg", "failed to backup private key to vault", "err", err, "domain", certData.Domain, "issuer", certData.Issuer, "owner", Owner)
151-
} else {
152-
_ = level.Info(logger).Log("msg", "private key backed up in vault", "domain", certData.Domain, "issuer", certData.Issuer, "owner", Owner)
153-
}
154-
}
155-
156126
certDataBytes, _ := json.Marshal(certData)
157127

158128
var certParams models.CertificateParams
@@ -178,13 +148,25 @@ func applyCertFileChanges(acmeClient *restclient.Client, diff MapDiff, logger lo
178148
err = vault.GlobalClient.PutSecretWithAppRole(vaultSecretPath, utils.StructToMapInterface(data))
179149
if err != nil {
180150
_ = level.Error(logger).Log("msg", "failed to backup certificate", "err", err, "domain", certData.Domain, "issuer", certData.Issuer, "owner", Owner)
181-
} else {
182-
_ = level.Info(logger).Log("msg", "certificate and private key backed up in vault", "domain", certData.Domain, "issuer", certData.Issuer, "owner", Owner)
183151
}
152+
_ = level.Info(logger).Log("msg", "certificate and private key backed up in vault", "domain", certData.Domain, "issuer", certData.Issuer, "owner", Owner)
184153
}
185154

186155
if GlobalConfig.Common.CertDeploy {
187156
hasChange = true
157+
err := utils.CreateNonExistingFolder(GlobalConfig.Common.CertDir+newCert.Issuer, GlobalConfig.Common.CertDirPerm)
158+
if err != nil {
159+
_ = level.Error(logger).Log("err", err, "domain", certData.Domain, "issuer", certData.Issuer, "owner", Owner)
160+
continue
161+
}
162+
err = createLocalPrivateKeyFile(keyFilePath, privateKey)
163+
if err != nil {
164+
hasErrors = true
165+
_ = level.Error(logger).Log("err", err, "domain", certData.Domain, "issuer", certData.Issuer, "owner", Owner)
166+
continue
167+
}
168+
_ = level.Info(logger).Log("msg", "local private key file created", "domain", certData.Domain, "issuer", certData.Issuer, "owner", Owner)
169+
188170
err = createLocalCertificateFile(newCert)
189171
if err != nil {
190172
hasErrors = true

0 commit comments

Comments
 (0)