-
Notifications
You must be signed in to change notification settings - Fork 81
/
devices.go
119 lines (94 loc) · 2.73 KB
/
devices.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
// SPDX-License-Identifier: MIT
package main
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/czerwonk/junos_exporter/internal/config"
"github.com/czerwonk/junos_exporter/pkg/connector"
"github.com/pkg/errors"
)
func devicesForConfig(cfg *config.Config) ([]*connector.Device, error) {
if cfg.Devices == nil {
if cfg.Targets == nil {
cfg.Targets = strings.Split(strings.Trim(*sshHosts, " "), ",")
}
cfg.Devices = devicesFromTargets(cfg.Targets)
}
devs := make([]*connector.Device, 0)
for _, d := range cfg.Devices {
if d.IsHostPattern {
continue
}
dev, err := deviceFromDeviceConfig(d, d.Host, cfg)
if err != nil {
return nil, err
}
devs = append(devs, dev)
}
return devs, nil
}
func devicesFromTargets(targets []string) []*config.DeviceConfig {
devices := make([]*config.DeviceConfig, len(targets))
for i, t := range targets {
devices[i] = &config.DeviceConfig{
Host: t,
}
}
return devices
}
func deviceFromDeviceConfig(device *config.DeviceConfig, hostname string, cfg *config.Config) (*connector.Device, error) {
auth, err := authForDevice(device, cfg)
if err != nil {
return nil, errors.Wrapf(err, "could not initialize config for device %s", device.Host)
}
// check whether there is a device specific regex otherwise fallback to global regex
if len(device.IfDescRegStr) == 0 {
device.IfDescReg = cfg.IfDescReg
} else {
re, err := regexp.Compile(device.IfDescRegStr)
if err != nil {
return nil, fmt.Errorf("unable to compile device description regex for %q: %q: %w", hostname, device.IfDescRegStr, err)
}
device.IfDescReg = re
}
return &connector.Device{
Host: hostname,
Auth: auth,
}, nil
}
func authForDevice(device *config.DeviceConfig, cfg *config.Config) (connector.AuthMethod, error) {
user := *sshUsername
if device.Username != "" {
user = device.Username
}
if device.KeyFile != "" {
return authForKeyFile(user, device.KeyFile, device.KeyPassphrase)
}
if *sshKeyFile != "" {
return authForKeyFile(user, *sshKeyFile, *sshKeyPassphrase)
}
if device.Password != "" {
return connector.AuthByPassword(user, device.Password), nil
}
if cfg.Password != "" {
return connector.AuthByPassword(user, cfg.Password), nil
}
if *sshPassword != "" {
return connector.AuthByPassword(user, *sshPassword), nil
}
return nil, errors.New("no valid authentication method available")
}
func authForKeyFile(username, keyFile, keyPassphrase string) (connector.AuthMethod, error) {
f, err := os.Open(keyFile)
if err != nil {
return nil, errors.Wrap(err, "could not open ssh key file")
}
defer f.Close()
auth, err := connector.AuthByKey(username, f, keyPassphrase)
if err != nil {
return nil, errors.Wrap(err, "could not load ssh private key file")
}
return auth, nil
}