Skip to content

Commit 9cbc91b

Browse files
committed
Dedup some httpd related templates
Signed-off-by: Francesco Pantano <fpantano@redhat.com>
1 parent a2ed0e1 commit 9cbc91b

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

modules/common/util/template_util.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package util
1919
import (
2020
"bufio"
2121
"bytes"
22+
"embed"
2223
"fmt"
24+
"io/fs"
2325
"os"
2426
"path"
2527
"path/filepath"
@@ -29,6 +31,9 @@ import (
2931
corev1 "k8s.io/api/core/v1"
3032
)
3133

34+
//go:embed templates/common/config/*
35+
var commonTemplates embed.FS
36+
3237
// TType - TemplateType
3338
type TType string
3439

@@ -344,3 +349,34 @@ func GetTemplateData(t Template) (map[string]string, error) {
344349

345350
return data, nil
346351
}
352+
353+
// GetCommonTemplates renders the common config templates (ssl.conf, etc.)
354+
// shipped with lib-common using the provided configOptions, and returns
355+
// map[filename]renderedContent.
356+
// Callers merge the result into their Template.CustomData before calling
357+
// EnsureSecrets/EnsureConfigMaps so that common templates are included in the
358+
// generated Secret/ConfigMaps without each operator duplicating the files.
359+
func GetCommonTemplates(configOptions map[string]any) (map[string]string, error) {
360+
dir := "templates/common/config"
361+
entries, err := fs.ReadDir(commonTemplates, dir)
362+
if err != nil {
363+
return nil, err
364+
}
365+
366+
result := make(map[string]string, len(entries))
367+
for _, e := range entries {
368+
if e.IsDir() {
369+
continue
370+
}
371+
data, err := fs.ReadFile(commonTemplates, path.Join(dir, e.Name()))
372+
if err != nil {
373+
return nil, err
374+
}
375+
rendered, err := ExecuteTemplateData(string(data), configOptions)
376+
if err != nil {
377+
return nil, err
378+
}
379+
result[e.Name()] = rendered
380+
}
381+
return result, nil
382+
}

modules/common/util/template_util_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,3 +618,16 @@ baz=1
618618

619619
g.Expect(cleaned2).To(Equal(cleaned))
620620
}
621+
622+
func TestGetCommonTemplates(t *testing.T) {
623+
t.Run("Returns ssl.conf", func(t *testing.T) {
624+
g := NewWithT(t)
625+
626+
result, err := GetCommonTemplates(map[string]interface{}{})
627+
g.Expect(err).NotTo(HaveOccurred())
628+
g.Expect(result).To(HaveKey("ssl.conf"))
629+
g.Expect(result["ssl.conf"]).To(ContainSubstring("mod_ssl"))
630+
g.Expect(result["ssl.conf"]).To(ContainSubstring("SSLProtocol"))
631+
g.Expect(result["ssl.conf"]).To(ContainSubstring("SSLCipherSuite"))
632+
})
633+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<IfModule mod_ssl.c>
2+
SSLRandomSeed startup builtin
3+
SSLRandomSeed startup file:/dev/urandom 512
4+
SSLRandomSeed connect builtin
5+
SSLRandomSeed connect file:/dev/urandom 512
6+
7+
AddType application/x-x509-ca-cert .crt
8+
AddType application/x-pkcs7-crl .crl
9+
10+
SSLPassPhraseDialog builtin
11+
SSLSessionCache "shmcb:/var/cache/mod_ssl/scache(512000)"
12+
SSLSessionCacheTimeout 300
13+
Mutex default
14+
SSLCryptoDevice builtin
15+
SSLHonorCipherOrder On
16+
SSLUseStapling Off
17+
SSLStaplingCache "shmcb:/run/httpd/ssl_stapling(32768)"
18+
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!RC4:!3DES
19+
SSLProtocol all -SSLv2 -SSLv3 -TLSv1
20+
SSLOptions StdEnvVars
21+
</IfModule>

0 commit comments

Comments
 (0)