Skip to content

Commit

Permalink
feat: add token authentication (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
liorfranko authored Sep 29, 2024
1 parent b829b5b commit 381b6e3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/ntopng-exporter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ntopng:
allowUnsafeTLS: false # set to true to accept self-signed or otherwise unverifiable certs from ntopng (default: false)
user: admin
password: admin
authMethod: cookie # cookie, basic, or none are accepted values
authMethod: cookie # cookie, basic, token or none are accepted values
scrapeInterval: 15s # scrape from the ntopng API every x period of time (should be synced with your prometheus scrapes) (default: 1 minute)
scrapeTargets: # you can also specify "all" as a single list item to scrape all available endpoints (default: all)
- hosts
Expand Down
20 changes: 18 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/spf13/viper"
"net"
"os"
"strings"
"time"
)
Expand All @@ -27,6 +28,7 @@ type ntopng struct {
EndPoint string
User string
Password string
Token string
AuthMethod string
ScrapeInterval string
ScrapeTargets []string
Expand Down Expand Up @@ -81,13 +83,27 @@ func ParseConfig() (Config, error) {
if err != nil {
return config, err
}
// Check if environment variable NTOPNG_TOKEN is set
if tokenEnv, exists := os.LookupEnv("NTOPNG_TOKEN"); exists {
config.Ntopng.Token = tokenEnv
}
err = config.validate()
return config, err
}

func (c *Config) validate() error {
if c.Ntopng.AuthMethod != "cookie" && c.Ntopng.AuthMethod != "basic" && c.Ntopng.AuthMethod != "none" {
return fmt.Errorf("ntopng authMethod must be either cookie, basic, or none")
if c.Ntopng.AuthMethod != "cookie" && c.Ntopng.AuthMethod != "basic" && c.Ntopng.AuthMethod != "token" && c.Ntopng.AuthMethod != "none" {
return fmt.Errorf("ntopng authMethod must be either cookie, basic, token or none")
}
if c.Ntopng.AuthMethod == "cookie" || c.Ntopng.AuthMethod == "basic" {
if c.Ntopng.User == "" || c.Ntopng.Password == "" {
return fmt.Errorf("ntopng user and password must be set when using cookie or basic auth")
}
}
if c.Ntopng.AuthMethod == "token" {
if c.Ntopng.Token == "" {
return fmt.Errorf("ntopng token must be set when using token auth")
}
}
if c.Host.InterfacesToMonitor == nil || len(c.Host.InterfacesToMonitor) < 1 {
return fmt.Errorf("must specify at least one interface to monitor")
Expand Down
2 changes: 2 additions & 0 deletions internal/ntopng/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ func (c *Controller) setCommonOptions(req *http.Request, isJsonRequest bool) {
c.config.Ntopng.User, c.config.Ntopng.Password))
} else if c.config.Ntopng.AuthMethod == "basic" {
req.SetBasicAuth(c.config.Ntopng.User, c.config.Ntopng.Password)
} else if c.config.Ntopng.AuthMethod == "token" {
req.Header.Add("Authorization", fmt.Sprintf("Token %s", c.config.Ntopng.Token))
}
}

Expand Down

0 comments on commit 381b6e3

Please sign in to comment.