Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls: accept placeholders in string values of certificate loaders #5963

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions modules/caddytls/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@
// FileLoader loads certificates and their associated keys from disk.
type FileLoader []CertKeyFilePair

// Provision implements caddy.Provisioner.
func (fl FileLoader) Provision(ctx caddy.Context) error {
repl, ok := ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
if !ok {
repl = caddy.NewReplacer()
}
for k, pair := range fl {
for i, tag := range pair.Tags {
pair.Tags[i] = repl.ReplaceKnown(tag, "")
}
fl[k] = CertKeyFilePair{
Certificate: repl.ReplaceKnown(pair.Certificate, ""),
Key: repl.ReplaceKnown(pair.Key, ""),
Format: repl.ReplaceKnown(pair.Format, ""),
Tags: pair.Tags,
}
}
return nil
}

// CaddyModule returns the Caddy module information.
func (FileLoader) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
Expand Down Expand Up @@ -87,4 +107,5 @@
}

// Interface guard
var _ CertificateLoader = (FileLoader)(nil)

Check failure on line 110 in modules/caddytls/fileloader.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

File is not `gofumpt`-ed (gofumpt)
var _ caddy.Provisioner = (FileLoader)(nil)
13 changes: 13 additions & 0 deletions modules/caddytls/folderloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@
}
}

// Provision implements caddy.Provisioner.
func (fl FolderLoader) Provision(ctx caddy.Context) error {
repl, ok := ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
if !ok {
repl = caddy.NewReplacer()
}
for k, path := range fl {
fl[k] = repl.ReplaceKnown(path, "")
}
return nil
}

// LoadCertificates loads all the certificates+keys in the directories
// listed in fl from all files ending with .pem. This method of loading
// certificates expects the certificate and key to be bundled into the
Expand Down Expand Up @@ -146,4 +158,5 @@
return cert, nil
}

var _ CertificateLoader = (FolderLoader)(nil)

Check failure on line 161 in modules/caddytls/folderloader.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

File is not `gofumpt`-ed (gofumpt)
var _ caddy.Provisioner = (FolderLoader)(nil)
20 changes: 20 additions & 0 deletions modules/caddytls/pemloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@
// of not needing to store them on disk at all.
type PEMLoader []CertKeyPEMPair

// Provision implements caddy.Provisioner.
func (pl PEMLoader) Provision(ctx caddy.Context) error {
repl, ok := ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
if !ok {
repl = caddy.NewReplacer()
}
for k, pair := range pl {
for i, tag := range pair.Tags {
pair.Tags[i] = repl.ReplaceKnown(tag, "")
}
pl[k] = CertKeyPEMPair{
CertificatePEM: repl.ReplaceKnown(pair.CertificatePEM, ""),
KeyPEM: repl.ReplaceKnown(pair.KeyPEM, ""),
Tags: pair.Tags,
}
}
return nil
}

// CaddyModule returns the Caddy module information.
func (PEMLoader) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
Expand Down Expand Up @@ -69,4 +88,5 @@
}

// Interface guard
var _ CertificateLoader = (PEMLoader)(nil)

Check failure on line 91 in modules/caddytls/pemloader.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

File is not `gofumpt`-ed (gofumpt)
var _ caddy.Provisioner = (PEMLoader)(nil)
16 changes: 16 additions & 0 deletions modules/caddytls/storageloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ func (StorageLoader) CaddyModule() caddy.ModuleInfo {
func (sl *StorageLoader) Provision(ctx caddy.Context) error {
sl.storage = ctx.Storage()
sl.ctx = ctx

repl, ok := ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
if !ok {
repl = caddy.NewReplacer()
}
for k, pair := range sl.Pairs {
for i, tag := range pair.Tags {
pair.Tags[i] = repl.ReplaceKnown(tag, "")
}
sl.Pairs[k] = CertKeyFilePair{
Certificate: repl.ReplaceKnown(pair.Certificate, ""),
Key: repl.ReplaceKnown(pair.Key, ""),
Format: repl.ReplaceKnown(pair.Format, ""),
Tags: pair.Tags,
}
}
return nil
}

Expand Down
Loading