2022-11-21 19:45:08 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2023-05-25 17:50:38 +02:00
|
|
|
"github.com/openziti/edge-api/rest_management_api_client"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/controller/zrokEdgeSdk"
|
|
|
|
"github.com/openziti/zrok/rest_server_zrok/operations/share"
|
2023-11-21 20:27:17 +01:00
|
|
|
"github.com/openziti/zrok/sdk/golang/sdk"
|
2022-11-21 19:45:08 +01:00
|
|
|
)
|
|
|
|
|
2022-11-22 21:31:02 +01:00
|
|
|
type publicResourceAllocator struct{}
|
2022-11-21 19:45:08 +01:00
|
|
|
|
2022-11-22 17:35:25 +01:00
|
|
|
func newPublicResourceAllocator() *publicResourceAllocator {
|
|
|
|
return &publicResourceAllocator{}
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:42:58 +01:00
|
|
|
func (a *publicResourceAllocator) allocate(envZId, shrToken string, frontendZIds, frontendTemplates []string, params share.ShareParams, edge *rest_management_api_client.ZitiEdgeManagement) (shrZId string, frontendEndpoints []string, err error) {
|
2023-09-25 20:04:05 +02:00
|
|
|
var authUsers []*sdk.AuthUserConfig
|
2022-11-22 21:31:02 +01:00
|
|
|
for _, authUser := range params.Body.AuthUsers {
|
2023-09-25 20:04:05 +02:00
|
|
|
authUsers = append(authUsers, &sdk.AuthUserConfig{Username: authUser.Username, Password: authUser.Password})
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
2023-09-25 20:15:19 +02:00
|
|
|
authScheme, err := sdk.ParseAuthScheme(params.Body.AuthScheme)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2023-09-25 20:04:05 +02:00
|
|
|
options := &zrokEdgeSdk.FrontendOptions{
|
2023-09-25 20:15:19 +02:00
|
|
|
AuthScheme: authScheme,
|
|
|
|
BasicAuthUsers: authUsers,
|
2023-09-25 20:06:14 +02:00
|
|
|
Oauth: &sdk.OauthConfig{
|
2023-09-25 20:04:05 +02:00
|
|
|
Provider: params.Body.OauthProvider,
|
|
|
|
EmailDomains: params.Body.OauthEmailDomains,
|
|
|
|
AuthorizationCheckInterval: params.Body.OauthAuthorizationCheckInterval,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cfgId, err := zrokEdgeSdk.CreateConfig(zrokProxyConfigId, envZId, shrToken, options, edge)
|
2022-11-21 19:45:08 +01:00
|
|
|
if err != nil {
|
2022-11-22 21:31:02 +01:00
|
|
|
return "", nil, err
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:42:58 +01:00
|
|
|
shrZId, err = zrokEdgeSdk.CreateShareService(envZId, shrToken, cfgId, edge)
|
2022-11-21 19:45:08 +01:00
|
|
|
if err != nil {
|
2022-11-22 21:31:02 +01:00
|
|
|
return "", nil, err
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:42:58 +01:00
|
|
|
if err := zrokEdgeSdk.CreateServicePolicyBind(envZId+"-"+shrZId+"-bind", shrZId, envZId, zrokEdgeSdk.ZrokShareTags(shrToken).SubTags, edge); err != nil {
|
2022-11-22 21:31:02 +01:00
|
|
|
return "", nil, err
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:42:58 +01:00
|
|
|
if err := zrokEdgeSdk.CreateServicePolicyDial(envZId+"-"+shrZId+"-dial", shrZId, frontendZIds, zrokEdgeSdk.ZrokShareTags(shrToken).SubTags, edge); err != nil {
|
2022-11-22 21:31:02 +01:00
|
|
|
return "", nil, err
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:42:58 +01:00
|
|
|
if err := zrokEdgeSdk.CreateShareServiceEdgeRouterPolicy(envZId, shrToken, shrZId, edge); err != nil {
|
2022-11-22 21:31:02 +01:00
|
|
|
return "", nil, err
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2022-12-06 17:48:59 +01:00
|
|
|
for _, frontendTemplate := range frontendTemplates {
|
2023-01-04 20:42:58 +01:00
|
|
|
frontendEndpoints = append(frontendEndpoints, proxyUrl(shrToken, frontendTemplate))
|
2022-12-06 17:48:59 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:42:58 +01:00
|
|
|
return shrZId, frontendEndpoints, nil
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|