-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.go
79 lines (67 loc) · 1.62 KB
/
decrypt.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
package eyaml
import (
"fmt"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/parser"
"github.com/goccy/go-yaml/printer"
"io/ioutil"
"strings"
)
func Decrypt(filepath string, searchPath string) {
if !fileExists(filepath) {
fmt.Println("file doesn't exist")
return
}
data, err := ioutil.ReadFile(filepath)
if err != nil {
fmt.Println(err)
return
}
astFile, err := parser.ParseBytes(data, 1)
if err != nil {
fmt.Println(err)
return
}
var metadata eyamlMetadata
metadata, err = ParseEyamlMetadata(astFile.Docs[0])
if err != nil {
fmt.Println("file is missing eyaml metadata")
return
}
kp, err := getKeypair(metadata.PublicKey)
if err != nil {
fmt.Println(err)
return
}
decrypter := kp.Decrypter()
if searchPath == "" {
for _, nodeTree := range astFile.Docs[1:] {
for _, literal:= range DfsSequence(nodeTree.Body).List() {
modify(literal.node, decrypter.Decrypt)
}
}
yamlPrinter := printer.Printer{}
for _, nodeTree := range astFile.Docs[1:] {
rawDecryptedData := yamlPrinter.PrintNode(nodeTree)
fmt.Print(string(rawDecryptedData))
}
} else {
for _, nodeTree := range astFile.Docs[1:] {
for _, literal := range DfsSequence(nodeTree.Body).List() {
if strings.Contains(literal.path, searchPath) {
stringValue := ""
switch typedNode := literal.node.(type) {
case *ast.LiteralNode:
modifyLiteral(typedNode, decrypter.Decrypt)
stringValue = typedNode.String()
case *ast.StringNode:
modifyString(typedNode, decrypter.Decrypt)
stringValue = typedNode.String()
}
fmt.Println(stringValue)
return
}
}
}
}
}