2017-08-03 12:13:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-17 13:05:03 +00:00
|
|
|
"crypto/rand"
|
2021-09-06 10:37:52 +00:00
|
|
|
"embed"
|
2023-06-17 13:05:03 +00:00
|
|
|
"encoding/base64"
|
2023-06-10 16:21:38 +00:00
|
|
|
"io/fs"
|
2017-08-03 16:32:30 +00:00
|
|
|
"mime"
|
2017-08-03 12:13:53 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-08-03 16:32:30 +00:00
|
|
|
"strings"
|
2021-09-26 12:49:18 +00:00
|
|
|
"text/template"
|
2023-06-10 16:21:38 +00:00
|
|
|
"time"
|
2017-08-03 12:13:53 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-09-26 12:49:18 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-06-10 16:21:38 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-05-10 14:19:20 +00:00
|
|
|
|
2023-06-10 16:21:38 +00:00
|
|
|
file_helpers "github.com/Luzifer/go_helpers/v2/file"
|
2020-01-24 15:15:17 +00:00
|
|
|
http_helpers "github.com/Luzifer/go_helpers/v2/http"
|
|
|
|
"github.com/Luzifer/rconfig/v2"
|
2017-08-03 12:13:53 +00:00
|
|
|
)
|
|
|
|
|
2023-06-17 13:05:03 +00:00
|
|
|
const scriptNonceSize = 32
|
|
|
|
|
2017-08-03 12:13:53 +00:00
|
|
|
var (
|
|
|
|
cfg struct {
|
2023-06-10 16:21:38 +00:00
|
|
|
Customize string `flag:"customize" default:"" description:"Customize-File to load"`
|
2017-08-03 12:13:53 +00:00
|
|
|
Listen string `flag:"listen" default:":3000" description:"IP/Port to listen on"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Set log level (debug, info, warning, error)"`
|
2023-06-09 23:36:01 +00:00
|
|
|
SecretExpiry int64 `flag:"secret-expiry" default:"0" description:"Maximum expiry of the stored secrets in seconds"`
|
2017-08-03 12:13:53 +00:00
|
|
|
StorageType string `flag:"storage-type" default:"mem" description:"Storage to use for putting secrets to" validate:"nonzero"`
|
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Print version information and exit"`
|
|
|
|
}
|
|
|
|
|
2023-06-10 16:21:38 +00:00
|
|
|
assets file_helpers.FSStack
|
|
|
|
cust customize
|
|
|
|
indexTpl *template.Template
|
|
|
|
|
2017-08-03 12:13:53 +00:00
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
2021-09-06 10:37:52 +00:00
|
|
|
//go:embed frontend/*
|
2023-06-10 16:21:38 +00:00
|
|
|
var embeddedAssets embed.FS
|
2021-09-06 10:37:52 +00:00
|
|
|
|
2023-06-17 13:05:03 +00:00
|
|
|
func defaultCSP() http_helpers.CSP {
|
|
|
|
c := http_helpers.CSP{}
|
|
|
|
|
|
|
|
c.Add("base-uri", http_helpers.CSPSrcSelf)
|
|
|
|
c.Add("default-src", http_helpers.CSPSrcNone)
|
|
|
|
c.Add("connect-src", http_helpers.CSPSrcSelf)
|
|
|
|
c.Add("font-src", http_helpers.CSPSrcSelf)
|
|
|
|
c.Add("img-src", http_helpers.CSPSrcSelf)
|
|
|
|
c.Add("img-src", http_helpers.CSPSrcSchemeData)
|
|
|
|
c.Add("script-src", http_helpers.CSPSrcSelf)
|
|
|
|
c.Add("style-src", http_helpers.CSPSrcSelf)
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-06-10 16:21:38 +00:00
|
|
|
func initApp() error {
|
|
|
|
rconfig.AutoEnv(true)
|
2017-08-03 12:13:53 +00:00
|
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
2023-06-10 16:21:38 +00:00
|
|
|
return errors.Wrap(err, "parsing cli options")
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-10 16:21:38 +00:00
|
|
|
l, err := logrus.ParseLevel(cfg.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "parsing log-level")
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
2023-06-10 16:21:38 +00:00
|
|
|
logrus.SetLevel(l)
|
2017-08-03 12:13:53 +00:00
|
|
|
|
2023-06-10 16:21:38 +00:00
|
|
|
if cust, err = loadCustomize(cfg.Customize); err != nil {
|
|
|
|
return errors.Wrap(err, "loading customizations")
|
|
|
|
}
|
|
|
|
|
|
|
|
frontendFS, err := fs.Sub(embeddedAssets, "frontend")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "creating sub-fs for assets")
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
2023-06-10 16:21:38 +00:00
|
|
|
assets = append(assets, frontendFS)
|
|
|
|
|
|
|
|
if cust.OverlayFSPath != "" {
|
|
|
|
assets = append(file_helpers.FSStack{os.DirFS(cust.OverlayFSPath)}, assets...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-06-10 16:21:38 +00:00
|
|
|
var err error
|
|
|
|
if err = initApp(); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("initializing app")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.VersionAndExit {
|
|
|
|
logrus.WithField("version", version).Info("ots")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize index template in order not to parse it multiple times
|
|
|
|
source, err := assets.ReadFile("index.html")
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Fatal("frontend folder should contain index.html Go template")
|
|
|
|
}
|
|
|
|
indexTpl = template.Must(template.New("index.html").Funcs(tplFuncs).Parse(string(source)))
|
|
|
|
|
|
|
|
// Initialize storage
|
2017-08-03 12:13:53 +00:00
|
|
|
store, err := getStorageByType(cfg.StorageType)
|
|
|
|
if err != nil {
|
2023-06-10 16:21:38 +00:00
|
|
|
logrus.WithError(err).Fatal("initializing storage")
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
|
|
|
api := newAPI(store)
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2021-09-26 12:49:18 +00:00
|
|
|
r.Use(http_helpers.GzipHandler)
|
|
|
|
|
2017-08-03 12:13:53 +00:00
|
|
|
api.Register(r.PathPrefix("/api").Subrouter())
|
2021-09-26 12:49:18 +00:00
|
|
|
|
|
|
|
r.HandleFunc("/", handleIndex)
|
|
|
|
r.PathPrefix("/").HandlerFunc(assetDelivery)
|
2017-08-03 16:32:30 +00:00
|
|
|
|
2023-06-10 18:40:59 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"secret_expiry": time.Duration(cfg.SecretExpiry) * time.Second,
|
|
|
|
"version": version,
|
|
|
|
}).Info("ots started")
|
2023-06-10 16:21:38 +00:00
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: cfg.Listen,
|
|
|
|
Handler: http_helpers.NewHTTPLogHandlerWithLogger(r, logrus.StandardLogger()),
|
|
|
|
ReadHeaderTimeout: time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = server.ListenAndServe(); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("HTTP server quit unexpectedly")
|
|
|
|
}
|
2017-08-03 16:32:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 12:49:18 +00:00
|
|
|
func assetDelivery(w http.ResponseWriter, r *http.Request) {
|
2023-06-10 16:21:38 +00:00
|
|
|
assetName := strings.TrimLeft(r.URL.Path, "/")
|
2017-08-03 16:32:30 +00:00
|
|
|
|
2021-09-06 10:33:38 +00:00
|
|
|
dot := strings.LastIndex(assetName, ".")
|
|
|
|
if dot < 0 {
|
2019-05-10 21:12:00 +00:00
|
|
|
// There are no assets with no dot in it
|
2021-09-26 12:49:18 +00:00
|
|
|
http.Error(w, "404 not found", http.StatusNotFound)
|
2019-05-10 21:12:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:33:38 +00:00
|
|
|
ext := assetName[dot:]
|
2023-06-10 16:21:38 +00:00
|
|
|
assetData, err := assets.ReadFile(assetName)
|
2017-08-03 16:32:30 +00:00
|
|
|
if err != nil {
|
2021-09-26 12:49:18 +00:00
|
|
|
http.Error(w, "404 not found", http.StatusNotFound)
|
2017-08-03 16:32:30 +00:00
|
|
|
return
|
|
|
|
}
|
2017-08-03 12:13:53 +00:00
|
|
|
|
2021-09-26 12:49:18 +00:00
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
2021-09-26 14:47:25 +00:00
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
2021-09-26 12:49:18 +00:00
|
|
|
w.Write(assetData)
|
2019-05-10 21:12:00 +00:00
|
|
|
}
|
2017-08-04 19:16:00 +00:00
|
|
|
|
2021-09-26 14:47:25 +00:00
|
|
|
func handleIndex(w http.ResponseWriter, r *http.Request) {
|
2023-06-17 13:05:03 +00:00
|
|
|
inlineContentNonce := make([]byte, scriptNonceSize)
|
|
|
|
if _, err := rand.Read(inlineContentNonce); err != nil {
|
|
|
|
logrus.WithError(err).Error("generating script nonce")
|
|
|
|
http.Error(w, "something went wrong", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
inlineContentNonceStr := base64.StdEncoding.EncodeToString(inlineContentNonce)
|
|
|
|
|
|
|
|
policy := defaultCSP()
|
|
|
|
policy.Add("script-src", http_helpers.CSPSrcNonce(inlineContentNonceStr))
|
|
|
|
policy.Add("style-src", http_helpers.CSPSrcNonce(inlineContentNonceStr))
|
|
|
|
|
2021-09-26 14:47:25 +00:00
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
w.Header().Set("Referrer-Policy", "no-referrer")
|
|
|
|
w.Header().Set("X-Frame-Options", "DENY")
|
|
|
|
w.Header().Set("X-Xss-Protection", "1; mode=block")
|
2023-06-17 13:05:03 +00:00
|
|
|
w.Header().Set("Content-Security-Policy", policy.ToHeaderValue())
|
2021-09-26 14:47:25 +00:00
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
|
|
|
|
if err := indexTpl.Execute(w, struct {
|
2023-06-17 13:05:03 +00:00
|
|
|
Customize customize
|
|
|
|
InlineContentNonce string
|
|
|
|
Version string
|
2021-09-26 12:49:18 +00:00
|
|
|
}{
|
2023-06-17 13:05:03 +00:00
|
|
|
Customize: cust,
|
|
|
|
InlineContentNonce: inlineContentNonceStr,
|
|
|
|
Version: version,
|
2021-09-26 12:49:18 +00:00
|
|
|
}); err != nil {
|
2021-09-26 14:47:25 +00:00
|
|
|
http.Error(w, errors.Wrap(err, "executing template").Error(), http.StatusInternalServerError)
|
2021-09-26 12:49:18 +00:00
|
|
|
return
|
2019-05-10 21:12:00 +00:00
|
|
|
}
|
|
|
|
}
|