Support authentication for zabbix < 5.4, #1544

This commit is contained in:
Alexander Zobnin
2022-12-27 13:04:46 +01:00
parent cba2e89a06
commit 42281a6577

View File

@@ -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,10 +136,31 @@ 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 err != nil { 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 {
return err
}
} else if err != nil {
return err return err
} }
@@ -146,6 +168,15 @@ func (api *ZabbixAPI) Authenticate(ctx context.Context, username string, passwor
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 {