2022-12-01 22:14:42 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2023-02-01 20:13:15 +01:00
|
|
|
"errors"
|
2022-12-01 22:14:42 +01:00
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2023-02-01 20:13:15 +01:00
|
|
|
"github.com/lib/pq"
|
|
|
|
"github.com/mattn/go-sqlite3"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/controller/store"
|
|
|
|
"github.com/openziti/zrok/controller/zrokEdgeSdk"
|
|
|
|
"github.com/openziti/zrok/rest_model_zrok"
|
|
|
|
"github.com/openziti/zrok/rest_server_zrok/operations/admin"
|
2022-12-01 22:14:42 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type createFrontendHandler struct{}
|
|
|
|
|
|
|
|
func newCreateFrontendHandler() *createFrontendHandler {
|
|
|
|
return &createFrontendHandler{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *createFrontendHandler) Handle(params admin.CreateFrontendParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
|
|
|
if !principal.Admin {
|
|
|
|
logrus.Errorf("invalid admin principal")
|
|
|
|
return admin.NewCreateFrontendUnauthorized()
|
|
|
|
}
|
|
|
|
|
2023-03-07 20:31:39 +01:00
|
|
|
client, err := zrokEdgeSdk.Client(cfg.Ziti)
|
2022-12-02 14:44:17 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error getting edge client: %v", err)
|
|
|
|
return admin.NewCreateFrontendInternalServerError()
|
|
|
|
}
|
|
|
|
|
|
|
|
zId := params.Body.ZID
|
2022-12-14 20:57:55 +01:00
|
|
|
detail, err := zrokEdgeSdk.GetIdentityByZId(zId, client)
|
2022-12-02 14:44:17 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error getting identity details for '%v': %v", zId, err)
|
|
|
|
return admin.NewCreateFrontendInternalServerError()
|
|
|
|
}
|
|
|
|
if len(detail.Payload.Data) != 1 {
|
|
|
|
logrus.Errorf("expected a single identity to be returned for '%v'", zId)
|
|
|
|
return admin.NewCreateFrontendNotFound()
|
|
|
|
}
|
|
|
|
logrus.Infof("found frontend identity '%v'", *detail.Payload.Data[0].Name)
|
|
|
|
|
2022-12-01 22:14:42 +01:00
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error starting transaction: %v", err)
|
|
|
|
return admin.NewCreateFrontendInternalServerError()
|
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
2024-01-30 18:59:56 +01:00
|
|
|
feToken, err := CreateToken()
|
2022-12-01 22:14:42 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error creating frontend token: %v", err)
|
|
|
|
return admin.NewCreateFrontendInternalServerError()
|
|
|
|
}
|
|
|
|
|
|
|
|
fe := &store.Frontend{
|
2024-06-18 19:38:00 +02:00
|
|
|
Token: feToken,
|
|
|
|
ZId: params.Body.ZID,
|
|
|
|
PublicName: ¶ms.Body.PublicName,
|
|
|
|
UrlTemplate: ¶ms.Body.URLTemplate,
|
|
|
|
Reserved: true,
|
|
|
|
PermissionMode: store.PermissionMode(params.Body.PermissionMode),
|
2022-12-01 22:14:42 +01:00
|
|
|
}
|
|
|
|
if _, err := str.CreateGlobalFrontend(fe, tx); err != nil {
|
2023-02-01 20:13:15 +01:00
|
|
|
perr := &pq.Error{}
|
|
|
|
sqliteErr := &sqlite3.Error{}
|
|
|
|
switch {
|
|
|
|
case errors.As(err, &perr):
|
|
|
|
if perr.Code == pq.ErrorCode("23505") {
|
|
|
|
return admin.NewCreateFrontendBadRequest()
|
|
|
|
}
|
|
|
|
case errors.As(err, sqliteErr):
|
|
|
|
if errors.Is(sqliteErr.Code, sqlite3.ErrConstraint) {
|
|
|
|
return admin.NewCreateFrontendBadRequest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 22:14:42 +01:00
|
|
|
logrus.Errorf("error creating frontend record: %v", err)
|
|
|
|
return admin.NewCreateFrontendInternalServerError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
logrus.Errorf("error committing frontend record: %v", err)
|
|
|
|
return admin.NewCreateFrontendInternalServerError()
|
|
|
|
}
|
|
|
|
|
2022-12-02 14:44:17 +01:00
|
|
|
logrus.Infof("created global frontend '%v' with public name '%v'", fe.Token, *fe.PublicName)
|
2022-12-01 22:14:42 +01:00
|
|
|
|
|
|
|
return admin.NewCreateFrontendCreated().WithPayload(&rest_model_zrok.CreateFrontendResponse{Token: feToken})
|
|
|
|
}
|