-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dedicated Traefik v3 implementation
- Loading branch information
Showing
10 changed files
with
576 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"default": { | ||
"Account": { | ||
"Email": "test@email.com", | ||
"Registration": { | ||
"body": { | ||
"status": "valid", | ||
"contact": [ | ||
"mailto:test@email.com" | ||
] | ||
}, | ||
"uri": "https://acme-v02.api.letsencrypt.org/acme/acct/12345678" | ||
}, | ||
"PrivateKey": "Q2VydGlmaWNhdGUgS2V5", | ||
"KeyType": "4096" | ||
}, | ||
"Certificates": [ | ||
{ | ||
"domain": { | ||
"main": "my.domain.com" | ||
}, | ||
"certificate": "Q2VydGlmaWNhdGU=", | ||
"key": "Q2VydGlmaWNhdGUgS2V5", | ||
"Store": "default" | ||
}, | ||
{ | ||
"domain": { | ||
"main": "my.domain2.com" | ||
}, | ||
"certificate": "Q2VydGlmaWNhdGU=", | ||
"key": "Q2VydGlmaWNhdGUgS2V5", | ||
"Store": "default" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package v3 | ||
|
||
import ( | ||
"encoding/pem" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/go-acme/lego/v4/certcrypto" | ||
"github.com/ldez/traefik-certs-dumper/v2/dumper" | ||
"github.com/traefik/traefik/v3/pkg/provider/acme" | ||
) | ||
|
||
const ( | ||
certsSubDir = "certs" | ||
keysSubDir = "private" | ||
) | ||
|
||
// Dump Dumps data to certificates. | ||
func Dump(data map[string]*acme.StoredData, baseConfig *dumper.BaseConfig) error { | ||
if baseConfig.Clean { | ||
err := cleanDir(baseConfig.DumpPath) | ||
if err != nil { | ||
return fmt.Errorf("folder cleaning failed: %w", err) | ||
} | ||
} | ||
|
||
if !baseConfig.DomainSubDir { | ||
if err := os.MkdirAll(filepath.Join(baseConfig.DumpPath, certsSubDir), 0o755); err != nil { | ||
return fmt.Errorf("certs folder creation failure: %w", err) | ||
} | ||
} | ||
|
||
if err := os.MkdirAll(filepath.Join(baseConfig.DumpPath, keysSubDir), 0o755); err != nil { | ||
return fmt.Errorf("keys folder creation failure: %w", err) | ||
} | ||
|
||
for _, store := range data { | ||
for _, cert := range store.Certificates { | ||
err := writeCert(baseConfig.DumpPath, cert.Certificate, baseConfig.CrtInfo, baseConfig.DomainSubDir) | ||
if err != nil { | ||
return fmt.Errorf("failed to write certificates: %w", err) | ||
} | ||
|
||
err = writeKey(baseConfig.DumpPath, cert.Certificate, baseConfig.KeyInfo, baseConfig.DomainSubDir) | ||
if err != nil { | ||
return fmt.Errorf("failed to write certificate keys: %w", err) | ||
} | ||
} | ||
|
||
if store.Account == nil { | ||
continue | ||
} | ||
|
||
privateKeyPem := extractPEMPrivateKey(store.Account) | ||
|
||
err := os.WriteFile(filepath.Join(baseConfig.DumpPath, keysSubDir, "letsencrypt"+baseConfig.KeyInfo.Ext), privateKeyPem, 0o600) | ||
if err != nil { | ||
return fmt.Errorf("failed to write private key: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func writeCert(dumpPath string, cert acme.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) | ||
if err := os.MkdirAll(filepath.Join(dumpPath, safeName(cert.Domain.Main)), 0o755); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return os.WriteFile(certPath, cert.Certificate, 0o666) | ||
} | ||
|
||
func writeKey(dumpPath string, cert acme.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) | ||
if err := os.MkdirAll(filepath.Join(dumpPath, safeName(cert.Domain.Main)), 0o755); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return os.WriteFile(keyPath, cert.Key, 0o600) | ||
} | ||
|
||
func extractPEMPrivateKey(account *acme.Account) []byte { | ||
var block *pem.Block | ||
switch account.KeyType { | ||
case certcrypto.RSA2048, certcrypto.RSA4096, certcrypto.RSA8192: | ||
block = &pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Bytes: account.PrivateKey, | ||
} | ||
case certcrypto.EC256, certcrypto.EC384: | ||
block = &pem.Block{ | ||
Type: "EC PRIVATE KEY", | ||
Bytes: account.PrivateKey, | ||
} | ||
default: | ||
panic(fmt.Sprintf("unsupported key type: '%v'", account.KeyType)) | ||
} | ||
|
||
return pem.EncodeToMemory(block) | ||
} | ||
|
||
func cleanDir(dumpPath string) error { | ||
_, errExists := os.Stat(dumpPath) | ||
if os.IsNotExist(errExists) { | ||
return nil | ||
} | ||
|
||
if errExists != nil { | ||
return errExists | ||
} | ||
|
||
dir, err := os.ReadDir(dumpPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, f := range dir { | ||
if err := os.RemoveAll(filepath.Join(dumpPath, f.Name())); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package v3 | ||
|
||
func safeName(filename string) string { | ||
return filename | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package v3 | ||
|
||
import "strings" | ||
|
||
func safeName(filename string) string { | ||
return strings.ReplaceAll(filename, "*", "_") | ||
} |
Oops, something went wrong.