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"
|
2023-10-21 15:12:08 +00:00
|
|
|
"github.com/Luzifer/ots/pkg/customization"
|
2023-10-23 12:05:20 +00:00
|
|
|
"github.com/Luzifer/ots/pkg/metrics"
|
2020-01-24 15:15:17 +00:00
|
|
|
"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"`
|
2024-09-22 09:26:04 +00:00
|
|
|
EnableTLS bool `flag:"enable-tls" default:"false" description:"Enable HTTPS/TLS"`
|
|
|
|
CertFile string `flag:"cert-file" default:"" description:"Path to the TLS certificate file"`
|
|
|
|
KeyFile string `flag:"key-file" default:"" description:"Path to the TLS private key file"`
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-10 16:21:38 +00:00
|
|
|
assets file_helpers.FSStack
|
2023-10-21 15:12:08 +00:00
|
|
|
cust customization.Customize
|
2023-06-10 16:21:38 +00:00
|
|
|
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-10-21 15:12:08 +00:00
|
|
|
if cust, err = customization.Load(cfg.Customize); err != nil {
|
2023-06-10 16:21:38 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-10-23 12:05:20 +00:00
|
|
|
// Initialize metrics collector
|
|
|
|
collector := metrics.New()
|
|
|
|
|
2023-06-10 16:21:38 +00:00
|
|
|
// 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
|
|
|
}
|
2023-10-23 12:05:20 +00:00
|
|
|
api := newAPI(store, collector)
|
2017-08-03 12:13:53 +00:00
|
|
|
|
2023-10-23 12:05:20 +00:00
|
|
|
// Initialize server
|
2017-08-03 12:13:53 +00:00
|
|
|
r := mux.NewRouter()
|
2021-09-26 12:49:18 +00:00
|
|
|
|
2017-08-03 12:13:53 +00:00
|
|
|
api.Register(r.PathPrefix("/api").Subrouter())
|
2021-09-26 12:49:18 +00:00
|
|
|
|
2023-10-23 12:05:20 +00:00
|
|
|
r.Handle("/metrics", metrics.Handler()).
|
|
|
|
Methods(http.MethodGet).
|
|
|
|
MatcherFunc(func(r *http.Request, _ *mux.RouteMatch) bool {
|
|
|
|
return requestInSubnetList(r, cust.MetricsAllowedSubnets)
|
|
|
|
})
|
2017-08-03 16:32:30 +00:00
|
|
|
|
2023-10-23 12:05:20 +00:00
|
|
|
r.HandleFunc("/", handleIndex).
|
|
|
|
Methods(http.MethodGet)
|
|
|
|
r.PathPrefix("/").HandlerFunc(assetDelivery).
|
|
|
|
Methods(http.MethodGet)
|
|
|
|
|
|
|
|
var hdl http.Handler = r
|
|
|
|
hdl = http_helpers.GzipHandler(hdl)
|
|
|
|
hdl = http_helpers.NewHTTPLogHandlerWithLogger(hdl, logrus.StandardLogger())
|
2023-06-10 16:21:38 +00:00
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: cfg.Listen,
|
2023-10-23 12:05:20 +00:00
|
|
|
Handler: hdl,
|
2023-06-10 16:21:38 +00:00
|
|
|
ReadHeaderTimeout: time.Second,
|
|
|
|
}
|
|
|
|
|
2023-10-23 12:05:20 +00:00
|
|
|
// Start periodic stored metrics update (required for multi-instance
|
|
|
|
// OTS hosting as other instances will create / delete secrets and
|
|
|
|
// we need to keep up with that)
|
|
|
|
go func() {
|
|
|
|
for t := time.NewTicker(time.Minute); ; <-t.C {
|
|
|
|
updateStoredSecretsCount(store, collector)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start server
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"secret_expiry": time.Duration(cfg.SecretExpiry) * time.Second,
|
|
|
|
"version": version,
|
|
|
|
}).Info("ots started")
|
|
|
|
|
2024-09-22 09:26:04 +00:00
|
|
|
if cfg.EnableTLS {
|
|
|
|
if cfg.CertFile == "" || cfg.KeyFile == "" {
|
|
|
|
logrus.Fatal("TLS is enabled but cert-file or key-file is not provided")
|
|
|
|
}
|
|
|
|
logrus.Infof("Starting HTTPS server on %s", cfg.Listen)
|
|
|
|
if err := server.ListenAndServeTLS(cfg.CertFile, cfg.KeyFile); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("HTTPS server quit unexpectedly")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Infof("Starting HTTP server on %s", cfg.Listen)
|
|
|
|
if err := server.ListenAndServe(); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("HTTP server quit unexpectedly")
|
|
|
|
}
|
2023-06-10 16:21:38 +00:00
|
|
|
}
|
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")
|
2023-10-04 20:53:29 +00:00
|
|
|
if _, err = w.Write(assetData); err != nil {
|
|
|
|
logrus.WithError(err).Error("writing asset data")
|
|
|
|
}
|
2019-05-10 21:12:00 +00:00
|
|
|
}
|
2017-08-04 19:16:00 +00:00
|
|
|
|
2023-10-04 20:53:29 +00:00
|
|
|
func handleIndex(w http.ResponseWriter, _ *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-10-21 15:12:08 +00:00
|
|
|
Customize customization.Customize
|
2023-06-17 13:05:03 +00:00
|
|
|
InlineContentNonce string
|
2023-06-26 21:01:06 +00:00
|
|
|
MaxSecretExpiry int64
|
2023-06-17 13:05:03 +00:00
|
|
|
Version string
|
2021-09-26 12:49:18 +00:00
|
|
|
}{
|
2023-06-17 13:05:03 +00:00
|
|
|
Customize: cust,
|
|
|
|
InlineContentNonce: inlineContentNonceStr,
|
2023-06-26 21:01:06 +00:00
|
|
|
MaxSecretExpiry: cfg.SecretExpiry,
|
2023-06-17 13:05:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|