-
Notifications
You must be signed in to change notification settings - Fork 185
/
ProxyController.java
168 lines (150 loc) · 6.68 KB
/
ProxyController.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
/*
* Copyright (c) 2017 SUSE LLC
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/
package com.suse.manager.webui.controllers;
import static com.suse.manager.webui.utils.SparkApplicationHelper.json;
import static com.suse.manager.webui.utils.SparkApplicationHelper.withCsrfToken;
import static com.suse.manager.webui.utils.SparkApplicationHelper.withDocsLocale;
import static com.suse.manager.webui.utils.SparkApplicationHelper.withUser;
import static spark.Spark.get;
import static spark.Spark.post;
import com.redhat.rhn.common.RhnRuntimeException;
import com.redhat.rhn.domain.user.User;
import com.redhat.rhn.manager.system.SystemManager;
import com.redhat.rhn.manager.system.SystemsExistException;
import com.suse.manager.reactor.utils.LocalDateTimeISOAdapter;
import com.suse.manager.reactor.utils.OptionalTypeAdapterFactory;
import com.suse.manager.webui.utils.gson.ProxyContainerConfigJson;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import spark.ModelAndView;
import spark.Request;
import spark.Response;
import spark.template.jade.JadeTemplateEngine;
/**
* Controller class providing backend code for proxy specific pages.
*/
public class ProxyController {
private final SystemManager systemManager;
/**
* Create a new controller instance
*
* @param systemManagerIn the system manager
*/
public ProxyController(SystemManager systemManagerIn) {
systemManager = systemManagerIn;
}
// Logger for this class
private static final Logger LOG = LogManager.getLogger(ProxyController.class);
private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeISOAdapter())
.registerTypeAdapterFactory(new OptionalTypeAdapterFactory())
.serializeNulls()
.create();
/**
* Invoked from Router. Initialize routes for Proxy Views.
* @param proxyController instance to register.
* @param jade Jade template engine
*/
public void initRoutes(ProxyController proxyController, JadeTemplateEngine jade) {
get("/manager/proxy/container-config",
withCsrfToken(withDocsLocale(withUser(proxyController::containerConfig))), jade);
post("/manager/api/proxy/container-config", withUser(this::generateContainerConfig));
get("/manager/api/proxy/container-config/:filename", withUser(proxyController::containerConfigFile));
}
/**
* Displays the form to create a new container-based proxy configuration
*
* @param requestIn the request object
* @param responseIn the response object
* @param userIn the user
* @return the ModelAndView object to render the page
*/
public ModelAndView containerConfig(Request requestIn, Response responseIn, User userIn) {
Map<String, Object> data = new HashMap<>();
return new ModelAndView(data, "templates/proxy/container-config.jade");
}
/**
* Create the proxy containers configuration.
*
* @param request the request object
* @param response the response object
* @param user the user
*
* @return the config file name
*/
public String generateContainerConfig(Request request, Response response, User user) {
ProxyContainerConfigJson data = GSON.fromJson(request.body(),
new TypeToken<ProxyContainerConfigJson>() { }.getType());
if (!data.isValid()) {
return json(response, HttpStatus.SC_BAD_REQUEST, "Invalid Input Data");
}
try {
byte[] config = systemManager.createProxyContainerConfig(user, data.getProxyFqdn(),
data.getProxyPort(), data.getServerFqdn(), data.getMaxCache(), data.getEmail(),
data.getRootCA(), data.getIntermediateCAs(), data.getProxyCertPair(),
data.getCaPair(), data.getCaPassword(), data.getCertData());
String filename = data.getProxyFqdn().split("\\.")[0];
request.session().attribute(filename + "-config.tar.gz", config);
return json(response, filename + "-config.tar.gz");
}
catch (IOException | InstantiationException e) {
LOG.error("Failed to generate proxy container configuration", e);
return json(response, HttpStatus.SC_INTERNAL_SERVER_ERROR,
"Failed to generate proxy container configuration");
}
catch (SystemsExistException e) {
String msg = String.format("Cannot create proxy as an existing system has FQDN '%s'", data.getProxyFqdn());
LOG.error(msg);
return json(response, HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
}
catch (RhnRuntimeException e) {
return json(response, HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
}
/**
* Return the config file stored in the session
*
*
* @param request the request object
* @param response the response object
* @param user the user
*
* @return the config file
*/
public byte[] containerConfigFile(Request request, Response response, User user) {
String filename = request.params("filename");
if (!request.session().attributes().contains(filename) || !filename.endsWith("-config.tar.gz")) {
return json(response, HttpStatus.SC_BAD_REQUEST, "Configuration file wasn't generated").getBytes();
}
Object config = request.session().attribute(filename);
if (config instanceof byte[]) {
byte[] data = (byte[]) config;
request.session().removeAttribute(filename);
response.header("Content-Disposition", "attachment; filename=\"" + filename + "\"");
response.header("Content-Length", Integer.toString(data.length));
response.type("application/gzip");
return data;
}
return json(response, HttpStatus.SC_BAD_REQUEST, "Invalid configuration file data").getBytes();
}
}