2020-03-25 06:58:58 +00:00
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
2022-04-04 07:51:35 +00:00
|
|
|
"embed"
|
2022-02-14 16:22:30 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-04-04 07:51:35 +00:00
|
|
|
"io/fs"
|
2020-05-13 12:41:43 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-06-22 11:17:29 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2020-05-13 12:41:43 +00:00
|
|
|
|
2022-02-14 16:22:30 +00:00
|
|
|
"github.com/caos/logging"
|
|
|
|
|
2022-03-29 09:53:19 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
|
|
|
|
2020-06-22 11:17:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/http/middleware"
|
2020-03-25 06:58:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2021-07-27 12:34:56 +00:00
|
|
|
ConsoleOverwriteDir string
|
|
|
|
ShortCache middleware.CacheConfig
|
|
|
|
LongCache middleware.CacheConfig
|
2020-05-13 12:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type spaHandler struct {
|
|
|
|
fileSystem http.FileSystem
|
|
|
|
}
|
|
|
|
|
2022-04-04 07:51:35 +00:00
|
|
|
var (
|
|
|
|
//go:embed static/*
|
|
|
|
static embed.FS
|
|
|
|
)
|
|
|
|
|
2020-06-09 05:38:44 +00:00
|
|
|
const (
|
2022-04-04 07:51:35 +00:00
|
|
|
envRequestPath = "/assets/environment.json"
|
|
|
|
HandlerPrefix = "/ui/console"
|
2020-06-24 12:26:27 +00:00
|
|
|
)
|
2020-06-22 11:17:29 +00:00
|
|
|
|
2020-06-24 12:26:27 +00:00
|
|
|
var (
|
2020-11-20 07:47:28 +00:00
|
|
|
shortCacheFiles = []string{
|
|
|
|
"/",
|
2020-06-24 12:26:27 +00:00
|
|
|
"/index.html",
|
|
|
|
"/manifest.webmanifest",
|
|
|
|
"/ngsw.json",
|
|
|
|
"/ngsw-worker.js",
|
|
|
|
"/safety-worker.js",
|
|
|
|
"/worker-basic.min.js",
|
|
|
|
}
|
2020-06-09 05:38:44 +00:00
|
|
|
)
|
|
|
|
|
2020-05-13 12:41:43 +00:00
|
|
|
func (i *spaHandler) Open(name string) (http.File, error) {
|
|
|
|
ret, err := i.fileSystem.Open(name)
|
|
|
|
if !os.IsNotExist(err) || path.Ext(name) != "" {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.fileSystem.Open("/index.html")
|
2020-03-25 06:58:58 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 09:53:19 +00:00
|
|
|
func Start(config Config, domain, url, issuer string, instanceHandler func(http.Handler) http.Handler) (http.Handler, error) {
|
2022-04-04 07:51:35 +00:00
|
|
|
fSys, err := fs.Sub(static, "static")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-09 05:38:44 +00:00
|
|
|
}
|
2022-02-14 16:22:30 +00:00
|
|
|
cache := assetsCacheInterceptorIgnoreManifest(
|
|
|
|
config.ShortCache.MaxAge,
|
|
|
|
config.ShortCache.SharedMaxAge,
|
|
|
|
config.LongCache.MaxAge,
|
|
|
|
config.LongCache.SharedMaxAge,
|
2020-06-24 12:26:27 +00:00
|
|
|
)
|
2022-02-14 16:22:30 +00:00
|
|
|
security := middleware.SecurityHeaders(csp(domain), nil)
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
handler := &http.ServeMux{}
|
2022-04-04 07:51:35 +00:00
|
|
|
handler.Handle("/", cache(security(http.FileServer(&spaHandler{http.FS(fSys)}))))
|
2022-03-29 09:53:19 +00:00
|
|
|
handler.Handle(envRequestPath, instanceHandler(cache(security(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
instance := authz.GetInstance(r.Context())
|
|
|
|
if instance.InstanceID() == "" {
|
|
|
|
http.Error(w, "empty instanceID", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
environmentJSON, err := createEnvironmentJSON(url, issuer, instance.ConsoleClientID())
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("unable to marshal env for console: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = w.Write(environmentJSON)
|
2022-02-14 16:22:30 +00:00
|
|
|
logging.OnError(err).Error("error serving environment.json")
|
2022-03-29 09:53:19 +00:00
|
|
|
})))))
|
2022-02-14 16:22:30 +00:00
|
|
|
return handler, nil
|
2020-03-25 06:58:58 +00:00
|
|
|
}
|
2020-06-22 11:17:29 +00:00
|
|
|
|
|
|
|
func csp(zitadelDomain string) *middleware.CSP {
|
|
|
|
if !strings.HasPrefix(zitadelDomain, "*.") {
|
|
|
|
zitadelDomain = "*." + zitadelDomain
|
|
|
|
}
|
|
|
|
csp := middleware.DefaultSCP
|
2022-02-03 06:21:00 +00:00
|
|
|
csp.StyleSrc = csp.StyleSrc.AddInline()
|
2020-06-22 11:17:29 +00:00
|
|
|
csp.ScriptSrc = csp.ScriptSrc.AddEval()
|
2022-02-03 06:21:00 +00:00
|
|
|
csp.ConnectSrc = csp.ConnectSrc.AddHost(zitadelDomain)
|
2021-06-14 11:53:40 +00:00
|
|
|
csp.ImgSrc = csp.ImgSrc.AddHost(zitadelDomain).AddScheme("blob")
|
2020-06-22 11:17:29 +00:00
|
|
|
return &csp
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:53:19 +00:00
|
|
|
func createEnvironmentJSON(api, issuer, clientID string) ([]byte, error) {
|
2022-02-14 16:22:30 +00:00
|
|
|
environment := struct {
|
2022-03-29 09:53:19 +00:00
|
|
|
API string `json:"api,omitempty"`
|
|
|
|
Issuer string `json:"issuer,omitempty"`
|
|
|
|
ClientID string `json:"clientid,omitempty"`
|
2022-02-14 16:22:30 +00:00
|
|
|
}{
|
2022-03-29 09:53:19 +00:00
|
|
|
API: api,
|
|
|
|
Issuer: issuer,
|
|
|
|
ClientID: clientID,
|
2022-02-14 16:22:30 +00:00
|
|
|
}
|
|
|
|
return json.Marshal(environment)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assetsCacheInterceptorIgnoreManifest(shortMaxAge, shortSharedMaxAge, longMaxAge, longSharedMaxAge time.Duration) func(http.Handler) http.Handler {
|
2020-06-22 11:17:29 +00:00
|
|
|
return func(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-11-20 07:47:28 +00:00
|
|
|
for _, file := range shortCacheFiles {
|
|
|
|
if r.URL.Path == file {
|
2020-06-24 12:26:27 +00:00
|
|
|
middleware.AssetsCacheInterceptor(shortMaxAge, shortSharedMaxAge, handler).ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-06-22 11:17:29 +00:00
|
|
|
}
|
2020-11-20 07:47:28 +00:00
|
|
|
middleware.AssetsCacheInterceptor(longMaxAge, longSharedMaxAge, handler).ServeHTTP(w, r)
|
|
|
|
return
|
2020-06-22 11:17:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|