forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openshift_oauth.go
209 lines (149 loc) · 4.91 KB
/
openshift_oauth.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package business
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
kube "k8s.io/client-go/kubernetes"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
)
type OpenshiftOAuthService struct {
k8s kubernetes.IstioClientInterface
}
type OAuthMetadata struct {
AuthorizationEndpoint string `json:"authorizationEndpoint"`
}
// Structure that's returned by the openshift oauth authorization server.
// It defaults to following the snake_case format, so we parse it to something
// more usable on our side.
type OAuthAuthorizationServer struct {
AuthorizationEndpoint string `json:"authorization_endpoint"`
}
type OAuthUser struct {
Metadata OAuthUserMetadata `json:"metadata"`
}
type OAuthUserMetadata struct {
Name string `json:"name"`
}
type OAuthRoute struct {
Spec OAuthRouteSpec `json:"spec"`
}
type OAuthRouteSpec struct {
Host string `json:"host"`
TLS *OAuthRouteTLSSpec `json:"tls,omitempty"`
}
type OAuthRouteTLSSpec struct {
Termination string `json:"termination"`
}
const serverPrefix = "https://kubernetes.default.svc/"
func (in *OpenshiftOAuthService) Metadata() (metadata *OAuthMetadata, err error) {
var server *OAuthAuthorizationServer
response, err := request("GET", ".well-known/oauth-authorization-server", nil)
if err != nil {
message := fmt.Errorf("Could not send request to the Openshift OAuth API: %v", err)
return nil, message
}
err = json.Unmarshal(response, &server)
if err != nil {
message := fmt.Errorf("Could not parse data from the Openshift API: %v", err)
return nil, message
}
redirectURL, err := getKialiRoutePath()
if err != nil {
message := fmt.Errorf("Could not get Kiali route for OAuth redirect: %v", err)
return nil, message
}
metadata = &OAuthMetadata{}
metadata.AuthorizationEndpoint = fmt.Sprintf("%s?client_id=%s&redirect_uri=%s&response_type=%s", server.AuthorizationEndpoint, "kiali", url.QueryEscape(*redirectURL), "token")
return metadata, nil
}
func (in *OpenshiftOAuthService) ValidateToken(token string) error {
k8sConfig, err := kubernetes.ConfigClient()
if err != nil {
return fmt.Errorf("could not connect to Openshift: %v", err)
}
k8sConfig.BearerToken = token
k8s, err := kube.NewForConfig(k8sConfig)
if err != nil {
return fmt.Errorf("could not get Openshift cluster config: %v", err)
}
_, err = k8s.Discovery().ServerVersion()
if err != nil {
return fmt.Errorf("could not get info from Openshift: %v", err)
}
return nil
}
func (in *OpenshiftOAuthService) GetUserInfo(token string) (*OAuthUser, error) {
var user *OAuthUser
response, err := request("GET", "oapi/v1/users/~", &token)
if err != nil {
return nil, fmt.Errorf("Could not get user info from Openshift: %v", err)
}
err = json.Unmarshal(response, &user)
if err != nil {
return nil, fmt.Errorf("Could not parse user info from Openshift: %v", err)
}
return user, nil
}
func getKialiRoutePath() (*string, error) {
var route *OAuthRoute
var protocol string
namespace := config.Get().IstioNamespace
conf, err := kubernetes.ConfigClient()
if err != nil {
return nil, fmt.Errorf("could not connect to Openshift: %v", err)
}
response, err := request("GET", fmt.Sprintf("oapi/v1/namespaces/%s/routes/kiali", namespace), &conf.BearerToken)
if err != nil {
return nil, fmt.Errorf("could not connect to Openshift: %v", err)
}
err = json.Unmarshal(response, &route)
if err != nil {
return nil, fmt.Errorf("cannot parse Kiali route: %v", err)
}
if route.Spec.TLS == nil {
protocol = "http://"
} else {
protocol = "https://"
}
url := strings.Join([]string{protocol, route.Spec.Host}, "")
return &url, nil
}
func request(method string, url string, auth *string) ([]byte, error) {
certPool := x509.NewCertPool()
cert, err := ioutil.ReadFile("/run/secrets/kubernetes.io/serviceaccount/ca.crt")
if err != nil {
return nil, fmt.Errorf("Failed to get root CA certificates: %s", err)
}
certPool.AppendCertsFromPEM(cert)
tlsConfig := &tls.Config{RootCAs: certPool}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
}}
request, err := http.NewRequest(method, strings.Join([]string{serverPrefix, url}, ""), nil)
if err != nil {
return nil, fmt.Errorf("Failed to get api endpoint %s for oauth consumption, error: %s", url, err)
}
if auth != nil {
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *auth))
}
response, err := client.Do(request)
if err != nil {
return nil, fmt.Errorf("Failed to get api endpoint %s for oauth consumption, error: %s", url, err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("Failed to get api endpoint %s for oauth consumption, error: %s", url, err)
}
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Failed to get api endpoint %s for oauth consumption, error: %s", url, string(body))
}
return body, nil
}