Skip to content

Commit

Permalink
feat: improve binary size
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Dec 5, 2024
1 parent ee3231c commit 172bd87
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 1,373 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ issues:
linters:
- gochecknoglobals
- gochecknoinits
- path: internal/traefikv[1-3]/
linters:
- tagalign

output:
show-stats: true
Expand Down
11 changes: 6 additions & 5 deletions dumper/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
dumperv2 "github.com/ldez/traefik-certs-dumper/v2/dumper/v2"
dumperv3 "github.com/ldez/traefik-certs-dumper/v2/dumper/v3"
"github.com/ldez/traefik-certs-dumper/v2/hook"
acmev2 "github.com/traefik/traefik/v2/pkg/provider/acme"
acmev3 "github.com/traefik/traefik/v3/pkg/provider/acme"
"github.com/ldez/traefik-certs-dumper/v2/internal/traefikv1"
"github.com/ldez/traefik-certs-dumper/v2/internal/traefikv2"
"github.com/ldez/traefik-certs-dumper/v2/internal/traefikv3"
)

// Dump Dumps "acme.json" file to certificates.
Expand Down Expand Up @@ -76,7 +77,7 @@ func dump(acmeFile string, baseConfig *dumper.BaseConfig) error {
}

func dumpV1(acmeFile string, baseConfig *dumper.BaseConfig) error {
data := &dumperv1.StoredData{}
data := &traefikv1.StoredData{}
err := readJSONFile(acmeFile, data)
if err != nil {
return err
Expand All @@ -86,7 +87,7 @@ func dumpV1(acmeFile string, baseConfig *dumper.BaseConfig) error {
}

func dumpV2(acmeFile string, baseConfig *dumper.BaseConfig) error {
data := map[string]*acmev2.StoredData{}
data := map[string]*traefikv2.StoredData{}
err := readJSONFile(acmeFile, &data)
if err != nil {
return err
Expand All @@ -96,7 +97,7 @@ func dumpV2(acmeFile string, baseConfig *dumper.BaseConfig) error {
}

func dumpV3(acmeFile string, baseConfig *dumper.BaseConfig) error {
data := map[string]*acmev3.StoredData{}
data := map[string]*traefikv3.StoredData{}
err := readJSONFile(acmeFile, &data)
if err != nil {
return err
Expand Down
14 changes: 7 additions & 7 deletions dumper/kv/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package kv
import (
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/registration"
v1 "github.com/ldez/traefik-certs-dumper/v2/dumper/v1"
"github.com/ldez/traefik-certs-dumper/v2/internal/traefikv1"
)

// CertificateOld is used to store certificate info.
Expand Down Expand Up @@ -39,24 +39,24 @@ type ChallengeCert struct {

// DomainsCertificate contains a certificate for multiple domains.
type DomainsCertificate struct {
Domains v1.Domain
Domains traefikv1.Domain
Certificate *CertificateOld
}

// convertOldAccount converts account information from old account format.
func convertOldAccount(account *AccountOld) *v1.StoredData {
storedData := &v1.StoredData{
Account: &v1.Account{
func convertOldAccount(account *AccountOld) *traefikv1.StoredData {
storedData := &traefikv1.StoredData{
Account: &traefikv1.Account{
PrivateKey: account.PrivateKey,
Registration: account.Registration,
Email: account.Email,
KeyType: account.KeyType,
},
}

var certs []*v1.Certificate
var certs []*traefikv1.Certificate
for _, oldCert := range account.DomainsCertificate.Certs {
certs = append(certs, &v1.Certificate{
certs = append(certs, &traefikv1.Certificate{
Certificate: oldCert.Certificate.Certificate,
Domain: oldCert.Domains,
Key: oldCert.Certificate.PrivateKey,
Expand Down
3 changes: 2 additions & 1 deletion dumper/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ldez/traefik-certs-dumper/v2/dumper"
v1 "github.com/ldez/traefik-certs-dumper/v2/dumper/v1"
"github.com/ldez/traefik-certs-dumper/v2/hook"
"github.com/ldez/traefik-certs-dumper/v2/internal/traefikv1"
)

// DefaultStoreKeySuffix is the default suffix/storage.
Expand Down Expand Up @@ -76,7 +77,7 @@ func dumpPair(pair *store.KVPair, baseConfig *dumper.BaseConfig) error {
return v1.Dump(data, baseConfig)
}

func getStoredDataFromGzip(pair *store.KVPair) (*v1.StoredData, error) {
func getStoredDataFromGzip(pair *store.KVPair) (*traefikv1.StoredData, error) {
reader, err := gzip.NewReader(bytes.NewBuffer(pair.Value))
if err != nil {
return nil, fmt.Errorf("fail to create GZip reader: %w", err)
Expand Down
9 changes: 5 additions & 4 deletions dumper/v1/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/go-acme/lego/v4/certcrypto"
"github.com/ldez/traefik-certs-dumper/v2/dumper"
"github.com/ldez/traefik-certs-dumper/v2/internal/traefikv1"
)

const (
Expand All @@ -16,7 +17,7 @@ const (
)

// Dump Dumps data to certificates.
func Dump(data *StoredData, baseConfig *dumper.BaseConfig) error {
func Dump(data *traefikv1.StoredData, baseConfig *dumper.BaseConfig) error {
if baseConfig.Clean {
err := cleanDir(baseConfig.DumpPath)
if err != nil {
Expand Down Expand Up @@ -54,7 +55,7 @@ func Dump(data *StoredData, baseConfig *dumper.BaseConfig) error {
return os.WriteFile(filepath.Join(baseConfig.DumpPath, keysSubDir, "letsencrypt"+baseConfig.KeyInfo.Ext), privateKeyPem, 0o600)
}

func writeCert(dumpPath string, cert *Certificate, info dumper.FileInfo, domainSubDir bool) error {
func writeCert(dumpPath string, cert *traefikv1.Certificate, info dumper.FileInfo, domainSubDir bool) error {
certPath := filepath.Join(dumpPath, certsSubDir, safeName(cert.Domain.Main+info.Ext))
if domainSubDir {
certPath = filepath.Join(dumpPath, safeName(cert.Domain.Main), info.Name+info.Ext)
Expand All @@ -66,7 +67,7 @@ func writeCert(dumpPath string, cert *Certificate, info dumper.FileInfo, domainS
return os.WriteFile(certPath, cert.Certificate, 0o666)
}

func writeKey(dumpPath string, cert *Certificate, info dumper.FileInfo, domainSubDir bool) error {
func writeKey(dumpPath string, cert *traefikv1.Certificate, info dumper.FileInfo, domainSubDir bool) error {
keyPath := filepath.Join(dumpPath, keysSubDir, safeName(cert.Domain.Main+info.Ext))
if domainSubDir {
keyPath = filepath.Join(dumpPath, safeName(cert.Domain.Main), info.Name+info.Ext)
Expand All @@ -78,7 +79,7 @@ func writeKey(dumpPath string, cert *Certificate, info dumper.FileInfo, domainSu
return os.WriteFile(keyPath, cert.Key, 0o600)
}

func extractPEMPrivateKey(account *Account) []byte {
func extractPEMPrivateKey(account *traefikv1.Account) []byte {
var block *pem.Block
switch account.KeyType {
case certcrypto.RSA2048, certcrypto.RSA4096, certcrypto.RSA8192:
Expand Down
10 changes: 5 additions & 5 deletions dumper/v2/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/go-acme/lego/v4/certcrypto"
"github.com/ldez/traefik-certs-dumper/v2/dumper"
"github.com/traefik/traefik/v2/pkg/provider/acme"
"github.com/ldez/traefik-certs-dumper/v2/internal/traefikv2"
)

const (
Expand All @@ -17,7 +17,7 @@ const (
)

// Dump Dumps data to certificates.
func Dump(data map[string]*acme.StoredData, baseConfig *dumper.BaseConfig) error {
func Dump(data map[string]*traefikv2.StoredData, baseConfig *dumper.BaseConfig) error {
if baseConfig.Clean {
err := cleanDir(baseConfig.DumpPath)
if err != nil {
Expand Down Expand Up @@ -63,7 +63,7 @@ func Dump(data map[string]*acme.StoredData, baseConfig *dumper.BaseConfig) error
return nil
}

func writeCert(dumpPath string, cert acme.Certificate, info dumper.FileInfo, domainSubDir bool) error {
func writeCert(dumpPath string, cert traefikv2.Certificate, info dumper.FileInfo, domainSubDir bool) error {
certPath := filepath.Join(dumpPath, certsSubDir, safeName(cert.Domain.Main+info.Ext))
if domainSubDir {
certPath = filepath.Join(dumpPath, safeName(cert.Domain.Main), info.Name+info.Ext)
Expand All @@ -75,7 +75,7 @@ func writeCert(dumpPath string, cert acme.Certificate, info dumper.FileInfo, dom
return os.WriteFile(certPath, cert.Certificate, 0o666)
}

func writeKey(dumpPath string, cert acme.Certificate, info dumper.FileInfo, domainSubDir bool) error {
func writeKey(dumpPath string, cert traefikv2.Certificate, info dumper.FileInfo, domainSubDir bool) error {
keyPath := filepath.Join(dumpPath, keysSubDir, safeName(cert.Domain.Main+info.Ext))
if domainSubDir {
keyPath = filepath.Join(dumpPath, safeName(cert.Domain.Main), info.Name+info.Ext)
Expand All @@ -87,7 +87,7 @@ func writeKey(dumpPath string, cert acme.Certificate, info dumper.FileInfo, doma
return os.WriteFile(keyPath, cert.Key, 0o600)
}

func extractPEMPrivateKey(account *acme.Account) []byte {
func extractPEMPrivateKey(account *traefikv2.Account) []byte {
var block *pem.Block
switch account.KeyType {
case certcrypto.RSA2048, certcrypto.RSA4096, certcrypto.RSA8192:
Expand Down
10 changes: 5 additions & 5 deletions dumper/v3/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/go-acme/lego/v4/certcrypto"
"github.com/ldez/traefik-certs-dumper/v2/dumper"
"github.com/traefik/traefik/v3/pkg/provider/acme"
"github.com/ldez/traefik-certs-dumper/v2/internal/traefikv3"
)

const (
Expand All @@ -17,7 +17,7 @@ const (
)

// Dump Dumps data to certificates.
func Dump(data map[string]*acme.StoredData, baseConfig *dumper.BaseConfig) error {
func Dump(data map[string]*traefikv3.StoredData, baseConfig *dumper.BaseConfig) error {
if baseConfig.Clean {
err := cleanDir(baseConfig.DumpPath)
if err != nil {
Expand Down Expand Up @@ -63,7 +63,7 @@ func Dump(data map[string]*acme.StoredData, baseConfig *dumper.BaseConfig) error
return nil
}

func writeCert(dumpPath string, cert acme.Certificate, info dumper.FileInfo, domainSubDir bool) error {
func writeCert(dumpPath string, cert traefikv3.Certificate, info dumper.FileInfo, domainSubDir bool) error {
certPath := filepath.Join(dumpPath, certsSubDir, safeName(cert.Domain.Main+info.Ext))
if domainSubDir {
certPath = filepath.Join(dumpPath, safeName(cert.Domain.Main), info.Name+info.Ext)
Expand All @@ -75,7 +75,7 @@ func writeCert(dumpPath string, cert acme.Certificate, info dumper.FileInfo, dom
return os.WriteFile(certPath, cert.Certificate, 0o666)
}

func writeKey(dumpPath string, cert acme.Certificate, info dumper.FileInfo, domainSubDir bool) error {
func writeKey(dumpPath string, cert traefikv3.Certificate, info dumper.FileInfo, domainSubDir bool) error {
keyPath := filepath.Join(dumpPath, keysSubDir, safeName(cert.Domain.Main+info.Ext))
if domainSubDir {
keyPath = filepath.Join(dumpPath, safeName(cert.Domain.Main), info.Name+info.Ext)
Expand All @@ -87,7 +87,7 @@ func writeKey(dumpPath string, cert acme.Certificate, info dumper.FileInfo, doma
return os.WriteFile(keyPath, cert.Key, 0o600)
}

func extractPEMPrivateKey(account *acme.Account) []byte {
func extractPEMPrivateKey(account *traefikv3.Account) []byte {
var block *pem.Block
switch account.KeyType {
case certcrypto.RSA2048, certcrypto.RSA4096, certcrypto.RSA8192:
Expand Down
Loading

0 comments on commit 172bd87

Please sign in to comment.