/
parser.go
180 lines (155 loc) · 3.55 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//go:build go1.17
// +build go1.17
package config
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"ermites.io/pkisauce/policy"
)
type Parser struct {
*BasepkscListener
entries int // let's count the number of entries found.
key []byte
config Config
}
func (pr *Parser) Config() Config {
//fmt.Printf("DANS LA CONFIG: %v\n", psr.config)
return pr.config
}
//func NewParser(key []byte) *Parser {
func NewParser() *Parser {
// random key for the hmac'ing of rules.
return &Parser{
key: nil, // might disappear in the end..
config: NewConfig(),
}
}
var (
// aliases of the same func
parseHName = parseTermNodeText
parsePath = parseTermNodeText
wc = "*"
)
func parseTermNodeText(hn antlr.TerminalNode) string {
quoted := hn.GetSymbol().GetText()
str := quoted[1 : len(quoted)-1]
return str
}
// IGen_typeContext
func (pr *Parser) parseGenType(igt IGen_typeContext) (dtype int, err error) {
gt, ok := igt.(*Gen_typeContext)
if !ok {
err = fmt.Errorf("error type")
return
}
switch {
case gt.KWGOGRPC() != nil:
dtype = FlavorGoGRPC
case gt.KWGOTLS() != nil:
dtype = FlavorGoTLS
case gt.KWPEM() != nil:
dtype = FlavorPEM
default:
err = fmt.Errorf("unknown type")
}
return
}
func parseNameBlock(nbr IName_blockContext) (names []string, err error) {
nb, ok := nbr.(*Name_blockContext)
if !ok {
err = fmt.Errorf("name block type error")
return
}
nbhn := nb.HNAME()
nbany := nb.KWANY()
nbnlr := nb.Name_list() // IName_listContext
switch {
case nbhn != nil:
names = append(names, parseHName(nbhn))
case nbany != nil:
names = append(names, "*")
case nbnlr != nil:
nbnl, ok := nbnlr.(*Name_listContext)
if !ok {
err = fmt.Errorf("name list type error")
return
}
// IName_elt_listContext
nbnlelr := nbnl.Name_elt_list()
nbnlel, ok := nbnlelr.(*Name_elt_listContext)
if !ok {
err = fmt.Errorf("name elt list type error")
return
}
nl := nbnlel.AllHNAME()
for _, n := range nl {
names = append(names, parseHName(n))
}
}
//fmt.Printf("nbhn: %v nbany: %v nbnl: %v\n", nbhn, nbany, nbnl)
return
}
//func parsePassBlock(pbr IPass_blockContext) (rules []*Rule, err error) {
func (pr *Parser) parsePassBlock(pbr IPass_blockContext) (p *policy.Policy, debug bool, err error) {
//var rules []*Rule
pb, ok := pbr.(*Pass_blockContext)
if !ok {
err = fmt.Errorf("error type")
return
}
p = policy.New(nil, "*")
// []IPass_stmtContext
pbrules := pb.AllPass_stmt()
for _, r := range pbrules {
// one rul each.
//fmt.Printf("[%d] \n", i)
pbrule := r.(*Pass_stmtContext)
// it returns []IName_blockContext
pbrunb := pbrule.AllName_block()
// nb0: srcs nb1: svcs nb2: rpcs
switch len(pbrunb) {
case 1:
// we only have src names...
srcs, perr := parseNameBlock(pbrunb[0])
if perr != nil {
err = perr
return
}
//fmt.Printf("rule: %p srcs: %p\n", rule, srcs)
for _, src := range srcs {
p.Entry(src, wc, wc)
}
case 3: // srcs, svcs, rpcs
// we have src names...
srcs, perr := parseNameBlock(pbrunb[0])
if perr != nil {
err = perr
return
}
// we have svc names...
svcs, perr := parseNameBlock(pbrunb[1])
if perr != nil {
err = perr
return
}
// we have svc names...
rpcs, perr := parseNameBlock(pbrunb[2])
if perr != nil {
err = perr
return
}
for _, src := range srcs {
for _, svc := range svcs {
for _, rpc := range rpcs {
p.Entry(src, svc, rpc)
}
}
}
default:
err = fmt.Errorf("unknown parse error")
return
}
}
// create the associated policy
return
}