Skip to content

Commit

Permalink
Fix #316
Browse files Browse the repository at this point in the history
  • Loading branch information
pavels committed Mar 30, 2016
1 parent 71b6b14 commit 07b4ee9
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.6.1
6.6.2
14 changes: 13 additions & 1 deletion common/src/main/java/cz/incad/kramerius/processes/LRProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public interface LRProcess {
* Plan process to start
* @param paramsMapping Parameters mapping
*/
public void planMe(Properties paramsMapping);
public void planMe(Properties paramsMapping, String ipAddress);



Expand Down Expand Up @@ -336,4 +336,16 @@ public interface LRProcess {
*/
public void setParametersMapping(Properties parametersMapping);

/**
* Returns IP address associated with HTTP request
* @return
*/
public String getPlannedIPAddress();

/**
* Sets IP address
* @param ipAddr
*/
public void setPlannedIPAddress(String ipAddr);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package cz.incad.kramerius.processes.database;

import static cz.incad.kramerius.database.cond.ConditionsInterpretHelper.versionCondition;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -61,6 +63,9 @@ public static void initDatabase(Connection connection, VersionService versionSer
alterProcessTableAuthToken(connection);
alterProcessTableProcess2TokenAuthToken(connection);
}
if (!DatabaseUtils.columnExists(connection, "PROCESSES","IP_ADDR")) {
alterProcessTableIPADDR(connection);
}
} else if ((v.equals("5.0.0")) || (v.equals("5.1.0"))) {
if (!DatabaseUtils.columnExists(connection, "PROCESSES","BATCH_STATUS")) {
alterProcessTableBatchState(connection);
Expand All @@ -76,6 +81,9 @@ public static void initDatabase(Connection connection, VersionService versionSer
alterProcessTableAuthToken(connection);
alterProcessTableProcess2TokenAuthToken(connection);
}
if (!DatabaseUtils.columnExists(connection, "PROCESSES","IP_ADDR")) {
alterProcessTableIPADDR(connection);
}
} else if (v.equals("5.1.0")) {
if (!DatabaseUtils.columnExists(connection, "PROCESSES","FINISHED")) {
alterProcessTableFinished(connection);
Expand All @@ -87,6 +95,9 @@ public static void initDatabase(Connection connection, VersionService versionSer
alterProcessTableAuthToken(connection);
alterProcessTableProcess2TokenAuthToken(connection);
}
if (!DatabaseUtils.columnExists(connection, "PROCESSES","IP_ADDR")) {
alterProcessTableIPADDR(connection);
}
} else if (v.equals("5.2.0")) {
if (!DatabaseUtils.columnExists(connection, "PROCESSES","TOKEN_ACTIVE")) {
alterProcessTableTokenActive(connection);
Expand All @@ -95,11 +106,21 @@ public static void initDatabase(Connection connection, VersionService versionSer
alterProcessTableAuthToken(connection);
alterProcessTableProcess2TokenAuthToken(connection);
}
if (!DatabaseUtils.columnExists(connection, "PROCESSES","IP_ADDR")) {
alterProcessTableIPADDR(connection);
}
} else if (v.equals("5.3.0")) {
if (!DatabaseUtils.columnExists(connection, "PROCESSES","AUTH_TOKEN")) {
alterProcessTableAuthToken(connection);
alterProcessTableProcess2TokenAuthToken(connection);
}
if (!DatabaseUtils.columnExists(connection, "PROCESSES","IP_ADDR")) {
alterProcessTableIPADDR(connection);
}
} else if (versionCondition(v, ">", "5.3.0")) {
if (!DatabaseUtils.columnExists(connection, "PROCESSES","IP_ADDR")) {
alterProcessTableIPADDR(connection);
}
}
} catch (SQLException e) {
LOGGER.log(Level.SEVERE,e.getMessage(),e);
Expand All @@ -108,6 +129,17 @@ public static void initDatabase(Connection connection, VersionService versionSer
}
}

private static void alterProcessTableIPADDR(Connection con) throws SQLException {
PreparedStatement prepareStatement = con.prepareStatement(
"ALTER TABLE PROCESSES ADD COLUMN IP_ADDR VARCHAR(255);");
try {
int r = prepareStatement.executeUpdate();
LOGGER.log(Level.FINEST, "ALTER TABLE: updated rows {0}", r);
} finally {
DatabaseUtils.tryClose(prepareStatement);
}
}

/** No version defined in db */
public static void nullVersionInitialization(Connection connection) throws SQLException, IOException {
if (!DatabaseUtils.tableExists(connection,"PROCESSES")) {
Expand Down Expand Up @@ -148,6 +180,9 @@ public static void nullVersionInitialization(Connection connection) throws SQLEx
alterProcessTableAuthToken(connection);
alterProcessTableProcess2TokenAuthToken(connection);
}
if (!DatabaseUtils.columnExists(connection, "PROCESSES","IP_ADDR")) {
alterProcessTableIPADDR(connection);
}
}

public static void createToken2SessionkeysMapping(Connection connection) throws SQLException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ public static void registerProcess(Connection con, LRProcess lp, User user, Stri
" PARAMS_MAPPING , " + //11
" BATCH_STATUS ," + //12
" TOKEN_ACTIVE, " + //
" AUTH_TOKEN) " + // 13
" values " +
" AUTH_TOKEN,"+ //13
" IP_ADDR"+ // 14
" ) " +
" values " +
" (" +
" ?," + //1 - DEFID
" ?," + //2 - UUID
Expand All @@ -126,7 +128,8 @@ public static void registerProcess(Connection con, LRProcess lp, User user, Stri
" ?," + //11 PARAMS_MAPPING
" ?," + //12 BATCH_STATUS
" TRUE," + //
" ?" + //13 AUTH_TOKEN
" ?," + //13 AUTH_TOKEN
" ?" + //14 IP_ADDR
" )");
try {
prepareStatement.setString(1, lp.getDefinitionId());
Expand Down Expand Up @@ -156,6 +159,7 @@ public static void registerProcess(Connection con, LRProcess lp, User user, Stri
prepareStatement.setString(11, storedParams);
prepareStatement.setInt(12, lp.getBatchState().getVal());
prepareStatement.setString(13, lp.getAuthToken());
prepareStatement.setString(14, lp.getPlannedIPAddress());

prepareStatement.executeUpdate();
} finally {
Expand Down Expand Up @@ -297,7 +301,7 @@ public static void deleteTokenMappings(LRProcess lrProcess, Connection con) thro
public static String [] QUERY_PROCESS_COLUMNS= {
"p.DEFID,PID", "p.UUID", "p.STATUS", "p.PLANNED", "p.STARTED",
"p.NAME AS PNAME", "p.PARAMS", "p.STARTEDBY", "p.TOKEN", "p.FINISHED",
"p.loginname","p.surname","p.firstname","p.user_key","p.params_mapping", "p.batch_status","p.AUTH_TOKEN"
"p.loginname","p.surname","p.firstname","p.user_key","p.params_mapping", "p.batch_status","p.AUTH_TOKEN","p.IP_ADDR"
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import cz.incad.kramerius.processes.LRProcessManager;
import cz.incad.kramerius.processes.States;
import cz.incad.kramerius.security.User;
import cz.incad.kramerius.utils.IPAddressUtils;
import cz.incad.kramerius.utils.conf.KConfiguration;

public abstract class AbstractLRProcessImpl implements LRProcess {
Expand All @@ -62,7 +63,7 @@ public abstract class AbstractLRProcessImpl implements LRProcess {
private String loginname;
private String firstname;
private String surname;

private User user;
private String loggedUserKey;

Expand All @@ -71,9 +72,12 @@ public abstract class AbstractLRProcessImpl implements LRProcess {
private boolean masterProcess;

private List<String> parameters = new ArrayList<String>();

private Properties parametersMapping = new Properties();

private String ipAddress;



public AbstractLRProcessImpl(LRProcessDefinition definition,
LRProcessManager manager, KConfiguration configuration) {
super();
Expand Down Expand Up @@ -113,10 +117,11 @@ public long getStartTime() {
return this.startTime;
}

public void planMe(Properties paramsMapping) {
public void planMe(Properties paramsMapping, String ipAddress) {
this.state = States.PLANNED;
this.ipAddress = ipAddress;
this.setPlannedTime(System.currentTimeMillis());

manager.registerLongRunningProcess(this, getLoggedUserKey(),
paramsMapping);
}
Expand All @@ -141,8 +146,8 @@ public void startMe(boolean wait, String krameriusAppLib,
command.add("-D" + ProcessStarter.MAIN_CLASS_KEY + "="
+ this.definition.getMainClass());

// command.add("-D" + IsActionAllowedFromRequest.X_IP_FORWARD + "="
// + remoteAddr);
command.add("-D" + IPAddressUtils.X_IP_FORWARD + "="
+ this.ipAddress);

command.add("-D" + ProcessStarter.UUID_KEY + "=" + this.uuid);
command.add("-D" + ProcessStarter.TOKEN_KEY + "="
Expand Down Expand Up @@ -473,4 +478,15 @@ public long getFinishedTime() {
public void setFinishedTime(long finishedtime) {
this.finishedTime = finishedtime;
}

@Override
public String getPlannedIPAddress() {
return this.ipAddress;
}

@Override
public void setPlannedIPAddress(String ipAddr) {
this.ipAddress = ipAddr;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ private LRProcess processFromResultSet(ResultSet rs) throws SQLException {
String userKey = rs.getString("USER_KEY");
String paramsMapping = rs.getString("params_mapping");
int batchStatus = rs.getInt("BATCH_STATUS");
String ipAddr = rs.getString("IP_ADDR");


LRProcessDefinition definition = this.lrpdm.getLongRunningProcessDefinition(definitionId);
Expand Down Expand Up @@ -485,6 +486,9 @@ private LRProcess processFromResultSet(ResultSet rs) throws SQLException {
if (finished != null) {
process.setFinishedTime(finished.getTime());
}
if (ipAddr != null) {
process.setPlannedIPAddress(ipAddr);
}

return process;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import cz.incad.kramerius.processes.annotations.Process;
import cz.incad.kramerius.processes.logging.LoggingLoader;
import cz.incad.kramerius.processes.utils.ProcessUtils;
import cz.incad.kramerius.security.impl.http.IsActionAllowedFromRequest;
import cz.incad.kramerius.utils.IPAddressUtils;

/**
* Process starting point
Expand Down Expand Up @@ -81,7 +81,7 @@ public static void main(String[] args) {
try {

String mainClass = System.getProperty(MAIN_CLASS_KEY);
String forwardIP = System.getProperty(IsActionAllowedFromRequest.X_IP_FORWARD);
//String forwardIP = System.getProperty(IPAddressUtils.X_IP_FORWARD);

outStream = createPrintStream(System.getProperty(SOUT_FILE));
errStream = createPrintStream(System.getProperty(SERR_FILE));
Expand Down Expand Up @@ -233,7 +233,7 @@ public static byte[] httpGet(String restURL) throws MalformedURLException, IOExc
URLConnection connection = url.openConnection();
// authentication token -> identify user
connection.addRequestProperty("auth-token",System.getProperty(AUTH_TOKEN_KEY));

connection.addRequestProperty(IPAddressUtils.X_IP_FORWARD, System.getProperty(IPAddressUtils.X_IP_FORWARD));
InputStream inputStream = connection.getInputStream();
byte[] buffer = new byte[1 << 12];
int read = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.Principal;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.configuration.Configuration;

import com.google.inject.Inject;
import com.google.inject.Provider;

Expand All @@ -42,26 +38,14 @@
import cz.incad.kramerius.security.User;
import cz.incad.kramerius.security.UserManager;
import cz.incad.kramerius.security.impl.UserImpl;
import cz.incad.kramerius.utils.IPAddressUtils;
import cz.incad.kramerius.utils.NetworkUtils;
import cz.incad.kramerius.utils.StringUtils;
import cz.incad.kramerius.utils.conf.KConfiguration;

public class IsActionAllowedFromRequest implements IsActionAllowed {

public static final Logger LOGGER = Logger.getLogger(IsActionAllowedFromRequest.class.getName());

public static final String X_IP_FORWARD = "X_IP_FORWARD";
static String[] LOCALHOSTS = {"127.0.0.1","localhost","0:0:0:0:0:0:0:1","::1"};
static {
try {
LOCALHOSTS = NetworkUtils.getLocalhostsAddress();
} catch (Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
LOCALHOSTS = new String[] {"127.0.0.1","localhost","0:0:0:0:0:0:0:1","::1"};
}
}


private Logger logger;
private Provider<HttpServletRequest> provider;

Expand Down Expand Up @@ -105,7 +89,7 @@ public boolean isActionAllowed(User user, String actionName, String pid,String s
public boolean[] isActionAllowedForAllPath(String actionName, String pid, String stream, ObjectPidsPath path) {
try {
User user = this.currentLoggedUser.get();
RightCriteriumContext ctx = this.ctxFactory.create(pid,stream, user, getRemoteHost(), getRemoteAddress(KConfiguration.getInstance().getConfiguration()));
RightCriteriumContext ctx = this.ctxFactory.create(pid,stream, user, getRemoteHost(), IPAddressUtils.getRemoteAddress(this.provider.get(), KConfiguration.getInstance().getConfiguration()));
EvaluatingResult[] evalResults = this.rightsManager.resolveAllPath(ctx, pid, path, actionName, user);
boolean[] results = new boolean[evalResults.length];
for (int i = 0; i < results.length; i++) {
Expand All @@ -118,35 +102,13 @@ public boolean[] isActionAllowedForAllPath(String actionName, String pid, String
}
}

String getRemoteAddress(Configuration conf) {
HttpServletRequest httpReq = this.provider.get();
String headerFowraded = httpReq.getHeader(X_IP_FORWARD);
if (StringUtils.isAnyString(headerFowraded) && matchConfigurationAddress(httpReq, conf)) {
return headerFowraded;
} else {
return httpReq.getRemoteAddr();
}
}


boolean matchConfigurationAddress(HttpServletRequest httpReq, Configuration conf) {
String remoteAddr = httpReq.getRemoteAddr();
List<String> forwaredEnabled = conf.getList("x_ip_forwared_enabled_for",Arrays.asList(LOCALHOSTS));
if (!forwaredEnabled.isEmpty()) {
for (String pattern : forwaredEnabled) {
if (remoteAddr.matches(pattern)) return true;
}
}
return false;
}

private String getRemoteHost() {
HttpServletRequest httpReq = this.provider.get();
return httpReq.getRemoteHost();
}

public boolean isAllowedInternalForFedoraDocuments(String actionName, String pid, String stream, ObjectPidsPath path, User user) throws RightCriteriumException {
RightCriteriumContext ctx = this.ctxFactory.create(pid, stream, user, getRemoteHost(), getRemoteAddress(KConfiguration.getInstance().getConfiguration()));
RightCriteriumContext ctx = this.ctxFactory.create(pid, stream, user, getRemoteHost(), IPAddressUtils.getRemoteAddress(this.provider.get(),KConfiguration.getInstance().getConfiguration()));
EvaluatingResult result = this.rightsManager.resolve(ctx, pid, path, actionName, user);
return result != null ? resultOfResult(result) : false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cz.incad.kramerius.processes.impl.ProcessStarter;
import cz.incad.kramerius.processes.utils.ProcessUtils;
import cz.incad.kramerius.utils.BasicAuthenticationFilter;
import cz.incad.kramerius.utils.IPAddressUtils;
import cz.incad.kramerius.utils.conf.KConfiguration;

import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -57,6 +58,10 @@ public ClientResponse handle(ClientRequest clientRequest) throws ClientHandlerEx
String pwd = System.getProperties().getProperty(PSWD_TOKEN);
BasicAuthenticationFilter.encodeUserAndPass(clientRequest, uname, pwd);
}
if (System.getProperties().containsKey(IPAddressUtils.X_IP_FORWARD)) {
clientRequest.getHeaders().add(IPAddressUtils.X_IP_FORWARD, System.getProperty(IPAddressUtils.X_IP_FORWARD));
}

return getNext().handle(clientRequest);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public static boolean columnExists(Connection con, String tableName, String colu
}
}



public static void tryClose(Connection c) {
try {
c.close();
Expand Down
Loading

2 comments on commit 07b4ee9

@leiblix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Děkuji za řešení chyby, kterou nahlásil kolega @Jenyk, ale bohužel tento commit momentálně rozbíjí build:

/home/rumanekm/workspace/ceskaexpedice/kramerius/rest/src/main/java/cz/incad/kramerius/rest/api/client/v50/admin/RightsClient.java:150: error: incompatible types: int cannot be converted to String
        jsonObj.put("role", new JSONObject(UsersAndRolesClient.role(3)));
                                                                    ^
/home/rumanekm/workspace/ceskaexpedice/kramerius/rest/src/main/java/cz/incad/kramerius/rest/api/client/v50/admin/RightsClient.java:173: error: incompatible types: int cannot be converted to String
        jsonObj.put("role", new JSONObject(UsersAndRolesClient.role(3)));
                                                                    ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /home/rumanekm/workspace/ceskaexpedice/kramerius/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/utils/SOLRUtils.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors
1 warning
:rest:compileJava FAILED

@pavel-stastny
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

28d8398

Ted by to melo byt v poradku.

Please sign in to comment.