0 Star 0 Fork 0

蒋佳李/vault

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
registry.go 2.12 KB
一键复制 编辑 原始数据 按行查看 历史
蒋佳李 提交于 2023-03-09 17:11 +08:00 . 删除transit
package registry
import (
"gitee.com/jiangjiali/vault/sdk/helper/consts"
"gitee.com/jiangjiali/vault/sdk/logical"
credUserpass "gitee.com/jiangjiali/vault/builtin/credential/userpass"
logicalTotp "gitee.com/jiangjiali/vault/builtin/logical/totp"
logicalKv "gitee.com/jiangjiali/vault/plugins/vault-plugin-secrets-kv"
)
// Registry is inherently thread-safe because it's immutable.
// Thus, rather than creating multiple instances of it, we only need one.
var Registry = newRegistry()
// BuiltinFactory is the func signature that should be returned by
// the plugin's New() func.
type BuiltinFactory func() (interface{}, error)
func newRegistry() *registry {
return &registry{
credentialBackends: map[string]logical.Factory{
"userpass": credUserpass.Factory,
},
logicalBackends: map[string]logical.Factory{
"kv": logicalKv.Factory,
"totp": logicalTotp.Factory,
},
}
}
type registry struct {
credentialBackends map[string]logical.Factory
logicalBackends map[string]logical.Factory
}
// Get returns the BuiltinFactory func for a particular backend plugin
// from the plugins map.
func (r *registry) Get(name string, pluginType consts.PluginType) (func() (interface{}, error), bool) {
switch pluginType {
case consts.PluginTypeCredential:
f, ok := r.credentialBackends[name]
return toFunc(f), ok
case consts.PluginTypeSecrets:
f, ok := r.logicalBackends[name]
return toFunc(f), ok
default:
return nil, false
}
}
// Keys returns the list of plugin names that are considered builtin plugins.
func (r *registry) Keys(pluginType consts.PluginType) []string {
var keys []string
switch pluginType {
case consts.PluginTypeCredential:
for key := range r.credentialBackends {
keys = append(keys, key)
}
case consts.PluginTypeSecrets:
for key := range r.logicalBackends {
keys = append(keys, key)
}
}
return keys
}
func (r *registry) Contains(name string, pluginType consts.PluginType) bool {
for _, key := range r.Keys(pluginType) {
if key == name {
return true
}
}
return false
}
func toFunc(ifc interface{}) func() (interface{}, error) {
return func() (interface{}, error) {
return ifc, nil
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jiangjiali/vault.git
git@gitee.com:jiangjiali/vault.git
jiangjiali
vault
vault
v1.1.11

搜索帮助