Skip to content

Commit

Permalink
fix: parse ssl certificate error #270
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Feb 6, 2024
1 parent e1c38e2 commit 371472e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
6 changes: 3 additions & 3 deletions api/certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
}
Expand Down Expand Up @@ -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"`
Expand Down
4 changes: 2 additions & 2 deletions app/src/views/certificate/CertificateEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions internal/cert/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions internal/validation/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 371472e

Please sign in to comment.