2022-07-21 22:43:42 +02:00
|
|
|
package controller
|
|
|
|
|
2022-07-22 17:16:30 +02:00
|
|
|
import (
|
2023-01-12 17:04:56 +01:00
|
|
|
"context"
|
2023-03-13 19:19:38 +01:00
|
|
|
"github.com/openziti/zrok/controller/config"
|
2023-03-16 20:05:39 +01:00
|
|
|
"github.com/openziti/zrok/controller/limits"
|
2023-03-15 21:14:06 +01:00
|
|
|
"github.com/openziti/zrok/controller/metrics"
|
2023-03-13 19:19:38 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-01-12 17:04:56 +01:00
|
|
|
|
2022-07-22 17:16:30 +02:00
|
|
|
"github.com/go-openapi/loads"
|
2022-10-19 21:21:15 +02:00
|
|
|
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/controller/store"
|
|
|
|
"github.com/openziti/zrok/rest_server_zrok"
|
|
|
|
"github.com/openziti/zrok/rest_server_zrok/operations"
|
|
|
|
"github.com/openziti/zrok/rest_server_zrok/operations/account"
|
|
|
|
"github.com/openziti/zrok/rest_server_zrok/operations/metadata"
|
2022-07-22 17:16:30 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2023-03-13 19:19:38 +01:00
|
|
|
var cfg *config.Config
|
2022-07-29 21:28:40 +02:00
|
|
|
var str *store.Store
|
2022-10-19 21:21:15 +02:00
|
|
|
var idb influxdb2.Client
|
2023-03-21 21:18:17 +01:00
|
|
|
var limitsAgent *limits.Agent
|
2022-07-22 19:53:39 +02:00
|
|
|
|
2023-03-13 19:19:38 +01:00
|
|
|
func Run(inCfg *config.Config) error {
|
2022-10-19 19:20:47 +02:00
|
|
|
cfg = inCfg
|
|
|
|
|
2022-07-26 21:16:02 +02:00
|
|
|
swaggerSpec, err := loads.Embedded(rest_server_zrok.SwaggerJSON, rest_server_zrok.FlatSwaggerJSON)
|
2022-07-22 17:16:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error loading embedded swagger spec")
|
|
|
|
}
|
|
|
|
|
|
|
|
api := operations.NewZrokAPI(swaggerSpec)
|
2022-12-01 20:48:23 +01:00
|
|
|
api.KeyAuth = newZrokAuthenticator(cfg).authenticate
|
2023-01-09 18:00:37 +01:00
|
|
|
api.AccountInviteHandler = newInviteHandler(cfg)
|
2022-11-30 17:43:00 +01:00
|
|
|
api.AccountLoginHandler = account.LoginHandlerFunc(loginHandler)
|
|
|
|
api.AccountRegisterHandler = newRegisterHandler()
|
2023-01-18 20:05:10 +01:00
|
|
|
api.AccountResetPasswordHandler = newResetPasswordHandler()
|
2023-01-20 18:08:40 +01:00
|
|
|
api.AccountResetPasswordRequestHandler = newResetPasswordRequestHandler()
|
2022-11-30 17:43:00 +01:00
|
|
|
api.AccountVerifyHandler = newVerifyHandler()
|
2022-12-01 22:14:42 +01:00
|
|
|
api.AdminCreateFrontendHandler = newCreateFrontendHandler()
|
2022-12-06 20:06:12 +01:00
|
|
|
api.AdminCreateIdentityHandler = newCreateIdentityHandler()
|
2022-12-02 14:58:41 +01:00
|
|
|
api.AdminDeleteFrontendHandler = newDeleteFrontendHandler()
|
2023-01-10 19:26:22 +01:00
|
|
|
api.AdminInviteTokenGenerateHandler = newInviteTokenGenerateHandler()
|
2022-12-02 15:30:06 +01:00
|
|
|
api.AdminListFrontendsHandler = newListFrontendsHandler()
|
2022-12-02 16:46:53 +01:00
|
|
|
api.AdminUpdateFrontendHandler = newUpdateFrontendHandler()
|
2023-03-21 21:18:17 +01:00
|
|
|
api.EnvironmentEnableHandler = newEnableHandler()
|
2022-11-30 17:43:00 +01:00
|
|
|
api.EnvironmentDisableHandler = newDisableHandler()
|
2023-05-12 17:57:34 +02:00
|
|
|
api.MetadataGetAccountDetailHandler = newAccountDetailHandler()
|
2023-01-31 19:44:03 +01:00
|
|
|
api.MetadataConfigurationHandler = newConfigurationHandler(cfg)
|
2023-05-09 20:16:01 +02:00
|
|
|
if cfg.Metrics != nil && cfg.Metrics.Influx != nil {
|
|
|
|
api.MetadataGetAccountMetricsHandler = newGetAccountMetricsHandler(cfg.Metrics.Influx)
|
2023-05-09 22:48:11 +02:00
|
|
|
api.MetadataGetEnvironmentMetricsHandler = newGetEnvironmentMetricsHandler(cfg.Metrics.Influx)
|
|
|
|
api.MetadataGetShareMetricsHandler = newGetShareMetricsHandler(cfg.Metrics.Influx)
|
2023-05-09 20:16:01 +02:00
|
|
|
}
|
2022-12-22 19:51:29 +01:00
|
|
|
api.MetadataGetEnvironmentDetailHandler = newEnvironmentDetailHandler()
|
2023-01-04 19:43:37 +01:00
|
|
|
api.MetadataGetShareDetailHandler = newShareDetailHandler()
|
2022-08-03 20:58:11 +02:00
|
|
|
api.MetadataOverviewHandler = metadata.OverviewHandlerFunc(overviewHandler)
|
2022-08-02 19:23:31 +02:00
|
|
|
api.MetadataVersionHandler = metadata.VersionHandlerFunc(versionHandler)
|
2023-01-04 19:43:37 +01:00
|
|
|
api.ShareAccessHandler = newAccessHandler()
|
2023-03-21 21:34:45 +01:00
|
|
|
api.ShareShareHandler = newShareHandler()
|
2023-01-04 19:43:37 +01:00
|
|
|
api.ShareUnaccessHandler = newUnaccessHandler()
|
|
|
|
api.ShareUnshareHandler = newUnshareHandler()
|
|
|
|
api.ShareUpdateShareHandler = newUpdateShareHandler()
|
2022-07-22 17:45:37 +02:00
|
|
|
|
2022-10-19 19:20:47 +02:00
|
|
|
if err := controllerStartup(); err != nil {
|
2022-08-15 20:25:50 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-27 19:38:35 +02:00
|
|
|
if v, err := store.Open(cfg.Store); err == nil {
|
2022-07-29 21:28:40 +02:00
|
|
|
str = v
|
2022-07-27 19:38:35 +02:00
|
|
|
} else {
|
|
|
|
return errors.Wrap(err, "error opening store")
|
|
|
|
}
|
|
|
|
|
2023-03-13 19:19:38 +01:00
|
|
|
if cfg.Metrics != nil && cfg.Metrics.Influx != nil {
|
|
|
|
idb = influxdb2.NewClient(cfg.Metrics.Influx.Url, cfg.Metrics.Influx.Token)
|
|
|
|
} else {
|
|
|
|
logrus.Warn("skipping influx client; no configuration")
|
2022-10-19 21:21:15 +02:00
|
|
|
}
|
|
|
|
|
2023-03-15 21:05:01 +01:00
|
|
|
if cfg.Metrics != nil && cfg.Metrics.Agent != nil && cfg.Metrics.Influx != nil {
|
2023-03-15 21:14:06 +01:00
|
|
|
ma, err := metrics.NewAgent(cfg.Metrics.Agent, str, cfg.Metrics.Influx)
|
2023-03-15 21:05:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error creating metrics agent")
|
|
|
|
}
|
|
|
|
if err := ma.Start(); err != nil {
|
|
|
|
return errors.Wrap(err, "error starting metrics agent")
|
|
|
|
}
|
|
|
|
defer func() { ma.Stop() }()
|
2023-03-16 20:05:39 +01:00
|
|
|
|
|
|
|
if cfg.Limits != nil && cfg.Limits.Enforcing {
|
2023-03-27 21:29:25 +02:00
|
|
|
limitsAgent, err = limits.NewAgent(cfg.Limits, cfg.Metrics.Influx, cfg.Ziti, cfg.Email, str)
|
2023-03-16 20:05:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error creating limits agent")
|
|
|
|
}
|
2023-03-21 21:18:17 +01:00
|
|
|
ma.AddUsageSink(limitsAgent)
|
|
|
|
limitsAgent.Start()
|
|
|
|
defer func() { limitsAgent.Stop() }()
|
2023-03-16 20:05:39 +01:00
|
|
|
}
|
2023-03-15 21:05:01 +01:00
|
|
|
}
|
|
|
|
|
2023-01-12 17:04:56 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
2023-01-20 17:00:46 +01:00
|
|
|
if cfg.Maintenance != nil {
|
|
|
|
if cfg.Maintenance.Registration != nil {
|
|
|
|
go newRegistrationMaintenanceAgent(ctx, cfg.Maintenance.Registration).run()
|
|
|
|
}
|
2023-01-20 18:08:40 +01:00
|
|
|
if cfg.Maintenance.ResetPassword != nil {
|
|
|
|
go newMaintenanceResetPasswordAgent(ctx, cfg.Maintenance.ResetPassword).run()
|
2023-01-20 17:00:46 +01:00
|
|
|
}
|
2023-01-12 17:04:56 +01:00
|
|
|
}
|
|
|
|
|
2022-07-26 21:16:02 +02:00
|
|
|
server := rest_server_zrok.NewServer(api)
|
2022-07-22 17:16:30 +02:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2022-08-09 17:18:24 +02:00
|
|
|
server.Host = cfg.Endpoint.Host
|
|
|
|
server.Port = cfg.Endpoint.Port
|
2022-07-22 17:16:30 +02:00
|
|
|
server.ConfigureAPI()
|
|
|
|
if err := server.Serve(); err != nil {
|
|
|
|
return errors.Wrap(err, "api server error")
|
|
|
|
}
|
2022-10-13 20:23:52 +02:00
|
|
|
|
2022-07-21 22:43:42 +02:00
|
|
|
return nil
|
|
|
|
}
|