Skip to content

Commit

Permalink
Merge branch 'release/v0.4.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
sdorra committed Dec 8, 2017
2 parents 5211c19 + f962f9a commit 5562f18
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 27 deletions.
2 changes: 1 addition & 1 deletion dogu.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Name": "official/smeagol",
"Version": "0.4.0",
"Version": "0.4.1",
"DisplayName": "Smeagol",
"Description": "Store your technical documentation with in your git repositories",
"Category": "Documentation",
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloudogu.wiki</groupId>
<artifactId>smeagol</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<name>smeagol</name>
<packaging>jar</packaging>

Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/cloudogu/wiki/CasLogoutServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2016 Cloudogu GmbH. All Rights Reserved.
*
* Copyright notice
*/
package com.cloudogu.wiki;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
*
* @author Michael Behlendorf
*/
public class CasLogoutServlet extends HttpServlet {

private static final Logger LOG = LoggerFactory.getLogger(CasLogoutServlet.class);
private final String logoutUrl;

public CasLogoutServlet(Map<String,String> casSettings) {
logoutUrl = casSettings.get("casServerUrlPrefix") + "/logout";
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
request.getSession().invalidate();
try {
response.sendRedirect(logoutUrl);
} catch (IOException ex) {
LOG.warn("Failed to send redirect.", ex);
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/cloudogu/wiki/SSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;

/**
Expand All @@ -32,7 +34,7 @@ private SSL() {
* Disables certificate checks. <strong>Warning:</strong> After the execution of this method, all certificates are
* accepted. Use this method only for development and never in production.
*/
public static void disableCertificateCheck() {
public static void disableCertificateCheck(HttpClientBuilder httpClientBuilder) {
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, UNSECURE_TRUSTMANAGER, new SecureRandom());
Expand All @@ -41,7 +43,7 @@ public static void disableCertificateCheck() {
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

// disable check for Unirest
Unirest.setHttpClient(HttpClients.custom().setSSLContext(sc).build());
httpClientBuilder.setSSLContext(sc);
} catch (KeyManagementException | NoSuchAlgorithmException ex) {
throw Throwables.propagate(ex);
}
Expand Down
33 changes: 24 additions & 9 deletions src/main/java/com/cloudogu/wiki/WikiServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@

import com.cloudogu.wiki.scmm.NotifyServlet;
import com.cloudogu.wiki.scmm.ScmWikiProvider;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import java.util.EnumSet;
import java.util.Map;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;

import com.mashape.unirest.http.Unirest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.FilterHolder;
Expand Down Expand Up @@ -58,17 +63,12 @@ public void start(WikiServerConfiguration cfg) {
"start wiki server on port {} with context path {} in stage {}",
cfg.getPort(), cfg.getContextPath(), cfg.getStage()
);

if ( cfg.getStage() == Stage.DEVELOPMENT ){
LOG.warn("smeagol is running in development stage, never use this stage for production deployments");
LOG.warn("disabling ssl/tls certificate checks");
SSL.disableCertificateCheck();
}
configureRestClient(cfg);

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath(cfg.getContextPath());
// set session timeout to 30 minutes
context.getSessionHandler().getSessionManager().setMaxInactiveInterval(1800);
// set session timeout to 2 hours
context.getSessionHandler().getSessionManager().setMaxInactiveInterval(7200);

ServletHolder resourceServletHolder = new ServletHolder(DefaultServlet.class);
resourceServletHolder.setInitParameter("resourceBase", cfg.getStaticPath());
Expand Down Expand Up @@ -101,6 +101,8 @@ public void start(WikiServerConfiguration cfg) {
// main servlet
context.addServlet(new ServletHolder(new WikiDispatcherServlet(provider, cfg)), "/*");

context.addServlet(new ServletHolder(new CasLogoutServlet(casSettings)), "/logout");

Server server = new Server(cfg.getPort());
server.setHandler(context);
try {
Expand All @@ -109,7 +111,20 @@ public void start(WikiServerConfiguration cfg) {
throw Throwables.propagate(ex);
}
}


@VisibleForTesting
static void configureRestClient(WikiServerConfiguration cfg) {
HttpClientBuilder httpClientBuilder = HttpClients.custom();
// Disable cookies to ensure that we always get a fresh scmm session
httpClientBuilder.disableCookieManagement();
if ( cfg.getStage() == Stage.DEVELOPMENT ){
LOG.warn("smeagol is running in development stage, never use this stage for production deployments");
LOG.warn("disabling ssl/tls certificate checks");
SSL.disableCertificateCheck(httpClientBuilder);
}
Unirest.setHttpClient(httpClientBuilder.build());
}

private void casFilter(ServletContextHandler context, Class<? extends Filter> filterClass, Map<String,String> cfg) {
FilterHolder filter = new FilterHolder(filterClass);
filter.setInitParameters(cfg);
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/cloudogu/wiki/scmm/RepositoryNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class RepositoryNotification {
private String repositoryToUpdate;
private final Set<String> updatedDirectories = new HashSet<>();
private final String COMMON_START = "servlet.";
private final String COMMON_END = "_**";

private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(RepositoryNotification.class);

Expand All @@ -56,20 +57,24 @@ private void updateRepository() {
}

private void updateWikisInSession(HttpSession session) {
Enumeration<String> attributes = session.getAttributeNames();
if (attributes != null) {
Account account = (Account) session.getAttribute("com.cloudogu.wiki.Account");
while (attributes.hasMoreElements()) {
String attribute = attributes.nextElement();
tryToUpdateWikiForAttribute(account, attribute);
try {
Enumeration<String> attributes = session.getAttributeNames();
if (attributes != null) {
Account account = (Account) session.getAttribute("com.cloudogu.wiki.Account");
while (attributes.hasMoreElements()) {
String attribute = attributes.nextElement();
tryToUpdateWikiForAttribute(account, attribute);
}
}
} catch (IllegalStateException ex) {
LOG.debug("Tried to get attributes from invalid session.");
}

}

private void tryToUpdateWikiForAttribute(Account account, String attribute) {
if (attribute.startsWith(COMMON_START)) {
String wikiName = attribute.substring(COMMON_START.length());
String wikiNameWithLanguage = attribute.substring(COMMON_START.length());
String wikiName = wikiNameWithLanguage.substring(0, wikiNameWithLanguage.length() - COMMON_END.length());
if (wikiRequiresUpdate(wikiName)) {
updateWiki(wikiName, account);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/com/cloudogu/wiki/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li>
<a href="{{casLogoutUrl}}">
<a href="{{request.contextPath}}/logout">
Logout
</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/com/cloudogu/wiki/header_de.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li>
<a href="{{casLogoutUrl}}">
<a href="{{request.contextPath}}/logout">
Logout
</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/com/cloudogu/wiki/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def call(env)
end

plantUmlUrl = ENV["PLANTUML_URL"]
if plantUmlUrl || plantUmlUrl.length == 0
if plantUmlUrl && plantUmlUrl.length > 0
Gollum::Filter::PlantUML.configure do |config|
config.url = plantUmlUrl
# do not verify ssl, in order to work with self signed certificates
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/com/cloudogu/wiki/NotifyServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void setUpMocks(){
sessions.add(session);
sessions.add(session2);

Set<String> attributes = ImmutableSet.of(ACCOUNT, "servlet."+REPOSITORY+"/"+BRANCH);
Set<String> attributes = ImmutableSet.of(ACCOUNT, "servlet."+REPOSITORY+"/"+BRANCH+"_de");
Enumeration<String> attributesEnum = java.util.Collections.enumeration(attributes);
when(session.getAttributeNames()).thenReturn(attributesEnum);
when(session.getAttribute(ACCOUNT)).thenReturn(account);
Expand All @@ -93,8 +93,7 @@ public void testNotifyGet() throws IOException, ServletException, GitAPIExceptio
when(wikiProvider.getRepositoryDirectory(REPOSITORY+"/"+BRANCH)).thenReturn(directory);

servlet.service(request, response);



verify(wikiProvider).pullChanges(account, directory, BRANCH);
}

Expand Down
66 changes: 66 additions & 0 deletions src/test/java/com/cloudogu/wiki/WikiServerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.cloudogu.wiki;

import com.mashape.unirest.http.Unirest;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static org.junit.Assert.assertEquals;

@RunWith(MockitoJUnitRunner.class)
public class WikiServerTest {

@Mock
private WikiServerConfiguration serverConfiguration;

@Test
public void ensureCookiesAreDisabled() throws Exception {
WikiServer.configureRestClient(serverConfiguration);

Server server = new Server(0);
server.setHandler(new SimpleHandler() {

@Override
protected void handle(HttpServletRequest request, HttpServletResponse response) {
if (request.getCookies() == null) {
response.addCookie(new Cookie("test", "value"));
}
}
});

server.start();
try {
String uri = server.getURI().toString();

List<String> cookies = Unirest.get(uri).asString().getHeaders().get("Set-Cookie");
assertEquals(1, cookies.size());
cookies = Unirest.get(uri).asString().getHeaders().get("Set-Cookie");
assertEquals(1, cookies.size());

} finally {
server.stop();
}
}

private static abstract class SimpleHandler extends AbstractHandler {

@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
handle(httpServletRequest, httpServletResponse);
}

protected abstract void handle(HttpServletRequest request, HttpServletResponse response);
}

}

0 comments on commit 5562f18

Please sign in to comment.