Add utils/name.go for cert and key file names

This commit is contained in:
Melon 2025-01-31 19:00:04 +00:00
parent 645d22b856
commit 7e70331179
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 19 additions and 3 deletions

View File

@ -13,6 +13,7 @@ import (
"fmt" "fmt"
"github.com/1f349/orchid/database" "github.com/1f349/orchid/database"
"github.com/1f349/orchid/pebble" "github.com/1f349/orchid/pebble"
"github.com/1f349/orchid/utils"
"github.com/go-acme/lego/v4/certificate" "github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/challenge" "github.com/go-acme/lego/v4/challenge"
"github.com/go-acme/lego/v4/challenge/dns01" "github.com/go-acme/lego/v4/challenge/dns01"
@ -368,7 +369,7 @@ func (s *Service) getDnsProvider(name, token string) (challenge.Provider, error)
// getPrivateKey reads the private key for the specified certificate id, or // getPrivateKey reads the private key for the specified certificate id, or
// generates one is the file doesn't exist // generates one is the file doesn't exist
func (s *Service) getPrivateKey(id int64) (*rsa.PrivateKey, error) { func (s *Service) getPrivateKey(id int64) (*rsa.PrivateKey, error) {
fPath := filepath.Join(s.keyDir, fmt.Sprintf("%d.key.pem", id)) fPath := filepath.Join(s.keyDir, utils.GetKeyFileName(id))
pemBytes, err := os.ReadFile(fPath) pemBytes, err := os.ReadFile(fPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -522,8 +523,8 @@ func (s *Service) setRetry(id int64) {
// writeCertFile writes the output certificate file and renames the current one // writeCertFile writes the output certificate file and renames the current one
// to include `-old` in the name. // to include `-old` in the name.
func (s *Service) writeCertFile(id int64, certBytes []byte) error { func (s *Service) writeCertFile(id int64, certBytes []byte) error {
oldPath := filepath.Join(s.certDir, fmt.Sprintf("%d-old.cert.pem", id)) oldPath := filepath.Join(s.certDir, utils.GetOldCertFileName(id))
newPath := filepath.Join(s.certDir, fmt.Sprintf("%d.cert.pem", id)) newPath := filepath.Join(s.certDir, utils.GetCertFileName(id))
// move certificate file to old name // move certificate file to old name
err := os.Rename(newPath, oldPath) err := os.Rename(newPath, oldPath)

15
utils/name.go Normal file
View File

@ -0,0 +1,15 @@
package utils
import "fmt"
func GetCertFileName(id int64) string {
return fmt.Sprintf("%d.cert.pem", id)
}
func GetOldCertFileName(id int64) string {
return fmt.Sprintf("%d-old.cert.pem", id)
}
func GetKeyFileName(id int64) string {
return fmt.Sprintf("%d.key.pem", id)
}