/
vault.go
141 lines (128 loc) · 4.74 KB
/
vault.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package test
import (
"fmt"
vault "github.com/hashicorp/vault/api"
"github.com/mitchellh/mapstructure"
mconfig "github.com/nebtex/menshend/pkg/config"
mutils "github.com/nebtex/menshend/pkg/utils"
"strings"
"github.com/fatih/structs"
"github.com/nebtex/menshend/pkg/strategy"
"github.com/nebtex/menshend/pkg/resolvers"
)
func CleanVault() {
type ListResult struct {
Keys []string
}
vc, err := vault.NewClient(vault.DefaultConfig())
mutils.CheckPanic(err)
vc.SetToken("myroot")
key := fmt.Sprintf("%s/roles", mconfig.Config.VaultPath)
secret, err := vc.Logical().List(key)
mutils.CheckPanic(err)
if (secret == nil) || (secret.Data == nil) {
return
}
rr := &ListResult{}
err = mapstructure.Decode(secret.Data, rr)
mutils.CheckPanic(err)
roleList := rr.Keys
for _, role := range roleList {
if !strings.HasSuffix(role, "/") {
continue
}
rKey := fmt.Sprintf("%s/roles/%s", mconfig.Config.VaultPath, role)
rSecret, err := vc.Logical().List(rKey)
if err != nil {
continue
}
sr := &ListResult{}
err = mapstructure.Decode(rSecret.Data, sr)
mutils.CheckPanic(err)
serviceList := sr.Keys
for _, service := range serviceList {
sKey := fmt.Sprintf("%s/roles/%s/%s", mconfig.Config.VaultPath, role, service)
_, err := vc.Logical().Delete(sKey)
mutils.CheckPanic(err)
}
}
}
type Role map[string]*Service
type TestServiceStrategy struct {
Proxy *strategy.Proxy `json:"proxy"`
PortForward *strategy.PortForward `json:"portForward"`
Redirect *strategy.Redirect `json:"redirect"`
}
type TestServiceMetadata struct {
ID string `json:"id"`
RoleID string `json:"roleId"`
SubDomain string `json:"subDomain"`
Name string `json:"name"`
}
type TestServiceCache struct {
// time to live seconds
TTL int `json:"ttl"`
Active bool `json:"active"`
}
type TestServiceResolver struct {
Yaml *resolvers.YAMLResolver `json:"yaml"`
Lua *resolvers.LuaResolver `json:"lua"`
Cache *TestServiceCache `json:"cache"`
}
//Service service definition struct
type Service struct {
Meta *TestServiceMetadata `json:"meta"`
Logo string `json:"logo"`
ShortDescription string `json:"shortDescription"`
LongDescription string
ProxyCode string
ProxyLanguage string
ImpersonateWithinRole bool
Proxy bool
IsActive bool
SecretPaths []string
CSRF bool
Strategy *TestServiceStrategy `json:"strategy"`
Resolver *TestServiceResolver `json:"resolver"`
}
func PopulateVault() {
vc, err := vault.NewClient(vault.DefaultConfig())
vc.SetToken("myroot")
mutils.CheckPanic(err)
roles := map[string]Role{
"ml-team": map[string]*Service{
"consul.":{IsActive:true,
Meta: &TestServiceMetadata{SubDomain: "consul."},
ImpersonateWithinRole: true,
CSRF: true,
Strategy: &TestServiceStrategy{Proxy:&strategy.Proxy{}},
Resolver: &TestServiceResolver{Lua: &resolvers.LuaResolver{Content: `function getBackend (tokenInfo, request)
return "http://localhost:5454", {}
end`}}, },
"consul-2.":{IsActive:true,
Meta: &TestServiceMetadata{SubDomain: "consul-2."},
ImpersonateWithinRole: true,
CSRF: false,
Strategy: &TestServiceStrategy{Proxy:&strategy.Proxy{}},
Resolver: &TestServiceResolver{Lua: &resolvers.LuaResolver{Content: `function getBackend (tokenInfo, request)
return "http://localhost:5454", {}
end`}}, },
"gitlab.":{
Meta: &TestServiceMetadata{SubDomain: "consul-2."},
IsActive:false, SecretPaths:[]string{"secret/gitlab/password", mconfig.Config.VaultPath + "/roles/ml-team/gitlab."},
},
"postgres.":{},
"redis.":{IsActive:true, Meta: &TestServiceMetadata{SubDomain: "redis."}, ShortDescription: "redisdb"}, },
"admin":map[string]*Service{
"kubernetes":{IsActive:true},
"vault.":{},
"redis.":{IsActive:true, Meta: &TestServiceMetadata{SubDomain: "redis."}, ShortDescription: "redisdb"},
}}
for role, services := range roles {
for service, val := range services {
key := fmt.Sprintf("%s/roles/%s/%s", mconfig.Config.VaultPath, role, service)
_, err := vc.Logical().Write(key, structs.Map(val))
mutils.CheckPanic(err)
}
}
}