-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.go
57 lines (48 loc) · 1.21 KB
/
parser.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
package main
import (
"encoding/json"
"fmt"
"go.uber.org/zap"
)
// ContentParser defines secret content parser behaviors
type ContentParser interface {
Parse(*SecretData) []*SecretData
}
// JSONContenParser represents a JSON parser
type JSONContentParser struct {
Tmpl string
}
// Parse lists each json entry as a secret
//
// Consider a secret called myscret with the content below
//
// {
// "user": "root",
// "password": "s3cr3t",
// "host" : "127.0.0.1:5432",
// }
//
// The following secrets will be returned
//
// myscret_user: root
// mysecret_password: s3cr3t
// mysecret_host: 127.0.0.1:5432
func (j *JSONContentParser) Parse(s *SecretData) []*SecretData {
m := map[string]interface{}{}
if err := json.Unmarshal([]byte(s.Data), &m); err != nil {
logger.Warn("invalid json", zap.String("name", s.Name))
}
var secrets []*SecretData
for k, v := range m {
secrets = append(secrets, &SecretData{Name: s.Name, Path: s.Path, Data: s.Data, ContentKey: k, ContentValue: fmt.Sprintf("%v", v)})
}
return secrets
}
// NoParser represents no parser
type NoParser struct {
Tmpl string
}
// Parse puts the secret data in a slice
func (n *NoParser) Parse(s *SecretData) []*SecretData {
return []*SecretData{s}
}