Skip to content

Commit

Permalink
Support authentication for zabbix < 5.4, #1544
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed Dec 27, 2022
1 parent cba2e89 commit 42281a6
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion pkg/zabbixapi/zabbix_api.go
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strings"

"github.com/alexanderzobnin/grafana-zabbix/pkg/metrics"
"github.com/bitly/go-simplejson"
Expand Down Expand Up @@ -135,17 +136,47 @@ 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
}

api.SetAuth(auth)
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 {
Expand Down

0 comments on commit 42281a6

Please sign in to comment.