37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
azure_actions.go 2.92 KB
一键复制 编辑 原始数据 按行查看 历史
Craig Jellick 提交于 2018-06-14 14:25 . Refactor token code
package azure
import (
"encoding/json"
"fmt"
"net/http"
"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/auth/providers/common"
"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"
"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,
}
//Call provider
userPrincipal, groupPrincipals, providerInfo, 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, providerInfo, 0, "Token via Azure Configuration", request)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.3-rc1

搜索帮助

0d507c66 1850385 C8b1a773 1850385