-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
102 lines (81 loc) · 2.37 KB
/
file.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
// ConfigAuth : auth configuration
type ConfigAuth struct {
Address string `json:"address"`
Credentials map[string]string `json:"credentials"`
Method string `json:"method"`
}
// ConfigAuthAppRole : approle auth method configuration
type ConfigAuthAppRole struct {
Name string `json:"name"`
Options map[string]interface{} `json:"options"`
Output *string `json:"output"`
Path string `json:"path"`
}
// ConfigAuthMethod : auth method configuration
type ConfigAuthMethod struct {
Options map[string]string `json:"options"`
Path string `json:"path"`
}
// ConfigSecret : secrets configuration
type ConfigSecret struct {
Engine string `json:"engine"`
Mount string `json:"mount"`
Options map[string]string `json:"options"`
Overwrite bool `json:"overwrite"`
Paths []string `json:"paths"`
}
// Config : configuration for vaultsync
type Config struct {
SourceAuth ConfigAuth `json:"source_auth"`
SourceSecrets []ConfigSecret `json:"source_secrets"`
SourcePoliciesPath string `json:"source_policies_path"`
TargetAuth ConfigAuth `json:"target_auth"`
TargetAuthAppRoles []ConfigAuthAppRole `json:"target_auth_approles"`
TargetAuthMethods []ConfigAuthMethod `json:"target_auth_methods"`
}
// FilenameWithoutExt : removes extension from filename
func FilenameWithoutExt(name string) string {
return strings.TrimSuffix(name, path.Ext(name))
}
// GetConfig : gets configuration from path
func GetConfig(path string) (*Config, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var config Config
if len(data) > 0 {
jsonErr := json.Unmarshal(data, &config)
if jsonErr != nil {
return nil, jsonErr
}
}
return &config, nil
}
// GetPolicies : gets all policies from path
func GetPolicies(path string) ([]os.FileInfo, error) {
policies := make([]os.FileInfo, 0)
dir, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, f := range dir {
name := f.Name()
file := fmt.Sprintf("%s/%s", path, name)
extension := filepath.Ext(file)
if extension == ".hcl" {
policies = append(policies, f)
}
}
return policies, nil
}