Skip to content

Commit de8dcc6

Browse files
committed
Add some logging
1 parent 491eead commit de8dcc6

File tree

4 files changed

+119
-12
lines changed

4 files changed

+119
-12
lines changed

conf/defaults.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ config_file = /etc/grafana/ldap.toml
202202
enabled = false
203203
auth_url = http://localhost:5000
204204
default_domain = default
205-
default_role = Viewer
205+
#default_role = Viewer
206206
global_admin_roles =
207207
admin_roles = admin
208208
editor_roles = _member_

pkg/api/keystone/keystone_requests.go

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io/ioutil"
1010
"net/http"
1111

12+
"fmt"
1213
"github.com/grafana/grafana/pkg/log"
1314
"github.com/grafana/grafana/pkg/setting"
1415
)
@@ -24,7 +25,7 @@ type auth_request_struct struct {
2425

2526
type auth_struct struct {
2627
Identity auth_identity_struct `json:"identity"`
27-
Scope string `json:"scope"`
28+
Scope string `json:"scope,omitempty"`
2829
}
2930

3031
type scoped_auth_token_request_struct struct {
@@ -36,15 +37,13 @@ type scoped_auth_password_request_struct struct {
3637
}
3738

3839
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"`
4242
}
4343

4444
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"`
4847
}
4948

5049
type auth_scoped_identity_struct struct {
@@ -133,6 +132,7 @@ type Auth_data struct {
133132

134133
func AuthenticateScoped(data *Auth_data) error {
135134
if data.UnscopedToken != "" {
135+
log.Trace("AuthenticateScoped() with token")
136136
var auth_post scoped_auth_token_request_struct
137137
auth_post.Auth.Identity.Methods = []string{"token"}
138138
auth_post.Auth.Identity.Token.Id = data.UnscopedToken
@@ -142,7 +142,7 @@ func AuthenticateScoped(data *Auth_data) error {
142142
return authenticate(data, b)
143143
} else {
144144
var auth_post scoped_auth_password_request_struct
145-
auth_post.Auth.Nocatalog = true
145+
log.Trace("AuthenticateScoped() with password")
146146
auth_post.Auth.Identity.Methods = []string{"password"}
147147
auth_post.Auth.Identity.Password.User.Name = data.Username
148148
auth_post.Auth.Identity.Password.User.Password = data.Password
@@ -155,6 +155,7 @@ func AuthenticateScoped(data *Auth_data) error {
155155
}
156156

157157
func AuthenticateUnscoped(data *Auth_data) error {
158+
log.Trace("AuthenticateUnscoped()")
158159
var auth_post auth_request_struct
159160
auth_post.Auth.Scope = "unscoped"
160161
auth_post.Auth.Identity.Methods = []string{"password"}
@@ -167,7 +168,12 @@ func AuthenticateUnscoped(data *Auth_data) error {
167168
}
168169

169170
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))
171177
if err != nil {
172178
return err
173179
}
@@ -182,7 +188,21 @@ func authenticate(data *Auth_data, b []byte) error {
182188
return errors.New("Keystone authentication failed: " + resp.Status)
183189
}
184190

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+
186206
var auth_response auth_response_struct
187207
err = decoder.Decode(&auth_response)
188208
if err != nil {
@@ -205,6 +225,8 @@ type Projects_data struct {
205225
}
206226

207227
func GetProjects(data *Projects_data) error {
228+
log.Info("Authentication request to URL: %s", data.Server+"/v3/auth/projects")
229+
208230
request, err := http.NewRequest("GET", data.Server+"/v3/auth/projects", nil)
209231
if err != nil {
210232
return err
@@ -221,7 +243,21 @@ func GetProjects(data *Projects_data) error {
221243
return errors.New("Keystone project-list failed: " + resp.Status)
222244
}
223245

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+
225261
var project_response project_response_struct
226262
err = decoder.Decode(&project_response)
227263
if err != nil {

pkg/log/log.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,105 @@ func NewLogger(bufLen int64, mode, config string) {
3535
}
3636
}
3737

38+
func IsTrace() bool {
39+
for _, logger := range loggers {
40+
if logger.level <= TRACE {
41+
return true
42+
}
43+
}
44+
return false
45+
}
46+
3847
func Trace(format string, v ...interface{}) {
3948
for _, logger := range loggers {
4049
logger.Trace(format, v...)
4150
}
4251
}
4352

53+
func IsDebug() bool {
54+
for _, logger := range loggers {
55+
if logger.level <= DEBUG {
56+
return true
57+
}
58+
}
59+
return false
60+
}
61+
4462
func Debug(format string, v ...interface{}) {
4563
for _, logger := range loggers {
4664
logger.Debug(format, v...)
4765
}
4866
}
4967

68+
func IsInfo() bool {
69+
for _, logger := range loggers {
70+
if logger.level <= INFO {
71+
return true
72+
}
73+
}
74+
return false
75+
}
76+
5077
func Info(format string, v ...interface{}) {
5178
for _, logger := range loggers {
5279
logger.Info(format, v...)
5380
}
5481
}
5582

83+
func IsWarn() bool {
84+
for _, logger := range loggers {
85+
if logger.level <= WARN {
86+
return true
87+
}
88+
}
89+
return false
90+
}
91+
5692
func Warn(format string, v ...interface{}) {
5793
for _, logger := range loggers {
5894
logger.Warn(format, v...)
5995
}
6096
}
6197

98+
func IsError() bool {
99+
for _, logger := range loggers {
100+
if logger.level <= ERROR {
101+
return true
102+
}
103+
}
104+
return false
105+
}
106+
62107
func Error(skip int, format string, v ...interface{}) {
63108
for _, logger := range loggers {
64109
logger.Error(skip, format, v...)
65110
}
66111
}
67112

113+
func IsCritical() bool {
114+
for _, logger := range loggers {
115+
if logger.level <= CRITICAL {
116+
return true
117+
}
118+
}
119+
return false
120+
}
121+
68122
func Critical(skip int, format string, v ...interface{}) {
69123
for _, logger := range loggers {
70124
logger.Critical(skip, format, v...)
71125
}
72126
}
73127

128+
func IsFatal() bool {
129+
for _, logger := range loggers {
130+
if logger.level <= FATAL {
131+
return true
132+
}
133+
}
134+
return false
135+
}
136+
74137
func Fatal(skip int, format string, v ...interface{}) {
75138
Error(skip, format, v...)
76139
for _, l := range loggers {

pkg/login/keystone.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package login
33
import (
44
"errors"
55

6+
"fmt"
67
"github.com/grafana/grafana/pkg/api/keystone"
78
"github.com/grafana/grafana/pkg/bus"
9+
"github.com/grafana/grafana/pkg/log"
810
m "github.com/grafana/grafana/pkg/models"
911
)
1012

@@ -32,14 +34,17 @@ func NewKeystoneAuthenticator(server, domainname, default_role string, global_ad
3234

3335
func (a *keystoneAuther) login(query *LoginUserQuery) error {
3436

37+
log.Trace("perform initial authentication")
3538
// perform initial authentication
3639
if err := a.authenticate(query.Username, query.Password); err != nil {
3740
return err
3841
}
3942

43+
log.Trace("Get grafana user")
4044
if grafanaUser, err := a.getGrafanaUserFor(query.Username); err != nil {
4145
return err
4246
} else {
47+
log.Trace("sync org roles")
4348
// sync org roles
4449
if err := a.syncOrgRoles(query.Username, query.Password, grafanaUser); err != nil {
4550
return err
@@ -161,10 +166,12 @@ func (a *keystoneAuther) updateGrafanaOrgUser(userid, orgid int64, role m.RoleTy
161166
}
162167

163168
func (a *keystoneAuther) syncOrgRoles(username, password string, user *m.User) error {
169+
log.Trace("syncOrgRoles()")
164170
err := a.getProjectList(username, password)
165171
if err != nil {
166172
return err
167173
}
174+
log.Debug("OpenStack project_list[roles]: %v", a.project_list)
168175

169176
orgsQuery := m.GetUserOrgListQuery{UserId: user.Id}
170177
if err := bus.Dispatch(&orgsQuery); err != nil {
@@ -176,6 +183,7 @@ func (a *keystoneAuther) syncOrgRoles(username, password string, user *m.User) e
176183
// update or remove org roles
177184
for _, org := range orgsQuery.Result {
178185
handledOrgIds[org.OrgId] = true
186+
log.Info(fmt.Sprintf("Checking Grafana org %v for roles", org.Name))
179187

180188
if user_roles, ok := a.project_list[org.Name]; ok {
181189
// Update roles if user belongs to org

0 commit comments

Comments
 (0)