-
Notifications
You must be signed in to change notification settings - Fork 74
/
OpenShiftTokenAuthentication.java
176 lines (144 loc) · 6.02 KB
/
OpenShiftTokenAuthentication.java
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
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.openshift.auth;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.JsonNodeFactory;
import org.codehaus.jackson.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author mwringe
*/
public class OpenShiftTokenAuthentication {
private static final Logger logger = LoggerFactory.getLogger(OpenShiftTokenAuthentication.class);
private static final String HAWKULAR_TENANT = "hawkular-tenant";
private static final String KUBERNETES_MASTER_URL =
System.getProperty("KUBERNETES_MASTER_URL", "https://kubernetes.default.svc.cluster.local");
//The resource to check against for security purposes. For this version we are allowing Metrics based on a users
//access to the pods in in a particular project.
private static final String RESOURCE = "pods";
private static final String KIND = "SubjectAccessReview";
private enum HTTP_METHOD {GET, PUT, POST, DELETE, PATCH};
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws
IOException, ServletException {
String token = request.getHeader(OpenShiftAuthenticationFilter.AUTHORIZATION_HEADER);
String tenant = request.getHeader(HAWKULAR_TENANT);
if (token == null || tenant == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"The '" + OpenShiftAuthenticationFilter.AUTHORIZATION_HEADER + "' and '"
+ HAWKULAR_TENANT + "' headers" + " are required");
} else if (isAuthorized(request.getMethod(), token, tenant)) {
filterChain.doFilter(request, response);
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
}
}
private boolean isAuthorized(String method, String token, String projectId) {
try {
String verb = getVerb(method);
String pod_namespace = System.getenv("POD_NAMESPACE");
if (pod_namespace == null) {
pod_namespace = "default";
}
String path = "/oapi/v1/namespaces/" + pod_namespace + "/subjectaccessreviews";
URL url = new URL(KUBERNETES_MASTER_URL + path);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
//Configure the outgoing request
connection.setRequestMethod("POST");
connection.setDoOutput(true);
//Set Headers
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", token);
//Add the body
try (
OutputStream outputStream = connection.getOutputStream();
) {
for (byte b : generateSubjectAccessReview(projectId, verb).getBytes()) {
outputStream.write(b);
}
}
//Perform the Operation
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 201) {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(connection.getInputStream());
if (jsonNode.get("allowed").asText().equals("true")) {
return true;
}
} else {
return false;
}
} catch (IOException e) {
logger.error("Error trying to authenticate against the OpenShift server", e);
}
return false;
}
/**
*
* Generates a SubjectAccessReview object used to request if a user has a certain permission or not.
*
* @param verb
* @return
* @throws IOException
*/
private String generateSubjectAccessReview(String namespace, String verb) throws IOException {
ObjectNode objectNode = JsonNodeFactory.instance.objectNode();
objectNode.put("kind", KIND);
objectNode.put("resource", RESOURCE);
objectNode.put("verb", verb);
objectNode.put("namespace", namespace);
return objectNode.toString();
}
/**
* Determine the verb we should apply based on the HTTP method being requested.
*
* In this case, an HTTP GET will require the 'get' permission in OpenShift
*
* HTTP PUT, POST & PATCH will require the 'update' permission in OpenShift
*
* HTTP DELETE will require the 'delete' permission in OpenShift
*
* @param method
* @return The verb to use or null if no verb could be determined
*/
private String getVerb(String method) {
HTTP_METHOD httpMethod = HTTP_METHOD.valueOf(method);
switch (httpMethod) {
case GET:
return "list";
case PUT:
return "update";
case POST:
return "update";
case DELETE:
return "delete";
case PATCH:
return "update";
}
return null;
}
}