2024-08-26 20:18:17 +02:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"github.com/openziti/zrok/agent/agentGrpc"
|
2024-09-12 20:05:17 +02:00
|
|
|
"github.com/openziti/zrok/agent/proctree"
|
2024-08-26 20:18:17 +02:00
|
|
|
"github.com/openziti/zrok/environment"
|
2024-09-12 20:25:25 +02:00
|
|
|
"github.com/openziti/zrok/sdk/golang/sdk"
|
2024-09-12 20:05:17 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"os"
|
2024-08-26 20:18:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (i *agentGrpcImpl) PublicShare(_ context.Context, req *agentGrpc.PublicShareRequest) (*agentGrpc.PublicShareReply, error) {
|
|
|
|
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'?")
|
|
|
|
}
|
|
|
|
|
2024-09-12 20:05:17 +02:00
|
|
|
shrCmd := []string{os.Args[0], "share", "public", "--agent", "-b", req.BackendMode}
|
2024-09-12 20:25:25 +02:00
|
|
|
shr := &share{
|
2024-09-13 20:39:30 +02:00
|
|
|
shareMode: sdk.PublicShareMode,
|
|
|
|
backendMode: sdk.BackendMode(req.BackendMode),
|
|
|
|
bootComplete: make(chan struct{}),
|
|
|
|
a: i.a,
|
2024-09-12 20:25:25 +02:00
|
|
|
}
|
2024-09-12 20:05:17 +02:00
|
|
|
|
|
|
|
for _, basicAuth := range req.BasicAuth {
|
|
|
|
shrCmd = append(shrCmd, "--basic-auth", basicAuth)
|
|
|
|
}
|
|
|
|
shr.basicAuth = req.BasicAuth
|
|
|
|
|
|
|
|
for _, frontendSelection := range req.FrontendSelection {
|
|
|
|
shrCmd = append(shrCmd, "--frontend", frontendSelection)
|
|
|
|
}
|
|
|
|
shr.frontendSelection = req.FrontendSelection
|
|
|
|
|
|
|
|
if req.Insecure {
|
|
|
|
shrCmd = append(shrCmd, "--insecure")
|
|
|
|
}
|
|
|
|
shr.insecure = req.Insecure
|
|
|
|
|
|
|
|
if req.OauthProvider != "" {
|
|
|
|
shrCmd = append(shrCmd, "--oauth-provider", req.OauthProvider)
|
|
|
|
}
|
|
|
|
shr.oauthProvider = req.OauthProvider
|
|
|
|
|
|
|
|
for _, pattern := range req.OauthEmailAddressPatterns {
|
|
|
|
shrCmd = append(shrCmd, "--oauth-email-address-patterns", pattern)
|
|
|
|
}
|
|
|
|
shr.oauthEmailAddressPatterns = req.OauthEmailAddressPatterns
|
|
|
|
|
|
|
|
if req.OauthCheckInterval != "" {
|
|
|
|
shrCmd = append(shrCmd, "--oauth-check-interval", req.OauthCheckInterval)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Closed {
|
|
|
|
shrCmd = append(shrCmd, "--closed")
|
|
|
|
}
|
|
|
|
shr.closed = req.Closed
|
|
|
|
|
|
|
|
for _, grant := range req.AccessGrants {
|
|
|
|
shrCmd = append(shrCmd, "--access-grant", grant)
|
|
|
|
}
|
|
|
|
shr.accessGrants = req.AccessGrants
|
|
|
|
|
|
|
|
shrCmd = append(shrCmd, req.Target)
|
|
|
|
shr.target = req.Target
|
|
|
|
|
|
|
|
logrus.Infof("executing '%v'", shrCmd)
|
|
|
|
|
|
|
|
shr.process, err = proctree.StartChild(shr.tail, shrCmd...)
|
|
|
|
if err != nil {
|
2024-09-16 20:21:23 +02:00
|
|
|
return nil, err
|
2024-09-12 20:05:17 +02:00
|
|
|
}
|
|
|
|
|
2024-09-13 20:39:30 +02:00
|
|
|
go shr.monitor()
|
|
|
|
<-shr.bootComplete
|
2024-09-12 20:05:17 +02:00
|
|
|
|
2024-09-13 20:39:30 +02:00
|
|
|
if shr.bootErr == nil {
|
|
|
|
i.a.inShares <- shr
|
|
|
|
return &agentGrpc.PublicShareReply{
|
|
|
|
Token: shr.token,
|
|
|
|
FrontendEndpoints: shr.frontendEndpoints,
|
|
|
|
}, nil
|
|
|
|
}
|
2024-09-16 20:21:23 +02:00
|
|
|
|
|
|
|
return nil, shr.bootErr
|
2024-08-26 20:18:17 +02:00
|
|
|
}
|