Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TLS and Insecure can be set per device in config yaml #145

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ devices:
address: 10.10.0.1
user: prometheus
password: changeme
port: 8729
tls: true
insecure: true
- name: my_second_router
address: 10.10.0.2
port: 8999
Expand Down
20 changes: 5 additions & 15 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ var (
)

type collector struct {
devices []config.Device
collectors []routerOSCollector
timeout time.Duration
enableTLS bool
insecureTLS bool
devices []config.Device
collectors []routerOSCollector
timeout time.Duration
}

// WithBGP enables BGP routing metrics
Expand Down Expand Up @@ -167,14 +165,6 @@ func WithTimeout(d time.Duration) Option {
}
}

// WithTLS enables TLS
func WithTLS(insecure bool) Option {
return func(c *collector) {
c.enableTLS = true
c.insecureTLS = insecure
}
}

// WithIpsec enables ipsec metrics
func WithIpsec() Option {
return func(c *collector) {
Expand Down Expand Up @@ -366,7 +356,7 @@ func (c *collector) connect(d *config.Device) (*routeros.Client, error) {
var err error

log.WithField("device", d.Name).Debug("trying to Dial")
if !c.enableTLS {
if !d.EnableTLS {
if (d.Port) == "" {
d.Port = apiPort
}
Expand All @@ -377,7 +367,7 @@ func (c *collector) connect(d *config.Device) (*routeros.Client, error) {
// return routeros.DialTimeout(d.Address+apiPort, d.User, d.Password, c.timeout)
} else {
tlsCfg := &tls.Config{
InsecureSkipVerify: c.insecureTLS,
InsecureSkipVerify: d.InsecureTLS,
}
if (d.Port) == "" {
d.Port = apiPortTLS
Expand Down
31 changes: 23 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package config

import (
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"

yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"
)

// Config represents the configuration for the exporter
Expand Down Expand Up @@ -35,12 +36,14 @@ type Config struct {

// Device represents a target device
type Device struct {
Name string `yaml:"name"`
Address string `yaml:"address,omitempty"`
Srv SrvRecord `yaml:"srv,omitempty"`
User string `yaml:"user"`
Password string `yaml:"password"`
Port string `yaml:"port"`
Name string `yaml:"name"`
Address string `yaml:"address,omitempty"`
Srv SrvRecord `yaml:"srv,omitempty"`
User string `yaml:"user"`
Password string `yaml:"password"`
Port string `yaml:"port,omitempty"`
EnableTLS bool `yaml:"tls,omitempty"`
InsecureTLS bool `yaml:"insecure,omitempty"`
}

type SrvRecord struct {
Expand All @@ -52,7 +55,7 @@ type DnsServer struct {
Port int `yaml:"port"`
}

// Load reads YAML from reader and unmashals in Config
// Load reads YAML from reader and unmarshalls in Config
func Load(r io.Reader) (*Config, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
Expand All @@ -67,3 +70,15 @@ func Load(r io.Reader) (*Config, error) {

return c, nil
}

func (d *Device) UnmarshalYAML(unmarshal func(interface{}) error) error {
type inputDevice Device
defaults := &inputDevice{Port: "8728", EnableTLS: false, InsecureTLS: false}
err := unmarshal(defaults)
if err != nil {
log.WithError(err).Error("Device unmarshal error")
return err
}
*d = (Device)(*defaults)
return nil
}
5 changes: 4 additions & 1 deletion config/config.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ devices:
- name: test2
address: 192.168.2.1
user: test
password: 123
password: '123'
port: '324'
tls: true
insecure: true

features:
bgp: true
Expand Down
15 changes: 15 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func TestShouldParse(t *testing.T) {

assertDevice("test1", "192.168.1.1", "foo", "bar", c.Devices[0], t)
assertDevice("test2", "192.168.2.1", "test", "123", c.Devices[1], t)
assertDeviceConnection("8728", false, false, c.Devices[0], t)
assertDeviceConnection("324", true, true, c.Devices[1], t)
assertFeature("BGP", c.Features.BGP, t)
assertFeature("Conntrack", c.Features.Conntrack, t)
assertFeature("DHCP", c.Features.DHCP, t)
Expand Down Expand Up @@ -60,6 +62,19 @@ func assertDevice(name, address, user, password string, c Device, t *testing.T)
}
}

func assertDeviceConnection(port string, tls, insecure bool, c Device, t *testing.T) {
if c.Port != port {
t.Fatalf("expected port %s, got %s", port, c.Port)
}

if c.EnableTLS != tls {
t.Fatalf("expected tls %t, got %t", tls, c.EnableTLS)
}
if c.InsecureTLS != insecure {
t.Fatalf("expected insecure %t, got %t", insecure, c.InsecureTLS)
}
}

func assertFeature(name string, v bool, t *testing.T) {
if !v {
t.Fatalf("exprected feature %s to be enabled", name)
Expand Down
16 changes: 7 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ func loadConfigFromFlags() (*config.Config, error) {
return &config.Config{
Devices: []config.Device{
config.Device{
Name: *device,
Address: *address,
User: *user,
Password: *password,
Port: *deviceport,
Name: *device,
Address: *address,
User: *user,
Password: *password,
Port: *deviceport,
EnableTLS: *tls,
InsecureTLS: *insecure,
},
},
}, nil
Expand Down Expand Up @@ -278,9 +280,5 @@ func collectorOptions() []collector.Option {
opts = append(opts, collector.WithTimeout(*timeout))
}

if *tls {
opts = append(opts, collector.WithTLS(*insecure))
}

return opts
}