2017-08-03 08:13:53 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-17 09:05:03 -04:00
|
|
|
"crypto/rand"
|
2021-09-06 06:37:52 -04:00
|
|
|
"embed"
|
2023-06-17 09:05:03 -04:00
|
|
|
"encoding/base64"
|
2023-06-10 12:21:38 -04:00
|
|
|
"io/fs"
|
2017-08-03 12:32:30 -04:00
|
|
|
"mime"
|
2017-08-03 08:13:53 -04:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-08-03 12:32:30 -04:00
|
|
|
"strings"
|
2021-09-26 08:49:18 -04:00
|
|
|
"text/template"
|
2023-06-10 12:21:38 -04:00
|
|
|
"time"
|
2017-08-03 08:13:53 -04:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-09-26 08:49:18 -04:00
|
|
|
"github.com/pkg/errors"
|
2023-06-10 12:21:38 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-05-10 10:19:20 -04:00
|
|
|
|
2023-06-10 12:21:38 -04:00
|
|
|
file_helpers "github.com/Luzifer/go_helpers/v2/file"
|
2020-01-24 10:15:17 -05:00
|
|
|
http_helpers "github.com/Luzifer/go_helpers/v2/http"
|
|
|
|
"github.com/Luzifer/rconfig/v2"
|
2017-08-03 08:13:53 -04:00
|
|
|
)
|
|
|
|
|
2023-06-17 09:05:03 -04:00
|
|
|
const scriptNonceSize = 32
|
|
|
|
|
2017-08-03 08:13:53 -04:00
|
|
|
var (
|
|
|
|
cfg struct {
|
2023-06-10 12:21:38 -04:00
|
|
|
Customize string `flag:"customize" default:"" description:"Customize-File to load"`
|
2017-08-03 08:13:53 -04: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 19:36:01 -04:00
|
|
|
SecretExpiry int64 `flag:"secret-expiry" default:"0" description:"Maximum expiry of the stored secrets in seconds"`
|
2017-08-03 08:13:53 -04: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 12:21:38 -04:00
|
|
|
assets file_helpers.FSStack
|
|
|
|
cust customize
|
|
|
|
indexTpl *template.Template
|
|
|
|
|
2017-08-03 08:13:53 -04:00
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
2021-09-06 06:37:52 -04:00
|
|
|
//go:embed frontend/*
|
2023-06-10 12:21:38 -04:00
|
|
|
var embeddedAssets embed.FS
|
2021-09-06 06:37:52 -04:00
|
|
|
|
2023-06-17 09:05:03 -04: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 12:21:38 -04:00
|
|
|
func initApp() error {
|
|
|
|
rconfig.AutoEnv(true)
|
2017-08-03 08:13:53 -04:00
|
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
2023-06-10 12:21:38 -04:00
|
|
|
return errors.Wrap(err, "parsing cli options")
|
2017-08-03 08:13:53 -04:00
|
|
|
}
|
|
|
|
|
2023-06-10 12:21:38 -04:00
|
|
|
l, err := logrus.ParseLevel(cfg.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "parsing log-level")
|
2017-08-03 08:13:53 -04:00
|
|
|
}
|
2023-06-10 12:21:38 -04:00
|
|
|
logrus.SetLevel(l)
|
2017-08-03 08:13:53 -04:00
|
|
|
|
2023-06-10 12:21:38 -04: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 08:13:53 -04:00
|
|
|
}
|
2023-06-10 12:21:38 -04:00
|
|
|
assets = append(assets, frontendFS)
|
|
|
|
|
|
|
|
if cust.OverlayFSPath != "" {
|
|
|
|
assets = append(file_helpers.FSStack{os.DirFS(cust.OverlayFSPath)}, assets...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-08-03 08:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-06-10 12:21:38 -04: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 08:13:53 -04:00
|
|
|
store, err := getStorageByType(cfg.StorageType)
|
|
|
|
if err != nil {
|
2023-06-10 12:21:38 -04:00
|
|
|
logrus.WithError(err).Fatal("initializing storage")
|
2017-08-03 08:13:53 -04:00
|
|
|
}
|
|
|
|
api := newAPI(store)
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2021-09-26 08:49:18 -04:00
|
|
|
r.Use(http_helpers.GzipHandler)
|
|
|
|
|
2017-08-03 08:13:53 -04:00
|
|
|
api.Register(r.PathPrefix("/api").Subrouter())
|
2021-09-26 08:49:18 -04:00
|
|
|
|
|
|
|
r.HandleFunc("/", handleIndex)
|
|
|
|
r.PathPrefix("/").HandlerFunc(assetDelivery)
|
2017-08-03 12:32:30 -04:00
|
|
|
|
2023-06-10 14:40:59 -04:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"secret_expiry": time.Duration(cfg.SecretExpiry) * time.Second,
|
|
|
|
"version": version,
|
|
|
|
}).Info("ots started")
|
2023-06-10 12:21:38 -04: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 12:32:30 -04:00
|
|
|
}
|
|
|
|
|
2021-09-26 08:49:18 -04:00
|
|
|
func assetDelivery(w http.ResponseWriter, r *http.Request) {
|
2023-06-10 12:21:38 -04:00
|
|
|
assetName := strings.TrimLeft(r.URL.Path, "/")
|
2017-08-03 12:32:30 -04:00
|
|
|
|
2021-09-06 06:33:38 -04:00
|
|
|
dot := strings.LastIndex(assetName, ".")
|
|
|
|
if dot < 0 {
|
2019-05-10 17:12:00 -04:00
|
|
|
// There are no assets with no dot in it
|
2021-09-26 08:49:18 -04:00
|
|
|
http.Error(w, "404 not found", http.StatusNotFound)
|
2019-05-10 17:12:00 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-06 06:33:38 -04:00
|
|
|
ext := assetName[dot:]
|
2023-06-10 12:21:38 -04:00
|
|
|
assetData, err := assets.ReadFile(assetName)
|
2017-08-03 12:32:30 -04:00
|
|
|
if err != nil {
|
2021-09-26 08:49:18 -04:00
|
|
|
http.Error(w, "404 not found", http.StatusNotFound)
|
2017-08-03 12:32:30 -04:00
|
|
|
return
|
|
|
|
}
|
2017-08-03 08:13:53 -04:00
|
|
|
|
2021-09-26 08:49:18 -04:00
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
2021-09-26 10:47:25 -04:00
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
2023-10-04 16:53:29 -04:00
|
|
|
if _, err = w.Write(assetData); err != nil {
|
|
|
|
logrus.WithError(err).Error("writing asset data")
|
|
|
|
}
|
2019-05-10 17:12:00 -04:00
|
|
|
}
|
2017-08-04 15:16:00 -04:00
|
|
|
|
2023-10-04 16:53:29 -04:00
|
|
|
func handleIndex(w http.ResponseWriter, _ *http.Request) {
|
2023-06-17 09:05:03 -04: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 10:47:25 -04: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 09:05:03 -04:00
|
|
|
w.Header().Set("Content-Security-Policy", policy.ToHeaderValue())
|
2021-09-26 10:47:25 -04:00
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
|
|
|
|
if err := indexTpl.Execute(w, struct {
|
2023-06-17 09:05:03 -04:00
|
|
|
Customize customize
|
|
|
|
InlineContentNonce string
|
2023-06-26 17:01:06 -04:00
|
|
|
MaxSecretExpiry int64
|
2023-06-17 09:05:03 -04:00
|
|
|
Version string
|
2021-09-26 08:49:18 -04:00
|
|
|
}{
|
2023-06-17 09:05:03 -04:00
|
|
|
Customize: cust,
|
|
|
|
InlineContentNonce: inlineContentNonceStr,
|
2023-06-26 17:01:06 -04:00
|
|
|
MaxSecretExpiry: cfg.SecretExpiry,
|
2023-06-17 09:05:03 -04:00
|
|
|
Version: version,
|
2021-09-26 08:49:18 -04:00
|
|
|
}); err != nil {
|
2021-09-26 10:47:25 -04:00
|
|
|
http.Error(w, errors.Wrap(err, "executing template").Error(), http.StatusInternalServerError)
|
2021-09-26 08:49:18 -04:00
|
|
|
return
|
2019-05-10 17:12:00 -04:00
|
|
|
}
|
|
|
|
}
|