mirror of
https://github.com/openziti/zrok.git
synced 2024-11-25 17:43:53 +01:00
identities -> environments
This commit is contained in:
parent
6ff9a90896
commit
cf49c10d87
@ -18,7 +18,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func enableHandler(_ identity.EnableParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
func enableHandler(params identity.EnableParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
||||||
// start transaction early; if it fails, don't bother creating ziti resources
|
// start transaction early; if it fails, don't bother creating ziti resources
|
||||||
tx, err := str.Begin()
|
tx, err := str.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -42,7 +42,7 @@ func enableHandler(_ identity.EnableParams, principal *rest_model_zrok.Principal
|
|||||||
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
iid, err := str.CreateIdentity(int(principal.ID), &store.Identity{ZitiId: ident.Payload.Data.ID}, tx)
|
iid, err := str.CreateEnvironment(int(principal.ID), &store.Environment{ZitiIdentityId: ident.Payload.Data.ID}, tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("error storing created identity: %v", err)
|
logrus.Errorf("error storing created identity: %v", err)
|
||||||
_ = tx.Rollback()
|
_ = tx.Rollback()
|
||||||
|
@ -14,18 +14,18 @@ func listEnvironmentsHandler(_ metadata.ListEnvironmentsParams, principal *rest_
|
|||||||
return metadata.NewListEnvironmentsInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
return metadata.NewListEnvironmentsInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
||||||
}
|
}
|
||||||
defer func() { _ = tx.Rollback() }()
|
defer func() { _ = tx.Rollback() }()
|
||||||
ids, err := str.FindIdentitiesForAccount(int(principal.ID), tx)
|
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("error finding identities for '%v': %v", principal.Username, err)
|
logrus.Errorf("error finding identities for '%v': %v", principal.Username, err)
|
||||||
return metadata.NewListEnvironmentsInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
return metadata.NewListEnvironmentsInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
||||||
}
|
}
|
||||||
var out rest_model_zrok.Environments
|
var out rest_model_zrok.Environments
|
||||||
for _, id := range ids {
|
for _, env := range envs {
|
||||||
out = append(out, &rest_model_zrok.Environment{
|
out = append(out, &rest_model_zrok.Environment{
|
||||||
Active: id.Active,
|
Active: env.Active,
|
||||||
CreatedAt: id.CreatedAt.String(),
|
CreatedAt: env.CreatedAt.String(),
|
||||||
UpdatedAt: id.UpdatedAt.String(),
|
UpdatedAt: env.UpdatedAt.String(),
|
||||||
ZitiID: id.ZitiId,
|
ZitiID: env.ZitiIdentityId,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return metadata.NewListEnvironmentsOK().WithPayload(out)
|
return metadata.NewListEnvironmentsOK().WithPayload(out)
|
||||||
|
68
controller/store/environment.go
Normal file
68
controller/store/environment.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Environment struct {
|
||||||
|
Model
|
||||||
|
AccountId int
|
||||||
|
Description string
|
||||||
|
Host string
|
||||||
|
Address string
|
||||||
|
ZitiIdentityId string
|
||||||
|
Active bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Store) CreateEnvironment(accountId int, i *Environment, tx *sqlx.Tx) (int, error) {
|
||||||
|
stmt, err := tx.Prepare("insert into environments (account_id, description, host, address, ziti_identity_id, active) values (?, ?, ?, ?, ?, true)")
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.Wrap(err, "error preparing environments insert statement")
|
||||||
|
}
|
||||||
|
res, err := stmt.Exec(accountId, i.Description, i.Host, i.Address, i.ZitiIdentityId)
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.Wrap(err, "error executing environments insert statement")
|
||||||
|
}
|
||||||
|
id, err := res.LastInsertId()
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.Wrap(err, "error retrieving last environments insert id")
|
||||||
|
}
|
||||||
|
return int(id), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Store) GetEnvironment(id int, tx *sqlx.Tx) (*Environment, error) {
|
||||||
|
i := &Environment{}
|
||||||
|
if err := tx.QueryRowx("select * from environments where id = ?", id).StructScan(i); err != nil {
|
||||||
|
return nil, errors.Wrap(err, "error selecting environment by id")
|
||||||
|
}
|
||||||
|
return i, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Store) FindEnvironmentsForAccount(accountId int, tx *sqlx.Tx) ([]*Environment, error) {
|
||||||
|
rows, err := tx.Queryx("select environments.* from environments where account_id = ?", accountId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "error selecting environments by account id")
|
||||||
|
}
|
||||||
|
var is []*Environment
|
||||||
|
for rows.Next() {
|
||||||
|
i := &Environment{}
|
||||||
|
if err := rows.StructScan(i); err != nil {
|
||||||
|
return nil, errors.Wrap(err, "error scanning environment")
|
||||||
|
}
|
||||||
|
is = append(is, i)
|
||||||
|
}
|
||||||
|
return is, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Store) DeleteEnvironment(id int, tx *sqlx.Tx) error {
|
||||||
|
stmt, err := tx.Prepare("delete from environments where id = ?")
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "error preparing environments delete statement")
|
||||||
|
}
|
||||||
|
_, err = stmt.Exec(id)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "error executing environments delete statement")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -1,65 +0,0 @@
|
|||||||
package store
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/jmoiron/sqlx"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Identity struct {
|
|
||||||
Model
|
|
||||||
AccountId int
|
|
||||||
ZitiId string
|
|
||||||
Active bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Store) CreateIdentity(accountId int, i *Identity, tx *sqlx.Tx) (int, error) {
|
|
||||||
stmt, err := tx.Prepare("insert into identities (account_id, ziti_id, active) values (?, ?, true)")
|
|
||||||
if err != nil {
|
|
||||||
return 0, errors.Wrap(err, "error preparing identities insert statement")
|
|
||||||
}
|
|
||||||
res, err := stmt.Exec(accountId, i.ZitiId)
|
|
||||||
if err != nil {
|
|
||||||
return 0, errors.Wrap(err, "error executing identities insert statement")
|
|
||||||
}
|
|
||||||
id, err := res.LastInsertId()
|
|
||||||
if err != nil {
|
|
||||||
return 0, errors.Wrap(err, "error retrieving last identities insert id")
|
|
||||||
}
|
|
||||||
return int(id), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Store) GetIdentity(id int, tx *sqlx.Tx) (*Identity, error) {
|
|
||||||
i := &Identity{}
|
|
||||||
if err := tx.QueryRowx("select * from identities where id = ?", id).StructScan(i); err != nil {
|
|
||||||
return nil, errors.Wrap(err, "error selecting identity by id")
|
|
||||||
}
|
|
||||||
return i, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Store) FindIdentitiesForAccount(accountId int, tx *sqlx.Tx) ([]*Identity, error) {
|
|
||||||
rows, err := tx.Queryx("select identities.* from identities where account_id = ?", accountId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "error selecting identities by account id")
|
|
||||||
}
|
|
||||||
var is []*Identity
|
|
||||||
for rows.Next() {
|
|
||||||
i := &Identity{}
|
|
||||||
if err := rows.StructScan(i); err != nil {
|
|
||||||
return nil, errors.Wrap(err, "error scanning identity")
|
|
||||||
}
|
|
||||||
is = append(is, i)
|
|
||||||
}
|
|
||||||
return is, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Store) DeleteIdentity(id int, tx *sqlx.Tx) error {
|
|
||||||
stmt, err := tx.Prepare("delete from identities where id = ?")
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "error preparing identities delete statement")
|
|
||||||
}
|
|
||||||
_, err = stmt.Exec(id)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "error executing identities delete statement")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -7,18 +7,18 @@ import (
|
|||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
Model
|
Model
|
||||||
AccountId int
|
AccountId int
|
||||||
ZitiId string
|
ZitiServiceId string
|
||||||
Endpoint string
|
Endpoint string
|
||||||
Active bool
|
Active bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Store) CreateService(accountId int, svc *Service, tx *sqlx.Tx) (int, error) {
|
func (self *Store) CreateService(accountId int, svc *Service, tx *sqlx.Tx) (int, error) {
|
||||||
stmt, err := tx.Prepare("insert into services (account_id, ziti_id, endpoint, active) values (?, ?, ?, true)")
|
stmt, err := tx.Prepare("insert into services (account_id, ziti_service_id, endpoint, active) values (?, ?, ?, true)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.Wrap(err, "error preparing services insert statement")
|
return 0, errors.Wrap(err, "error preparing services insert statement")
|
||||||
}
|
}
|
||||||
res, err := stmt.Exec(accountId, svc.ZitiId, svc.Endpoint)
|
res, err := stmt.Exec(accountId, svc.ZitiServiceId, svc.Endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.Wrap(err, "error executing services insert statement")
|
return 0, errors.Wrap(err, "error executing services insert statement")
|
||||||
}
|
}
|
||||||
@ -54,12 +54,12 @@ func (self *Store) FindServicesForAccount(accountId int, tx *sqlx.Tx) ([]*Servic
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *Store) UpdateService(svc *Service, tx *sqlx.Tx) error {
|
func (self *Store) UpdateService(svc *Service, tx *sqlx.Tx) error {
|
||||||
sql := "update services set ziti_id = ?, endpoint = ?, active = ?, updated_at = strftime('%Y-%m-%d %H:%M:%f', 'now') where id = ?"
|
sql := "update services set ziti_service_id = ?, endpoint = ?, active = ?, updated_at = strftime('%Y-%m-%d %H:%M:%f', 'now') where id = ?"
|
||||||
stmt, err := tx.Prepare(sql)
|
stmt, err := tx.Prepare(sql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error preparing services update statement")
|
return errors.Wrap(err, "error preparing services update statement")
|
||||||
}
|
}
|
||||||
_, err = stmt.Exec(svc.ZitiId, svc.Endpoint, svc.Active, svc.Id)
|
_, err = stmt.Exec(svc.ZitiServiceId, svc.Endpoint, svc.Active, svc.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error executing services update statement")
|
return errors.Wrap(err, "error executing services update statement")
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,12 @@
|
|||||||
-- accounts
|
-- accounts
|
||||||
--
|
--
|
||||||
create table accounts (
|
create table accounts (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
username string not null unique,
|
username string not null unique,
|
||||||
password string not null,
|
password string not null,
|
||||||
token string not null unique,
|
token string not null unique,
|
||||||
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
||||||
updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
||||||
|
|
||||||
constraint chk_username check (username <> ''),
|
constraint chk_username check (username <> ''),
|
||||||
constraint chk_password check (username <> ''),
|
constraint chk_password check (username <> ''),
|
||||||
@ -17,30 +17,33 @@ create table accounts (
|
|||||||
);
|
);
|
||||||
|
|
||||||
--
|
--
|
||||||
-- identities
|
-- environments
|
||||||
--
|
--
|
||||||
create table identities (
|
create table environments (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
account_id integer constraint fk_accounts_identities references accounts on delete cascade,
|
account_id integer constraint fk_accounts_identities references accounts on delete cascade,
|
||||||
ziti_id string not null unique,
|
description string,
|
||||||
active boolean not null,
|
host string,
|
||||||
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
address string,
|
||||||
updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
ziti_identity_id string not null unique,
|
||||||
|
active boolean not null,
|
||||||
|
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
||||||
|
updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
||||||
|
|
||||||
constraint chk_ziti_id check (ziti_id <> '')
|
constraint chk_ziti_identity_id check (ziti_identity_id <> '')
|
||||||
);
|
);
|
||||||
|
|
||||||
--
|
--
|
||||||
-- services
|
-- services
|
||||||
--
|
--
|
||||||
create table services (
|
create table services (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
account_id integer constraint fk_accounts_services references accounts on delete cascade,
|
account_id integer constraint fk_accounts_services references accounts on delete cascade,
|
||||||
ziti_id string not null unique,
|
ziti_service_id string not null unique,
|
||||||
endpoint string,
|
endpoint string,
|
||||||
active boolean not null,
|
active boolean not null,
|
||||||
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
||||||
updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
||||||
|
|
||||||
constraint chk_ziti_id check (ziti_id <> '')
|
constraint chk_ziti_service_id check (ziti_service_id <> '')
|
||||||
);
|
);
|
@ -28,21 +28,21 @@ func tunnelHandler(params tunnel.TunnelParams, principal *rest_model_zrok.Princi
|
|||||||
defer func() { _ = tx.Rollback() }()
|
defer func() { _ = tx.Rollback() }()
|
||||||
|
|
||||||
envId := params.Body.Identity
|
envId := params.Body.Identity
|
||||||
if is, err := str.FindIdentitiesForAccount(int(principal.ID), tx); err == nil {
|
if envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx); err == nil {
|
||||||
found := false
|
found := false
|
||||||
for _, i := range is {
|
for _, env := range envs {
|
||||||
if i.ZitiId == envId {
|
if env.ZitiIdentityId == envId {
|
||||||
logrus.Infof("found identity '%v' for user '%v'", envId, principal.Username)
|
logrus.Infof("found identity '%v' for user '%v'", envId, principal.Username)
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
logrus.Errorf("identity '%v' not found for user '%v'", envId, principal.Username)
|
logrus.Errorf("environment '%v' not found for user '%v'", envId, principal.Username)
|
||||||
return tunnel.NewTunnelUnauthorized().WithPayload("bad environment identity")
|
return tunnel.NewTunnelUnauthorized().WithPayload("bad environment identity")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logrus.Errorf("error finding identities for account '%v'", principal.Username)
|
logrus.Errorf("error finding environments for account '%v'", principal.Username)
|
||||||
return tunnel.NewTunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
return tunnel.NewTunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ func tunnelHandler(params tunnel.TunnelParams, principal *rest_model_zrok.Princi
|
|||||||
|
|
||||||
logrus.Infof("allocated service '%v'", svcName)
|
logrus.Infof("allocated service '%v'", svcName)
|
||||||
|
|
||||||
sid, err := str.CreateService(int(principal.ID), &store.Service{ZitiId: svcId, Endpoint: params.Body.Endpoint}, tx)
|
sid, err := str.CreateService(int(principal.ID), &store.Service{ZitiServiceId: svcId, Endpoint: params.Body.Endpoint}, tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("error creating service record: %v", err)
|
logrus.Errorf("error creating service record: %v", err)
|
||||||
_ = tx.Rollback()
|
_ = tx.Rollback()
|
||||||
|
@ -41,7 +41,7 @@ func untunnelHandler(params tunnel.UntunnelParams, principal *rest_model_zrok.Pr
|
|||||||
var ssvc *store.Service
|
var ssvc *store.Service
|
||||||
if svcs, err := str.FindServicesForAccount(int(principal.ID), tx); err == nil {
|
if svcs, err := str.FindServicesForAccount(int(principal.ID), tx); err == nil {
|
||||||
for _, svc := range svcs {
|
for _, svc := range svcs {
|
||||||
if svc.ZitiId == svcId {
|
if svc.ZitiServiceId == svcId {
|
||||||
ssvc = svc
|
ssvc = svc
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -91,10 +91,10 @@ h1, h2, h3, h4, h5, h6 {
|
|||||||
margin-left: 30px;
|
margin-left: 30px;
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
background-color: #222;
|
background-color: #30205d;
|
||||||
border-radius: 5px 5px 5px 5px;
|
border-radius: 5px 5px 5px 5px;
|
||||||
border: 1px solid #846fec;
|
border: 1px solid #7733ff;
|
||||||
color: #846fec;
|
color: #7733ff;
|
||||||
padding: 5px;
|
padding: 5px;
|
||||||
width: 100px;
|
width: 100px;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
|
Loading…
Reference in New Issue
Block a user