37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
activedirectory_actions.go 3.34 KB
一键复制 编辑 原始数据 按行查看 历史
carolyn 提交于 2018-08-22 10:51 . refactor ldap-based authentication
package activedirectory
import (
"fmt"
"github.com/mitchellh/mapstructure"
"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"
"github.com/sirupsen/logrus"
)
func (p *adProvider) formatter(apiContext *types.APIContext, resource *types.RawResource) {
common.AddCommonActions(apiContext, resource)
resource.AddAction(apiContext, "testAndApply")
}
func (p *adProvider) actionHandler(actionName string, action *types.Action, request *types.APIContext) error {
handled, err := common.HandleCommonAction(actionName, action, request, Name, p.authConfigs)
if err != nil {
return err
}
if handled {
return nil
}
if actionName == "testAndApply" {
return p.testAndApply(actionName, action, request)
}
return httperror.NewAPIError(httperror.ActionNotAvailable, "")
}
func (p *adProvider) testAndApply(actionName string, action *types.Action, request *types.APIContext) error {
input, err := handler.ParseAndValidateActionBody(request, request.Schemas.Schema(&managementschema.Version,
client.ActiveDirectoryTestAndApplyInputType))
if err != nil {
return err
}
configApplyInput := &v3.ActiveDirectoryTestAndApplyInput{}
if err := mapstructure.Decode(input, configApplyInput); err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent,
fmt.Sprintf("Failed to parse body: %v", err))
}
logrus.Debugf("configApplyInput %v", configApplyInput)
config := &configApplyInput.ActiveDirectoryConfig
login := &v3public.BasicLogin{
Username: configApplyInput.Username,
Password: configApplyInput.Password,
}
caPool, err := newCAPool(config.Certificate)
if err != nil {
return err
}
if len(config.Servers) < 1 {
return httperror.NewAPIError(httperror.InvalidBodyContent, "must supply a server")
}
if len(config.Servers) > 1 {
return httperror.NewAPIError(httperror.InvalidBodyContent, "multiple servers not yet supported")
}
userPrincipal, groupPrincipals, err := p.loginUser(login, config, caPool, true)
if err != nil {
return err
}
//if this works, save adConfig CR adding enabled flag
config.Enabled = configApplyInput.Enabled
err = p.saveActiveDirectoryConfig(config)
if err != nil {
return httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("Failed to save activedirectory config: %v", err))
}
user, err := p.userMGR.SetPrincipalOnCurrentUser(request, userPrincipal)
if err != nil {
return err
}
return p.tokenMGR.CreateTokenAndSetCookie(user.Name, userPrincipal, groupPrincipals, "", 0, "Token via AD Configuration", request)
}
func (p *adProvider) saveActiveDirectoryConfig(config *v3.ActiveDirectoryConfig) error {
storedConfig, _, err := p.getActiveDirectoryConfig()
if err != nil {
return err
}
config.APIVersion = "management.cattle.io/v3"
config.Kind = v3.AuthConfigGroupVersionKind.Kind
config.Type = client.ActiveDirectoryConfigType
config.ObjectMeta = storedConfig.ObjectMeta
logrus.Debugf("updating githubConfig")
_, err = p.authConfigs.ObjectClient().Update(config.ObjectMeta.Name, config)
if err != nil {
return err
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.2-rc5

搜索帮助

0d507c66 1850385 C8b1a773 1850385