9
9
"io/ioutil"
10
10
"net/http"
11
11
12
+ "fmt"
12
13
"github.com/grafana/grafana/pkg/log"
13
14
"github.com/grafana/grafana/pkg/setting"
14
15
)
@@ -24,7 +25,7 @@ type auth_request_struct struct {
24
25
25
26
type auth_struct struct {
26
27
Identity auth_identity_struct `json:"identity"`
27
- Scope string `json:"scope"`
28
+ Scope string `json:"scope,omitempty "`
28
29
}
29
30
30
31
type scoped_auth_token_request_struct struct {
@@ -36,15 +37,13 @@ type scoped_auth_password_request_struct struct {
36
37
}
37
38
38
39
type scoped_auth_token_struct struct {
39
- Nocatalog bool `json:"nocatalog"`
40
- Identity auth_scoped_identity_struct `json:"identity"`
41
- Scope auth_scope_struct `json:"scope"`
40
+ Identity auth_scoped_identity_struct `json:"identity"`
41
+ Scope auth_scope_struct `json:"scope"`
42
42
}
43
43
44
44
type scoped_auth_password_struct struct {
45
- Nocatalog bool `json:"nocatalog"`
46
- Identity auth_identity_struct `json:"identity"`
47
- Scope auth_scope_struct `json:"scope"`
45
+ Identity auth_identity_struct `json:"identity"`
46
+ Scope auth_scope_struct `json:"scope"`
48
47
}
49
48
50
49
type auth_scoped_identity_struct struct {
@@ -133,6 +132,7 @@ type Auth_data struct {
133
132
134
133
func AuthenticateScoped (data * Auth_data ) error {
135
134
if data .UnscopedToken != "" {
135
+ log .Trace ("AuthenticateScoped() with token" )
136
136
var auth_post scoped_auth_token_request_struct
137
137
auth_post .Auth .Identity .Methods = []string {"token" }
138
138
auth_post .Auth .Identity .Token .Id = data .UnscopedToken
@@ -142,7 +142,7 @@ func AuthenticateScoped(data *Auth_data) error {
142
142
return authenticate (data , b )
143
143
} else {
144
144
var auth_post scoped_auth_password_request_struct
145
- auth_post . Auth . Nocatalog = true
145
+ log . Trace ( "AuthenticateScoped() with password" )
146
146
auth_post .Auth .Identity .Methods = []string {"password" }
147
147
auth_post .Auth .Identity .Password .User .Name = data .Username
148
148
auth_post .Auth .Identity .Password .User .Password = data .Password
@@ -155,6 +155,7 @@ func AuthenticateScoped(data *Auth_data) error {
155
155
}
156
156
157
157
func AuthenticateUnscoped (data * Auth_data ) error {
158
+ log .Trace ("AuthenticateUnscoped()" )
158
159
var auth_post auth_request_struct
159
160
auth_post .Auth .Scope = "unscoped"
160
161
auth_post .Auth .Identity .Methods = []string {"password" }
@@ -167,7 +168,12 @@ func AuthenticateUnscoped(data *Auth_data) error {
167
168
}
168
169
169
170
func authenticate (data * Auth_data , b []byte ) error {
170
- request , err := http .NewRequest ("POST" , data .Server + "/v3/auth/tokens?nocatalog" , bytes .NewBuffer (b ))
171
+ auth_url := data .Server + "/v3/auth/tokens?nocatalog"
172
+
173
+ log .Debug ("Authentication request to URL: %s" , auth_url )
174
+ log .Debug ("Authentication request body: \n %s" , b )
175
+
176
+ request , err := http .NewRequest ("POST" , auth_url , bytes .NewBuffer (b ))
171
177
if err != nil {
172
178
return err
173
179
}
@@ -182,7 +188,21 @@ func authenticate(data *Auth_data, b []byte) error {
182
188
return errors .New ("Keystone authentication failed: " + resp .Status )
183
189
}
184
190
185
- decoder := json .NewDecoder (resp .Body )
191
+ var decoder * json.Decoder
192
+
193
+ if log .IsDebug () {
194
+ buf := new (bytes.Buffer )
195
+ buf .ReadFrom (resp .Body )
196
+ strBody := buf .Bytes ()
197
+
198
+ log .Debug ("Authentication response: \n %s" , strBody )
199
+
200
+ bodyReader := bytes .NewBufferString (fmt .Sprintf ("%s" , strBody ))
201
+ decoder = json .NewDecoder (bodyReader )
202
+ } else {
203
+ decoder = json .NewDecoder (resp .Body )
204
+ }
205
+
186
206
var auth_response auth_response_struct
187
207
err = decoder .Decode (& auth_response )
188
208
if err != nil {
@@ -205,6 +225,8 @@ type Projects_data struct {
205
225
}
206
226
207
227
func GetProjects (data * Projects_data ) error {
228
+ log .Info ("Authentication request to URL: %s" , data .Server + "/v3/auth/projects" )
229
+
208
230
request , err := http .NewRequest ("GET" , data .Server + "/v3/auth/projects" , nil )
209
231
if err != nil {
210
232
return err
@@ -221,7 +243,21 @@ func GetProjects(data *Projects_data) error {
221
243
return errors .New ("Keystone project-list failed: " + resp .Status )
222
244
}
223
245
224
- decoder := json .NewDecoder (resp .Body )
246
+ var decoder * json.Decoder
247
+
248
+ if log .IsDebug () {
249
+ buf := new (bytes.Buffer )
250
+ buf .ReadFrom (resp .Body )
251
+ strBody := buf .Bytes ()
252
+
253
+ log .Debug ("Projects response: \n %s" , strBody )
254
+
255
+ bodyReader := bytes .NewBufferString (fmt .Sprintf ("%s" , strBody ))
256
+ decoder = json .NewDecoder (bodyReader )
257
+ } else {
258
+ decoder = json .NewDecoder (resp .Body )
259
+ }
260
+
225
261
var project_response project_response_struct
226
262
err = decoder .Decode (& project_response )
227
263
if err != nil {
0 commit comments