2023-03-28 20:43:45 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-04-05 23:21:43 +02:00
|
|
|
"net"
|
2023-03-28 20:43:45 +02:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/app/spec"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/jwk"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/bus"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/bus/memory"
|
|
|
|
edgeHTTP "forge.cadoles.com/arcad/edge/pkg/http"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/module"
|
|
|
|
appModule "forge.cadoles.com/arcad/edge/pkg/module/app"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/module/blob"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/module/cast"
|
2023-04-02 18:05:53 +02:00
|
|
|
fetchModule "forge.cadoles.com/arcad/edge/pkg/module/fetch"
|
2023-04-05 23:21:43 +02:00
|
|
|
netModule "forge.cadoles.com/arcad/edge/pkg/module/net"
|
2023-04-21 13:10:03 +02:00
|
|
|
shareModule "forge.cadoles.com/arcad/edge/pkg/module/share"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage"
|
2023-10-03 06:09:15 +02:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/driver"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/share"
|
2023-03-28 20:43:45 +02:00
|
|
|
"github.com/Masterminds/sprig/v3"
|
2023-04-20 12:23:17 +02:00
|
|
|
"github.com/go-chi/chi/v5"
|
2023-03-28 20:43:45 +02:00
|
|
|
"github.com/lestrrat-go/jwx/v2/jwa"
|
|
|
|
"github.com/pkg/errors"
|
2023-04-05 23:21:43 +02:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
2023-10-03 06:09:15 +02:00
|
|
|
|
|
|
|
// Register storage drivers
|
|
|
|
_ "forge.cadoles.com/arcad/edge/pkg/storage/driver/rpc"
|
|
|
|
_ "forge.cadoles.com/arcad/edge/pkg/storage/driver/sqlite"
|
2023-03-28 20:43:45 +02:00
|
|
|
)
|
|
|
|
|
2023-04-21 13:10:03 +02:00
|
|
|
type Dependencies struct {
|
2023-10-03 06:09:15 +02:00
|
|
|
Bus bus.Bus
|
|
|
|
DocumentStore storage.DocumentStore
|
|
|
|
BlobStore storage.BlobStore
|
|
|
|
ShareStore share.Store
|
|
|
|
KeySet jwk.Set
|
|
|
|
AppRepository appModule.Repository
|
|
|
|
AppID app.ID
|
2023-04-21 13:10:03 +02:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
func (c *Controller) getHandlerOptions(ctx context.Context, appKey string, specs *spec.Spec) ([]edgeHTTP.HandlerOptionFunc, error) {
|
2023-10-03 06:09:15 +02:00
|
|
|
appEntry, exists := specs.Apps[appKey]
|
|
|
|
if !exists {
|
|
|
|
return nil, errors.Errorf("could not find app entry '%s'", appKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := appEntry.Storage
|
|
|
|
if storage == nil {
|
|
|
|
return nil, errors.Errorf("could not find app entry '%s' storage configuration", appKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
documentStore, err := driver.NewDocumentStore(appEntry.Storage.DocumentStoreDSN)
|
2023-03-28 20:43:45 +02:00
|
|
|
if err != nil {
|
2023-10-03 06:09:15 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2023-03-28 20:43:45 +02:00
|
|
|
}
|
|
|
|
|
2023-10-03 06:09:15 +02:00
|
|
|
blobStore, err := driver.NewBlobStore(appEntry.Storage.BlobStoreDSN)
|
2023-03-28 20:43:45 +02:00
|
|
|
if err != nil {
|
2023-10-03 06:09:15 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2023-03-28 20:43:45 +02:00
|
|
|
}
|
|
|
|
|
2023-10-03 06:09:15 +02:00
|
|
|
shareStore, err := driver.NewShareStore(appEntry.Storage.ShareStoreDSN)
|
2023-04-21 13:10:03 +02:00
|
|
|
if err != nil {
|
2023-10-03 06:09:15 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2023-04-21 13:10:03 +02:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
keySet, err := getAuthKeySet(specs.Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not retrieve auth key set")
|
|
|
|
}
|
|
|
|
|
2023-04-20 12:23:17 +02:00
|
|
|
mounts := make([]func(r chi.Router), 0)
|
|
|
|
|
|
|
|
authMount, err := getAuthMount(specs.Config.Auth, keySet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if authMount != nil {
|
|
|
|
mounts = append(mounts, authMount)
|
|
|
|
}
|
|
|
|
|
|
|
|
mounts = append(mounts, appModule.Mount(c.appRepository))
|
|
|
|
|
2023-04-21 13:10:03 +02:00
|
|
|
deps := Dependencies{
|
2023-10-03 06:09:15 +02:00
|
|
|
Bus: memory.NewBus(),
|
|
|
|
DocumentStore: documentStore,
|
|
|
|
BlobStore: blobStore,
|
|
|
|
ShareStore: shareStore,
|
|
|
|
KeySet: keySet,
|
|
|
|
AppRepository: c.appRepository,
|
|
|
|
AppID: app.ID(appKey),
|
2023-04-21 13:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
modules := c.getAppModules(deps)
|
2023-03-28 20:43:45 +02:00
|
|
|
|
2023-09-20 18:02:59 +02:00
|
|
|
anonymousUserMiddleware, err := getAnonymousUserMiddleware(specs.Config.Auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get anonymous user middleware")
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
options := []edgeHTTP.HandlerOptionFunc{
|
2023-04-21 13:10:03 +02:00
|
|
|
edgeHTTP.WithBus(deps.Bus),
|
2023-03-28 20:43:45 +02:00
|
|
|
edgeHTTP.WithServerModules(modules...),
|
2023-04-20 12:23:17 +02:00
|
|
|
edgeHTTP.WithHTTPMounts(mounts...),
|
2023-09-20 18:02:59 +02:00
|
|
|
edgeHTTP.WithHTTPMiddlewares(
|
|
|
|
anonymousUserMiddleware,
|
|
|
|
),
|
2023-03-28 20:43:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return options, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAuthKeySet(config *spec.Config) (jwk.Set, error) {
|
|
|
|
keySet := jwk.NewSet()
|
|
|
|
if config == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := config.Auth
|
|
|
|
|
|
|
|
if auth == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case auth.Local != nil:
|
|
|
|
var (
|
|
|
|
key jwk.Key
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch typedKey := auth.Local.Key.(type) {
|
|
|
|
case string:
|
|
|
|
key, err = jwk.FromRaw([]byte(typedKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not parse local auth key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := key.Set(jwk.AlgorithmKey, jwa.HS256); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unexpected key type '%T'", auth.Local.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := keySet.AddKey(key); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return keySet, nil
|
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
func createResolveAppURL(specs *spec.Spec) (ResolveAppURLFunc, error) {
|
|
|
|
rawIfaceMappings := make(map[string]string, 0)
|
|
|
|
if specs.Config != nil && specs.Config.AppURLResolving != nil && specs.Config.AppURLResolving.IfaceMappings != nil {
|
|
|
|
rawIfaceMappings = specs.Config.AppURLResolving.IfaceMappings
|
|
|
|
}
|
2023-03-28 20:43:45 +02:00
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
ifaceMappings := make(map[string]*template.Template, len(rawIfaceMappings))
|
|
|
|
for iface, rawTemplate := range rawIfaceMappings {
|
|
|
|
tmpl, err := template.New("").Funcs(sprig.TxtFuncMap()).Parse(rawTemplate)
|
2023-03-28 20:43:45 +02:00
|
|
|
if err != nil {
|
2023-04-05 23:21:43 +02:00
|
|
|
return nil, errors.Wrapf(err, "could not parse iface '%s' template", iface)
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaceMappings[iface] = tmpl
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultRawTemplate := `http://{{ .DeviceIP }}:{{ .AppPort }}`
|
|
|
|
if specs.Config != nil && specs.Config.AppURLResolving != nil && specs.Config.AppURLResolving.DefaultURLTemplate != "" {
|
|
|
|
defaultRawTemplate = specs.Config.AppURLResolving.DefaultURLTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultTemplate, err := template.New("").Funcs(sprig.TxtFuncMap()).Parse(defaultRawTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(ctx context.Context, manifest *app.Manifest, from string) (string, error) {
|
|
|
|
var (
|
|
|
|
urlTemplate *template.Template
|
|
|
|
deviceIP net.IP
|
|
|
|
)
|
|
|
|
|
|
|
|
fromIP := net.ParseIP(from)
|
|
|
|
|
|
|
|
if fromIP != nil {
|
|
|
|
LOOP:
|
|
|
|
for ifaceName, ifaceTmpl := range ifaceMappings {
|
|
|
|
iface, err := net.InterfaceByName(ifaceName)
|
|
|
|
if err != nil {
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
|
|
|
logger.Warn(
|
2023-04-05 23:21:43 +02:00
|
|
|
ctx, "could not find interface",
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.CapturedE(err), logger.F("iface", ifaceName),
|
2023-04-05 23:21:43 +02:00
|
|
|
)
|
2023-04-06 15:55:51 +02:00
|
|
|
|
|
|
|
continue
|
2023-04-05 23:21:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addresses, err := iface.Addrs()
|
|
|
|
if err != nil {
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-04-05 23:21:43 +02:00
|
|
|
logger.Error(
|
|
|
|
ctx, "could not list interface addresses",
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.CapturedE(err),
|
2023-04-05 23:21:43 +02:00
|
|
|
logger.F("iface", iface.Name),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addresses {
|
|
|
|
ifaIP, network, err := net.ParseCIDR(addr.String())
|
|
|
|
if err != nil {
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-04-05 23:21:43 +02:00
|
|
|
logger.Error(
|
|
|
|
ctx, "could not parse interface ip",
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.CapturedE(err),
|
2023-04-05 23:21:43 +02:00
|
|
|
logger.F("iface", iface.Name),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !network.Contains(fromIP) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
deviceIP = ifaIP
|
|
|
|
urlTemplate = ifaceTmpl
|
|
|
|
|
|
|
|
break LOOP
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 20:43:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
if urlTemplate == nil {
|
|
|
|
urlTemplate = defaultTemplate
|
|
|
|
}
|
2023-03-28 20:43:45 +02:00
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
if deviceIP == nil {
|
|
|
|
deviceIP = net.ParseIP("127.0.0.1")
|
2023-03-28 20:43:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
var appEntry *spec.AppEntry
|
|
|
|
for appID, entry := range specs.Apps {
|
|
|
|
if manifest.ID != app.ID(appID) {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-28 20:43:45 +02:00
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
appEntry = &entry
|
2023-04-06 18:25:34 +02:00
|
|
|
break
|
2023-04-05 23:21:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if appEntry == nil {
|
|
|
|
return "", errors.Errorf("could not find app '%s' in specs", manifest.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, port, err := net.SplitHostPort(appEntry.Address)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
2023-03-28 20:43:45 +02:00
|
|
|
|
|
|
|
data := struct {
|
|
|
|
Manifest *app.Manifest
|
|
|
|
Specs *spec.Spec
|
2023-04-05 23:21:43 +02:00
|
|
|
DeviceIP string
|
|
|
|
AppPort string
|
2023-03-28 20:43:45 +02:00
|
|
|
}{
|
|
|
|
Manifest: manifest,
|
|
|
|
Specs: specs,
|
2023-04-05 23:21:43 +02:00
|
|
|
DeviceIP: deviceIP.String(),
|
|
|
|
AppPort: port,
|
2023-03-28 20:43:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
if err := urlTemplate.Execute(&buf, data); err != nil {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), nil
|
2023-04-05 23:21:43 +02:00
|
|
|
}, nil
|
2023-03-28 20:43:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-21 13:10:03 +02:00
|
|
|
func (c *Controller) getAppModules(deps Dependencies) []app.ServerModuleFactory {
|
2023-03-28 20:43:45 +02:00
|
|
|
return []app.ServerModuleFactory{
|
|
|
|
module.ContextModuleFactory(),
|
|
|
|
module.ConsoleModuleFactory(),
|
|
|
|
cast.CastModuleFactory(),
|
|
|
|
module.LifecycleModuleFactory(),
|
2023-04-21 13:10:03 +02:00
|
|
|
netModule.ModuleFactory(deps.Bus),
|
|
|
|
module.RPCModuleFactory(deps.Bus),
|
|
|
|
module.StoreModuleFactory(deps.DocumentStore),
|
|
|
|
blob.ModuleFactory(deps.Bus, deps.BlobStore),
|
|
|
|
authModuleFactory(deps.KeySet),
|
|
|
|
appModule.ModuleFactory(deps.AppRepository),
|
|
|
|
fetchModule.ModuleFactory(deps.Bus),
|
2023-10-03 06:09:15 +02:00
|
|
|
shareModule.ModuleFactory(deps.AppID, deps.ShareStore),
|
2023-03-28 20:43:45 +02:00
|
|
|
}
|
|
|
|
}
|