-
Notifications
You must be signed in to change notification settings - Fork 5
/
format.go
190 lines (157 loc) · 4.49 KB
/
format.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
181
182
183
184
185
186
187
188
189
190
package conflate
import (
"crypto/x509"
"encoding/base64"
"encoding/xml"
"fmt"
"github.com/xeipuuv/gojsonschema"
"golang.org/x/net/html"
"regexp"
"strings"
)
func init() {
// annoyingly the format checker list is a global variable
gojsonschema.FormatCheckers.Add(newXMLFormatChecker("xml-template"))
gojsonschema.FormatCheckers.Add(newHTMLFormatChecker("html-template"))
gojsonschema.FormatCheckers.Add(newRegexFormatChecker("regex"))
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("pkcs1-private-key", pkcs1PrivateKey))
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("pkcs1-public-key", pkcs1PublicKey))
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("pkcs8-private-key", pkcs8PrivateKey))
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("pkcs8-public-key", pkcs8PublicKey))
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("x509-certificate", x509Certificate))
}
// ----------------
type formatErrors map[string]error
var formatErrs = formatErrors{}
func (errs formatErrors) clear() {
formatErrs = formatErrors{}
}
func (errs formatErrors) add(name interface{}, value interface{}, err error) {
errs[errs.key(name, value)] = err
}
func (errs formatErrors) get(name interface{}, value interface{}) error {
return errs[errs.key(name, value)]
}
func (errs formatErrors) key(name interface{}, value interface{}) string {
return fmt.Sprintf("%v#%v", name, value)
}
// ----------------
type xmlFormatChecker struct {
tags *regexp.Regexp
name string
}
func newXMLFormatChecker(name string) (string, gojsonschema.FormatChecker) {
return name, xmlFormatChecker{name: name, tags: regexp.MustCompile(`{{[^{}]*}}`)}
}
func (f xmlFormatChecker) IsFormat(input interface{}) bool {
var err error
if s, ok := input.(string); ok {
s = f.tags.ReplaceAllString(s, "")
if len(s) > 0 {
var v interface{}
err = xml.Unmarshal([]byte(s), &v)
err = wrapError(err, "Failed to parse xml")
}
} else {
err = makeError("The value is not a string")
}
if err != nil {
formatErrs.add(f.name, input, err)
return false
}
return true
}
// ----------------
type htmlFormatChecker struct {
tags *regexp.Regexp
name string
}
func newHTMLFormatChecker(name string) (string, gojsonschema.FormatChecker) {
return name, htmlFormatChecker{name: name, tags: regexp.MustCompile(`{{[^{}]*}}`)}
}
func (f htmlFormatChecker) IsFormat(input interface{}) bool {
var err error
if s, ok := input.(string); ok {
s = f.tags.ReplaceAllString(s, "")
_, err = html.Parse(strings.NewReader(s))
err = wrapError(err, "Failed to parse html")
} else {
err = makeError("The value is not a string")
}
if err != nil {
formatErrs.add(f.name, input, err)
return false
}
return true
}
// ----------------
type cryptoFormatChecker struct {
name string
cType cryptoType
}
type cryptoType int
const (
pkcs1PrivateKey cryptoType = 1 + iota
pkcs1PublicKey
pkcs8PrivateKey
pkcs8PublicKey
x509Certificate
)
func newCryptoFormatChecker(name string, cType cryptoType) (string, gojsonschema.FormatChecker) {
return name, cryptoFormatChecker{
name: name,
cType: cType,
}
}
func (f cryptoFormatChecker) IsFormat(input interface{}) bool {
var err error
if s, ok := input.(string); ok {
var data []byte
data, err = base64.StdEncoding.DecodeString(s)
if err == nil {
switch f.cType {
case pkcs1PrivateKey:
_, err = x509.ParsePKCS1PrivateKey(data)
case pkcs1PublicKey:
_, err = x509.ParsePKIXPublicKey(data)
case pkcs8PrivateKey:
_, err = x509.ParsePKCS8PrivateKey(data)
case pkcs8PublicKey:
_, err = x509.ParsePKIXPublicKey(data)
case x509Certificate:
_, err = x509.ParseCertificate(data)
default:
err = makeError(f.name + " called with unsupported type")
}
err = wrapError(err, "Failed to parse key")
} else {
err = wrapError(err, "Failed to base-64 decode the data")
}
} else {
err = makeError("The value is not a string")
}
if err != nil {
formatErrs.add(f.name, input, err)
return false
}
return true
}
// ----------------
type regexFormatChecker struct{ name string }
func newRegexFormatChecker(name string) (string, gojsonschema.FormatChecker) {
return name, regexFormatChecker{name: name}
}
func (f regexFormatChecker) IsFormat(input interface{}) bool {
var err error
if s, ok := input.(string); ok {
_, err = regexp.Compile(s)
err = wrapError(err, "Failed to parse regular expression")
} else {
err = makeError("The value is not a string")
}
if err != nil {
formatErrs.add(f.name, input, err)
return false
}
return true
}