/
sds_server_validation_parser.go
57 lines (43 loc) · 1.21 KB
/
sds_server_validation_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 parser
import (
"errors"
"fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
)
type SdsServerValidation struct {
Resources []ValidationResource `yaml:"resources,omitempty"`
}
type ValidationResource struct {
ValidationContext ValidationContext `yaml:"validation_context,omitempty"`
}
type ValidationContext struct {
TrustedCA TrustedCA `yaml:"trusted_ca,omitempty"`
}
type TrustedCA struct {
InlineString string `yaml:"inline_string,omitempty"`
}
type SdsServerValidationParser struct {
file string
}
func NewSdsServerValidationParser(file string) SdsServerValidationParser {
return SdsServerValidationParser{
file: file,
}
}
func (p SdsServerValidationParser) GetCACert() (string, error) {
contents, err := ioutil.ReadFile(p.file)
if err != nil {
return "", fmt.Errorf("Failed to read sds server validation context: %s", err)
}
auth := SdsServerValidation{}
err = yaml.Unmarshal(contents, &auth)
if err != nil {
return "", fmt.Errorf("Failed to unmarshal sds server validation context: %s", err)
}
if len(auth.Resources) < 1 {
return "", errors.New("resources section not found in sds-server-validation-context.yaml")
}
ca := auth.Resources[0].ValidationContext.TrustedCA.InlineString
return ca, nil
}