Support authentication for zabbix < 5.4, #1544
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/metrics"
|
||||
"github.com/bitly/go-simplejson"
|
||||
@@ -135,10 +136,31 @@ func (api *ZabbixAPI) Login(ctx context.Context, username string, password strin
|
||||
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.
|
||||
func (api *ZabbixAPI) Authenticate(ctx context.Context, username string, password string) error {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -146,6 +168,15 @@ func (api *ZabbixAPI) Authenticate(ctx context.Context, username string, passwor
|
||||
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) {
|
||||
jsonResp, err := simplejson.NewJson([]byte(response))
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user