37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sign_utils.go 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
kinarashah 提交于 2019-02-08 14:10 . validate credID
package httpproxy
import (
"fmt"
"github.com/rancher/rancher/pkg/namespace"
"github.com/rancher/types/apis/core/v1"
"strings"
)
func getAuthData(auth string, secrets v1.SecretInterface, fields []string) (map[string]string, map[string]string, error) {
data := getRequestParams(auth)
if !requiredFieldsExist(data, fields) {
return data, nil, fmt.Errorf("required fields %s not set", fields)
}
if data["credID"] == "" {
return data, nil, nil
}
secret, err := getCredential(data["credID"], secrets)
if err != nil {
return data, nil, err
}
return data, secret, nil
}
func getRequestParams(auth string) map[string]string {
params := map[string]string{}
if auth != "" {
splitAuth := strings.Split(auth, " ")
for _, term := range splitAuth[1:] {
splitTerm := strings.SplitN(term, "=", 2)
params[splitTerm[0]] = splitTerm[1]
}
}
return params
}
func requiredFieldsExist(data map[string]string, fields []string) bool {
for _, field := range fields {
if val, ok := data[field]; !ok || val == "" {
return false
}
}
return true
}
func getCredential(credentialID string, credentials v1.SecretInterface) (map[string]string, error) {
split := strings.SplitN(credentialID, ":", 2)
if len(split) != 2 || split[0] == "" || split[1] == "" {
return nil, fmt.Errorf("invalid credential id %s", credentialID)
}
cred, err := credentials.Controller().Lister().Get(namespace.GlobalNamespace, split[1])
if err != nil {
return nil, err
}
ans := map[string]string{}
for key, val := range cred.Data {
splitKeys := strings.Split(key, "-")
if len(splitKeys) == 2 && strings.HasSuffix(splitKeys[0], "Config") {
ans[splitKeys[1]] = string(val)
}
}
return ans, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.4-rc23

搜索帮助