diff --git a/controller/bootstrap.go b/controller/bootstrap.go index e56c86f7..d9060f1e 100644 --- a/controller/bootstrap.go +++ b/controller/bootstrap.go @@ -196,13 +196,13 @@ func assertIdentity(zId string, edge *rest_management_api_client.ZitiEdgeManagem } func bootstrapIdentity(name string, edge *rest_management_api_client.ZitiEdgeManagement) (string, error) { - idc, err := createIdentity(name, rest_model_edge.IdentityTypeDevice, nil, edge) + idc, err := zrok_edge_sdk.CreateIdentity(name, rest_model_edge.IdentityTypeDevice, nil, edge) if err != nil { return "", errors.Wrapf(err, "error creating '%v' identity", name) } zId := idc.Payload.Data.ID - cfg, err := enrollIdentity(zId, edge) + cfg, err := zrok_edge_sdk.EnrollIdentity(zId, edge) if err != nil { return "", errors.Wrapf(err, "error enrolling '%v' identity", name) } diff --git a/controller/create_frontend.go b/controller/create_frontend.go index f70c21a2..b60c7c80 100644 --- a/controller/create_frontend.go +++ b/controller/create_frontend.go @@ -3,6 +3,7 @@ package controller import ( "github.com/go-openapi/runtime/middleware" "github.com/openziti-test-kitchen/zrok/controller/store" + "github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/admin" "github.com/sirupsen/logrus" @@ -27,7 +28,7 @@ func (h *createFrontendHandler) Handle(params admin.CreateFrontendParams, princi } zId := params.Body.ZID - detail, err := getIdentity(zId, client) + detail, err := zrok_edge_sdk.GetIdentity(zId, client) if err != nil { logrus.Errorf("error getting identity details for '%v': %v", zId, err) return admin.NewCreateFrontendInternalServerError() diff --git a/controller/create_identity.go b/controller/create_identity.go index f0aff416..42d41242 100644 --- a/controller/create_identity.go +++ b/controller/create_identity.go @@ -34,14 +34,14 @@ func (h *createIdentityHandler) Handle(params admin.CreateIdentityParams, princi return admin.NewCreateIdentityInternalServerError() } - idc, err := createIdentity(name, rest_model_edge.IdentityTypeService, nil, edge) + idc, err := zrok_edge_sdk.CreateIdentity(name, rest_model_edge.IdentityTypeService, nil, edge) if err != nil { logrus.Errorf("error creating identity: %v", err) return admin.NewCreateIdentityInternalServerError() } zId := idc.Payload.Data.ID - idCfg, err := enrollIdentity(zId, edge) + idCfg, err := zrok_edge_sdk.EnrollIdentity(zId, edge) if err != nil { logrus.Errorf("error enrolling identity: %v", err) return admin.NewCreateIdentityInternalServerError() diff --git a/controller/disable.go b/controller/disable.go index 4e185d3d..7b0a779a 100644 --- a/controller/disable.go +++ b/controller/disable.go @@ -52,7 +52,7 @@ func (h *disableHandler) Handle(params environment.DisableParams, principal *res logrus.Errorf("error deleting edge router policy: %v", err) return environment.NewDisableInternalServerError() } - if err := deleteIdentity(params.Body.Identity, edge); err != nil { + if err := zrok_edge_sdk.DeleteIdentity(params.Body.Identity, edge); err != nil { logrus.Errorf("error deleting identity: %v", err) return environment.NewDisableInternalServerError() } diff --git a/controller/enable.go b/controller/enable.go index 85a549f2..7266aad2 100644 --- a/controller/enable.go +++ b/controller/enable.go @@ -31,13 +31,18 @@ func (h *enableHandler) Handle(params environment.EnableParams, principal *rest_ logrus.Errorf("error getting edge client: %v", err) return environment.NewEnableInternalServerError() } - ident, err := createEnvironmentIdentity(principal.Email, client) + accountToken, err := createToken() + if err != nil { + logrus.Errorf("error creating account token: %v", err) + return environment.NewEnableInternalServerError() + } + ident, err := zrok_edge_sdk.CreateEnvironmentIdentity(principal.Email, accountToken, client) if err != nil { logrus.Error(err) return environment.NewEnableInternalServerError() } envZId := ident.Payload.Data.ID - cfg, err := enrollIdentity(envZId, client) + cfg, err := zrok_edge_sdk.EnrollIdentity(envZId, client) if err != nil { logrus.Error(err) return environment.NewEnableInternalServerError() diff --git a/controller/edge.go b/controller/zrok_edge_sdk/identity.go similarity index 62% rename from controller/edge.go rename to controller/zrok_edge_sdk/identity.go index e472acfe..6b0b2255 100644 --- a/controller/edge.go +++ b/controller/zrok_edge_sdk/identity.go @@ -1,35 +1,30 @@ -package controller +package zrok_edge_sdk import ( "context" "fmt" - "github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk" "github.com/openziti/edge/rest_management_api_client" - identity_edge "github.com/openziti/edge/rest_management_api_client/identity" + "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/config" "github.com/openziti/sdk-golang/ziti/enroll" "github.com/sirupsen/logrus" "time" ) -func createEnvironmentIdentity(accountEmail string, client *rest_management_api_client.ZitiEdgeManagement) (*identity_edge.CreateIdentityCreated, error) { - name, err := createToken() - if err != nil { - return nil, err - } +func CreateEnvironmentIdentity(accountEmail, accountToken string, client *rest_management_api_client.ZitiEdgeManagement) (*identity.CreateIdentityCreated, error) { identityType := rest_model_edge.IdentityTypeUser moreTags := map[string]interface{}{"zrokEmail": accountEmail} - return createIdentity(name, identityType, moreTags, client) + return CreateIdentity(accountToken, identityType, moreTags, client) } -func createIdentity(name string, identityType rest_model_edge.IdentityType, moreTags map[string]interface{}, client *rest_management_api_client.ZitiEdgeManagement) (*identity_edge.CreateIdentityCreated, error) { +func CreateIdentity(name string, identityType rest_model_edge.IdentityType, moreTags map[string]interface{}, client *rest_management_api_client.ZitiEdgeManagement) (*identity.CreateIdentityCreated, error) { isAdmin := false - tags := zrok_edge_sdk.ZrokTags() + tags := ZrokTags() for k, v := range moreTags { tags.SubTags[k] = v } - req := identity_edge.NewCreateIdentityParams() + req := identity.NewCreateIdentityParams() req.Identity = &rest_model_edge.IdentityCreate{ Enrollment: &rest_model_edge.IdentityCreateEnrollment{Ott: true}, IsAdmin: &isAdmin, @@ -47,11 +42,11 @@ func createIdentity(name string, identityType rest_model_edge.IdentityType, more return resp, nil } -func getIdentity(zId string, client *rest_management_api_client.ZitiEdgeManagement) (*identity_edge.ListIdentitiesOK, error) { +func GetIdentity(zId string, client *rest_management_api_client.ZitiEdgeManagement) (*identity.ListIdentitiesOK, error) { filter := fmt.Sprintf("id=\"%v\"", zId) limit := int64(0) offset := int64(0) - req := &identity_edge.ListIdentitiesParams{ + req := &identity.ListIdentitiesParams{ Filter: &filter, Limit: &limit, Offset: &offset, @@ -65,8 +60,8 @@ func getIdentity(zId string, client *rest_management_api_client.ZitiEdgeManageme return resp, nil } -func enrollIdentity(zId string, client *rest_management_api_client.ZitiEdgeManagement) (*sdk_config.Config, error) { - p := &identity_edge.DetailIdentityParams{ +func EnrollIdentity(zId string, client *rest_management_api_client.ZitiEdgeManagement) (*config.Config, error) { + p := &identity.DetailIdentityParams{ Context: context.Background(), ID: zId, } @@ -90,8 +85,8 @@ func enrollIdentity(zId string, client *rest_management_api_client.ZitiEdgeManag return conf, nil } -func deleteIdentity(id string, edge *rest_management_api_client.ZitiEdgeManagement) error { - req := &identity_edge.DeleteIdentityParams{ +func DeleteIdentity(id string, edge *rest_management_api_client.ZitiEdgeManagement) error { + req := &identity.DeleteIdentityParams{ ID: id, Context: context.Background(), }