2022-07-25 23:05:44 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2022-07-26 18:26:58 +02:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-07-25 23:05:44 +02:00
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2022-11-02 20:07:43 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/build"
|
2022-07-29 21:54:13 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/controller/store"
|
2022-07-26 21:16:02 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
2022-11-30 17:43:00 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/environment"
|
2022-07-26 18:26:58 +02:00
|
|
|
"github.com/openziti/edge/rest_management_api_client"
|
2022-08-17 19:43:16 +02:00
|
|
|
"github.com/openziti/edge/rest_management_api_client/edge_router_policy"
|
2022-07-26 18:26:58 +02:00
|
|
|
identity_edge "github.com/openziti/edge/rest_management_api_client/identity"
|
|
|
|
rest_model_edge "github.com/openziti/edge/rest_model"
|
|
|
|
sdk_config "github.com/openziti/sdk-golang/ziti/config"
|
|
|
|
"github.com/openziti/sdk-golang/ziti/enroll"
|
2022-07-25 23:05:44 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-07-26 18:26:58 +02:00
|
|
|
"time"
|
2022-07-25 23:05:44 +02:00
|
|
|
)
|
|
|
|
|
2022-08-12 17:03:15 +02:00
|
|
|
type enableHandler struct {
|
|
|
|
}
|
|
|
|
|
2022-10-19 19:20:47 +02:00
|
|
|
func newEnableHandler() *enableHandler {
|
|
|
|
return &enableHandler{}
|
2022-08-12 17:03:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-30 17:43:00 +01:00
|
|
|
func (h *enableHandler) Handle(params environment.EnableParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
2022-07-29 22:03:53 +02:00
|
|
|
// start transaction early; if it fails, don't bother creating ziti resources
|
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error starting transaction: %v", err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewEnableInternalServerError()
|
2022-07-29 22:03:53 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 19:20:47 +02:00
|
|
|
client, err := edgeClient()
|
2022-07-26 18:26:58 +02:00
|
|
|
if err != nil {
|
2022-07-26 22:21:49 +02:00
|
|
|
logrus.Errorf("error getting edge client: %v", err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewEnableInternalServerError()
|
2022-07-26 18:26:58 +02:00
|
|
|
}
|
2022-11-30 17:43:00 +01:00
|
|
|
ident, err := h.createIdentity(principal.Email, client)
|
2022-07-26 18:26:58 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewEnableInternalServerError()
|
2022-07-26 18:26:58 +02:00
|
|
|
}
|
2022-11-30 17:43:00 +01:00
|
|
|
cfg, err := h.enrollIdentity(ident.Payload.Data.ID, client)
|
2022-07-26 18:26:58 +02:00
|
|
|
if err != nil {
|
2022-07-26 22:21:49 +02:00
|
|
|
logrus.Error(err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewEnableInternalServerError()
|
2022-07-26 18:26:58 +02:00
|
|
|
}
|
2022-11-30 17:43:00 +01:00
|
|
|
if err := h.createEdgeRouterPolicy(ident.Payload.Data.ID, client); err != nil {
|
2022-08-17 19:43:16 +02:00
|
|
|
logrus.Error(err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewEnableInternalServerError()
|
2022-08-17 19:43:16 +02:00
|
|
|
}
|
2022-08-03 20:25:27 +02:00
|
|
|
envId, err := str.CreateEnvironment(int(principal.ID), &store.Environment{
|
2022-10-19 18:24:43 +02:00
|
|
|
Description: params.Body.Description,
|
|
|
|
Host: params.Body.Host,
|
|
|
|
Address: realRemoteAddress(params.HTTPRequest),
|
|
|
|
ZId: ident.Payload.Data.ID,
|
2022-08-03 20:25:27 +02:00
|
|
|
}, tx)
|
2022-07-29 21:54:13 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error storing created identity: %v", err)
|
|
|
|
_ = tx.Rollback()
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewEnableInternalServerError()
|
2022-07-29 21:54:13 +02:00
|
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
logrus.Errorf("error committing: %v", err)
|
2022-11-30 17:43:00 +01:00
|
|
|
return environment.NewEnableInternalServerError()
|
2022-07-29 21:54:13 +02:00
|
|
|
}
|
2022-11-08 21:07:18 +01:00
|
|
|
logrus.Infof("created environment for '%v', with ziti identity '%v', and database id '%v'", principal.Email, ident.Payload.Data.ID, envId)
|
2022-07-29 21:54:13 +02:00
|
|
|
|
2022-11-30 17:43:00 +01:00
|
|
|
resp := environment.NewEnableCreated().WithPayload(&rest_model_zrok.EnableResponse{
|
2022-07-26 18:26:58 +02:00
|
|
|
Identity: ident.Payload.Data.ID,
|
2022-07-25 23:05:44 +02:00
|
|
|
})
|
2022-07-26 18:26:58 +02:00
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&out)
|
|
|
|
enc.SetEscapeHTML(false)
|
|
|
|
err = enc.Encode(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
resp.Payload.Cfg = out.String()
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:43:00 +01:00
|
|
|
func (h *enableHandler) createIdentity(email string, client *rest_management_api_client.ZitiEdgeManagement) (*identity_edge.CreateIdentityCreated, error) {
|
2022-07-26 18:26:58 +02:00
|
|
|
iIsAdmin := false
|
2022-10-18 21:21:53 +02:00
|
|
|
name, err := createToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-27 19:38:35 +02:00
|
|
|
identityType := rest_model_edge.IdentityTypeUser
|
2022-11-30 17:43:00 +01:00
|
|
|
tags := h.zrokTags()
|
2022-09-19 20:42:05 +02:00
|
|
|
tags.SubTags["zrokEmail"] = email
|
2022-07-26 18:26:58 +02:00
|
|
|
i := &rest_model_edge.IdentityCreate{
|
|
|
|
Enrollment: &rest_model_edge.IdentityCreateEnrollment{Ott: true},
|
|
|
|
IsAdmin: &iIsAdmin,
|
2022-07-27 19:38:35 +02:00
|
|
|
Name: &name,
|
2022-07-26 18:26:58 +02:00
|
|
|
RoleAttributes: nil,
|
|
|
|
ServiceHostingCosts: nil,
|
2022-09-19 20:42:05 +02:00
|
|
|
Tags: tags,
|
2022-07-27 19:38:35 +02:00
|
|
|
Type: &identityType,
|
2022-07-26 18:26:58 +02:00
|
|
|
}
|
2022-07-27 19:38:35 +02:00
|
|
|
req := identity_edge.NewCreateIdentityParams()
|
|
|
|
req.Identity = i
|
|
|
|
resp, err := client.Identity.CreateIdentity(req, nil)
|
2022-07-26 18:26:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-27 19:38:35 +02:00
|
|
|
return resp, nil
|
2022-07-26 18:26:58 +02:00
|
|
|
}
|
|
|
|
|
2022-11-30 17:43:00 +01:00
|
|
|
func (h *enableHandler) enrollIdentity(id string, client *rest_management_api_client.ZitiEdgeManagement) (*sdk_config.Config, error) {
|
2022-07-26 18:26:58 +02:00
|
|
|
p := &identity_edge.DetailIdentityParams{
|
|
|
|
Context: context.Background(),
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
p.SetTimeout(30 * time.Second)
|
|
|
|
resp, err := client.Identity.DetailIdentity(p, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tkn, _, err := enroll.ParseToken(resp.GetPayload().Data.Enrollment.Ott.JWT)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
flags := enroll.EnrollmentFlags{
|
|
|
|
Token: tkn,
|
|
|
|
KeyAlg: "RSA",
|
|
|
|
}
|
|
|
|
conf, err := enroll.Enroll(flags)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conf, nil
|
2022-07-25 23:05:44 +02:00
|
|
|
}
|
2022-08-17 19:43:16 +02:00
|
|
|
|
2022-11-30 17:43:00 +01:00
|
|
|
func (h *enableHandler) createEdgeRouterPolicy(id string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
2022-08-17 19:43:16 +02:00
|
|
|
edgeRouterRoles := []string{"#all"}
|
|
|
|
identityRoles := []string{fmt.Sprintf("@%v", id)}
|
|
|
|
semantic := rest_model_edge.SemanticAllOf
|
|
|
|
erp := &rest_model_edge.EdgeRouterPolicyCreate{
|
|
|
|
EdgeRouterRoles: edgeRouterRoles,
|
|
|
|
IdentityRoles: identityRoles,
|
2022-09-19 20:42:05 +02:00
|
|
|
Name: &id,
|
2022-08-17 19:43:16 +02:00
|
|
|
Semantic: &semantic,
|
2022-11-30 17:43:00 +01:00
|
|
|
Tags: h.zrokTags(),
|
2022-08-17 19:43:16 +02:00
|
|
|
}
|
|
|
|
req := &edge_router_policy.CreateEdgeRouterPolicyParams{
|
|
|
|
Policy: erp,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
req.SetTimeout(30 * time.Second)
|
|
|
|
resp, err := edge.EdgeRouterPolicy.CreateEdgeRouterPolicy(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-08 21:07:18 +01:00
|
|
|
logrus.Infof("created edge router policy '%v' for ziti identity '%v'", resp.Payload.Data.ID, id)
|
2022-08-17 19:43:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-15 22:06:38 +02:00
|
|
|
|
2022-11-30 17:43:00 +01:00
|
|
|
func (h *enableHandler) zrokTags() *rest_model_edge.Tags {
|
2022-09-15 22:06:38 +02:00
|
|
|
return &rest_model_edge.Tags{
|
|
|
|
SubTags: map[string]interface{}{
|
2022-11-02 20:07:43 +01:00
|
|
|
"zrok": build.String(),
|
2022-09-15 22:06:38 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|