Skip to content

Commit 4cca045

Browse files
author
Marshall Wu
committed
Add certification check
1 parent f2379ca commit 4cca045

6 files changed

Lines changed: 47 additions & 22 deletions

File tree

builder/xenserver/clone/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri
8080
}
8181

8282
func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
83-
c, err := xscommon.NewXenAPIClient(self.config.HostIp, self.config.Username, self.config.Password)
83+
c, err := xscommon.NewXenAPIClient(self.config.HostIp, self.config.Username, self.config.Password, self.config.SkipCertVerification, self.config.ServerCert)
8484

8585
if err != nil {
8686
return nil, err

builder/xenserver/common/client.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"errors"
66
"fmt"
77
"log"
8-
8+
"strings"
9+
910
"xenapi"
11+
1012
version "github.com/xenserver/packer-plugin-xenserver/version"
1113
)
1214

@@ -64,8 +66,6 @@ func GetDisks(c *Connection, vmRef xenapi.VMRef) (vdis []xenapi.VDIRef, err erro
6466
return vdis, nil
6567
}
6668

67-
68-
6969
func ConnectVdi(c *Connection, vmRef xenapi.VMRef, vdiRef xenapi.VDIRef, vbdType xenapi.VbdType) (err error) {
7070

7171
var mode xenapi.VbdMode
@@ -154,7 +154,6 @@ func DisconnectVdi(c *Connection, vmRef xenapi.VMRef, vdi xenapi.VDIRef) error {
154154
return fmt.Errorf("Could not find VBD for VDI '%s'", vdi)
155155
}
156156

157-
158157
func ConnectNetwork(c *Connection, networkRef xenapi.NetworkRef, vmRef xenapi.VMRef, device string) (*xenapi.VIFRef, error) {
159158
vif, err := xenapi.VIF.Create(c.session, xenapi.VIFRecord{
160159
Network: networkRef,
@@ -171,9 +170,6 @@ func ConnectNetwork(c *Connection, networkRef xenapi.NetworkRef, vmRef xenapi.VM
171170
return &vif, nil
172171
}
173172

174-
175-
176-
177173
// Expose a VDI using the Transfer VM
178174
// (Legacy VHD export)
179175

@@ -270,12 +266,10 @@ func Unexpose(c *Connection, vdiRef xenapi.VDIRef) (err error) {
270266
return nil
271267
}
272268

273-
274-
275269
// Client Initiator
276270
type Connection struct {
277271
session *xenapi.Session
278-
ref xenapi.SessionRef
272+
ref xenapi.SessionRef
279273
Host string
280274
Username string
281275
Password string
@@ -285,23 +279,36 @@ func (c Connection) GetSession() *xenapi.Session {
285279
return c.session
286280
}
287281

288-
func NewXenAPIClient(host, username, password string) (*Connection, error) {
282+
func NewXenAPIClient(host, username, password string, skipCertVerification bool, serverCert string) (*Connection, error) {
289283
log.Printf("XenServer Packer Plugin Version: %s", version.PluginVersion.FormattedVersion())
290284

291-
session := xenapi.NewSession(&xenapi.ClientOpts{
292-
URL: "http://" + host,
285+
if !strings.HasPrefix(host, "http") {
286+
host = "https://" + host
287+
}
288+
289+
opts := &xenapi.ClientOpts{
290+
URL: host,
293291
Headers: map[string]string{
294292
"User-Agent": fmt.Sprintf("XenServerPacker/%s", version.PluginVersion.FormattedVersion()),
295293
},
296-
})
297-
294+
}
295+
if !skipCertVerification {
296+
opts.SecureOpts = &xenapi.SecureOpts{
297+
ServerCert: serverCert,
298+
}
299+
}
300+
session := xenapi.NewSession(opts)
298301

299302
ref, err := session.LoginWithPassword(username, password, "1.0", "packer")
300303
if err != nil {
301304
return nil, err
302305
}
303306

304-
return &Connection{session,ref, host, username, password}, nil
307+
// Strip the scheme from host before storing — callers that build HTTP URLs
308+
// (step_upload_vdi, step_export, …) already prepend "https://".
309+
bareHost := strings.TrimPrefix(strings.TrimPrefix(host, "https://"), "http://")
310+
311+
return &Connection{session, ref, bareHost, username, password}, nil
305312
}
306313

307314
func (c *Connection) GetSessionRef() xenapi.SessionRef {

builder/xenserver/common/common_config.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package common
33
import (
44
"errors"
55
"fmt"
6+
"os"
67
"time"
78

89
"github.com/hashicorp/packer-plugin-sdk/common"
910
"github.com/hashicorp/packer-plugin-sdk/multistep"
1011
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
11-
12+
1213
"xenapi"
1314
)
1415

@@ -40,12 +41,15 @@ type CommonConfig struct {
4041
HTTPPortMin uint `mapstructure:"http_port_min"`
4142
HTTPPortMax uint `mapstructure:"http_port_max"`
4243

43-
SSHConfig `mapstructure:",squash"`
44-
44+
SSHConfig `mapstructure:",squash"`
45+
4546
OutputDir string `mapstructure:"output_directory"`
4647
Format string `mapstructure:"format"`
4748
KeepVM string `mapstructure:"keep_vm"`
4849
IPGetter string `mapstructure:"ip_getter"`
50+
51+
SkipCertVerification bool `mapstructure:"skip_cert_verification"`
52+
ServerCert string `mapstructure:"server_cert"`
4953
}
5054

5155
func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) []error {
@@ -113,6 +117,16 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig
113117
errs = append(errs, errors.New("remote_host must be specified."))
114118
}
115119

120+
if !c.SkipCertVerification && c.ServerCert == "" {
121+
errs = append(errs, errors.New("server_cert must be specified when skip_cert_verification is false."))
122+
}
123+
124+
for _, f := range c.CDFiles {
125+
if _, err := os.Stat(f); os.IsNotExist(err) {
126+
errs = append(errs, fmt.Errorf("cd_files: '%s' does not exist", f))
127+
}
128+
}
129+
116130
if c.HostPortMin > c.HostPortMax {
117131
errs = append(errs, errors.New("the host min port must be less than the max"))
118132
}

builder/xenserver/common/config.hcl2spec.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/xenserver/iso/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri
155155
}
156156

157157
func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
158-
c, err := xscommon.NewXenAPIClient(self.config.HostIp, self.config.Username, self.config.Password)
158+
c, err := xscommon.NewXenAPIClient(self.config.HostIp, self.config.Username, self.config.Password, self.config.SkipCertVerification, self.config.ServerCert)
159159

160160
if err != nil {
161161
return nil, err

builder/xenserver/xva/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri
9090

9191
func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
9292
//Setup XAPI client
93-
c, err := xscommon.NewXenAPIClient(self.config.HostIp, self.config.Username, self.config.Password)
93+
c, err := xscommon.NewXenAPIClient(self.config.HostIp, self.config.Username, self.config.Password, self.config.SkipCertVerification, self.config.ServerCert)
9494

9595
if err != nil {
9696
return nil, err

0 commit comments

Comments
 (0)