mirror of
https://github.com/certimate-go/certimate.git
synced 2026-06-13 21:01:32 +08:00
refactor: clean code
This commit is contained in:
parent
d9b303457c
commit
8e2bebfadb
@ -46,7 +46,7 @@ func (c *Certificate) PopulateFromX509(certX509 *x509.Certificate) *Certificate
|
||||
c.ValidityNotAfter = certX509.NotAfter
|
||||
c.ValidityInterval = int32(certX509.NotAfter.Sub(certX509.NotBefore).Seconds())
|
||||
|
||||
keyAlgorithm, keySize, _ := xcertkey.GetPublicKeyAlgorithm(certX509.PublicKey)
|
||||
keyAlgorithm, keySize, _ := xcertkey.DetectPublicKeyAlgorithm(certX509.PublicKey)
|
||||
switch keyAlgorithm {
|
||||
case x509.RSA:
|
||||
c.KeyAlgorithm = CertificateKeyAlgorithmType(fmt.Sprintf("RSA%d", keySize))
|
||||
|
||||
@ -248,7 +248,7 @@ func (ne *bizApplyNodeExecutor) executeObtain(execCtx *NodeExecutionContext, nod
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not parse custom private key: %w", err)
|
||||
} else {
|
||||
privkeyAlg, privkeySize, _ := xcertkey.GetPrivateKeyAlgorithm(privkey)
|
||||
privkeyAlg, privkeySize, _ := xcertkey.DetectPrivateKeyAlgorithm(privkey)
|
||||
switch privkeyAlg {
|
||||
case x509.RSA:
|
||||
if nodeCfg.KeyAlgorithm != fmt.Sprintf("RSA%d", privkeySize) {
|
||||
|
||||
@ -1,18 +1,19 @@
|
||||
package ftp
|
||||
|
||||
import (
|
||||
"github.com/certimate-go/certimate/pkg/core/deployer/providers/local/shared"
|
||||
"github.com/certimate-go/certimate/internal/domain"
|
||||
xcertpfx "github.com/certimate-go/certimate/pkg/utils/cert/pfx"
|
||||
)
|
||||
|
||||
const (
|
||||
FILE_FORMAT_PEM = shared.FILE_FORMAT_PEM
|
||||
FILE_FORMAT_PFX = shared.FILE_FORMAT_PFX
|
||||
FILE_FORMAT_JKS = shared.FILE_FORMAT_JKS
|
||||
FILE_FORMAT_PEM = string(domain.CertificateFormatTypePEM)
|
||||
FILE_FORMAT_PFX = string(domain.CertificateFormatTypePFX)
|
||||
FILE_FORMAT_JKS = string(domain.CertificateFormatTypeJKS)
|
||||
)
|
||||
|
||||
const (
|
||||
PFX_ENCODER_LEGACYRC2 = shared.PFX_ENCODER_LEGACYRC2
|
||||
PFX_ENCODER_LEGACYDES = shared.PFX_ENCODER_LEGACYDES
|
||||
PFX_ENCODER_MODERN2023 = shared.PFX_ENCODER_MODERN2023
|
||||
PFX_ENCODER_MODERN2026 = shared.PFX_ENCODER_MODERN2026
|
||||
PFX_ENCODER_LEGACYRC2 = string(xcertpfx.EncoderNameLegacyRC2)
|
||||
PFX_ENCODER_LEGACYDES = string(xcertpfx.EncoderNameLegacyDES)
|
||||
PFX_ENCODER_MODERN2023 = string(xcertpfx.EncoderNameModern2023)
|
||||
PFX_ENCODER_MODERN2026 = string(xcertpfx.EncoderNameModern2026)
|
||||
)
|
||||
|
||||
@ -8,8 +8,8 @@ import (
|
||||
|
||||
"github.com/certimate-go/certimate/internal/tools/ftp"
|
||||
"github.com/certimate-go/certimate/pkg/core/deployer"
|
||||
shared "github.com/certimate-go/certimate/pkg/core/deployer/providers/local/shared"
|
||||
xcert "github.com/certimate-go/certimate/pkg/utils/cert"
|
||||
xcertpfx "github.com/certimate-go/certimate/pkg/utils/cert/pfx"
|
||||
)
|
||||
|
||||
type DeployerConfig struct {
|
||||
@ -155,7 +155,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*dep
|
||||
return nil, fmt.Errorf("config `pfxPassword` is required")
|
||||
}
|
||||
|
||||
pfxEncoder, err := shared.ResolvePfxEncoder(d.config.PfxEncoder)
|
||||
pfxEncoder, err := xcertpfx.ResolvePfxEncoder(d.config.PfxEncoder)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("config `pfxEncoder` is invalid: %w", err)
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package local
|
||||
|
||||
import (
|
||||
"github.com/certimate-go/certimate/internal/domain"
|
||||
xcertpfx "github.com/certimate-go/certimate/pkg/utils/cert/pfx"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -17,8 +18,8 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
PFX_ENCODER_LEGACYRC2 = "LegacyRC2"
|
||||
PFX_ENCODER_LEGACYDES = "LegacyDES"
|
||||
PFX_ENCODER_MODERN2023 = "Modern2023"
|
||||
PFX_ENCODER_MODERN2026 = "Modern2026"
|
||||
PFX_ENCODER_LEGACYRC2 = string(xcertpfx.EncoderNameLegacyRC2)
|
||||
PFX_ENCODER_LEGACYDES = string(xcertpfx.EncoderNameLegacyDES)
|
||||
PFX_ENCODER_MODERN2023 = string(xcertpfx.EncoderNameModern2023)
|
||||
PFX_ENCODER_MODERN2026 = string(xcertpfx.EncoderNameModern2026)
|
||||
)
|
||||
|
||||
@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/certimate-go/certimate/pkg/core/deployer"
|
||||
xcert "github.com/certimate-go/certimate/pkg/utils/cert"
|
||||
xcertpfx "github.com/certimate-go/certimate/pkg/utils/cert/pfx"
|
||||
xfile "github.com/certimate-go/certimate/pkg/utils/file"
|
||||
)
|
||||
|
||||
@ -142,7 +143,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*dep
|
||||
return nil, fmt.Errorf("config `pfxPassword` is required")
|
||||
}
|
||||
|
||||
pfxEncoder, err := ResolvePfxEncoder(d.config.PfxEncoder)
|
||||
pfxEncoder, err := xcertpfx.ResolvePfxEncoder(d.config.PfxEncoder)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("config `pfxEncoder` is invalid: %w", err)
|
||||
}
|
||||
|
||||
@ -1,24 +0,0 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"software.sslmate.com/src/go-pkcs12"
|
||||
|
||||
"github.com/certimate-go/certimate/pkg/core/deployer/providers/local"
|
||||
)
|
||||
|
||||
const (
|
||||
FILE_FORMAT_PEM = local.FILE_FORMAT_PEM
|
||||
FILE_FORMAT_PFX = local.FILE_FORMAT_PFX
|
||||
FILE_FORMAT_JKS = local.FILE_FORMAT_JKS
|
||||
)
|
||||
|
||||
const (
|
||||
PFX_ENCODER_LEGACYRC2 = local.PFX_ENCODER_LEGACYRC2
|
||||
PFX_ENCODER_LEGACYDES = local.PFX_ENCODER_LEGACYDES
|
||||
PFX_ENCODER_MODERN2023 = local.PFX_ENCODER_MODERN2023
|
||||
PFX_ENCODER_MODERN2026 = local.PFX_ENCODER_MODERN2026
|
||||
)
|
||||
|
||||
func ResolvePfxEncoder(encoderName string) (*pkcs12.Encoder, error) {
|
||||
return local.ResolvePfxEncoder(encoderName)
|
||||
}
|
||||
@ -1,28 +0,0 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"software.sslmate.com/src/go-pkcs12"
|
||||
)
|
||||
|
||||
func ResolvePfxEncoder(encoderName string) (*pkcs12.Encoder, error) {
|
||||
var encoder *pkcs12.Encoder
|
||||
|
||||
if encoderName != "" {
|
||||
if strings.EqualFold(encoderName, PFX_ENCODER_LEGACYRC2) {
|
||||
encoder = pkcs12.LegacyRC2
|
||||
} else if strings.EqualFold(encoderName, PFX_ENCODER_LEGACYDES) {
|
||||
encoder = pkcs12.LegacyDES
|
||||
} else if strings.EqualFold(encoderName, PFX_ENCODER_MODERN2023) {
|
||||
encoder = pkcs12.Modern2023
|
||||
} else if strings.EqualFold(encoderName, PFX_ENCODER_MODERN2026) {
|
||||
encoder = pkcs12.Modern2026
|
||||
} else {
|
||||
return nil, fmt.Errorf("unsupported encoder name: '%s'", encoderName)
|
||||
}
|
||||
}
|
||||
|
||||
return encoder, nil
|
||||
}
|
||||
@ -1,18 +1,19 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"github.com/certimate-go/certimate/pkg/core/deployer/providers/local/shared"
|
||||
"github.com/certimate-go/certimate/internal/domain"
|
||||
xcertpfx "github.com/certimate-go/certimate/pkg/utils/cert/pfx"
|
||||
)
|
||||
|
||||
const (
|
||||
FILE_FORMAT_PEM = shared.FILE_FORMAT_PEM
|
||||
FILE_FORMAT_PFX = shared.FILE_FORMAT_PFX
|
||||
FILE_FORMAT_JKS = shared.FILE_FORMAT_JKS
|
||||
FILE_FORMAT_PEM = string(domain.CertificateFormatTypePEM)
|
||||
FILE_FORMAT_PFX = string(domain.CertificateFormatTypePFX)
|
||||
FILE_FORMAT_JKS = string(domain.CertificateFormatTypeJKS)
|
||||
)
|
||||
|
||||
const (
|
||||
PFX_ENCODER_LEGACYRC2 = shared.PFX_ENCODER_LEGACYRC2
|
||||
PFX_ENCODER_LEGACYDES = shared.PFX_ENCODER_LEGACYDES
|
||||
PFX_ENCODER_MODERN2023 = shared.PFX_ENCODER_MODERN2023
|
||||
PFX_ENCODER_MODERN2026 = shared.PFX_ENCODER_MODERN2026
|
||||
PFX_ENCODER_LEGACYRC2 = string(xcertpfx.EncoderNameLegacyRC2)
|
||||
PFX_ENCODER_LEGACYDES = string(xcertpfx.EncoderNameLegacyDES)
|
||||
PFX_ENCODER_MODERN2023 = string(xcertpfx.EncoderNameModern2023)
|
||||
PFX_ENCODER_MODERN2026 = string(xcertpfx.EncoderNameModern2026)
|
||||
)
|
||||
|
||||
@ -7,8 +7,8 @@ import (
|
||||
|
||||
"github.com/certimate-go/certimate/internal/tools/s3"
|
||||
"github.com/certimate-go/certimate/pkg/core/deployer"
|
||||
"github.com/certimate-go/certimate/pkg/core/deployer/providers/local/shared"
|
||||
xcert "github.com/certimate-go/certimate/pkg/utils/cert"
|
||||
xcertpfx "github.com/certimate-go/certimate/pkg/utils/cert/pfx"
|
||||
)
|
||||
|
||||
type DeployerConfig struct {
|
||||
@ -137,7 +137,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*dep
|
||||
return nil, fmt.Errorf("config `pfxPassword` is required")
|
||||
}
|
||||
|
||||
pfxEncoder, err := shared.ResolvePfxEncoder(d.config.PfxEncoder)
|
||||
pfxEncoder, err := xcertpfx.ResolvePfxEncoder(d.config.PfxEncoder)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("config `pfxEncoder` is invalid: %w", err)
|
||||
}
|
||||
|
||||
@ -1,18 +1,19 @@
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"github.com/certimate-go/certimate/pkg/core/deployer/providers/local/shared"
|
||||
"github.com/certimate-go/certimate/internal/domain"
|
||||
xcertpfx "github.com/certimate-go/certimate/pkg/utils/cert/pfx"
|
||||
)
|
||||
|
||||
const (
|
||||
FILE_FORMAT_PEM = shared.FILE_FORMAT_PEM
|
||||
FILE_FORMAT_PFX = shared.FILE_FORMAT_PFX
|
||||
FILE_FORMAT_JKS = shared.FILE_FORMAT_JKS
|
||||
FILE_FORMAT_PEM = string(domain.CertificateFormatTypePEM)
|
||||
FILE_FORMAT_PFX = string(domain.CertificateFormatTypePFX)
|
||||
FILE_FORMAT_JKS = string(domain.CertificateFormatTypeJKS)
|
||||
)
|
||||
|
||||
const (
|
||||
PFX_ENCODER_LEGACYRC2 = shared.PFX_ENCODER_LEGACYRC2
|
||||
PFX_ENCODER_LEGACYDES = shared.PFX_ENCODER_LEGACYDES
|
||||
PFX_ENCODER_MODERN2023 = shared.PFX_ENCODER_MODERN2023
|
||||
PFX_ENCODER_MODERN2026 = shared.PFX_ENCODER_MODERN2026
|
||||
PFX_ENCODER_LEGACYRC2 = string(xcertpfx.EncoderNameLegacyRC2)
|
||||
PFX_ENCODER_LEGACYDES = string(xcertpfx.EncoderNameLegacyDES)
|
||||
PFX_ENCODER_MODERN2023 = string(xcertpfx.EncoderNameModern2023)
|
||||
PFX_ENCODER_MODERN2026 = string(xcertpfx.EncoderNameModern2026)
|
||||
)
|
||||
|
||||
@ -8,8 +8,8 @@ import (
|
||||
|
||||
"github.com/certimate-go/certimate/internal/tools/ssh"
|
||||
"github.com/certimate-go/certimate/pkg/core/deployer"
|
||||
"github.com/certimate-go/certimate/pkg/core/deployer/providers/local/shared"
|
||||
xcert "github.com/certimate-go/certimate/pkg/utils/cert"
|
||||
xcertpfx "github.com/certimate-go/certimate/pkg/utils/cert/pfx"
|
||||
xssh "github.com/certimate-go/certimate/pkg/utils/ssh"
|
||||
)
|
||||
|
||||
@ -173,7 +173,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*dep
|
||||
return nil, fmt.Errorf("config `pfxPassword` is required")
|
||||
}
|
||||
|
||||
pfxEncoder, err := shared.ResolvePfxEncoder(d.config.PfxEncoder)
|
||||
pfxEncoder, err := xcertpfx.ResolvePfxEncoder(d.config.PfxEncoder)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("config `pfxEncoder` is invalid: %w", err)
|
||||
}
|
||||
|
||||
@ -193,7 +193,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*dep
|
||||
return nil, fmt.Errorf("failed to parse private key: %w", err)
|
||||
}
|
||||
|
||||
privkeyAlg, _, _ := xcertkey.GetPrivateKeyAlgorithm(privkey)
|
||||
privkeyAlg, _, _ := xcertkey.DetectPrivateKeyAlgorithm(privkey)
|
||||
privkeyAlgStr := ""
|
||||
switch privkeyAlg {
|
||||
case x509.RSA:
|
||||
|
||||
@ -11,7 +11,7 @@ import (
|
||||
|
||||
type KeyAlgorithm = x509.PublicKeyAlgorithm
|
||||
|
||||
func GetPublicKeyAlgorithm(pubkey crypto.PublicKey) (_algorithm KeyAlgorithm, _size int, _error error) {
|
||||
func DetectPublicKeyAlgorithm(pubkey crypto.PublicKey) (_algorithm KeyAlgorithm, _size int, _error error) {
|
||||
switch t := pubkey.(type) {
|
||||
case *rsa.PublicKey:
|
||||
size := t.N.BitLen()
|
||||
@ -28,7 +28,7 @@ func GetPublicKeyAlgorithm(pubkey crypto.PublicKey) (_algorithm KeyAlgorithm, _s
|
||||
return x509.UnknownPublicKeyAlgorithm, 0, fmt.Errorf("unknown public key type")
|
||||
}
|
||||
|
||||
func GetPrivateKeyAlgorithm(privkey crypto.PrivateKey) (_algorithm KeyAlgorithm, _size int, _error error) {
|
||||
func DetectPrivateKeyAlgorithm(privkey crypto.PrivateKey) (_algorithm KeyAlgorithm, _size int, _error error) {
|
||||
switch t := privkey.(type) {
|
||||
case *rsa.PrivateKey:
|
||||
size := t.N.BitLen()
|
||||
|
||||
35
pkg/utils/cert/pfx/encoder.go
Normal file
35
pkg/utils/cert/pfx/encoder.go
Normal file
@ -0,0 +1,35 @@
|
||||
package pfx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"software.sslmate.com/src/go-pkcs12"
|
||||
)
|
||||
|
||||
const (
|
||||
EncoderNameLegacyRC2 = "LegacyRC2"
|
||||
EncoderNameLegacyDES = "LegacyDES"
|
||||
EncoderNameModern2023 = "Modern2023"
|
||||
EncoderNameModern2026 = "Modern2026"
|
||||
)
|
||||
|
||||
func ResolvePfxEncoder(encoderName string) (*pkcs12.Encoder, error) {
|
||||
var encoder *pkcs12.Encoder
|
||||
|
||||
if encoderName != "" {
|
||||
if strings.EqualFold(encoderName, EncoderNameLegacyRC2) {
|
||||
encoder = pkcs12.LegacyRC2
|
||||
} else if strings.EqualFold(encoderName, EncoderNameLegacyDES) {
|
||||
encoder = pkcs12.LegacyDES
|
||||
} else if strings.EqualFold(encoderName, EncoderNameModern2023) {
|
||||
encoder = pkcs12.Modern2023
|
||||
} else if strings.EqualFold(encoderName, EncoderNameModern2026) {
|
||||
encoder = pkcs12.Modern2026
|
||||
} else {
|
||||
return nil, fmt.Errorf("unknown encoder name: %s", encoderName)
|
||||
}
|
||||
}
|
||||
|
||||
return encoder, nil
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user