Support authentication for zabbix < 5.4, #1544
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/metrics"
|
"github.com/alexanderzobnin/grafana-zabbix/pkg/metrics"
|
||||||
"github.com/bitly/go-simplejson"
|
"github.com/bitly/go-simplejson"
|
||||||
@@ -135,17 +136,47 @@ func (api *ZabbixAPI) Login(ctx context.Context, username string, password strin
|
|||||||
return auth.MustString(), nil
|
return auth.MustString(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Login methid for Zabbix prior to 5.4
|
||||||
|
func (api *ZabbixAPI) LoginDeprecated(ctx context.Context, username string, password string) (string, error) {
|
||||||
|
params := ZabbixAPIParams{
|
||||||
|
"user": username,
|
||||||
|
"password": password,
|
||||||
|
}
|
||||||
|
|
||||||
|
auth, err := api.request(ctx, "user.login", params, "")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return auth.MustString(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Authenticate performs API authentication and sets authentication token.
|
// Authenticate performs API authentication and sets authentication token.
|
||||||
func (api *ZabbixAPI) Authenticate(ctx context.Context, username string, password string) error {
|
func (api *ZabbixAPI) Authenticate(ctx context.Context, username string, password string) error {
|
||||||
auth, err := api.Login(ctx, username, password)
|
auth, err := api.Login(ctx, username, password)
|
||||||
|
if isDeprecatedUserParamError(err) {
|
||||||
|
api.logger.Debug("user.login method error, switching to deprecated user parameter", "error", err)
|
||||||
|
auth, err = api.LoginDeprecated(ctx, username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
api.SetAuth(auth)
|
api.SetAuth(auth)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isDeprecatedUserParamError(err error) bool {
|
||||||
|
if err == nil {
|
||||||
|
return false
|
||||||
|
} else if strings.Contains(err.Error(), `unexpected parameter "user`) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func handleAPIResult(response []byte) (*simplejson.Json, error) {
|
func handleAPIResult(response []byte) (*simplejson.Json, error) {
|
||||||
jsonResp, err := simplejson.NewJson([]byte(response))
|
jsonResp, err := simplejson.NewJson([]byte(response))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user