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

Add HSTS to caddy configuration #395

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 12 additions & 3 deletions provisioning/resources/configs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,18 @@ paths:
description: "Telemetry has been reconfigured"
"401":
description: "Unauthorized"



/add-hsts:
put:
tags:
- "configuration"
summary: "Add HSTS header"
description: "Adds HSTS header to underlying caddy configuration. When added, 'Strict-Transport-Security' header is returned for each HTTPS response"
operationId: "addHsts"
responses:
"200":
description: "HSTS header has been added"
"401":
description: "Unauthorized"

/restart-services:
put:
Expand Down
39 changes: 39 additions & 0 deletions provisioning/resources/control-plane/add_hsts_header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2024-present Snowplow Analytics Ltd. All rights reserved.
*
* This software is made available by Snowplow Analytics, Ltd.,
* under the terms of the Snowplow Limited Use License Agreement, Version 1.0
* located at https://docs.snowplow.io/limited-use-license-1.0
* BY INSTALLING, DOWNLOADING, ACCESSING, USING OR DISTRIBUTING ANY PORTION
* OF THE SOFTWARE, YOU AGREE TO THE TERMS OF SUCH LICENSE AGREEMENT.
*/

package main

import (
"io/ioutil"
"strings"
)

func addHstsHeader(configPath string) error {
currentConfig, err := ioutil.ReadFile(configPath)

if err != nil {
return err
}
toReplacePattern :=
`
handle @isHttps {
import handleProtectedPaths
}
`
replaceWithHsts :=
`
handle @isHttps {
import handleProtectedPaths
header Strict-Transport-Security "max-age=31536000; includeSubDomains"
}
`
newCaddyConfig := strings.Replace(string(currentConfig), toReplacePattern, replaceWithHsts, 1)
return ioutil.WriteFile(configPath, []byte(newCaddyConfig), 0644)
}
21 changes: 21 additions & 0 deletions provisioning/resources/control-plane/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func main() {
http.HandleFunc("/version", getSpminiVersion)
http.HandleFunc("/telemetry", manageTelemetry)
http.HandleFunc("/reset-service", resetService)
http.HandleFunc("/add-hsts", addHsts)
log.Fatal(http.ListenAndServe(":10000", nil))
}

Expand Down Expand Up @@ -131,6 +132,26 @@ func resetService(resp http.ResponseWriter, req *http.Request) {
}
}

func addHsts(resp http.ResponseWriter, req *http.Request) {
if req.Method == "PUT" {
err := addHstsHeader(config.ConfigNames.Caddy)
if err != nil {
http.Error(resp, err.Error(), 500)
return
}
err, status := restartSPService("caddy")
if err != nil {
http.Error(resp, err.Error(), status)
return
}
resp.WriteHeader(http.StatusOK)
io.WriteString(resp, "OK")
} else {
// Return 404 for other methods
http.Error(resp, "", 404)
}
}

func uploadEnrichments(resp http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
// maxMemory bytes of body's file parts are stored in memory,
Expand Down
Loading