diff options
Diffstat (limited to 'utils')
13 files changed, 1070 insertions, 0 deletions
diff --git a/utils/webseal-simulator/README.txt b/utils/webseal-simulator/README.txt new file mode 100644 index 0000000000..46fd622bc8 --- /dev/null +++ b/utils/webseal-simulator/README.txt @@ -0,0 +1,18 @@ +------------------------ +| | +| webseal simulator | +| | +------------------------ + +Working with webseal simulator: +------------------------------- + +1. Build the project using: mvn clean install +2. Ftp war file: webseal-simulator\target\WSSimulator.war to your localhost vagrant machine: /home/vagrant/webseal-simulator/webapps folder +3. Ftp configuration file: webseal-simulator\src\main\resources\webseal.conf to your localhost vagrant machine: /home/vagrant/webseal-simulator/config +4. To run the simulator, enter to your local vagrant and run: startWebsealSimulator.sh +5. Open browser and navigate to: http://localhost:8285/sdc1.login + +Note: the user in webseal configuration file will appear in the login screen. Pressing on user link will perform authentication and redirect to SDC. +The users should be predefined in SDC + diff --git a/utils/webseal-simulator/pom.xml b/utils/webseal-simulator/pom.xml new file mode 100644 index 0000000000..7d5df432fe --- /dev/null +++ b/utils/webseal-simulator/pom.xml @@ -0,0 +1,110 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <groupId>org.openecomp.sdc</groupId> + <artifactId>webseal-simulator</artifactId> + <packaging>war</packaging> + <version>0.0.1-SNAPSHOT</version> + + <properties> + <jetty-version>9.2.10.v20150310</jetty-version> + </properties> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.5</version> + <scope>compile</scope> + </dependency> + + <dependency> + <groupId>commons-httpclient</groupId> + <artifactId>commons-httpclient</artifactId> + <version>3.1</version> + </dependency> + + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging-api</artifactId> + <version>1.0.4</version> + </dependency> + + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.4</version> + </dependency> + + <dependency> + <groupId>commons-codec</groupId> + <artifactId>commons-codec</artifactId> + <version>2.0-SNAPSHOT</version> + <scope>compile</scope> + </dependency> + + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-proxy</artifactId> + <version>9.2.10.v20150310</version> + <scope>compile</scope> + </dependency> + + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-servlets</artifactId> + <version>${jetty-version}</version> + <scope>compile</scope> + </dependency> + + <!-- Proxy servlet --> + <dependency> + <groupId>com.typesafe</groupId> + <artifactId>config</artifactId> + <version>1.0.2</version> + <scope>compile</scope> + </dependency> + + </dependencies> + <build> + <finalName>WSSimulator</finalName> + <plugins> + <!-- ================================================== --> + <!-- Set the JDK compiler version. --> + <!-- ================================================== --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>2.5.1</version> + <inherited>true</inherited> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-maven-plugin</artifactId> + <version>${jetty-version}</version> + <configuration> + <contextPath>/</contextPath> + <webApp> + <contextPath>/</contextPath> + <webInfIncludeJarPattern>.*/.*jersey-[^/]\.jar$</webInfIncludeJarPattern> + </webApp> + <war>WSSimulator.war</war> + </configuration> + </plugin> + + </plugins> + </build> +</project> diff --git a/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/Login.java b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/Login.java new file mode 100644 index 0000000000..db4853a8f1 --- /dev/null +++ b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/Login.java @@ -0,0 +1,153 @@ +package org.openecomp.sdc.webseal.simulator; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Collection; +import java.util.Iterator; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.openecomp.sdc.webseal.simulator.User; +import org.openecomp.sdc.webseal.simulator.conf.Conf; + +public class Login extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + public void init(final ServletConfig config) throws ServletException { + super.init(config); + } + + @Override + protected void doGet(final HttpServletRequest request, final HttpServletResponse response) + throws ServletException, IOException { + + if (null != request.getParameter("userId")) { + doPost(request, response); + return; + } + System.out.println("about to build login page"); + response.setContentType("text/html"); + PrintWriter writer = response.getWriter(); + String message = (String) request.getAttribute("message"); + if (message == null) { + message = ""; + } + + Collection<User> allUsers = Conf.getInstance().getUsers().values(); + writer.println("<html>"); + + writer.println("<head>"); + writer.println("<style>"); + writer.println("body {padding: 40px; font-family: Arial; font-size: 14px;}"); + writer.println("h1 {background-color: #DDDDDD; padding: 4px 10px;}"); + writer.println("h2 {margin-top: 20px;}"); + writer.println(".label {width: 100px; float:left;}"); + writer.println(".break {display: block; margin-bottom: 10px;}"); + writer.println("tr {padding: 4px 10px;}"); + writer.println("th {padding: 4px 10px; text-align: left; background-color: #dddddd;}"); + writer.println("td {padding: 4px 10px; text-align: left;}"); + writer.println("</style>"); + writer.println("</head>"); + + writer.println("<body>"); + + writer.println("<h1>Webseal simulator</h1>"); + writer.println("<h2>Login:</h2>"); + + writer.println("<form action=\"\" method=\"post\">"); + writer.println(" <div class='label'>User id:</div>"); + writer.println(" <input type='text' name='userId'>"); + writer.println(" <div class='break'></div>"); + + writer.println(" <div class='label'>Password:</div>"); + writer.println(" <input type='password' name='password'>"); + writer.println(" <div class='break'></div>"); + + writer.println(" <input type='submit' value='Login'>"); + writer.println(" <label name='message'></label>"); + writer.println("</form>"); + + writer.println("<hr/>"); + writer.println("<h2>Quick links:</h2>"); + writer.println("<table>"); + writer.println("<tr>"); + writer.println("<th>full name</th>"); + writer.println("<th>user id</th>"); + writer.println("<th>role</th>"); + writer.println("<th>action</th>"); + writer.println("</tr>"); + Iterator<User> iterator = allUsers.iterator(); + while (iterator.hasNext()) { + User user = iterator.next(); + writer.println("<tr>"); + writer.println("<td>" + user.getUserRef() + "</td>"); + writer.println("<td>" + user.getUserId() + "</td>"); + writer.println("<td>" + user.getRole() + "</td>"); + writer.println("<td>" + user.getUserCreateRef() + "</td>"); + writer.println("</tr>"); + } + writer.println("</table>"); + + writer.println("<a href='create?all=true' target='resultFrame'>Create All</a>"); + writer.println("<hr/><iframe name='resultFrame' width='400' height='300'></iframe>"); + + writer.println("</body>"); + writer.println("</html>"); + + } + + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + String userId = request.getParameter("userId"); + String password = request.getParameter("password"); + request.setAttribute("message", "OK"); + + System.out.println("Login -> doPOst userId=" + userId); + User user = getUser(userId, password); + if (user == null) { + request.setAttribute("message", "ERROR: userId or password incorect"); + doGet(request, response); + } else { + System.out.println("Login -> doPOst redirext to /sdc1 (to proxy)"); + Cookie cookieUser = new Cookie("HTTP_IV_USER", user.getUserId()); + Cookie cookieUserId = new Cookie("USER_ID", user.getUserId()); + Cookie cookieFirstName = new Cookie("HTTP_CSP_FIRSTNAME", user.getFirstName()); + Cookie cookieEmail = new Cookie("HTTP_CSP_EMAIL", user.getEmail()); + Cookie cookieLastName = new Cookie("HTTP_CSP_LASTNAME", user.getLastName()); + Cookie cookieRemoteAddress = new Cookie("HTTP_IV_REMOTE_ADDRESS", "0.0.0.0"); + Cookie cookieWsType = new Cookie("HTTP_CSP_WSTYPE", "Intranet"); + response.addCookie(cookieUser); + response.addCookie(cookieUserId); + response.addCookie(cookieFirstName); + response.addCookie(cookieEmail); + response.addCookie(cookieLastName); + response.addCookie(cookieRemoteAddress); + response.addCookie(cookieWsType); + response.sendRedirect("/sdc1"); + } + + } + + private User getUser(String userId, String password) { + User user = Conf.getInstance().getUsers().get(userId); + if (user == null) { + return null; + } + if (!password.equals(user.getPassword())) { + return null; + } + return user; + } + + @Override + public String getServletInfo() { + return "Http Proxy Servlet"; + } +} diff --git a/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/MethodEnum.java b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/MethodEnum.java new file mode 100644 index 0000000000..6b9b01d259 --- /dev/null +++ b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/MethodEnum.java @@ -0,0 +1,10 @@ +package org.openecomp.sdc.webseal.simulator; + +public enum MethodEnum { + + GET, + POST, + PUT, + DELETE + +} diff --git a/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/MutableHttpServletRequest.java b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/MutableHttpServletRequest.java new file mode 100644 index 0000000000..74c8a15c80 --- /dev/null +++ b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/MutableHttpServletRequest.java @@ -0,0 +1,55 @@ +package org.openecomp.sdc.webseal.simulator; + + +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; + +public final class MutableHttpServletRequest extends HttpServletRequestWrapper { + // holds custom header and value mapping + private final Map<String, String> customHeaders; + + public MutableHttpServletRequest(HttpServletRequest request){ + super(request); + this.customHeaders = new HashMap<String, String>(); + } + + public void putHeader(String name, String value){ + this.customHeaders.put(name, value); + } + + public String getHeader(String name) { + // check the custom headers first + String headerValue = customHeaders.get(name); + + if (headerValue != null){ + return headerValue; + } + // else return from into the original wrapped object + return ((HttpServletRequest) getRequest()).getHeader(name); + } + + public Enumeration<String> getHeaderNames() { + // create a set of the custom header names + Set<String> set = new HashSet<String>(customHeaders.keySet()); + + // now add the headers from the wrapped request object + @SuppressWarnings("unchecked") + Enumeration<String> e = ((HttpServletRequest) getRequest()).getHeaderNames(); + while (e.hasMoreElements()) { + // add the names of the request headers into the list + String n = e.nextElement(); + set.add(n); + } + + // create an enumeration from the set and return + return Collections.enumeration(set); + } +} + diff --git a/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/RequestsClient.java b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/RequestsClient.java new file mode 100644 index 0000000000..a5e4a8a0eb --- /dev/null +++ b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/RequestsClient.java @@ -0,0 +1,141 @@ +package org.openecomp.sdc.webseal.simulator; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.IOUtils; +import org.openecomp.sdc.webseal.simulator.conf.Conf; + +public class RequestsClient extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { + + String hostname = request.getParameter("hostname") != null ? request.getParameter("hostname") : "127.0.0.1"; + String port = request.getParameter("port") != null ? request.getParameter("port") : "8080"; + String adminId = request.getParameter("adminId") != null ? request.getParameter("adminId") : "jh0003"; + + String createAll = request.getParameter("all"); + + PrintWriter writer = response.getWriter(); + + int resultCode; + + if ("true".equals(createAll)) { + Map<String, User> users = Conf.getInstance().getUsers(); + for (User user : users.values()) { + resultCode = createUser(response, user.getUserId(), user.getRole().toUpperCase(), user.getFirstName(), user.getLastName(), user.getEmail(), hostname, port, adminId); + writer.println("User "+ user.getFirstName() + " " + user.getLastName() + getResultMessage(resultCode) + "<br>"); + } + } else { + String userId = request.getParameter("userId"); + String role = request.getParameter("role").toUpperCase(); + String firstName = request.getParameter("firstName"); + String lastName = request.getParameter("lastName"); + String email = request.getParameter("email"); + resultCode = createUser(response, userId, role, firstName, lastName, email, hostname, port, adminId); + writer.println("User "+ firstName + " " + lastName +getResultMessage(resultCode)); + } + + + + } + + private String getResultMessage(int resultCode){ + return 201 == resultCode? " created successfuly":" not created ("+ resultCode +")"; + } + + private int createUser(final HttpServletResponse response, String userId, String role, String firstName, String lastName, String email, String hostname, String port, String adminId) throws IOException { + response.setContentType("text/html"); + + // Fill the data of the request + String url = "http://" + hostname + ":" + port + "/sdc2/rest/v1/user"; + String body = "{'firstName':'" + firstName + "', 'lastName':'" + lastName + "', 'userId':'" + userId + "', 'email':'" + email + "','role':'" + role + "'}"; + HashMap<String, String> headers = new HashMap<String, String>(); + headers.put("Content-Type", "application/json"); + headers.put("USER_ID", adminId); + return sendHttpPost(url, body, headers); + } + + private int sendHttpPost(String url, String body, Map<String, String> headers) throws IOException { + + String responseString = ""; + URL obj = new URL(url); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + + // add request method + con.setRequestMethod("POST"); + + // add request headers + if (headers != null) { + for (Entry<String, String> header : headers.entrySet()) { + String key = header.getKey(); + String value = header.getValue(); + con.setRequestProperty(key, value); + } + } + + // Send post request + if (body != null) { + con.setDoOutput(true); + DataOutputStream wr = new DataOutputStream(con.getOutputStream()); + wr.writeBytes(body); + wr.flush(); + wr.close(); + } + + int responseCode = con.getResponseCode(); + // logger.debug("Send POST http request, url: {}", url); + // logger.debug("Response Code: {}", responseCode); + + StringBuffer response = new StringBuffer(); + try { + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + } catch (Exception e) { + // logger.debug("response body is null"); + } + + String result; + + try { + result = IOUtils.toString(con.getErrorStream()); + response.append(result); + + } catch (Exception e2) { + result = null; + } + // logger.debug("Response body: {}", response); + + if (response != null) { + responseString = response.toString(); + } + + // Map<String, List<String>> headerFields = con.getHeaderFields(); + // String responseMessage = con.getResponseMessage(); + + con.disconnect(); + return responseCode; + + } + +} diff --git a/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/SdcProxy.java b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/SdcProxy.java new file mode 100644 index 0000000000..48e3a71aa5 --- /dev/null +++ b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/SdcProxy.java @@ -0,0 +1,319 @@ +package org.openecomp.sdc.webseal.simulator; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; +import java.util.zip.GZIPInputStream; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpMethodBase; +import org.apache.commons.httpclient.methods.DeleteMethod; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.InputStreamRequestEntity; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.PutMethod; +import org.openecomp.sdc.webseal.simulator.conf.Conf; + +public class SdcProxy extends HttpServlet { + + private static final long serialVersionUID = 1L; + private URL url; + private HttpClient proxy; + private Conf conf; + + private final String SDC1 = "/sdc1"; + private final String ONBOARDING = "/onboarding/"; + private final String SCRIPTS = "/scripts"; + private final String STYLES = "/styles"; + private final String LANGUAGES = "/languages"; + private final String CONFIGURATIONS = "/configurations"; + + public void init(ServletConfig config) throws ServletException { + super.init(config); + conf = Conf.getInstance(); + try { + String feHost = conf.getFeHost(); + this.url = new URL(feHost); + } catch (MalformedURLException me) { + throw new ServletException("Proxy URL is invalid", me); + } + this.proxy = new HttpClient(); + this.proxy.getHostConfiguration().setHost(this.url.getHost()); + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + proxy(request, response, MethodEnum.GET); + } + + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + String userId = request.getParameter("userId"); + String password = request.getParameter("password"); + + // Already sign-in + if (userId == null){ + userId = request.getHeader("USER_ID"); + } + + System.out.println("SdcProxy -> doPost userId=" + userId); + request.setAttribute("message", "OK"); + if (password != null && getUser(userId, password) == null) { + MutableHttpServletRequest mutableRequest = new MutableHttpServletRequest(request); + RequestDispatcher view = request.getRequestDispatcher("login"); + request.setAttribute("message", "ERROR: userid or password incorect"); + view.forward(mutableRequest, response); + } else { + System.out.println("SdcProxy -> doPost going to doGet"); + request.setAttribute("HTTP_IV_USER", userId); + proxy(request, response, MethodEnum.POST); + } + } + + public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + proxy(request, response, MethodEnum.PUT); + } + + public void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + proxy(request, response, MethodEnum.DELETE); + } + + private synchronized void proxy(HttpServletRequest request, HttpServletResponse response, MethodEnum methodEnum) throws IOException, UnsupportedEncodingException, HttpException { + Map<String, String[]> requestParameters = request.getParameterMap(); + System.out.print(request.getRequestURI() + " -> "); + + String userIdHeader = getUseridFromRequest(request); + //System.out.print(" (userIdHeader=" + userIdHeader + ") "); + User user = getUser(userIdHeader); + + // new request - forward to login page + if (userIdHeader == null) { + System.out.print("Going to login"); + response.sendRedirect("/login"); + return; + } + + String uri = getUri(request, requestParameters); + HttpMethodBase proxyMethod = getMethod(request, methodEnum, uri); + System.out.println(uri); + addHeaders(user, proxyMethod); + addHeaders(request, proxyMethod); + this.proxy.executeMethod(proxyMethod); + response.setStatus(proxyMethod.getStatusCode()); + + if (request.getRequestURI().indexOf(".svg") > -1) { + response.setContentType("image/svg+xml"); + } + + InputStream responseBodyStream = proxyMethod.getResponseBodyAsStream(); + Header contentEncodingHeader = proxyMethod.getResponseHeader("Content-Encoding"); + if (contentEncodingHeader != null && contentEncodingHeader.getValue().equalsIgnoreCase("gzip")) { + responseBodyStream = new GZIPInputStream(responseBodyStream); + } + write(responseBodyStream, response.getOutputStream()); + } + + private void addHeaders(HttpServletRequest request, HttpMethodBase proxyMethod) { + Enumeration<String> headerNames = request.getHeaderNames(); + while (headerNames.hasMoreElements()) { + String headerName = headerNames.nextElement(); + Enumeration<String> headers = request.getHeaders(headerName); + while (headers.hasMoreElements()) { + String headerValue = headers.nextElement(); + proxyMethod.addRequestHeader(headerName, headerValue); + } + } + } + + private User getUser(String userId, String password) { + User user = getUser(userId); + if (user.getPassword().equals(password)) { + return user; + } + return null; + } + + private User getUser(String userId) { + return conf.getUsers().get(userId); + + } + + private List<String> getContextPaths(){ + List<String> contextPaths = new ArrayList<>(); + contextPaths.add(SDC1); + contextPaths.add(ONBOARDING); + contextPaths.add(STYLES); + contextPaths.add(SCRIPTS); + contextPaths.add(LANGUAGES); + contextPaths.add(CONFIGURATIONS); + return contextPaths; + } + + private String getUri(HttpServletRequest request, Map<String, String[]> requestParameters) throws UnsupportedEncodingException { + String suffix = request.getRequestURI(); + if (getContextPaths().stream().anyMatch(request.getRequestURI()::contains)) { + suffix = alignUrlProxy(suffix); + } + StringBuilder query = alignUrlParameters(requestParameters); + String uri = String.format("%s%s", new Object[] {this.url.toString() + suffix, query.toString() }); + return uri; + } + + private HttpMethodBase getMethod(HttpServletRequest request, MethodEnum methodEnum, String uri) throws IOException { + HttpMethodBase proxyMethod = null; + switch (methodEnum) { + case GET: + proxyMethod = new GetMethod(uri); + break; + case POST: + proxyMethod = new PostMethod(uri); + ((PostMethod) proxyMethod).setRequestEntity(new InputStreamRequestEntity(request.getInputStream())); + break; + case PUT: + proxyMethod = new PutMethod(uri); + ((PutMethod) proxyMethod).setRequestBody(getBody(request)); + break; + case DELETE: + proxyMethod = new DeleteMethod(uri); + break; + } + return proxyMethod; + } + + private String getUseridFromRequest(HttpServletRequest request) { + + String userIdHeader = request.getHeader("USER_ID"); + if (userIdHeader != null){ + return userIdHeader; + } + Object o = request.getAttribute("HTTP_IV_USER"); + if (o != null) { + return o.toString(); + } + Cookie[] cookies = request.getCookies(); + + if (cookies != null){ + for (int i=0; i<cookies.length; ++i){ + if (cookies[i].getName().equals("USER_ID")){ + userIdHeader = cookies[i].getValue(); + } + } + } + return userIdHeader; + } + + private void addHeaders(User user, HttpMethodBase proxyMethod) { + proxyMethod.addRequestHeader("HTTP_IV_USER", user.getUserId()); + proxyMethod.addRequestHeader("USER_ID", user.getUserId()); + proxyMethod.addRequestHeader("HTTP_CSP_FIRSTNAME", user.getFirstName()); + proxyMethod.addRequestHeader("HTTP_CSP_EMAIL", user.getEmail()); + proxyMethod.addRequestHeader("HTTP_CSP_LASTNAME", user.getLastName()); + proxyMethod.addRequestHeader("HTTP_IV_REMOTE_ADDRESS", "0.0.0.0"); + proxyMethod.addRequestHeader("HTTP_CSP_WSTYPE", "Intranet"); + proxyMethod.addRequestHeader("HTTP_CSP_EMAIL", "me@mail.com"); + } + + private String alignUrlProxy(String requestURI) { + + int i = requestURI.indexOf(ONBOARDING); + if (-1 != i){ + return requestURI.substring(i); + } + + i = requestURI.indexOf(SDC1+SDC1); + if (-1 != i){ + return requestURI.substring(SDC1.length()); + } + + i = requestURI.indexOf(SDC1); + if (-1 != i){ + return requestURI; + } + + return SDC1+requestURI; + } + + private StringBuilder alignUrlParameters(Map<String, String[]> requestParameters) throws UnsupportedEncodingException { + StringBuilder query = new StringBuilder(); + for (String name : requestParameters.keySet()) { + for (String value : (String[]) requestParameters.get(name)) { + if (query.length() == 0) { + query.append("?"); + } else { + query.append("&"); + } + name = URLEncoder.encode(name, "UTF-8"); + value = URLEncoder.encode(value, "UTF-8"); + + query.append(String.format("&%s=%s", new Object[] { name, value })); + } + } + return query; + } + + private void write(InputStream inputStream, OutputStream outputStream) throws IOException { + int b; + while (inputStream != null && (b = inputStream.read()) != -1) { + outputStream.write(b); + } + outputStream.flush(); + } + + public String getServletInfo() { + return "Http Proxy Servlet"; + } + + + public String getBody(HttpServletRequest request) throws IOException { + + String body = null; + StringBuilder stringBuilder = new StringBuilder(); + BufferedReader bufferedReader = null; + + try { + InputStream inputStream = request.getInputStream(); + if (inputStream != null) { + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + char[] charBuffer = new char[128]; + int bytesRead = -1; + while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { + stringBuilder.append(charBuffer, 0, bytesRead); + } + } else { + stringBuilder.append(""); + } + } catch (IOException ex) { + throw ex; + } finally { + if (bufferedReader != null) { + try { + bufferedReader.close(); + } catch (IOException ex) { + throw ex; + } + } + } + + body = stringBuilder.toString(); + return body; + } +} diff --git a/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/User.java b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/User.java new file mode 100644 index 0000000000..9417e93ee0 --- /dev/null +++ b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/User.java @@ -0,0 +1,83 @@ +package org.openecomp.sdc.webseal.simulator; + +public class User { + + private String firstName; + private String lastName; + private String email; + private String userId; + private String role; + + private String password; + + public User(){ + } + + public User(String userId){ + setUserId(userId); + } + + public User(String firstName,String lastName,String email,String userId, String role, String password){ + setUserId(userId); + setFirstName(firstName); + setLastName(lastName); + setEmail(email); + setPassword(password); + setRole(role); + } + + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + public String getUserId() { + return userId; + } + public void setUserId(String userId) { + this.userId = userId; + } + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } + + public String getUserRef() { + return "<a href='?userId="+getUserId()+"&password="+getPassword()+"'>"+getFirstName()+" "+getLastName()+"</a>"; + } + + public String getUserCreateRef() { + return "<a href='create?userId="+getUserId()+"&firstName="+getFirstName()+"&lastName="+getLastName()+"&role=" + getRole() + "&email=" + getEmail() + "' target='resultFrame'>create</a>"; + } + + @Override + public String toString() { + return "User [firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + ", userId=" + userId + + ", role=" + role + ", password=" + password + "]"; + } + +} diff --git a/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/conf/Conf.java b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/conf/Conf.java new file mode 100644 index 0000000000..c00af32734 --- /dev/null +++ b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/conf/Conf.java @@ -0,0 +1,68 @@ +package org.openecomp.sdc.webseal.simulator.conf; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.openecomp.sdc.webseal.simulator.User; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; + +public class Conf { + + private static Conf conf= null; + private String feHost; + Map<String,User> users = new HashMap<String,User>(); + + private Conf(){ + initConf(); + } + + private void initConf() { + try{ + String confPath = System.getProperty("config.resource"); + if (confPath == null){ + System.out.println("config.resource is empty - goint to get it from config.home"); + confPath = System.getProperty("config.home") + "/webseal.conf"; + } + System.out.println("confPath=" + confPath ); + Config confFile = ConfigFactory.parseFileAnySyntax(new File(confPath)); + Config resolve = confFile.resolve(); + setFeHost(resolve.getString("webseal.fe")); + List<? extends Config> list = resolve.getConfigList("webseal.users"); + for (Config conf : list ){ + String userId = conf.getString("userId"); + String password = conf.getString("password"); + String firstName = conf.getString("firstName"); + String lastName = conf.getString("lastName"); + String email = conf.getString("email"); + String role = conf.getString("role"); + users.put(userId,new User(firstName,lastName,email,userId,role,password)); + } + + }catch(Exception e){ + e.printStackTrace(); + } + } + + public static Conf getInstance(){ + if (conf == null){ + conf = new Conf(); + } + return conf; + } + + public String getFeHost() { + return feHost; + } + + public void setFeHost(String feHost) { + this.feHost = feHost; + } + + public Map<String,User> getUsers() { + return users; + } + +} diff --git a/utils/webseal-simulator/src/main/resources/webseal.conf b/utils/webseal-simulator/src/main/resources/webseal.conf new file mode 100644 index 0000000000..a7026e39fa --- /dev/null +++ b/utils/webseal-simulator/src/main/resources/webseal.conf @@ -0,0 +1,49 @@ +{ + webseal { + fe="http://localhost:8181" + users = [ + { + userId="cs0008" + password="123123a" + firstName="Carlos" + lastName="Santana" + role="Designer" + email="csantana@sdc.com" + }, + { + userId="op0001" + password="123123a" + firstName="Aretha" + lastName="Franklin" + role="Ops" + email="afranklin@sdc.com" + }, + { + userId="jh0003" + password="123123a" + firstName="Jimmy" + lastName="Hendrix" + role="Admin" + email="admin@sdc.com" + }, + { + userId="jm0007" + password="123123a" + firstName="Johnny" + lastName="Depp" + role="Tester" + email="tester@sdc.com" + }, + { + userId="gv0001" + password="123123a" + firstName="David" + lastName="Shadmi" + role="Governor" + email="governor@sdc.com" + } + ] + } + +} + diff --git a/utils/webseal-simulator/src/main/webapp/WEB-INF/jetty-web.xml b/utils/webseal-simulator/src/main/webapp/WEB-INF/jetty-web.xml new file mode 100644 index 0000000000..dc85045b36 --- /dev/null +++ b/utils/webseal-simulator/src/main/webapp/WEB-INF/jetty-web.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> + +<Configure class="org.eclipse.jetty.webapp.WebAppContext"> + <Set name="contextPath">/</Set> +</Configure>
\ No newline at end of file diff --git a/utils/webseal-simulator/src/main/webapp/WEB-INF/web.xml b/utils/webseal-simulator/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..7535e1bdf8 --- /dev/null +++ b/utils/webseal-simulator/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="UTF-8"?> +<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" + version="3.0"> + + <display-name>Archetype Created Web Application</display-name> + + <servlet> + <servlet-name>Proxy</servlet-name> + <servlet-class>org.openecomp.sdc.webseal.simulator.SdcProxy</servlet-class> + </servlet> + <servlet-mapping> + <servlet-name>Proxy</servlet-name> + <url-pattern>/*</url-pattern> + </servlet-mapping> + + <servlet> + <servlet-name>Login</servlet-name> + <servlet-class>org.openecomp.sdc.webseal.simulator.Login</servlet-class> + </servlet> + <servlet-mapping> + <servlet-name>Login</servlet-name> + <url-pattern>/login</url-pattern> + </servlet-mapping> + + <servlet> + <servlet-name>CreateUser</servlet-name> + <servlet-class>org.openecomp.sdc.webseal.simulator.RequestsClient</servlet-class> + </servlet> + <servlet-mapping> + <servlet-name>CreateUser</servlet-name> + <url-pattern>/create</url-pattern> + </servlet-mapping> + + <welcome-file-list> + <welcome-file>login</welcome-file> + </welcome-file-list> + +</web-app> + diff --git a/utils/webseal-simulator/src/main/webapp/login.html b/utils/webseal-simulator/src/main/webapp/login.html new file mode 100644 index 0000000000..084a0b4255 --- /dev/null +++ b/utils/webseal-simulator/src/main/webapp/login.html @@ -0,0 +1,18 @@ +<html> +<body> +Login + + +<form action="access" method="post"> + UserId:<br> + <input type="text" name="userId" > + <br> + PASSWORD:<br> + <input type="password" name="password" > + <br><br> + <input type="submit" value="Submit"> + + <label name="message"/> +</form> +</body> +</html>
\ No newline at end of file |