-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeycloak.go
74 lines (64 loc) · 1.48 KB
/
keycloak.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
package keycloak
import (
"fmt"
"io/ioutil"
"path/filepath"
)
// The file format of an encrypted file.
type FileFormat int
const (
// JSON -
JSON FileFormat = iota
// YAML -
YAML
)
// Store defines an encrypted data file.
// Files are assumed to be tree-structured (or at least eflat arrays).
// This interface allows users to do basic operations on these secret files.
type Store interface {
// EncryptSubtree -
EncryptSubtree(string, ...string) error
// DecryptSubtree -
DecryptSubtree(string, ...string) error
// Subtree -
Subtree(...string) (map[string]interface{}, error)
// ToFile -
ToFile(string) error
}
// GetStoreForFile -
func GetStoreForFile(path string) (Store, error) {
frmt, err := getFormat(path)
if err != nil {
return nil, err
}
return GetStoreWithFormat(path, frmt)
}
// GetStoreWithFormat -
func GetStoreWithFormat(path string, frmt FileFormat) (Store, error) {
bites, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return GetStoreFromBytes(bites, frmt)
}
// GetStoreFromBytes -
func GetStoreFromBytes(bites []byte, frmt FileFormat) (Store, error) {
switch frmt {
case JSON:
return newJSONStore(bites)
case YAML:
return newYAMLStore(bites)
default:
return nil, fmt.Errorf("invalid format")
}
}
func getFormat(path string) (FileFormat, error) {
switch filepath.Ext(path) {
case ".json":
return JSON, nil
case ".yaml", ".yml":
return YAML, nil
default:
return JSON, fmt.Errorf("unsupported format: %s", filepath.Ext(path))
}
}