forked from mnacharov/grafana-datasource-oauth-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grafana.go
56 lines (53 loc) · 1.25 KB
/
grafana.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"encoding/json"
"log"
"net/http"
"net/url"
"os"
"path"
)
type GrafanaOrgUser struct {
Email string `json:"email"`
Role string `json:"role"`
}
func isViewer(email string, orgID string) bool {
auth := os.Getenv("GRAFANA_AUTH")
client := &http.Client{}
grafanaURL, err := url.Parse(os.Getenv("GRAFANA_BASE_URL"))
if err != nil {
log.Printf("Error parsing GRAFANA_BASE_URL: %s\n", err)
return true
}
grafanaURL.Path = path.Join(grafanaURL.Path, "/api/org/users")
req, err := http.NewRequest("GET", grafanaURL.String(), nil)
if err != nil {
log.Printf("Error creating request to Grafana API: %v\n", err)
return true
}
req.Header = http.Header{
"Authorization": {auth},
"X-Grafana-Org-Id": {orgID},
}
res, err := client.Do(req)
if err != nil {
log.Printf("Error executing request to Grafana API: %v\n", err)
return true
}
var grafanaOrgUsers []GrafanaOrgUser
err = json.NewDecoder(res.Body).Decode(&grafanaOrgUsers)
if err != nil {
log.Printf("Error decoding response from Grafana API: %v\n", err)
return true
}
for _, grafanaOrgUser := range grafanaOrgUsers {
if grafanaOrgUser.Email == email {
if grafanaOrgUser.Role == "Viewer" {
return true
} else {
return false
}
}
}
return true
}