Skip to content

Commit 5d531a9

Browse files
committed
Add HSTS to caddy configuration
1 parent 06c001b commit 5d531a9

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) 2024-present Snowplow Analytics Ltd. All rights reserved.
3+
*
4+
* This software is made available by Snowplow Analytics, Ltd.,
5+
* under the terms of the Snowplow Limited Use License Agreement, Version 1.0
6+
* located at https://docs.snowplow.io/limited-use-license-1.0
7+
* BY INSTALLING, DOWNLOADING, ACCESSING, USING OR DISTRIBUTING ANY PORTION
8+
* OF THE SOFTWARE, YOU AGREE TO THE TERMS OF SUCH LICENSE AGREEMENT.
9+
*/
10+
11+
package main
12+
13+
import (
14+
"io/ioutil"
15+
"strings"
16+
)
17+
18+
func addHstsHeader(configPath string) error {
19+
currentConfig, err := ioutil.ReadFile(configPath)
20+
21+
if err != nil {
22+
return err
23+
}
24+
toReplacePattern :=
25+
`
26+
handle @isHttps {
27+
import handleProtectedPaths
28+
}
29+
`
30+
replaceWithHsts :=
31+
`
32+
handle @isHttps {
33+
import handleProtectedPaths
34+
header Strict-Transport-Security max-age=31536000; includeSubDomains
35+
}
36+
`
37+
newCaddyConfig := strings.Replace(string(currentConfig), toReplacePattern, replaceWithHsts, 1)
38+
return ioutil.WriteFile(configPath, []byte(newCaddyConfig), 0644)
39+
}

provisioning/resources/control-plane/main.go

+16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func main() {
4747
http.HandleFunc("/version", getSpminiVersion)
4848
http.HandleFunc("/telemetry", manageTelemetry)
4949
http.HandleFunc("/reset-service", resetService)
50+
http.HandleFunc("/add-hsts", addHsts)
5051
log.Fatal(http.ListenAndServe(":10000", nil))
5152
}
5253

@@ -131,6 +132,21 @@ func resetService(resp http.ResponseWriter, req *http.Request) {
131132
}
132133
}
133134

135+
func addHsts (resp http.ResponseWriter, req *http.Request) {
136+
if req.Method == "PUT" {
137+
err := addHstsHeader(config.Dirs.Config+"/"+config.ConfigNames.Caddy)
138+
if err != nil {
139+
http.Error(resp, err.Error(), 500)
140+
} else {
141+
resp.WriteHeader(http.StatusOK)
142+
io.WriteString(resp, "OK")
143+
}
144+
} else {
145+
// Return 404 for other methods
146+
http.Error(resp, "", 404)
147+
}
148+
}
149+
134150
func uploadEnrichments(resp http.ResponseWriter, req *http.Request) {
135151
if req.Method == "POST" {
136152
// maxMemory bytes of body's file parts are stored in memory,

0 commit comments

Comments
 (0)