代码拉取完成,页面将自动刷新
package azure
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/pkg/errors"
"github.com/rancher/norman/api/handler"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/api/store/auth"
"github.com/rancher/rancher/pkg/auth/providers/common"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
managementschema "github.com/rancher/types/apis/management.cattle.io/v3/schema"
"github.com/rancher/types/apis/management.cattle.io/v3public"
client "github.com/rancher/types/client/management/v3"
)
func (ap *azureProvider) formatter(apiContext *types.APIContext, resource *types.RawResource) {
common.AddCommonActions(apiContext, resource)
resource.AddAction(apiContext, "configureTest")
resource.AddAction(apiContext, "testAndApply")
}
func (ap *azureProvider) actionHandler(actionName string, action *types.Action, request *types.APIContext) error {
handled, err := common.HandleCommonAction(actionName, action, request, Name, ap.authConfigs)
if err != nil {
return err
}
if handled {
return nil
}
if actionName == "configureTest" {
return ap.configureTest(actionName, action, request)
} else if actionName == "testAndApply" {
return ap.testAndApply(actionName, action, request)
}
return httperror.NewAPIError(httperror.ActionNotAvailable, "")
}
func (ap *azureProvider) configureTest(actionName string, action *types.Action, request *types.APIContext) error {
// Verify the body has all required fields
input, err := handler.ParseAndValidateActionBody(request, request.Schemas.Schema(&managementschema.Version,
client.AzureADConfigType))
if err != nil {
return err
}
data := map[string]interface{}{
"redirectUrl": formAzureRedirectURL(input),
"type": "azureADConfigTestOutput",
}
request.WriteResponse(http.StatusOK, data)
return nil
}
func (ap *azureProvider) testAndApply(actionName string, action *types.Action, request *types.APIContext) error {
azureADConfigApplyInput := &v3.AzureADConfigApplyInput{}
if err := json.NewDecoder(request.Request.Body).Decode(azureADConfigApplyInput); err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent,
fmt.Sprintf("Failed to parse body: %v", err))
}
azureADConfig := azureADConfigApplyInput.Config
azureLogin := &v3public.AzureADLogin{
Code: azureADConfigApplyInput.Code,
}
if azureADConfig.ApplicationSecret != "" {
value, err := common.ReadFromSecret(ap.secrets, azureADConfig.ApplicationSecret,
strings.ToLower(auth.TypeToField[client.AzureADConfigType]))
if err != nil {
return err
}
azureADConfig.ApplicationSecret = value
}
//Call provider
userPrincipal, groupPrincipals, providerToken, err := ap.loginUser(azureLogin, &azureADConfig, true)
if err != nil {
if httperror.IsAPIError(err) {
return err
}
return errors.Wrap(err, "server error while authenticating")
}
user, err := ap.userMGR.SetPrincipalOnCurrentUser(request, userPrincipal)
if err != nil {
return err
}
err = ap.saveAzureConfigK8s(&azureADConfig)
if err != nil {
return httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("Failed to save azure config: %v", err))
}
return ap.tokenMGR.CreateTokenAndSetCookie(user.Name, userPrincipal, groupPrincipals, providerToken, 0, "Token via Azure Configuration", request)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。