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 _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
if !cert.IsPublicKey(string(sslCertificationBytes)) {
if !cert.IsCertificate(string(sslCertificationBytes)) {
sslCertificationBytes = []byte{}
}
}
@ -77,9 +77,9 @@ func GetCert(c *gin.Context) {
type certJson struct {
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"`
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"`
ChallengeMethod string `json:"challenge_method"`
DnsCredentialID int `json:"dns_credential_id"`

View file

@ -164,7 +164,7 @@ const isManaged = computed(() => {
:label="$gettext('SSL Certificate Path')"
:validate-status="errors.ssl_certificate_path ? 'error' : ''"
: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') : ''"
>
<p v-if="isManaged">
@ -193,7 +193,7 @@ const isManaged = computed(() => {
<AFormItem
:label="$gettext('SSL Certificate Content')"
:validate-status="errors.ssl_certificate ? 'error' : ''"
:help="errors.ssl_certificate === 'publickey'
:help="errors.ssl_certificate === 'certificate'
? $gettext('The input is not a SSL Certificate') : ''"
>
<CodeEditor

View file

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

View file

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

View file

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