Skip to content

Commit 8da914a

Browse files
bugfix: Do not crash on invalid lookups (closes #109, thanks @tniebergall)
- check return value of lookup methods for nil, as documented in case something cannot be found)
1 parent 32908bb commit 8da914a

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

driver/cleanup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (d *Driver) destroyServer() error {
4242
return nil
4343
}
4444

45-
srv, err := d.getServerHandle()
45+
srv, err := d.getServerHandleNullable()
4646
if err != nil {
4747
return errors.Wrap(err, "could not get server handle")
4848
}

driver/driver.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ type Driver struct {
5555
AdditionalKeyIDs []int
5656
cachedAdditionalKeys []*hcloud.SSHKey
5757

58-
WaitOnError int
59-
WaitOnPolling int
58+
WaitOnError int
59+
WaitOnPolling int
6060
WaitForRunningTimeout int
6161

6262
// internal housekeeping
@@ -99,12 +99,12 @@ const (
9999
defaultSSHPort = 22
100100
defaultSSHUser = "root"
101101

102-
flagWaitOnError = "hetzner-wait-on-error"
103-
defaultWaitOnError = 0
104-
flagWaitOnPolling = "hetzner-wait-on-polling"
105-
defaultWaitOnPolling = 1
106-
flagWaitForRunningTimeout = "hetzner-wait-for-running-timeout"
107-
defaultWaitForRunningTimeout = 0
102+
flagWaitOnError = "hetzner-wait-on-error"
103+
defaultWaitOnError = 0
104+
flagWaitOnPolling = "hetzner-wait-on-polling"
105+
defaultWaitOnPolling = 1
106+
flagWaitForRunningTimeout = "hetzner-wait-for-running-timeout"
107+
defaultWaitForRunningTimeout = 0
108108

109109
legacyFlagUserDataFromFile = "hetzner-user-data-from-file"
110110
legacyFlagDisablePublic4 = "hetzner-disable-public-4"
@@ -608,9 +608,6 @@ func (d *Driver) Start() error {
608608
if err != nil {
609609
return errors.Wrap(err, "could not get server handle")
610610
}
611-
if srv == nil {
612-
return errors.New("server not found")
613-
}
614611

615612
act, _, err := d.getClient().Server.Poweron(context.Background(), srv)
616613
if err != nil {
@@ -628,9 +625,6 @@ func (d *Driver) Stop() error {
628625
if err != nil {
629626
return errors.Wrap(err, "could not get server handle")
630627
}
631-
if srv == nil {
632-
return errors.New("server not found")
633-
}
634628

635629
act, _, err := d.getClient().Server.Shutdown(context.Background(), srv)
636630
if err != nil {
@@ -648,9 +642,6 @@ func (d *Driver) Kill() error {
648642
if err != nil {
649643
return errors.Wrap(err, "could not get server handle")
650644
}
651-
if srv == nil {
652-
return errors.New("server not found")
653-
}
654645

655646
act, _, err := d.getClient().Server.Poweroff(context.Background(), srv)
656647
if err != nil {

driver/hetzner_query.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ func (d *Driver) getLocation() (*hcloud.Location, error) {
2121

2222
location, _, err := d.getClient().Location.GetByName(context.Background(), d.Location)
2323
if err != nil {
24-
return location, errors.Wrap(err, "could not get location by name")
24+
return nil, errors.Wrap(err, "could not get location by name")
25+
}
26+
if location == nil {
27+
return nil, fmt.Errorf("unknown location: %v", d.Location)
2528
}
2629
d.cachedLocation = location
2730
return location, nil
@@ -34,7 +37,10 @@ func (d *Driver) getType() (*hcloud.ServerType, error) {
3437

3538
stype, _, err := d.getClient().ServerType.GetByName(context.Background(), d.Type)
3639
if err != nil {
37-
return stype, errors.Wrap(err, "could not get type by name")
40+
return nil, errors.Wrap(err, "could not get type by name")
41+
}
42+
if stype == nil {
43+
return nil, fmt.Errorf("unknown server type: %v", d.Type)
3844
}
3945
d.cachedType = stype
4046
return instrumented(stype), nil
@@ -51,7 +57,10 @@ func (d *Driver) getImage() (*hcloud.Image, error) {
5157
if d.ImageID != 0 {
5258
image, _, err = d.getClient().Image.GetByID(context.Background(), d.ImageID)
5359
if err != nil {
54-
return image, errors.Wrap(err, fmt.Sprintf("could not get image by id %v", d.ImageID))
60+
return nil, errors.Wrap(err, fmt.Sprintf("could not get image by id %v", d.ImageID))
61+
}
62+
if image == nil {
63+
return nil, fmt.Errorf("image id not found: %v", d.ImageID)
5564
}
5665
} else {
5766
arch, err := d.getImageArchitectureForLookup()
@@ -61,7 +70,10 @@ func (d *Driver) getImage() (*hcloud.Image, error) {
6170

6271
image, _, err = d.getClient().Image.GetByNameAndArchitecture(context.Background(), d.Image, arch)
6372
if err != nil {
64-
return image, errors.Wrap(err, fmt.Sprintf("could not get image by name %v", d.Image))
73+
return nil, errors.Wrap(err, fmt.Sprintf("could not get image by name %v", d.Image))
74+
}
75+
if image == nil {
76+
return nil, fmt.Errorf("image not found: %v[%v]", d.Image, arch)
6577
}
6678
}
6779

@@ -87,12 +99,15 @@ func (d *Driver) getKey() (*hcloud.SSHKey, error) {
8799
return d.cachedKey, nil
88100
}
89101

90-
stype, _, err := d.getClient().SSHKey.GetByID(context.Background(), d.KeyID)
102+
key, _, err := d.getClient().SSHKey.GetByID(context.Background(), d.KeyID)
91103
if err != nil {
92-
return stype, errors.Wrap(err, "could not get sshkey by ID")
104+
return nil, errors.Wrap(err, "could not get sshkey by ID")
93105
}
94-
d.cachedKey = stype
95-
return instrumented(stype), nil
106+
if key == nil {
107+
return nil, fmt.Errorf("key not found: %v", d.KeyID)
108+
}
109+
d.cachedKey = key
110+
return instrumented(key), nil
96111
}
97112

98113
func (d *Driver) getRemoteKeyWithSameFingerprint(publicKeyBytes []byte) (*hcloud.SSHKey, error) {
@@ -107,10 +122,24 @@ func (d *Driver) getRemoteKeyWithSameFingerprint(publicKeyBytes []byte) (*hcloud
107122
if err != nil {
108123
return remoteKey, errors.Wrap(err, "could not get sshkey by fingerprint")
109124
}
125+
if remoteKey == nil {
126+
return nil, fmt.Errorf("key not found by fingerprint: %v", fp)
127+
}
110128
return instrumented(remoteKey), nil
111129
}
112130

113131
func (d *Driver) getServerHandle() (*hcloud.Server, error) {
132+
srv, err := d.getServerHandleNullable()
133+
if err != nil {
134+
return nil, err
135+
}
136+
if srv == nil {
137+
return nil, fmt.Errorf("server does not exist: %v", d.ServerID)
138+
}
139+
return srv, nil
140+
}
141+
142+
func (d *Driver) getServerHandleNullable() (*hcloud.Server, error) {
114143
if d.cachedServer != nil {
115144
return d.cachedServer, nil
116145
}
@@ -134,6 +163,9 @@ func (d *Driver) waitForAction(a *hcloud.Action) error {
134163
if err != nil {
135164
return errors.Wrap(err, "could not get client by ID")
136165
}
166+
if act == nil {
167+
return fmt.Errorf("action not found: %v", a.ID)
168+
}
137169

138170
if act.Status == hcloud.ActionStatusSuccess {
139171
log.Debugf(" -> finished %s[%d]", act.Command, act.ID)

0 commit comments

Comments
 (0)