zrok/controller/enable.go

168 lines
5.4 KiB
Go
Raw Normal View History

2022-07-25 23:05:44 +02:00
package controller
import (
"bytes"
"context"
"encoding/json"
"fmt"
2022-07-25 23:05:44 +02:00
"github.com/go-openapi/runtime/middleware"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity"
"github.com/openziti/edge/rest_management_api_client"
"github.com/openziti/edge/rest_management_api_client/edge_router_policy"
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-08-03 20:25:27 +02:00
"strings"
"time"
2022-07-25 23:05:44 +02:00
)
2022-08-12 17:03:15 +02:00
type enableHandler struct {
cfg *Config
}
func newEnableHandler(cfg *Config) *enableHandler {
return &enableHandler{cfg: cfg}
}
func (self *enableHandler) Handle(params identity.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-09-02 16:50:13 +02:00
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
2022-07-29 22:03:53 +02:00
}
2022-08-12 17:03:15 +02:00
client, err := edgeClient(self.cfg.Ziti)
if err != nil {
2022-07-26 22:21:49 +02:00
logrus.Errorf("error getting edge client: %v", err)
2022-07-27 19:38:35 +02:00
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
2022-09-09 16:20:05 +02:00
ident, err := self.createIdentity(principal.Email, client)
if err != nil {
logrus.Error(err)
2022-07-27 19:38:35 +02:00
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
2022-08-12 17:03:15 +02:00
cfg, err := self.enrollIdentity(ident.Payload.Data.ID, client)
if err != nil {
2022-07-26 22:21:49 +02:00
logrus.Error(err)
2022-07-27 19:38:35 +02:00
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
if err := self.createEdgeRouterPolicy(ident.Payload.Data.ID, client); err != nil {
logrus.Error(err)
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
2022-08-03 20:25:27 +02:00
addrTokens := strings.Split(params.HTTPRequest.RemoteAddr, ":")
addr := addrTokens[0]
envId, err := str.CreateEnvironment(int(principal.ID), &store.Environment{
Description: params.Body.Description,
Host: params.Body.Host,
Address: addr,
ZitiIdentityId: ident.Payload.Data.ID,
}, tx)
if err != nil {
logrus.Errorf("error storing created identity: %v", err)
_ = tx.Rollback()
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
if err := tx.Commit(); err != nil {
logrus.Errorf("error committing: %v", err)
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
2022-09-09 16:20:05 +02:00
logrus.Infof("recorded identity '%v' with id '%v' for '%v'", ident.Payload.Data.ID, envId, principal.Email)
resp := identity.NewEnableCreated().WithPayload(&rest_model_zrok.EnableResponse{
Identity: ident.Payload.Data.ID,
2022-07-25 23:05:44 +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-09-09 16:20:05 +02:00
func (_ *enableHandler) createIdentity(email string, client *rest_management_api_client.ZitiEdgeManagement) (*identity_edge.CreateIdentityCreated, error) {
iIsAdmin := false
2022-07-26 22:21:49 +02:00
iId, err := randomId()
if err != nil {
return nil, err
}
2022-09-09 16:20:05 +02:00
name := fmt.Sprintf("%v-%v", email, iId)
2022-07-27 19:38:35 +02:00
identityType := rest_model_edge.IdentityTypeUser
i := &rest_model_edge.IdentityCreate{
Enrollment: &rest_model_edge.IdentityCreateEnrollment{Ott: true},
IsAdmin: &iIsAdmin,
2022-07-27 19:38:35 +02:00
Name: &name,
RoleAttributes: nil,
ServiceHostingCosts: nil,
Tags: nil,
2022-07-27 19:38:35 +02:00
Type: &identityType,
}
2022-07-27 19:38:35 +02:00
req := identity_edge.NewCreateIdentityParams()
req.Identity = i
resp, err := client.Identity.CreateIdentity(req, nil)
if err != nil {
return nil, err
}
2022-07-27 19:38:35 +02:00
return resp, nil
}
2022-08-12 17:03:15 +02:00
func (_ *enableHandler) enrollIdentity(id string, client *rest_management_api_client.ZitiEdgeManagement) (*sdk_config.Config, error) {
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
}
func (_ *enableHandler) createEdgeRouterPolicy(id string, edge *rest_management_api_client.ZitiEdgeManagement) error {
edgeRouterRoles := []string{"#all"}
identityRoles := []string{fmt.Sprintf("@%v", id)}
name := fmt.Sprintf("zrok-%v", id)
semantic := rest_model_edge.SemanticAllOf
erp := &rest_model_edge.EdgeRouterPolicyCreate{
EdgeRouterRoles: edgeRouterRoles,
IdentityRoles: identityRoles,
Name: &name,
Semantic: &semantic,
}
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
}
logrus.Infof("created edge router policy '%v'", resp.Payload.Data.ID)
return nil
}