Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/main/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func configureApp() (*app, error) {

// CW_CLIENT_KEY_PERM
keyPerm := os.Getenv(prefix + "KEY_PERM")
keyPermInt, err := strconv.ParseInt(keyPerm, 0, 0)
keyPermInt, err := strconv.ParseInt(keyPerm, 8, 32)
if keyPerm == "" || err != nil {
app.logger.Debugf("%sKEY_PERM not specified or invalid, using default \"%o\"", prefix, defaultKeyPermissions)
cert.KeyPermissions = defaultKeyPermissions
Expand All @@ -404,7 +404,7 @@ func configureApp() (*app, error) {

// CW_CLIENT_CERT_PERM
certPerm := os.Getenv(prefix + "CERT_PERM")
certPermInt, err := strconv.ParseInt(certPerm, 0, 0)
certPermInt, err := strconv.ParseInt(certPerm, 8, 32)
if certPerm == "" || err != nil {
app.logger.Debugf("%sCERT_PERM not specified, using default \"%o\"", prefix, defaultCertPermissions)
cert.CertPermissions = defaultCertPermissions
Expand Down
56 changes: 44 additions & 12 deletions pkg/main/update_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,43 @@ func (app *app) updateCertFilesAndRestartContainers(certIndex int, onlyIfMissing
// write key pem (always if not exist, if exists but updated: only write if NOT only missing files OR any file is missing)
// AKA write file anyway even if !onlyIfMissing if something else is missing, because something will be written and trigger restart anyway
if !keyFileExists || (keyFileUpdated && (!onlyIfMissing || anyFileMissing)) {
err := os.WriteFile(app.cfg.Certs[certIndex].CertStoragePath+"/"+app.cfg.Certs[certIndex].KeyPemFilename, keyPemApp, app.cfg.Certs[certIndex].KeyPermissions)
keyFilePath := app.cfg.Certs[certIndex].CertStoragePath + "/" + app.cfg.Certs[certIndex].KeyPemFilename
err := os.WriteFile(keyFilePath, keyPemApp, app.cfg.Certs[certIndex].KeyPermissions)
if err != nil {
app.logger.Errorf("failed to write key %s file (%s)", app.cfg.Certs[certIndex].KeyPemFilename, err)
failedAnyWrite = true
// failed, but keep trying
} else {
wroteAnyFiles = true
app.logger.Infof("wrote new key %s file", app.cfg.Certs[certIndex].KeyPemFilename)
// Explicitly set permissions after write to ensure they are applied correctly
err = os.Chmod(keyFilePath, app.cfg.Certs[certIndex].KeyPermissions)
if err != nil {
app.logger.Errorf("failed to set permissions on key %s file (%s)", app.cfg.Certs[certIndex].KeyPemFilename, err)
failedAnyWrite = true
} else {
wroteAnyFiles = true
app.logger.Infof("wrote new key %s file with permissions %o", app.cfg.Certs[certIndex].KeyPemFilename, app.cfg.Certs[certIndex].KeyPermissions)
}
}
}

// write cert pem
if !certFileExists || (certFileUpdated && (!onlyIfMissing || anyFileMissing)) {
err := os.WriteFile(app.cfg.Certs[certIndex].CertStoragePath+"/"+app.cfg.Certs[certIndex].CertPemFilename, certPemApp, app.cfg.Certs[certIndex].CertPermissions)
certFilePath := app.cfg.Certs[certIndex].CertStoragePath + "/" + app.cfg.Certs[certIndex].CertPemFilename
err := os.WriteFile(certFilePath, certPemApp, app.cfg.Certs[certIndex].CertPermissions)
if err != nil {
app.logger.Errorf("failed to write cert %s file (%s)", app.cfg.Certs[certIndex].CertPemFilename, err)
failedAnyWrite = true
// failed, but keep trying
} else {
wroteAnyFiles = true
app.logger.Infof("wrote new cert %s file", app.cfg.Certs[certIndex].CertPemFilename)
// Explicitly set permissions after write to ensure they are applied correctly
err = os.Chmod(certFilePath, app.cfg.Certs[certIndex].CertPermissions)
if err != nil {
app.logger.Errorf("failed to set permissions on cert %s file (%s)", app.cfg.Certs[certIndex].CertPemFilename, err)
failedAnyWrite = true
} else {
wroteAnyFiles = true
app.logger.Infof("wrote new cert %s file with permissions %o", app.cfg.Certs[certIndex].CertPemFilename, app.cfg.Certs[certIndex].CertPermissions)
}
}
}

Expand All @@ -130,14 +146,22 @@ func (app *app) updateCertFilesAndRestartContainers(certIndex int, onlyIfMissing
// failed, but keep trying
failedAnyWrite = true
} else {
err = os.WriteFile(app.cfg.Certs[certIndex].CertStoragePath+"/"+app.cfg.Certs[certIndex].PfxFilename, pfx, app.cfg.Certs[certIndex].KeyPermissions)
pfxFilePath := app.cfg.Certs[certIndex].CertStoragePath + "/" + app.cfg.Certs[certIndex].PfxFilename
err = os.WriteFile(pfxFilePath, pfx, app.cfg.Certs[certIndex].KeyPermissions)
if err != nil {
app.logger.Errorf("failed to write %s (%s)", app.cfg.Certs[certIndex].PfxFilename, err)
// failed, but keep trying
failedAnyWrite = true
} else {
app.logger.Infof("wrote new modern pfx %s file", app.cfg.Certs[certIndex].PfxFilename)
wroteAnyFiles = true
// Explicitly set permissions after write to ensure they are applied correctly
err = os.Chmod(pfxFilePath, app.cfg.Certs[certIndex].KeyPermissions)
if err != nil {
app.logger.Errorf("failed to set permissions on %s (%s)", app.cfg.Certs[certIndex].PfxFilename, err)
failedAnyWrite = true
} else {
app.logger.Infof("wrote new modern pfx %s file with permissions %o", app.cfg.Certs[certIndex].PfxFilename, app.cfg.Certs[certIndex].KeyPermissions)
wroteAnyFiles = true
}
}
}
}
Expand All @@ -150,14 +174,22 @@ func (app *app) updateCertFilesAndRestartContainers(certIndex int, onlyIfMissing
// failed, but keep trying
failedAnyWrite = true
} else {
err = os.WriteFile(app.cfg.Certs[certIndex].CertStoragePath+"/"+app.cfg.Certs[certIndex].PfxLegacyFilename, pfx, app.cfg.Certs[certIndex].KeyPermissions)
pfxLegacyFilePath := app.cfg.Certs[certIndex].CertStoragePath + "/" + app.cfg.Certs[certIndex].PfxLegacyFilename
err = os.WriteFile(pfxLegacyFilePath, pfx, app.cfg.Certs[certIndex].KeyPermissions)
if err != nil {
app.logger.Errorf("failed to write legacy pfx %s (%s)", app.cfg.Certs[certIndex].PfxLegacyFilename, err)
// failed, but keep trying
failedAnyWrite = true
} else {
app.logger.Infof("wrote new legacy pfx %s file", app.cfg.Certs[certIndex].PfxLegacyFilename)
wroteAnyFiles = true
// Explicitly set permissions after write to ensure they are applied correctly
err = os.Chmod(pfxLegacyFilePath, app.cfg.Certs[certIndex].KeyPermissions)
if err != nil {
app.logger.Errorf("failed to set permissions on legacy pfx %s (%s)", app.cfg.Certs[certIndex].PfxLegacyFilename, err)
failedAnyWrite = true
} else {
app.logger.Infof("wrote new legacy pfx %s file with permissions %o", app.cfg.Certs[certIndex].PfxLegacyFilename, app.cfg.Certs[certIndex].KeyPermissions)
wroteAnyFiles = true
}
}
}
}
Expand Down