2022-08-03 20:58:11 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2023-05-16 19:45:43 +02:00
|
|
|
"github.com/jmoiron/sqlx"
|
2023-05-15 20:14:52 +02:00
|
|
|
"github.com/openziti/zrok/controller/store"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/rest_model_zrok"
|
|
|
|
"github.com/openziti/zrok/rest_server_zrok/operations/metadata"
|
2022-08-03 21:05:28 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-08-03 20:58:11 +02:00
|
|
|
)
|
|
|
|
|
2023-05-16 19:45:43 +02:00
|
|
|
type overviewHandler struct{}
|
|
|
|
|
|
|
|
func newOverviewHandler() *overviewHandler {
|
|
|
|
return &overviewHandler{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *overviewHandler) Handle(_ metadata.OverviewParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
|
|
|
trx, err := str.Begin()
|
2022-08-03 21:05:28 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error starting transaction: %v", err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return metadata.NewOverviewInternalServerError()
|
2022-08-03 21:05:28 +02:00
|
|
|
}
|
2023-05-16 19:45:43 +02:00
|
|
|
defer func() { _ = trx.Rollback() }()
|
|
|
|
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), trx)
|
2022-08-03 21:05:28 +02:00
|
|
|
if err != nil {
|
2022-09-09 16:20:05 +02:00
|
|
|
logrus.Errorf("error finding environments for '%v': %v", principal.Email, err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return metadata.NewOverviewInternalServerError()
|
2022-08-03 21:05:28 +02:00
|
|
|
}
|
2023-05-16 19:45:43 +02:00
|
|
|
elm, err := newEnvironmentsLimitedMap(envs, trx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error finding limited environments for '%v': %v", principal.Email, err)
|
|
|
|
return metadata.NewOverviewInternalServerError()
|
|
|
|
}
|
2023-05-17 17:23:16 +02:00
|
|
|
accountLimited, err := h.isAccountLimited(principal, trx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error checking account limited for '%v': %v", principal.Email, err)
|
|
|
|
}
|
|
|
|
ovr := &rest_model_zrok.Overview{AccountLimited: accountLimited}
|
2022-08-03 21:05:28 +02:00
|
|
|
for _, env := range envs {
|
2023-05-17 17:23:16 +02:00
|
|
|
envRes := &rest_model_zrok.EnvironmentAndResources{
|
2022-10-19 21:21:15 +02:00
|
|
|
Environment: &rest_model_zrok.Environment{
|
|
|
|
Address: env.Address,
|
|
|
|
Description: env.Description,
|
|
|
|
Host: env.Host,
|
|
|
|
ZID: env.ZId,
|
2023-05-16 19:45:43 +02:00
|
|
|
Limited: elm.isLimited(env),
|
|
|
|
CreatedAt: env.CreatedAt.UnixMilli(),
|
|
|
|
UpdatedAt: env.UpdatedAt.UnixMilli(),
|
2022-10-19 21:21:15 +02:00
|
|
|
},
|
|
|
|
}
|
2023-05-17 19:21:01 +02:00
|
|
|
shrs, err := str.FindSharesForEnvironment(env.Id, trx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error finding shares for environment '%v': %v", env.ZId, err)
|
|
|
|
return metadata.NewOverviewInternalServerError()
|
|
|
|
}
|
|
|
|
slm, err := newSharesLimitedMap(shrs, trx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error finding limited shares for environment '%v': %v", env.ZId, err)
|
|
|
|
return metadata.NewOverviewInternalServerError()
|
|
|
|
}
|
2023-01-04 19:13:50 +01:00
|
|
|
for _, shr := range shrs {
|
2022-12-01 18:54:14 +01:00
|
|
|
feEndpoint := ""
|
2023-01-04 19:13:50 +01:00
|
|
|
if shr.FrontendEndpoint != nil {
|
|
|
|
feEndpoint = *shr.FrontendEndpoint
|
2022-11-22 17:14:58 +01:00
|
|
|
}
|
2022-12-01 18:54:14 +01:00
|
|
|
feSelection := ""
|
2023-01-04 19:13:50 +01:00
|
|
|
if shr.FrontendSelection != nil {
|
|
|
|
feSelection = *shr.FrontendSelection
|
2022-12-01 18:54:14 +01:00
|
|
|
}
|
|
|
|
beProxyEndpoint := ""
|
2023-01-04 19:13:50 +01:00
|
|
|
if shr.BackendProxyEndpoint != nil {
|
|
|
|
beProxyEndpoint = *shr.BackendProxyEndpoint
|
2022-11-22 17:14:58 +01:00
|
|
|
}
|
2023-05-16 17:51:03 +02:00
|
|
|
envShr := &rest_model_zrok.Share{
|
2023-01-04 19:13:50 +01:00
|
|
|
Token: shr.Token,
|
|
|
|
ZID: shr.ZId,
|
|
|
|
ShareMode: shr.ShareMode,
|
|
|
|
BackendMode: shr.BackendMode,
|
2022-12-01 18:54:14 +01:00
|
|
|
FrontendSelection: feSelection,
|
|
|
|
FrontendEndpoint: feEndpoint,
|
|
|
|
BackendProxyEndpoint: beProxyEndpoint,
|
2023-01-04 19:13:50 +01:00
|
|
|
Reserved: shr.Reserved,
|
2023-05-16 19:45:43 +02:00
|
|
|
Limited: slm.isLimited(shr),
|
2023-01-04 19:13:50 +01:00
|
|
|
CreatedAt: shr.CreatedAt.UnixMilli(),
|
|
|
|
UpdatedAt: shr.UpdatedAt.UnixMilli(),
|
2023-05-15 20:14:52 +02:00
|
|
|
}
|
2023-05-17 17:23:16 +02:00
|
|
|
envRes.Shares = append(envRes.Shares, envShr)
|
2022-10-19 21:21:15 +02:00
|
|
|
}
|
2023-05-17 19:21:01 +02:00
|
|
|
fes, err := str.FindFrontendsForEnvironment(env.Id, trx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error finding frontends for environment '%v': %v", env.ZId, err)
|
|
|
|
return metadata.NewOverviewInternalServerError()
|
|
|
|
}
|
|
|
|
for _, fe := range fes {
|
|
|
|
envFe := &rest_model_zrok.Frontend{
|
|
|
|
ID: int64(fe.Id),
|
2024-01-05 19:26:18 +01:00
|
|
|
Token: fe.Token,
|
2023-05-17 19:21:01 +02:00
|
|
|
ZID: fe.ZId,
|
|
|
|
CreatedAt: fe.CreatedAt.UnixMilli(),
|
|
|
|
UpdatedAt: fe.UpdatedAt.UnixMilli(),
|
|
|
|
}
|
|
|
|
if fe.PrivateShareId != nil {
|
|
|
|
feShr, err := str.GetShare(*fe.PrivateShareId, trx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error getting share for frontend '%v': %v", fe.ZId, err)
|
|
|
|
return metadata.NewOverviewInternalServerError()
|
|
|
|
}
|
|
|
|
envFe.ShrToken = feShr.Token
|
|
|
|
}
|
|
|
|
envRes.Frontends = append(envRes.Frontends, envFe)
|
|
|
|
}
|
2023-05-17 17:23:16 +02:00
|
|
|
ovr.Environments = append(ovr.Environments, envRes)
|
2023-05-16 19:45:43 +02:00
|
|
|
}
|
2023-05-17 17:23:16 +02:00
|
|
|
return metadata.NewOverviewOK().WithPayload(ovr)
|
2023-05-16 19:45:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *overviewHandler) isAccountLimited(principal *rest_model_zrok.Principal, trx *sqlx.Tx) (bool, error) {
|
2023-05-16 18:05:30 +02:00
|
|
|
var alj *store.AccountLimitJournal
|
2023-05-16 19:45:43 +02:00
|
|
|
aljEmpty, err := str.IsAccountLimitJournalEmpty(int(principal.ID), trx)
|
2023-05-16 18:05:30 +02:00
|
|
|
if err != nil {
|
2023-05-16 19:45:43 +02:00
|
|
|
return false, err
|
2023-05-16 18:05:30 +02:00
|
|
|
}
|
|
|
|
if !aljEmpty {
|
2023-05-16 19:45:43 +02:00
|
|
|
alj, err = str.FindLatestAccountLimitJournal(int(principal.ID), trx)
|
2023-05-16 18:05:30 +02:00
|
|
|
if err != nil {
|
2023-05-16 19:45:43 +02:00
|
|
|
return false, err
|
2023-05-16 18:05:30 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-16 19:45:43 +02:00
|
|
|
return alj != nil && alj.Action == store.LimitAction, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type sharesLimitedMap struct {
|
|
|
|
v map[int]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSharesLimitedMap(shrs []*store.Share, trx *sqlx.Tx) (*sharesLimitedMap, error) {
|
|
|
|
var shrIds []int
|
|
|
|
for i := range shrs {
|
|
|
|
shrIds = append(shrIds, shrs[i].Id)
|
|
|
|
}
|
|
|
|
shrsLimited, err := str.FindSelectedLatestShareLimitjournal(shrIds, trx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
slm := &sharesLimitedMap{v: make(map[int]struct{})}
|
|
|
|
for i := range shrsLimited {
|
|
|
|
if shrsLimited[i].Action == store.LimitAction {
|
|
|
|
slm.v[shrsLimited[i].ShareId] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return slm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *sharesLimitedMap) isLimited(shr *store.Share) bool {
|
|
|
|
_, limited := m.v[shr.Id]
|
|
|
|
return limited
|
|
|
|
}
|
|
|
|
|
|
|
|
type environmentsLimitedMap struct {
|
|
|
|
v map[int]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEnvironmentsLimitedMap(envs []*store.Environment, trx *sqlx.Tx) (*environmentsLimitedMap, error) {
|
|
|
|
var envIds []int
|
|
|
|
for i := range envs {
|
|
|
|
envIds = append(envIds, envs[i].Id)
|
|
|
|
}
|
|
|
|
envsLimited, err := str.FindSelectedLatestEnvironmentLimitJournal(envIds, trx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
elm := &environmentsLimitedMap{v: make(map[int]struct{})}
|
|
|
|
for i := range envsLimited {
|
|
|
|
if envsLimited[i].Action == store.LimitAction {
|
|
|
|
elm.v[envsLimited[i].EnvironmentId] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return elm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *environmentsLimitedMap) isLimited(env *store.Environment) bool {
|
|
|
|
_, limited := m.v[env.Id]
|
|
|
|
return limited
|
2022-10-19 21:21:15 +02:00
|
|
|
}
|