forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
193 lines (169 loc) · 6.19 KB
/
authentication.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package handlers
import (
"fmt"
"net/http"
"time"
"k8s.io/client-go/tools/clientcmd/api"
"github.com/kiali/kiali/business"
"github.com/kiali/kiali/business/authentication"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/util/httputil"
)
type AuthenticationHandler struct {
saToken string
}
type AuthInfo struct {
Strategy string `json:"strategy"`
AuthorizationEndpoint string `json:"authorizationEndpoint,omitempty"`
LogoutEndpoint string `json:"logoutEndpoint,omitempty"`
LogoutRedirect string `json:"logoutRedirect,omitempty"`
SessionInfo sessionInfo `json:"sessionInfo"`
SecretMissing bool `json:"secretMissing,omitempty"`
}
type sessionInfo struct {
Username string `json:"username,omitempty"`
ExpiresOn string `json:"expiresOn,omitempty"`
}
func NewAuthenticationHandler() (AuthenticationHandler, error) {
// Read token from the filesystem
saToken, _, err := kubernetes.GetKialiTokenForHomeCluster()
if err != nil {
return AuthenticationHandler{}, err
}
return AuthenticationHandler{saToken: saToken}, nil
}
func (aHandler AuthenticationHandler) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusCode := http.StatusOK
conf := config.Get()
var authInfo *api.AuthInfo
switch conf.Auth.Strategy {
case config.AuthStrategyToken, config.AuthStrategyOpenId, config.AuthStrategyOpenshift, config.AuthStrategyHeader:
session, validateErr := authentication.GetAuthController().ValidateSession(r, w)
if validateErr != nil {
statusCode = http.StatusInternalServerError
} else if session != nil {
authInfo = session.AuthInfo
statusCode = http.StatusOK
} else {
statusCode = http.StatusUnauthorized
}
case config.AuthStrategyAnonymous:
log.Tracef("Access to the server endpoint is not secured with credentials - letting request come in. Url: [%s]", r.URL.String())
token, _, err := kubernetes.GetKialiTokenForHomeCluster()
if err != nil {
token = ""
}
authInfo = &api.AuthInfo{Token: token}
}
switch statusCode {
case http.StatusOK:
if authInfo == nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
log.Errorf("No authInfo: %v", http.StatusBadRequest)
}
ctx := authentication.SetAuthInfoContext(r.Context(), authInfo)
next.ServeHTTP(w, r.WithContext(ctx))
case http.StatusUnauthorized:
err := authentication.GetAuthController().TerminateSession(r, w)
if err != nil {
log.Errorf("Failed to clean a stale session: %s", err.Error())
}
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
default:
http.Error(w, http.StatusText(statusCode), statusCode)
log.Errorf("Cannot send response to unauthorized user: %v", statusCode)
}
})
}
func (aHandler AuthenticationHandler) HandleUnauthenticated(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := authentication.SetAuthInfoContext(r.Context(), &api.AuthInfo{Token: ""})
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func Authenticate(w http.ResponseWriter, r *http.Request) {
conf := config.Get()
switch conf.Auth.Strategy {
case config.AuthStrategyToken, config.AuthStrategyOpenId, config.AuthStrategyOpenshift, config.AuthStrategyHeader:
response, err := authentication.GetAuthController().Authenticate(r, w)
if err != nil {
if e, ok := err.(*authentication.AuthenticationFailureError); ok {
status := http.StatusUnauthorized
if e.HttpStatus != 0 {
status = e.HttpStatus
}
RespondWithError(w, status, e.Error())
} else {
RespondWithError(w, http.StatusInternalServerError, err.Error())
}
} else {
RespondWithJSONIndent(w, http.StatusOK, response)
}
case config.AuthStrategyAnonymous:
log.Warning("Authentication attempt with anonymous access enabled.")
default:
message := fmt.Sprintf("Cannot authenticate users, because strategy <%s> is unknown.", conf.Auth.Strategy)
log.Errorf(message)
RespondWithError(w, http.StatusInternalServerError, message)
}
}
func AuthenticationInfo(w http.ResponseWriter, r *http.Request) {
var response AuthInfo
conf := config.Get()
response.Strategy = conf.Auth.Strategy
switch conf.Auth.Strategy {
case config.AuthStrategyOpenshift:
token, _, err := kubernetes.GetKialiTokenForHomeCluster()
if err != nil {
RespondWithDetailedError(w, http.StatusInternalServerError, "Error obtaining Kiali SA token", err.Error())
return
}
layer, err := business.Get(&api.AuthInfo{Token: token})
if err != nil {
RespondWithDetailedError(w, http.StatusInternalServerError, "Error authenticating (getting business layer)", err.Error())
return
}
metadata, err := layer.OpenshiftOAuth.Metadata(r)
if err != nil {
RespondWithDetailedError(w, http.StatusInternalServerError, "Error trying to get OAuth metadata", err.Error())
return
}
response.AuthorizationEndpoint = metadata.AuthorizationEndpoint
response.LogoutEndpoint = metadata.LogoutEndpoint
response.LogoutRedirect = metadata.LogoutRedirect
case config.AuthStrategyOpenId:
// Do the redirection through an intermediary own endpoint
response.AuthorizationEndpoint = fmt.Sprintf("%s/api/auth/openid_redirect",
httputil.GuessKialiURL(r))
}
if conf.Auth.Strategy != config.AuthStrategyAnonymous {
session, _ := authentication.GetAuthController().ValidateSession(r, w)
if session != nil {
response.SessionInfo = sessionInfo{
ExpiresOn: session.ExpiresOn.Format(time.RFC1123Z),
Username: session.Username,
}
}
}
RespondWithJSON(w, http.StatusOK, response)
}
func Logout(w http.ResponseWriter, r *http.Request) {
conf := config.Get()
if conf.Auth.Strategy == config.AuthStrategyAnonymous {
RespondWithCode(w, http.StatusNoContent)
} else {
err := authentication.GetAuthController().TerminateSession(r, w)
if err != nil {
if e, ok := err.(*authentication.TerminateSessionError); ok {
RespondWithError(w, e.HttpStatus, e.Error())
} else {
RespondWithError(w, http.StatusInternalServerError, err.Error())
}
} else {
RespondWithCode(w, http.StatusNoContent)
}
}
}