Skip to content

Commit

Permalink
removed previous agent innards (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Sep 12, 2024
1 parent 42c3ec4 commit cf7cce0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 130 deletions.
5 changes: 2 additions & 3 deletions agent/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
)

type share struct {
shr *sdk.Share
target string

token string
target string
basicAuth []string
frontendSelection []string
shareMode sdk.ShareMode
Expand Down
109 changes: 1 addition & 108 deletions agent/publicShare.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import (
"context"
"errors"
"github.com/openziti/zrok/agent/agentGrpc"
"github.com/openziti/zrok/endpoints/proxy"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/sdk/golang/sdk"
"github.com/sirupsen/logrus"
"time"
)

func (i *agentGrpcImpl) PublicShare(_ context.Context, req *agentGrpc.PublicShareRequest) (*agentGrpc.PublicShareReply, error) {
Expand All @@ -21,108 +17,5 @@ func (i *agentGrpcImpl) PublicShare(_ context.Context, req *agentGrpc.PublicShar
return nil, errors.New("unable to load environment; did you 'zrok enable'?")
}

zif, err := root.ZitiIdentityNamed(root.EnvironmentIdentityName())
if err != nil {
return nil, err
}

shrReq := &sdk.ShareRequest{
BackendMode: sdk.BackendMode(req.BackendMode),
ShareMode: sdk.PublicShareMode,
Frontends: req.FrontendSelection,
BasicAuth: req.BasicAuth,
Target: req.Target,
}
if req.Closed {
shrReq.PermissionMode = sdk.ClosedPermissionMode
shrReq.AccessGrants = req.AccessGrants
}
if req.OauthProvider != "" {
shrReq.OauthProvider = req.OauthProvider
shrReq.OauthEmailAddressPatterns = req.OauthEmailAddressPatterns
checkInterval, err := time.ParseDuration(req.GetOauthCheckInterval())
if err != nil {
return nil, err
}
shrReq.OauthAuthorizationCheckInterval = checkInterval
}
shr, err := sdk.CreateShare(root, shrReq)
if err != nil {
return nil, err
}

switch req.BackendMode {
case "proxy":
cfg := &proxy.BackendConfig{
IdentityPath: zif,
EndpointAddress: req.Target,
ShrToken: shr.Token,
Insecure: req.Insecure,
}

be, err := proxy.NewBackend(cfg)
if err != nil {
return nil, err
}

agentShr := &share{
shr: shr,
target: req.Target,
basicAuth: req.BasicAuth,
frontendSelection: shr.FrontendEndpoints,
shareMode: sdk.PublicShareMode,
backendMode: sdk.BackendMode(req.BackendMode),
insecure: req.Insecure,
oauthProvider: req.OauthProvider,
oauthEmailAddressPatterns: req.OauthEmailAddressPatterns,
oauthCheckInterval: shrReq.OauthAuthorizationCheckInterval,
closed: req.Closed,
accessGrants: req.AccessGrants,
handler: be,
}

i.a.shares[shr.Token] = agentShr
go func() {
if err := agentShr.handler.Run(); err != nil {
logrus.Errorf("error running proxy backend: %v", err)
}
}()

case "web":
cfg := &proxy.CaddyWebBackendConfig{
IdentityPath: zif,
WebRoot: req.Target,
ShrToken: shr.Token,
}

be, err := proxy.NewCaddyWebBackend(cfg)
if err != nil {
return nil, err
}

agentShr := &share{
shr: shr,
target: req.Target,
basicAuth: req.BasicAuth,
frontendSelection: shr.FrontendEndpoints,
shareMode: sdk.PublicShareMode,
backendMode: sdk.BackendMode(req.BackendMode),
insecure: req.Insecure,
oauthProvider: req.OauthProvider,
oauthEmailAddressPatterns: req.OauthEmailAddressPatterns,
oauthCheckInterval: shrReq.OauthAuthorizationCheckInterval,
closed: req.Closed,
accessGrants: req.AccessGrants,
handler: be,
}

i.a.shares[shr.Token] = agentShr
go func() {
if err := agentShr.handler.Run(); err != nil {
logrus.Errorf("error running web backend: %v", err)
}
}()
}

return &agentGrpc.PublicShareReply{Token: shr.Token}, nil
return &agentGrpc.PublicShareReply{}, nil
}
23 changes: 4 additions & 19 deletions agent/releaseShare.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,19 @@ package agent
import (
"context"
"github.com/openziti/zrok/agent/agentGrpc"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/sdk/golang/sdk"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

func (i *agentGrpcImpl) ReleaseShare(_ context.Context, req *agentGrpc.ReleaseShareRequest) (*agentGrpc.ReleaseShareReply, error) {
if shr, found := i.a.shares[req.Token]; found {
logrus.Infof("stopping share '%v'", shr.shr.Token)
logrus.Infof("stopping share '%v'", shr.token)
if err := shr.handler.Stop(); err != nil {
logrus.Errorf("error stopping share '%v': %v", shr.shr.Token, err)
logrus.Errorf("error stopping share '%v': %v", shr.token, err)
}

root, err := environment.LoadRoot()
if err != nil {
return nil, err
}

if !root.IsEnabled() {
return nil, errors.New("unable to load environment; did you 'zrok enable'?")
}

if err := sdk.DeleteShare(root, shr.shr); err != nil {
logrus.Errorf("error releasing share '%v': %v", shr.shr.Token, err)
}

delete(i.a.shares, shr.shr.Token)
logrus.Infof("released share '%v'", shr.shr.Token)
delete(i.a.shares, shr.token)
logrus.Infof("released share '%v'", shr.token)
} else {
return nil, errors.Errorf("agent has no share with token '%v'", req.Token)
}
Expand Down

0 comments on commit cf7cce0

Please sign in to comment.