From 19c4f468f0bce85522afc868af303f54d364cf42 Mon Sep 17 00:00:00 2001 From: Wilton Rodrigues Date: Tue, 26 Nov 2024 15:44:14 -0300 Subject: [PATCH] Simplifies authentication - Remove Authenticate and isDeprecatedUserParamError methods - Using the Zabbix API version determines the use of Login or LoginDeprecated - This will prevent errors login in old Zabbix 3 as reported in #1616 --- pkg/zabbix/zabbix.go | 11 ++++++++++- pkg/zabbixapi/zabbix_api.go | 28 +--------------------------- pkg/zabbixapi/zabbix_api_test.go | 4 +++- 3 files changed, 14 insertions(+), 29 deletions(-) diff --git a/pkg/zabbix/zabbix.go b/pkg/zabbix/zabbix.go index 3cebbc03b..90437cf2e 100644 --- a/pkg/zabbix/zabbix.go +++ b/pkg/zabbix/zabbix.go @@ -141,11 +141,20 @@ func (zabbix *Zabbix) Authenticate(ctx context.Context) error { zabbixPassword = jsonData.Get("password").MustString() } - err = zabbix.api.Authenticate(ctx, zabbixLogin, zabbixPassword) + var auth string + + if zabbix.version >= 54 { + auth, err = zabbix.api.Login(ctx, zabbixLogin, zabbixPassword) + } else { + auth, err = zabbix.api.LoginDeprecated(ctx, zabbixLogin, zabbixPassword) + } + if err != nil { zabbix.logger.Error("Zabbix authentication error", "error", err) return err } + + zabbix.api.SetAuth(auth) zabbix.logger.Debug("Successfully authenticated", "url", zabbix.api.GetUrl().String(), "user", zabbixLogin) return nil diff --git a/pkg/zabbixapi/zabbix_api.go b/pkg/zabbixapi/zabbix_api.go index 009d41e33..23d3b0c93 100644 --- a/pkg/zabbixapi/zabbix_api.go +++ b/pkg/zabbixapi/zabbix_api.go @@ -9,7 +9,6 @@ import ( "io" "net/http" "net/url" - "strings" "github.com/alexanderzobnin/grafana-zabbix/pkg/metrics" "github.com/bitly/go-simplejson" @@ -139,6 +138,7 @@ func (api *ZabbixAPI) Login(ctx context.Context, username string, password strin // Login method for Zabbix prior to 5.4 func (api *ZabbixAPI) LoginDeprecated(ctx context.Context, username string, password string) (string, error) { + api.logger.Debug("user.login using deprecated user parameter") params := ZabbixAPIParams{ "user": username, "password": password, @@ -152,23 +152,6 @@ func (api *ZabbixAPI) LoginDeprecated(ctx context.Context, username string, pass 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 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 -} - // AuthenticateWithToken performs authentication with API token. func (api *ZabbixAPI) AuthenticateWithToken(ctx context.Context, token string) error { if token == "" { @@ -178,15 +161,6 @@ func (api *ZabbixAPI) AuthenticateWithToken(ctx context.Context, token string) e 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 { diff --git a/pkg/zabbixapi/zabbix_api_test.go b/pkg/zabbixapi/zabbix_api_test.go index 625f184e7..c5f129900 100644 --- a/pkg/zabbixapi/zabbix_api_test.go +++ b/pkg/zabbixapi/zabbix_api_test.go @@ -17,7 +17,9 @@ func TestZabbixAPIUnauthenticatedQuery(t *testing.T) { func TestLogin(t *testing.T) { zabbixApi, _ := MockZabbixAPI(`{"result":"secretauth"}`, 200) - err := zabbixApi.Authenticate(context.Background(), "user", "password") + auth, err := zabbixApi.Login(context.Background(), "user", "password") + + zabbixApi.SetAuth(auth) assert.Nil(t, err) assert.Equal(t, "secretauth", zabbixApi.auth)