Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifies authentication #1923

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkg/zabbix/zabbix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 1 addition & 27 deletions pkg/zabbixapi/zabbix_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io"
"net/http"
"net/url"
"strings"

"github.com/alexanderzobnin/grafana-zabbix/pkg/metrics"
"github.com/bitly/go-simplejson"
Expand Down Expand Up @@ -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,
Expand All @@ -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 == "" {
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion pkg/zabbixapi/zabbix_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down