fix: parse ssl certificate error #270

This commit is contained in:
0xJacky 2024-02-06 15:41:36 +08:00
parent e1c38e28a8
commit 371472e67b
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0
5 changed files with 16 additions and 17 deletions

View file

@ -25,7 +25,7 @@ func Transformer(certModel *model.Cert) (certificate *APICertificate) {
if certModel.SSLCertificatePath != "" { if certModel.SSLCertificatePath != "" {
if _, err := os.Stat(certModel.SSLCertificatePath); err == nil { if _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath) sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
if !cert.IsPublicKey(string(sslCertificationBytes)) { if !cert.IsCertificate(string(sslCertificationBytes)) {
sslCertificationBytes = []byte{} sslCertificationBytes = []byte{}
} }
} }
@ -77,9 +77,9 @@ func GetCert(c *gin.Context) {
type certJson struct { type certJson struct {
Name string `json:"name" binding:"required"` Name string `json:"name" binding:"required"`
SSLCertificatePath string `json:"ssl_certificate_path" binding:"required,publickey_path"` SSLCertificatePath string `json:"ssl_certificate_path" binding:"required,certificate_path"`
SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required,privatekey_path"` SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required,privatekey_path"`
SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"` SSLCertificate string `json:"ssl_certificate" binding:"omitempty,certificate"`
SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"` SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"`
ChallengeMethod string `json:"challenge_method"` ChallengeMethod string `json:"challenge_method"`
DnsCredentialID int `json:"dns_credential_id"` DnsCredentialID int `json:"dns_credential_id"`

View file

@ -164,7 +164,7 @@ const isManaged = computed(() => {
:label="$gettext('SSL Certificate Path')" :label="$gettext('SSL Certificate Path')"
:validate-status="errors.ssl_certificate_path ? 'error' : ''" :validate-status="errors.ssl_certificate_path ? 'error' : ''"
:help="errors.ssl_certificate_path === 'required' ? $gettext('This field is required') :help="errors.ssl_certificate_path === 'required' ? $gettext('This field is required')
: errors.ssl_certificate_path === 'publickey_path' : errors.ssl_certificate_path === 'certificate_path'
? $gettext('The path exists, but the file is not a public key') : ''" ? $gettext('The path exists, but the file is not a public key') : ''"
> >
<p v-if="isManaged"> <p v-if="isManaged">
@ -193,7 +193,7 @@ const isManaged = computed(() => {
<AFormItem <AFormItem
:label="$gettext('SSL Certificate Content')" :label="$gettext('SSL Certificate Content')"
:validate-status="errors.ssl_certificate ? 'error' : ''" :validate-status="errors.ssl_certificate ? 'error' : ''"
:help="errors.ssl_certificate === 'publickey' :help="errors.ssl_certificate === 'certificate'
? $gettext('The input is not a SSL Certificate') : ''" ? $gettext('The input is not a SSL Certificate') : ''"
> >
<CodeEditor <CodeEditor

View file

@ -6,13 +6,12 @@ import (
"os" "os"
) )
func IsPublicKey(pemStr string) bool { func IsCertificate(pemStr string) bool {
block, _ := pem.Decode([]byte(pemStr)) block, _ := pem.Decode([]byte(pemStr))
if block == nil { if block == nil {
return false return false
} }
_, err := x509.ParseCertificate(block.Bytes)
_, err := x509.ParsePKIXPublicKey(block.Bytes)
return err == nil return err == nil
} }
@ -31,8 +30,8 @@ func IsPrivateKey(pemStr string) bool {
return errECDSA == nil return errECDSA == nil
} }
// IsPublicKeyPath checks if the file at the given path is a public key or not exists. // IsCertificatePath checks if the file at the given path is a certificate or not exists.
func IsPublicKeyPath(path string) bool { func IsCertificatePath(path string) bool {
if path == "" { if path == "" {
return false return false
} }
@ -50,7 +49,7 @@ func IsPublicKeyPath(path string) bool {
return false return false
} }
return IsPublicKey(string(bytes)) return IsCertificate(string(bytes))
} }
// IsPrivateKeyPath checks if the file at the given path is a private key or not exists. // IsPrivateKeyPath checks if the file at the given path is a private key or not exists.

View file

@ -5,16 +5,16 @@ import (
val "github.com/go-playground/validator/v10" val "github.com/go-playground/validator/v10"
) )
func isPublicKey(fl val.FieldLevel) bool { func isCertificate(fl val.FieldLevel) bool {
return cert.IsPublicKey(fl.Field().String()) return cert.IsCertificate(fl.Field().String())
} }
func isPrivateKey(fl val.FieldLevel) bool { func isPrivateKey(fl val.FieldLevel) bool {
return cert.IsPrivateKey(fl.Field().String()) return cert.IsPrivateKey(fl.Field().String())
} }
func isPublicKeyPath(fl val.FieldLevel) bool { func isCertificatePath(fl val.FieldLevel) bool {
return cert.IsPublicKeyPath(fl.Field().String()) return cert.IsCertificatePath(fl.Field().String())
} }
func isPrivateKeyPath(fl val.FieldLevel) bool { func isPrivateKeyPath(fl val.FieldLevel) bool {

View file

@ -18,7 +18,7 @@ func Init() {
logger.Fatal(err) logger.Fatal(err)
} }
err = v.RegisterValidation("publickey", isPublicKey) err = v.RegisterValidation("certificate", isCertificate)
if err != nil { if err != nil {
logger.Fatal(err) logger.Fatal(err)
@ -30,7 +30,7 @@ func Init() {
logger.Fatal(err) logger.Fatal(err)
} }
err = v.RegisterValidation("publickey_path", isPublicKeyPath) err = v.RegisterValidation("certificate_path", isCertificatePath)
if err != nil { if err != nil {
logger.Fatal(err) logger.Fatal(err)