2023-07-10 19:44:58 +02:00
|
|
|
package sdk
|
|
|
|
|
|
|
|
import (
|
2023-07-17 22:45:20 +02:00
|
|
|
httptransport "github.com/go-openapi/runtime/client"
|
|
|
|
"github.com/openziti/zrok/environment/env_core"
|
|
|
|
"github.com/openziti/zrok/rest_client_zrok/share"
|
|
|
|
"github.com/openziti/zrok/rest_model_zrok"
|
2023-07-10 19:44:58 +02:00
|
|
|
"github.com/pkg/errors"
|
2023-07-17 22:45:20 +02:00
|
|
|
"strings"
|
2023-07-10 19:44:58 +02:00
|
|
|
)
|
|
|
|
|
2023-07-17 22:45:20 +02:00
|
|
|
func CreateShare(root env_core.Root, request *ShareRequest) (*Share, error) {
|
2023-07-26 22:25:52 +02:00
|
|
|
if !root.IsEnabled() {
|
|
|
|
return nil, errors.New("environment is not enabled; enable with 'zrok enable' first!")
|
|
|
|
}
|
|
|
|
|
2023-07-17 22:51:28 +02:00
|
|
|
var err error
|
|
|
|
var out *share.ShareParams
|
|
|
|
|
2023-07-10 19:44:58 +02:00
|
|
|
switch request.ShareMode {
|
|
|
|
case PrivateShareMode:
|
2023-07-17 22:51:28 +02:00
|
|
|
out = newPrivateShare(root, request)
|
2023-07-10 19:44:58 +02:00
|
|
|
case PublicShareMode:
|
2023-07-17 22:51:28 +02:00
|
|
|
out = newPublicShare(root, request)
|
2023-07-10 19:44:58 +02:00
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unknown share mode '%v'", request.ShareMode)
|
|
|
|
}
|
2023-10-25 20:16:18 +02:00
|
|
|
out.Body.Reserved = request.Reserved
|
2023-07-10 19:44:58 +02:00
|
|
|
|
2023-09-28 19:06:00 +02:00
|
|
|
if len(request.BasicAuth) > 0 {
|
2023-07-17 22:51:28 +02:00
|
|
|
out.Body.AuthScheme = string(Basic)
|
2023-09-28 19:06:00 +02:00
|
|
|
for _, basicAuthUser := range request.BasicAuth {
|
|
|
|
tokens := strings.Split(basicAuthUser, ":")
|
2023-07-17 22:45:20 +02:00
|
|
|
if len(tokens) == 2 {
|
2023-07-17 22:51:28 +02:00
|
|
|
out.Body.AuthUsers = append(out.Body.AuthUsers, &rest_model_zrok.AuthUser{Username: strings.TrimSpace(tokens[0]), Password: strings.TrimSpace(tokens[1])})
|
2023-07-17 22:45:20 +02:00
|
|
|
} else {
|
2023-09-28 19:06:00 +02:00
|
|
|
return nil, errors.Errorf("invalid username:password '%v'", basicAuthUser)
|
2023-07-17 22:45:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-17 22:51:28 +02:00
|
|
|
|
2023-09-05 18:50:41 +02:00
|
|
|
if request.OauthProvider != "" {
|
|
|
|
out.Body.AuthScheme = string(Oauth)
|
|
|
|
}
|
|
|
|
|
2023-07-17 22:45:20 +02:00
|
|
|
zrok, err := root.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error getting zrok client")
|
|
|
|
}
|
|
|
|
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
|
2023-07-17 22:51:28 +02:00
|
|
|
|
|
|
|
in, err := zrok.Share.Share(out, auth)
|
2023-07-17 22:45:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "unable to create share")
|
|
|
|
}
|
2023-07-17 22:51:28 +02:00
|
|
|
|
2023-08-28 19:50:23 +02:00
|
|
|
return &Share{
|
|
|
|
Token: in.Payload.ShrToken,
|
|
|
|
FrontendEndpoints: in.Payload.FrontendProxyEndpoints,
|
|
|
|
}, nil
|
2023-07-10 19:44:58 +02:00
|
|
|
}
|
|
|
|
|
2023-07-17 22:51:28 +02:00
|
|
|
func newPrivateShare(root env_core.Root, request *ShareRequest) *share.ShareParams {
|
2023-07-17 22:45:20 +02:00
|
|
|
req := share.NewShareParams()
|
|
|
|
req.Body = &rest_model_zrok.ShareRequest{
|
|
|
|
EnvZID: root.Environment().ZitiIdentity,
|
|
|
|
ShareMode: string(request.ShareMode),
|
|
|
|
BackendMode: string(request.BackendMode),
|
|
|
|
BackendProxyEndpoint: request.Target,
|
|
|
|
AuthScheme: string(None),
|
|
|
|
}
|
2023-07-17 22:51:28 +02:00
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPublicShare(root env_core.Root, request *ShareRequest) *share.ShareParams {
|
|
|
|
req := share.NewShareParams()
|
|
|
|
req.Body = &rest_model_zrok.ShareRequest{
|
2023-09-05 18:50:41 +02:00
|
|
|
EnvZID: root.Environment().ZitiIdentity,
|
|
|
|
ShareMode: string(request.ShareMode),
|
|
|
|
FrontendSelection: request.Frontends,
|
|
|
|
BackendMode: string(request.BackendMode),
|
|
|
|
BackendProxyEndpoint: request.Target,
|
|
|
|
AuthScheme: string(None),
|
|
|
|
OauthEmailDomains: request.OauthEmailDomains,
|
|
|
|
OauthProvider: request.OauthProvider,
|
|
|
|
OauthAuthorizationCheckInterval: request.OauthAuthorizationCheckInterval.String(),
|
2023-07-17 22:45:20 +02:00
|
|
|
}
|
2023-07-17 22:51:28 +02:00
|
|
|
return req
|
2023-07-17 22:45:20 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 21:30:46 +02:00
|
|
|
func DeleteShare(root env_core.Root, shr *Share) error {
|
2023-07-17 22:45:20 +02:00
|
|
|
req := share.NewUnshareParams()
|
|
|
|
req.Body = &rest_model_zrok.UnshareRequest{
|
|
|
|
EnvZID: root.Environment().ZitiIdentity,
|
2023-07-18 21:30:46 +02:00
|
|
|
ShrToken: shr.Token,
|
2023-07-17 22:45:20 +02:00
|
|
|
}
|
2023-07-17 22:51:28 +02:00
|
|
|
|
2023-07-17 22:45:20 +02:00
|
|
|
zrok, err := root.Client()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error getting zrok client")
|
|
|
|
}
|
|
|
|
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
|
2023-07-17 22:51:28 +02:00
|
|
|
|
2023-07-17 22:45:20 +02:00
|
|
|
_, err = zrok.Share.Unshare(req, auth)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error deleting share")
|
|
|
|
}
|
2023-07-18 21:30:46 +02:00
|
|
|
|
2023-07-17 22:45:20 +02:00
|
|
|
return nil
|
2023-07-10 19:44:58 +02:00
|
|
|
}
|