gpt4 book ai didi

go - 在HashiCorp库中搜索值

转载 作者:行者123 更新时间:2023-12-01 22:33:29 25 4
gpt4 key购买 nike

有没有办法在Hashicorp Vault中搜索值?我正在尝试编写Golang代码以搜索和列出在Vault中显示值的所有位置。这类似于golang在目录上的walk函数。有人对此有好的方法吗?我当时在考虑使用并发性在Vault中搜索值。谢谢
下面是我提出的代码示例。我正在研究如何通过使用并发来使其更快。有没有一种方法可以同时遍历目录?

func walkDir(client *api.Client, path string) {
var value *api.Secret
var err error
if path != "" {
value, err = client.Logical().List(path)
} else {
path = vault_path
value, err = client.Logical().List(path)
}
if err != nil {
fmt.Println(err)
}
var datamap map[string]interface{}
datamap = value.Data
data := datamap["keys"].([]interface{})
for _, item := range data {
itemString := item.(string)
if strings.HasSuffix(itemString, "/") {
walkDir(client, path+itemString)
} else {
//its a secret
data := read(client, path+itemString)

if *searchKey!="" && searchForKey(data,*searchKey){
fmt.Println(path + itemString)
}
if *searchValue!="" && searchForValue(data,*searchValue){
fmt.Println(path + itemString)
}
}
}
}

func read(client *api.Client, path string) map[string]interface{} {
value, err := client.Logical().Read(path)
if err != nil {
fmt.Println(err)
}
values := value.Data
return values
}

func searchForValue(mapp map[string]interface{}, searchValue string) bool {
for _, value := range mapp {
if searchValue == value {
return true
}
}
return false
}

func searchForKey(mapp map[string]interface{}, searchKey string) bool {
for key := range mapp {
if searchKey == key {
return true
}
}
return false
}

最佳答案

您可以在保险柜中LIST“目录”(我假设您只是在看kv引擎)。因此,将其像常规文件系统一样对待:从根目录开始,列出条目,检查每个条目的内容以获取该值,然后遍历每个条目并列出其内容,依此类推。
https://www.vaultproject.io/api-docs/secret/kv/kv-v1#list-secrets

关于go - 在HashiCorp库中搜索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63620403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com