summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/server
diff options
context:
space:
mode:
authorNils Schwabe2014-04-22 16:56:07 +0200
committerNils Schwabe2014-04-22 16:56:07 +0200
commit01e662bdfff823c4f5f0b6a270807fd0a3d2825f (patch)
treedaf063bc84c06781768794c17cc76f08decaa9c3 /src/main/java/org/openslx/imagemaster/server
parentAdded test case for sha512crypt, removed try/catch blocks from other tests, s... (diff)
downloadmasterserver-01e662bdfff823c4f5f0b6a270807fd0a3d2825f.tar.gz
masterserver-01e662bdfff823c4f5f0b6a270807fd0a3d2825f.tar.xz
masterserver-01e662bdfff823c4f5f0b6a270807fd0a3d2825f.zip
added a method to imageserver so that finshed uploads can be signaled
did some todos from simon moved FtpCredentials creation to MasterFtpServer.java moved some options to to config file
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/server')
-rw-r--r--src/main/java/org/openslx/imagemaster/server/ApiServer.java81
-rw-r--r--src/main/java/org/openslx/imagemaster/server/MasterFtpServer.java53
2 files changed, 94 insertions, 40 deletions
diff --git a/src/main/java/org/openslx/imagemaster/server/ApiServer.java b/src/main/java/org/openslx/imagemaster/server/ApiServer.java
index eda48eb..fc1d9b0 100644
--- a/src/main/java/org/openslx/imagemaster/server/ApiServer.java
+++ b/src/main/java/org/openslx/imagemaster/server/ApiServer.java
@@ -1,7 +1,9 @@
package org.openslx.imagemaster.server;
import java.io.File;
+import java.util.HashMap;
+import org.apache.ftpserver.ftplet.FtpException;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.imagemaster.Globals;
@@ -17,10 +19,13 @@ import org.openslx.imagemaster.session.SessionManager;
import org.openslx.imagemaster.session.User;
import org.openslx.imagemaster.thrift.iface.AuthenticationError;
import org.openslx.imagemaster.thrift.iface.AuthenticationException;
+import org.openslx.imagemaster.thrift.iface.AuthorizationError;
import org.openslx.imagemaster.thrift.iface.AuthorizationException;
import org.openslx.imagemaster.thrift.iface.FtpCredentials;
import org.openslx.imagemaster.thrift.iface.ImageData;
import org.openslx.imagemaster.thrift.iface.InvalidTokenException;
+import org.openslx.imagemaster.thrift.iface.ServerAuthenticationError;
+import org.openslx.imagemaster.thrift.iface.ServerAuthenticationException;
import org.openslx.imagemaster.thrift.iface.ServerSessionData;
import org.openslx.imagemaster.thrift.iface.SessionData;
import org.openslx.imagemaster.thrift.iface.UserInfo;
@@ -85,29 +90,22 @@ public class ApiServer {
* @throws AuthorizationException if the uni/hs server has no valid session
* @throws TException
*/
- public static FtpCredentials submitImage(ImageData imageDescription,
- ServerSessionData serverSessionData) throws AuthorizationException,
+ public static FtpCredentials submitImage(String serverSessionId,
+ ImageData imageDescription) throws AuthorizationException,
TException {
- if (ServerSessionManager.getSession(serverSessionData.sessionId) == null) {
- throw new AuthenticationException(AuthenticationError.GENERIC_ERROR, "No valid serverSessionData");
+ if (ServerSessionManager.getSession(serverSessionId) == null) {
+ throw new AuthorizationException(AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionData");
}
- String generatedUser = RandomString.generate(10, false);
- String generatedPass = RandomString.generate(16, true);
+ // create new user
+ FtpCredentials ftpCredentials = Globals.ftpServer.addUser(serverSessionId);
- if (!ImageProcessor.addImageDataToProcess(imageDescription, generatedUser)) {
+ if (!ImageProcessor.addImageDataToProcess(imageDescription, ftpCredentials.username)) {
+ Globals.ftpServer.removeUser(serverSessionId);
throw new TException("ImageData is not valid.");
}
-
- String dir = Globals.properties.getProperty("ftp_base_dir") + "/"
- + generatedUser + "/";
- if (new File(dir).mkdir()) {
- Globals.ftpServer.addUser(generatedUser, generatedPass, dir, true);
- log.info("Generated user/pass: " + generatedUser + "\t"
- + generatedPass + "\n with home dir: " + dir);
- }
-
- return new FtpCredentials(generatedUser, generatedPass);
+
+ return ftpCredentials;
}
/**
@@ -115,16 +113,15 @@ public class ApiServer {
* @param organization the organization that the server belongs to
* @return a random string that needs to be encrypted with the private
* key of the requesting satellite server
- * @throws TException
+ * @throws ServerAuthenticationException when organization is invalid/unknown
*/
public static String startServerAuthentication(String organization)
- throws TException {
- // TODO: Proper exceptions
+ throws ServerAuthenticationException {
if (organization == null || organization == "") {
- throw new TException("Empty organization");
+ throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Empty organization");
}
if (DbSatellite.fromOrganization(organization) == null) {
- throw new TException("Unkown organization");
+ throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Unknown organization");
}
return ServerAuthenticator.startServerAuthentication(organization);
}
@@ -140,13 +137,12 @@ public class ApiServer {
public static ServerSessionData serverAuthenticate(String organization,
String challengeResponse) throws AuthenticationException,
TException {
- // TODO: Proper exceptions
if (organization == null || challengeResponse == null) {
- throw new TException("Empty organization org challengeResponse");
+ throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Empty organization or challengeResponse");
}
DbSatellite satellite = DbSatellite.fromOrganization(organization);
if (satellite == null) {
- throw new TException("Unkown organization");
+ throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Unknown organization");
}
final ServerUser serverUser = ServerAuthenticator.serverAuthenticate(
organization, satellite.getAddress(), challengeResponse);
@@ -155,4 +151,39 @@ public class ApiServer {
return ServerSessionManager.addSession(session);
}
+ /**
+ * Tell the masterserver that the image upload finished.
+ * @param serverSessionId The session id of the hs/uni server
+ * @param imageDescription the description of the uploaded image
+ * @return if nothing went wrong
+ * @throws AuthorizationException if no valid session exists
+ */
+ public static boolean finishedUpload(String serverSessionId,
+ ImageData imageDescription) throws AuthorizationException {
+ // check if valid session exists
+ if (ServerSessionManager.getSession(serverSessionId) == null) {
+ throw new AuthorizationException(AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionData");
+ }
+
+ // process the image
+ String username = Globals.ftpServer.getCredentialsFromSessionId(serverSessionId).username;
+
+ File userDirectory = new File(Globals.properties.getProperty(Globals.ftpBaseDir) + "/" + username);
+ File[] list = userDirectory.listFiles();
+
+ if (list.length != 1) return false;
+
+ log.info(username + " is done with upload");
+
+ // remove user that is not needed anymore
+ Globals.ftpServer.removeUser(username);
+ log.info("Removed user: " + username);
+
+ ImageProcessor.processImageAfterUpload(username, list[0].getName());
+
+ Globals.ftpServer.removeUser(serverSessionId);
+
+ return true;
+ }
+
}
diff --git a/src/main/java/org/openslx/imagemaster/server/MasterFtpServer.java b/src/main/java/org/openslx/imagemaster/server/MasterFtpServer.java
index 973c768..c6592bc 100644
--- a/src/main/java/org/openslx/imagemaster/server/MasterFtpServer.java
+++ b/src/main/java/org/openslx/imagemaster/server/MasterFtpServer.java
@@ -17,11 +17,15 @@ import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.apache.log4j.Logger;
+import org.openslx.imagemaster.Globals;
+import org.openslx.imagemaster.thrift.iface.FtpCredentials;
+import org.openslx.imagemaster.util.RandomString;
public class MasterFtpServer implements Runnable {
private static Logger log = Logger.getLogger( MasterFtpServer.class );
private FtpServer server;
private UserManager userManager;
+ private HashMap<String, FtpCredentials> users = new HashMap<>();
public MasterFtpServer(int port, String adminUsername, String adminPassword, String ftproot) {
FtpServerFactory serverFactory = new FtpServerFactory();
@@ -39,7 +43,7 @@ public class MasterFtpServer implements Runnable {
userManager = userManagerFactory.createUserManager();
// create new admin user
- addUser(adminUsername, adminPassword, ftproot, true);
+ //addUser(adminUsername, adminPassword, ftproot, true);
serverFactory.setUserManager(userManager);
// add the Ftplet
@@ -51,38 +55,57 @@ public class MasterFtpServer implements Runnable {
server = serverFactory.createServer();
}
- public boolean addUser(final String username, final String password, final String ftproot, final boolean writeAccess) {
+ public FtpCredentials addUser(final String serverSessionId) {
// TODO: enable SSL
- boolean result = true;
+
+ FtpCredentials ftpCredentials = null;
+
+ String generatedUser = RandomString.generate(10, false);
+ String generatedPass = RandomString.generate(16, true);
+
+ String dir = Globals.properties.getProperty(Globals.ftpBaseDir) + "/"
+ + generatedUser + "/";
+
+ if (!new File(dir).mkdir()) {
+ return ftpCredentials;
+ }
BaseUser user = new BaseUser();
- user.setName(username);
- user.setPassword(password);
- user.setHomeDirectory(ftproot);
+ user.setName(generatedUser);
+ user.setPassword(generatedPass);
+ user.setHomeDirectory(dir);
List<Authority> authorities = new ArrayList<Authority>();
- if (writeAccess) authorities.add(new WritePermission());
+ authorities.add(new WritePermission());
user.setAuthorities(authorities);
try {
userManager.save(user);
+ ftpCredentials = new FtpCredentials(generatedUser, generatedPass);
+ users.put(serverSessionId, ftpCredentials);
} catch (FtpException e) {
- result = false;
}
- return result;
+ log.info("Generated user/pass: " + generatedUser + "\t"
+ + generatedPass + "\n with home dir: " + dir);
+
+ return ftpCredentials;
}
- public boolean removeUser(final String username) {
- boolean result = true;
+ public boolean removeUser(final String serverSessionId) {
+ if (!users.containsKey(serverSessionId)) return false;
try {
- userManager.delete(username);
+ userManager.delete(users.get(serverSessionId).username);
+ users.remove(serverSessionId);
+ return true;
} catch (FtpException e) {
- result = false;
+ return false;
}
-
- return result;
+ }
+
+ public FtpCredentials getCredentialsFromSessionId(String serverSessionId) {
+ return users.get(serverSessionId);
}
@Override