-
Notifications
You must be signed in to change notification settings - Fork 162
/
parser.go
141 lines (118 loc) · 4.36 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
package manifest
import (
"encoding/pem"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
biproperty "github.com/cloudfoundry/bosh-utils/property"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
"github.com/cppforlife/go-patch/patch"
"gopkg.in/yaml.v2"
biutil "github.com/cloudfoundry/bosh-cli/v7/common/util"
boshtpl "github.com/cloudfoundry/bosh-cli/v7/director/template"
birelsetmanifest "github.com/cloudfoundry/bosh-cli/v7/release/set/manifest"
)
type Parser interface {
Parse(string, boshtpl.Variables, patch.Op, birelsetmanifest.Manifest) (Manifest, error)
}
type parser struct {
fs boshsys.FileSystem
uuidGenerator boshuuid.Generator
logger boshlog.Logger
logTag string
validator Validator
}
type manifest struct {
Name string
CloudProvider installation `yaml:"cloud_provider"`
}
type installation struct {
Template template
Properties map[interface{}]interface{}
SSHTunnel SSHTunnel `yaml:"ssh_tunnel"`
Mbus string
Cert Certificate
}
func (i installation) HasSSHTunnel() bool {
return i.SSHTunnel != SSHTunnel{}
}
type template struct {
Name string
Release string
}
func NewParser(fs boshsys.FileSystem, uuidGenerator boshuuid.Generator, logger boshlog.Logger, validator Validator) Parser {
return &parser{
fs: fs,
uuidGenerator: uuidGenerator,
logger: logger,
logTag: "deploymentParser",
validator: validator,
}
}
func (p *parser) Parse(path string, vars boshtpl.Variables, op patch.Op, releaseSetManifest birelsetmanifest.Manifest) (Manifest, error) {
contents, err := p.fs.ReadFile(path)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Reading file %s", path)
}
tpl := boshtpl.NewTemplate(contents)
bytes, err := tpl.Evaluate(vars, op, boshtpl.EvaluateOpts{ExpectAllKeys: true})
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Evaluating manifest")
}
comboManifest := manifest{}
err = yaml.Unmarshal(bytes, &comboManifest)
if err != nil {
return Manifest{}, bosherr.WrapError(err, "Unmarshalling installation manifest")
}
p.logger.Debug(p.logTag, "Parsed installation manifest: %#v", comboManifest)
if comboManifest.CloudProvider.SSHTunnel.PrivateKey != "" {
if p.lookForPrivateSshHeader(comboManifest.CloudProvider.SSHTunnel.PrivateKey) {
pkey, _ := pem.Decode([]byte(comboManifest.CloudProvider.SSHTunnel.PrivateKey))
if pkey == nil {
return Manifest{}, bosherr.Error("Invalid private key for ssh tunnel")
}
} else if strings.HasPrefix(comboManifest.CloudProvider.SSHTunnel.PrivateKey, "----") {
return Manifest{}, bosherr.Error("Unsupported private key format for ssh tunnel")
} else {
absolutePath, err := biutil.AbsolutifyPath(path, comboManifest.CloudProvider.SSHTunnel.PrivateKey, p.fs)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Expanding private_key path")
}
keyContents, err := p.fs.ReadFile(absolutePath)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Reading private key from %s", absolutePath)
}
comboManifest.CloudProvider.SSHTunnel.PrivateKey = string(keyContents)
}
}
if comboManifest.CloudProvider.Cert.CA != "" {
pkey, _ := pem.Decode([]byte(comboManifest.CloudProvider.Cert.CA))
if pkey == nil {
return Manifest{}, bosherr.Error("Invalid CA cert")
}
}
installationManifest := Manifest{
Name: comboManifest.Name,
Template: ReleaseJobRef{
Name: comboManifest.CloudProvider.Template.Name,
Release: comboManifest.CloudProvider.Template.Release,
},
Mbus: comboManifest.CloudProvider.Mbus,
Cert: comboManifest.CloudProvider.Cert,
}
properties, err := biproperty.BuildMap(comboManifest.CloudProvider.Properties)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Parsing cloud_provider manifest properties: %#v", comboManifest.CloudProvider.Properties)
}
installationManifest.Properties = properties
err = p.validator.Validate(installationManifest, releaseSetManifest)
if err != nil {
return Manifest{}, bosherr.WrapError(err, "Validating installation manifest")
}
return installationManifest, nil
}
func (p *parser) lookForPrivateSshHeader(key string) bool {
return strings.HasPrefix(key, "-----BEGIN RSA PRIVATE KEY-----") ||
strings.HasPrefix(key, "-----BEGIN OPENSSH PRIVATE KEY-----")
}