-
Notifications
You must be signed in to change notification settings - Fork 6
/
policies.go
67 lines (58 loc) · 1.84 KB
/
policies.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
package initlib
import (
"os"
"path/filepath"
eUtils "github.com/trimble-oss/tierceron/pkg/utils"
sys "github.com/trimble-oss/tierceron/pkg/vaulthelper/system"
)
// UploadPolicies accepts a file directory and vault object to upload policies to. Logs to pased logger
func UploadPolicies(config *eUtils.DriverConfig, dir string, v *sys.Vault, noPermissions bool) error {
config.Log.SetPrefix("[POLICY]")
config.Log.Printf("Writing policies from %s\n", dir)
files, err := os.ReadDir(dir)
if err != nil {
return err
}
eUtils.LogErrorObject(config, err, true)
for _, file := range files {
// Extract and truncate file name
filename := file.Name()
ext := filepath.Ext(filename)
filename = filename[0 : len(filename)-len(ext)]
if ext == ".hcl" { // Write policy to vault
config.Log.Printf("\tFound policy file: %s\n", file.Name())
if noPermissions {
err = v.CreateEmptyPolicy(filename)
} else {
err = v.CreatePolicyFromFile(filename, dir+"/"+file.Name())
}
eUtils.LogErrorObject(config, err, false)
if err != nil {
return err
}
}
}
return nil
}
// GetExistsPolicies accepts a file directory and vault object to check policies for. Logs to pased logger
func GetExistsPolicies(config *eUtils.DriverConfig, dir string, v *sys.Vault) (bool, error) {
config.Log.SetPrefix("[POLICY]")
config.Log.Printf("Checking exists token policies from %s\n", dir)
files, err := os.ReadDir(dir)
if err != nil {
return false, nil
}
allExists := false
eUtils.LogErrorObject(config, err, true)
for _, file := range files {
// Extract and truncate file name
config.Log.Printf("\tFound token policy file: %s\n", file.Name())
exists, err := v.GetExistsPolicyFromFileName(file.Name())
eUtils.LogErrorObject(config, err, false)
if err != nil {
return false, err
}
allExists = allExists || exists
}
return allExists, nil
}