Skip to content

Commit

Permalink
Merge pull request #3 from italiangrid/develop
Browse files Browse the repository at this point in the history
Merge 1.0.3 back to master
  • Loading branch information
enricovianello committed Aug 18, 2015
2 parents 7d78b85 + dd0adb9 commit cda9f9a
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 21 deletions.
11 changes: 10 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.italiangrid</groupId>
<artifactId>storm-webdav-server</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<packaging>jar</packaging>
<name>storm-webdav-server</name>

Expand Down Expand Up @@ -42,6 +42,7 @@
<spring-security.version>3.2.5.RELEASE</spring-security.version>
<thymeleaf.version>2.1.3.RELEASE</thymeleaf.version>

<mockito-core.version>1.10.19</mockito-core.version>

<!-- Default install prefix: keep it empty -->
<!-- It can be changed by passing variable at build time, with -Dinstall.prefix=/opt/storm-webdav -->
Expand Down Expand Up @@ -337,6 +338,14 @@
<artifactId>commons-cli</artifactId>
<version>${commons-cli.version}</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-core.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.italiangrid.storm.webdav.config.StorageAreaConfiguration;
import org.italiangrid.storm.webdav.config.StorageAreaInfo;
import org.italiangrid.storm.webdav.server.PathResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDecisionVoter;
Expand All @@ -45,10 +46,13 @@ public class CopyMoveAuthzVoter implements
.compile(WEBDAV_PATH_REGEX);

final StorageAreaConfiguration saConfig;

public CopyMoveAuthzVoter(StorageAreaConfiguration saConfig) {
final PathResolver pathResolver;

public CopyMoveAuthzVoter(StorageAreaConfiguration saConfig,
PathResolver pathResolver) {

this.saConfig = saConfig;
this.pathResolver = pathResolver;
}

@Override
Expand Down Expand Up @@ -85,18 +89,8 @@ private StorageAreaInfo getSAFromPath(String destinationURL)
throws MalformedURLException {

URL url = new URL(destinationURL);

String path = dropSlashWebdavFromPath(url.getPath());

for (StorageAreaInfo sa : saConfig.getStorageAreaInfo()) {
for (String ap : sa.accessPoints()) {
if (path.startsWith(ap)) {
return sa;
}
}
}

return null;
String pathInContext = dropSlashWebdavFromPath(url.getPath());
return pathResolver.resolveStorageArea(pathInContext);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.jetty.util.URIUtil;
import org.italiangrid.storm.webdav.config.StorageAreaConfiguration;
import org.italiangrid.storm.webdav.config.StorageAreaInfo;
import org.italiangrid.storm.webdav.server.PathResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,18 +33,18 @@ public class DefaultPathResolver implements PathResolver {
private static final Logger logger = LoggerFactory
.getLogger(DefaultPathResolver.class);

private final NavigableMap<String, String> contextMap;
private final NavigableMap<String, StorageAreaInfo> contextMap;

public DefaultPathResolver(StorageAreaConfiguration cfg) {

this.saConfig = cfg;
contextMap = new TreeMap<String, String>();
contextMap = new TreeMap<String, StorageAreaInfo>();

for (StorageAreaInfo sa : saConfig.getStorageAreaInfo()) {
for (String ap : sa.accessPoints()) {
logger.debug("Adding path mapping for sa {}: {} -> {}", sa.name(), ap,
sa.rootPath());
contextMap.put(ap, sa.rootPath());
contextMap.put(ap, sa);
}
}

Expand All @@ -61,11 +62,11 @@ protected String stripContextPath(String context, String path) {
@Override
public String resolvePath(String pathInContext) {

for (Map.Entry<String, String> e : contextMap.descendingMap().entrySet()) {
for (Map.Entry<String, StorageAreaInfo> e : contextMap.descendingMap().entrySet()) {

if (pathInContext.startsWith(e.getKey())) {

String resolvedPath = URIUtil.addPaths(e.getValue(),
String resolvedPath = URIUtil.addPaths(e.getValue().rootPath(),
stripContextPath(e.getKey(), pathInContext));

if (logger.isDebugEnabled()) {
Expand All @@ -80,4 +81,22 @@ public String resolvePath(String pathInContext) {
return null;
}

@Override
public StorageAreaInfo resolveStorageArea(String pathInContext) {

for (Map.Entry<String, StorageAreaInfo> e : contextMap.descendingMap().entrySet()) {

if (pathInContext.startsWith(e.getKey())) {

if (logger.isDebugEnabled()) {
logger.debug("{} matches with access point {}. Resolved storage area name: {}",
pathInContext, e.getKey(), e.getValue().name());
}

return e.getValue();
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/
package org.italiangrid.storm.webdav.server;

import org.italiangrid.storm.webdav.config.StorageAreaInfo;


public interface PathResolver {

public String resolvePath(String pathInContext);

public StorageAreaInfo resolveStorageArea(String pathInContext);

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.italiangrid.storm.webdav.config.ServiceConfiguration;
import org.italiangrid.storm.webdav.config.StorageAreaConfiguration;
import org.italiangrid.storm.webdav.config.StorageAreaInfo;
import org.italiangrid.storm.webdav.server.PathResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -61,11 +62,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
VOMapDetailsService vomsMapDetailsService;

@Autowired
PathResolver pathResolver;

@Bean
public AccessDecisionVoter<FilterInvocation> customVoter() {

return new CopyMoveAuthzVoter(saConfiguration);
return new CopyMoveAuthzVoter(saConfiguration, pathResolver);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* Copyright (c) Istituto Nazionale di Fisica Nucleare, 2014.
*
* 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.italiangrid.storm.webdav.authz.vomsmap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.italiangrid.storm.webdav.config.StorageAreaConfiguration;
import org.italiangrid.storm.webdav.config.StorageAreaInfo;
import org.italiangrid.storm.webdav.server.DefaultPathResolver;
import org.italiangrid.storm.webdav.server.PathResolver;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class PathResolverTests {

private final String ROOTDIR = "/storage";

/*
* Map SA-name -> VO name
*/
private static Map<String, String> input;

@BeforeClass
public static void init() {

input = new HashMap<String, String>();
input.put("test.vo.bis", "testers.eu-emi.eu");
input.put("test.vo", "test.vo");
input.put("test1", "testers.eu-emi.eu");
input.put("test12", "test.vo");
input.put("12test.", "testers.eu-emi.eu");
input.put("12test", "test.vo");
input.put("1", "testers.eu-emi.eu");
input.put("12", "test.vo");
input.put(".", "testers.eu-emi.eu");
input.put(".1", "test.vo");
}

PathResolver pathResolver;
StorageAreaConfiguration saConfig;
List<StorageAreaInfo> saInfoList;

@Before
public void setup() {

saInfoList = new ArrayList<StorageAreaInfo>();
for (String ap : input.keySet()) {
saInfoList.add(getMockSAInfo(ap, input.get(ap)));
}

saConfig = mock(StorageAreaConfiguration.class);
when(saConfig.getStorageAreaInfo()).thenReturn(saInfoList);

pathResolver = new DefaultPathResolver(saConfig);
}

private StorageAreaInfo getMockSAInfo(String name, String voname) {

StorageAreaInfo saInfo = mock(StorageAreaInfo.class);

String ap = "/".concat(name);
String rootPath = ROOTDIR.concat("/").concat(voname);

when(saInfo.name()).thenReturn(name);
when(saInfo.accessPoints()).thenReturn(Arrays.asList(ap));
when(saInfo.rootPath()).thenReturn(rootPath);
when(saInfo.vos()).thenReturn(new HashSet<String>(Arrays.asList(voname)));

return saInfo;
}

@Test
public void checkResolvedRootPath() {

for (String name : input.keySet()) {

String pathToTest = "/".concat(name).concat("/testdir");
String expectedRootPath = ROOTDIR.concat("/").concat(input.get(name))
.concat("/testdir");

String rootPath = pathResolver.resolvePath(pathToTest);
Assert.assertEquals(expectedRootPath, rootPath);
}

}

@Test
public void checkResolvedStorageArea() {

for (String name : input.keySet()) {

String pathToTest = "/".concat(name).concat("/testdir");
String expectedRootPath = ROOTDIR.concat("/").concat(input.get(name))
.concat("/testdir");

StorageAreaInfo sa = pathResolver.resolveStorageArea(pathToTest);
Assert.assertEquals(expectedRootPath, sa.rootPath() + "/testdir");
}
}

}

0 comments on commit cda9f9a

Please sign in to comment.