Skip to content

Commit

Permalink
fix: fix format of received certs
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenschneider committed Nov 17, 2023
1 parent 85d1eb3 commit aa0fe38
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
17 changes: 13 additions & 4 deletions internal/server/acme/lego.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package acme
import (
"errors"
"fmt"
"regexp"

"github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/challenge"
Expand All @@ -16,6 +17,9 @@ import (

const DnsProviderRoute53 = "route53"

// removes double line breaks
var lineBreaksRegex = regexp.MustCompile(`(\r\n?|\n){2,}`)

type GoLego struct {
client *lego.Client
}
Expand Down Expand Up @@ -159,9 +163,14 @@ func fromLego(other *certificate.Resource) certstorage.AcmeCertificate {
Domain: other.Domain,
CertURL: other.CertURL,
CertStableURL: other.CertStableURL,
PrivateKey: other.PrivateKey,
Certificate: other.Certificate,
IssuerCertificate: other.IssuerCertificate,
CSR: other.CSR,
PrivateKey: fixLineBreaks(other.PrivateKey),
Certificate: fixLineBreaks(other.Certificate),
IssuerCertificate: fixLineBreaks(other.IssuerCertificate),
CSR: fixLineBreaks(other.CSR),
}
}

func fixLineBreaks(input []byte) (ret []byte) {
ret = []byte(lineBreaksRegex.ReplaceAll(input, []byte("$1")))
return
}
30 changes: 30 additions & 0 deletions internal/server/acme/lego_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package acme

import (
"reflect"
"testing"
)

func Test_fixLineBreaks(t *testing.T) {
cert := []byte(`-----BEGIN CERTIFICATE-----
example data
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
more example data
-----END CERTIFICATE-----
`)
wanted := []byte(`-----BEGIN CERTIFICATE-----
example data
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
more example data
-----END CERTIFICATE-----
`)

got := fixLineBreaks(cert)
if !reflect.DeepEqual(got, wanted) {
t.Errorf("Expected %s, got %s", string(wanted), string(got))
}
}

0 comments on commit aa0fe38

Please sign in to comment.