forked from sensu/uchiwa
/
helpers.go
108 lines (92 loc) · 2.14 KB
/
helpers.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package uchiwa
import (
"errors"
"fmt"
"net"
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/mitchellh/mapstructure"
"github.com/palourde/logger"
"github.com/sensu/uchiwa/uchiwa/auth"
"github.com/sensu/uchiwa/uchiwa/sensu"
)
func getAPI(datacenters *[]sensu.Sensu, name string) (*sensu.Sensu, error) {
if name == "" {
return nil, errors.New("The datacenter name can't be empty")
}
for _, datacenter := range *datacenters {
if datacenter.Name == name {
return &datacenter, nil
}
}
return nil, fmt.Errorf("Could not find the datacenter '%s'", name)
}
func findModel(id string, dc string, checks []interface{}) map[string]interface{} {
for _, k := range checks {
m, ok := k.(map[string]interface{})
if !ok {
logger.Warningf("Could not assert check interface %+v", k)
continue
}
if m["name"] == id && m["dc"] == dc {
return m
}
}
return nil
}
// GetIP returns the real user IP address
func GetIP(r *http.Request) string {
if xForwardedFor := r.Header.Get("X-FORWARDED-FOR"); len(xForwardedFor) > 0 {
return xForwardedFor
}
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
return ip
}
// GetRoleFromToken ...
func GetRoleFromToken(token *jwt.Token) (*auth.Role, error) {
r, ok := token.Claims["Role"]
if !ok {
return &auth.Role{}, errors.New("Could not retrieve the user Role from the JWT")
}
var role auth.Role
err := mapstructure.Decode(r, &role)
if err != nil {
return &auth.Role{}, err
}
return &role, nil
}
// MergeStringSlices merges two slices of strings and remove duplicated values
func MergeStringSlices(a1, a2 []string) []string {
if len(a1) == 0 {
return a2
} else if len(a2) == 0 {
return a1
}
s := make([]string, len(a1), len(a1)+len(a2))
copy(s, a1)
next:
for _, x := range a2 {
for _, y := range s {
if x == y {
continue next
}
}
s = append(s, x)
}
return s
}
// SliceIntersection searches for values in both slices
// Returns true if there's at least one intersection
func SliceIntersection(a1, a2 []string) bool {
if len(a1) == 0 || len(a2) == 0 {
return false
}
for _, x := range a1 {
for _, y := range a2 {
if x == y {
return true
}
}
}
return false
}