44 "bytes"
55 "encoding/json"
66 "fmt"
7- "io/ioutil "
7+ "io"
88 "net/http"
99
1010 "github.com/fabiang/go-zabbix/types"
@@ -14,10 +14,12 @@ import (
1414var (
1515 ErrNotFound = & NotFoundError {"No results were found matching the given search parameters" }
1616 zabbixVersion600 * types.ZBXVersion
17+ zabbixVersion640 * types.ZBXVersion
1718)
1819
1920func init () {
2021 zabbixVersion600 , _ = types .NewZBXVersion ("6.0.0" )
22+ zabbixVersion640 , _ = types .NewZBXVersion ("6.4.0" )
2123}
2224
2325// A Session is an authenticated Zabbix JSON-RPC API client. It must be
@@ -70,7 +72,7 @@ func (c *Session) login(username, password string) error {
7072 params ["username" ] = username
7173 }
7274
73- res , err := c .Do (NewRequest ("user.login" , params ))
75+ res , err := c .Do (NewRequest ("user.login" , params ), true )
7476 if err != nil {
7577 return fmt .Errorf ("Error logging in to Zabbix API: %v" , err )
7678 }
@@ -87,7 +89,7 @@ func (c *Session) login(username, password string) error {
8789func (c * Session ) GetVersion () (* types.ZBXVersion , error ) {
8890 if c .APIVersion == nil {
8991 // get Zabbix API version
90- res , err := c .Do (NewRequest ("apiinfo.version" , nil ))
92+ res , err := c .Do (NewRequest ("apiinfo.version" , nil ), true )
9193 if err != nil {
9294 return nil , err
9395 }
@@ -116,9 +118,21 @@ func (c *Session) AuthToken() string {
116118// When err is nil, resp always contains a non-nil resp.Body.
117119//
118120// Generally Get or a wrapper function will be used instead of Do.
119- func (c * Session ) Do (req * Request ) (resp * Response , err error ) {
120- // configure request
121- req .AuthToken = c .Token
121+ func (c * Session ) Do (req * Request , noAuthRequired bool ) (resp * Response , err error ) {
122+ if noAuthRequired == false {
123+ // get Zabbix API version
124+ ver , err := c .GetVersion ()
125+ if err != nil {
126+ return nil , fmt .Errorf ("Failed to retrieve Zabbix API version: %v" , err )
127+ }
128+
129+ // Zabbix 6.4 uses `Authorization` header, therefore "auth" parameter
130+ // has been deprecated and was removed in 7.2
131+ // See: https://www.zabbix.com/documentation/7.2/en/manual/api/changes
132+ if ver .Compare (zabbixVersion640 ) < 0 {
133+ req .AuthToken = c .Token
134+ }
135+ }
122136
123137 // encode request as json
124138 b , err := json .Marshal (req )
@@ -135,6 +149,9 @@ func (c *Session) Do(req *Request) (resp *Response, err error) {
135149 }
136150 r .ContentLength = int64 (len (b ))
137151 r .Header .Add ("Content-Type" , "application/json-rpc" )
152+ if noAuthRequired == false {
153+ r .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , c .Token ))
154+ }
138155
139156 // send request
140157 client := c .client
@@ -149,7 +166,7 @@ func (c *Session) Do(req *Request) (resp *Response, err error) {
149166 defer res .Body .Close ()
150167
151168 // read response body
152- b , err = ioutil .ReadAll (res .Body )
169+ b , err = io .ReadAll (res .Body )
153170 if err != nil {
154171 return nil , fmt .Errorf ("Error reading response: %v" , err )
155172 }
@@ -181,7 +198,7 @@ func (c *Session) Do(req *Request) (resp *Response, err error) {
181198// An error is return if a transport, marshalling or API error happened.
182199func (c * Session ) Get (method string , params interface {}, v interface {}) error {
183200 req := NewRequest (method , params )
184- resp , err := c .Do (req )
201+ resp , err := c .Do (req , false )
185202 if err != nil {
186203 return err
187204 }
0 commit comments