2020-03-25 07:58:58 +01:00
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
2020-05-13 14:41:43 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-06-22 13:17:29 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
2020-05-13 14:41:43 +02:00
|
|
|
|
|
|
|
"github.com/rakyll/statik/fs"
|
2020-03-25 07:58:58 +01:00
|
|
|
|
2020-06-22 13:17:29 +02:00
|
|
|
"github.com/caos/zitadel/internal/api/http/middleware"
|
2020-07-08 13:56:37 +02:00
|
|
|
_ "github.com/caos/zitadel/internal/ui/console/statik"
|
2020-03-25 07:58:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2020-06-09 08:44:55 +02:00
|
|
|
Port string
|
|
|
|
EnvOverwriteDir string
|
2020-06-24 14:26:27 +02:00
|
|
|
ShortCache middleware.CacheConfig
|
|
|
|
LongCache middleware.CacheConfig
|
2020-06-22 13:17:29 +02:00
|
|
|
CSPDomain string
|
2020-05-13 14:41:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type spaHandler struct {
|
|
|
|
fileSystem http.FileSystem
|
|
|
|
}
|
|
|
|
|
2020-06-09 07:38:44 +02:00
|
|
|
const (
|
|
|
|
envRequestPath = "/assets/environment.json"
|
2020-06-09 08:44:55 +02:00
|
|
|
envDefaultDir = "/console/"
|
2020-08-31 08:49:35 +02:00
|
|
|
handlerPrefix = "/console"
|
2020-06-24 14:26:27 +02:00
|
|
|
)
|
2020-06-22 13:17:29 +02:00
|
|
|
|
2020-06-24 14:26:27 +02:00
|
|
|
var (
|
2020-11-20 08:47:28 +01:00
|
|
|
shortCacheFiles = []string{
|
|
|
|
"/",
|
2020-06-24 14:26:27 +02:00
|
|
|
"/index.html",
|
|
|
|
"/manifest.webmanifest",
|
|
|
|
"/ngsw.json",
|
|
|
|
"/ngsw-worker.js",
|
|
|
|
"/safety-worker.js",
|
|
|
|
"/worker-basic.min.js",
|
|
|
|
}
|
2020-06-09 07:38:44 +02:00
|
|
|
)
|
|
|
|
|
2020-05-13 14:41:43 +02: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 07:58:58 +01:00
|
|
|
}
|
|
|
|
|
2020-08-31 08:49:35 +02:00
|
|
|
func Start(config Config) (http.Handler, string, error) {
|
2020-06-05 07:50:04 +02:00
|
|
|
statikFS, err := fs.NewWithNamespace("console")
|
2020-05-13 14:41:43 +02:00
|
|
|
if err != nil {
|
2020-08-31 08:49:35 +02:00
|
|
|
return nil, "", err
|
2020-05-13 14:41:43 +02:00
|
|
|
}
|
2020-06-09 08:44:55 +02:00
|
|
|
envDir := envDefaultDir
|
|
|
|
if config.EnvOverwriteDir != "" {
|
|
|
|
envDir = config.EnvOverwriteDir
|
2020-06-09 07:38:44 +02:00
|
|
|
}
|
2020-06-24 14:26:27 +02:00
|
|
|
cache := AssetsCacheInterceptorIgnoreManifest(
|
|
|
|
config.ShortCache.MaxAge.Duration,
|
|
|
|
config.ShortCache.SharedMaxAge.Duration,
|
|
|
|
config.LongCache.MaxAge.Duration,
|
|
|
|
config.LongCache.SharedMaxAge.Duration,
|
|
|
|
)
|
2020-06-22 13:17:29 +02:00
|
|
|
security := middleware.SecurityHeaders(csp(config.CSPDomain), nil)
|
2020-07-08 13:56:37 +02:00
|
|
|
handler := &http.ServeMux{}
|
|
|
|
handler.Handle("/", cache(security(http.FileServer(&spaHandler{statikFS}))))
|
|
|
|
handler.Handle(envRequestPath, cache(security(http.StripPrefix("/assets", http.FileServer(http.Dir(envDir))))))
|
2020-08-31 08:49:35 +02:00
|
|
|
return handler, handlerPrefix, nil
|
2020-03-25 07:58:58 +01:00
|
|
|
}
|
2020-06-22 13:17:29 +02:00
|
|
|
|
|
|
|
func csp(zitadelDomain string) *middleware.CSP {
|
|
|
|
if !strings.HasPrefix(zitadelDomain, "*.") {
|
|
|
|
zitadelDomain = "*." + zitadelDomain
|
|
|
|
}
|
|
|
|
csp := middleware.DefaultSCP
|
|
|
|
csp.StyleSrc = csp.StyleSrc.AddInline().AddHost("fonts.googleapis.com").AddHost("maxst.icons8.com") //TODO: host it
|
|
|
|
csp.FontSrc = csp.FontSrc.AddHost("fonts.gstatic.com").AddHost("maxst.icons8.com") //TODO: host it
|
|
|
|
csp.ScriptSrc = csp.ScriptSrc.AddEval()
|
2020-06-23 08:43:41 +02:00
|
|
|
csp.ConnectSrc = csp.ConnectSrc.AddHost(zitadelDomain).
|
|
|
|
AddHost("fonts.googleapis.com").
|
|
|
|
AddHost("fonts.gstatic.com").
|
|
|
|
AddHost("maxst.icons8.com") //TODO: host it
|
feat: label policy (#1708)
* feat: label policy proto extension
* feat: label policy and activate event
* feat: label policy asset events
* feat: label policy asset commands
* feat: add storage key
* feat: storage key validation
* feat: label policy asset tests
* feat: label policy query side
* feat: avatar
* feat: avatar event
* feat: human avatar
* feat: avatar read side
* feat: font on iam label policy
* feat: label policy font
* feat: possiblity to create bucket on put file
* uplaoder
* login policy logo
* set bucket prefix
* feat: avatar upload
* feat: avatar upload
* feat: use assets on command side
* feat: fix human avatar removed event
* feat: remove human avatar
* feat: mock asset storage
* feat: remove human avatar
* fix(operator): add configuration of asset storage to zitadel operator
* feat(console): private labeling policy (#1697)
* private labeling component, routing, preview
* font, colors, upload, i18n
* show logo
* fix: uniqueness (#1710)
* fix: uniqueconstraint to lower
* feat: change org
* feat: org change test
* feat: change org
* fix: tests
* fix: handle domain claims correctly
* feat: update org
Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
* fix: handle domain claimed event correctly for service users (#1711)
* fix: handle domain claimed event correctly on user view
* fix: ignore domain claimed events for email notifications
* fix: change org
* handle org changed in read models correctly
* fix: change org in user grant handler
Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
* fix: correct value (#1695)
* docs(api): correct link (#1712)
* upload service
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
Co-authored-by: Florian Forster <florian@caos.ch>
* feat: fix tests,
* feat: remove assets from label policy
* fix npm, set environment
* lint ts
* remove stylelinting
* fix(operator): add mapping for console with changed unit tests
* fix(operator): add secrets as env variables to pod
* feat: remove human avatar
* fix(operator): add secrets as env variables to pod
* feat: map label policy
* feat: labelpolicy, admin, mgmt, adv settings (#1715)
* fetch label policy, mgmt, admin service
* feat: advanced beh, links, add, update
* lint ts
* feat: watermark
* feat: remove human avatar
* feat: remove human avatar
* feat: remove human avatar
* feat: remove human avatar
* feat: remove human avatar
* feat: remove human avatar
* feat: remove human avatar
* feat: custom css
* css
* css
* css
* css
* css
* getobject
* feat: dynamic handler
* feat: varibale css
* content info
* css overwrite
* feat: variablen css
* feat: generate css file
* feat: dark mode
* feat: dark mode
* fix logo css
* feat: upload logos
* dark mode with cookie
* feat: handle images in login
* avatar css and begin font
* feat: avatar
* feat: user avatar
* caching of static assets in login
* add avatar.js to main.html
* feat: header dont show logo if no url
* feat: label policy colors
* feat: mock asset storage
* feat: mock asset storage
* feat: fix tests
* feat: user avatar
* feat: header logo
* avatar
* avatar
* make it compatible with go 1.15
* feat: remove unused logos
* fix handler
* fix: styling error handling
* fonts
* fix: download func
* switch to mux
* fix: change upload api to assets
* fix build
* fix: download avatar
* fix: download logos
* fix: my avatar
* font
* fix: remove error msg popup possibility
* fix: docs
* fix: svalidate colors
* rem msg popup from frontend
* fix: email with private labeling
* fix: tests
* fix: email templates
* fix: change migration version
* fix: fix duplicate imports
* fix(console): assets, service url, upload, policy current and preview (#1781)
* upload endpoint, layout
* fetch current, preview, fix upload
* cleanup private labeling
* fix linting
* begin generated asset handler
* generate asset api in dockerfile
* features for label policy
* features for label policy
* features
* flag for asset generator
* change asset generator flag
* fix label policy view in grpc
* fix: layout, activate policy (#1786)
* theme switcher up on top
* change layout
* activate policy
* feat(console): label policy back color, layout (#1788)
* theme switcher up on top
* change layout
* activate policy
* fix overwrite value fc
* reset policy, reset service
* autosave policy, preview desc, layout impv
* layout, i18n
* background colors, inject material styles
* load images
* clean, lint
* fix layout
* set custom hex
* fix content size conversion
* remove font format in generated css
* fix features for assets
* fix(console): label policy colors, image downloads, preview (#1804)
* load images
* colors, images binding
* lint
* refresh emitter
* lint
* propagate font colors
* upload error handling
* label policy feature check
* add blob in csp for console
* log
* fix: feature edits for label policy, refresh state on upload (#1807)
* show error on load image, stop spinner
* fix merge
* fix migration versions
* fix assets
* fix csp
* fix background color
* scss
* fix build
* lint scss
* fix statik for console
* fix features check for label policy
* cleanup
* lint
* public links
* fix notifications
* public links
* feat: merge main
* feat: fix translation files
* fix migration
* set api domain
* fix logo in email
* font face in email
* font face in email
* validate assets on upload
* cleanup
* add missing translations
* add missing translations
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
Co-authored-by: Stefan Benz <stefan@caos.ch>
Co-authored-by: Max Peintner <max@caos.ch>
Co-authored-by: Florian Forster <florian@caos.ch>
2021-06-04 14:53:51 +02:00
|
|
|
csp.ImgSrc = csp.ImgSrc.AddScheme("blob")
|
2020-06-22 13:17:29 +02:00
|
|
|
return &csp
|
|
|
|
}
|
|
|
|
|
2020-06-24 14:26:27 +02:00
|
|
|
func AssetsCacheInterceptorIgnoreManifest(shortMaxAge, shortSharedMaxAge, longMaxAge, longSharedMaxAge time.Duration) func(http.Handler) http.Handler {
|
2020-06-22 13:17:29 +02:00
|
|
|
return func(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-11-20 08:47:28 +01:00
|
|
|
for _, file := range shortCacheFiles {
|
|
|
|
if r.URL.Path == file {
|
2020-06-24 14:26:27 +02:00
|
|
|
middleware.AssetsCacheInterceptor(shortMaxAge, shortSharedMaxAge, handler).ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-06-22 13:17:29 +02:00
|
|
|
}
|
2020-11-20 08:47:28 +01:00
|
|
|
middleware.AssetsCacheInterceptor(longMaxAge, longSharedMaxAge, handler).ServeHTTP(w, r)
|
|
|
|
return
|
2020-06-22 13:17:29 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|