Skip to content

Commit

Permalink
Merge pull request #149 from batfish/unstable
Browse files Browse the repository at this point in the history
Unstable
  • Loading branch information
arifogel committed Jun 20, 2017
2 parents ef2cf71 + 017a65f commit 6453ed0
Show file tree
Hide file tree
Showing 29 changed files with 125 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ public Pair<WorkStatusCode, String> getWorkStatus(UUID parseWorkUUID) {
}
}

public String initContainer(String containerPrefix) {
public String initContainer(String containerName, String containerPrefix) {
try {
Client client = getClientBuilder().build();
WebTarget webTarget = getTarget(client,
Expand Down
6 changes: 3 additions & 3 deletions projects/batfish-client/src/org/batfish/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ private boolean help(List<String> parameters) {
private boolean initContainer(String[] words) {
String containerPrefix = (words.length > 1) ? words[1]
: DEFAULT_CONTAINER_PREFIX;
_currContainerName = _workHelper.initContainer(containerPrefix);
_currContainerName = _workHelper.initContainer(null, containerPrefix);
if (_currContainerName == null) {
_logger.errorf("Could not init container\n");
return false;
Expand Down Expand Up @@ -1201,8 +1201,8 @@ private boolean initTestrig(FileWriter outWriter, List<String> parameters,

// initialize the container if it hasn't been init'd before
if (!isSetContainer(false)) {
_currContainerName = _workHelper
.initContainer(DEFAULT_CONTAINER_PREFIX);
_currContainerName = _workHelper.initContainer(null,
DEFAULT_CONTAINER_PREFIX);
if (_currContainerName == null) {
_logger.errorf("Could not init container\n");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public final class Version {

private static final String UNKNOWN_VERSION = "0.0.0";

private static final String VERSION = "0.26.0";
private static final String VERSION = "0.27.0";

public static void checkCompatibleVersion(String myName, String otherName,
String otherVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public static String sha256Digest(String saltedSecret) {
return sha256;
}

public static void startSSLServer(ResourceConfig rcWork, URI mgrUri,
public static void startSSLServer(ResourceConfig resourceConfig, URI mgrUri,
String keystoreFilename, String keystorePassword,
boolean trustAllCerts, String truststoreFilename,
String truststorePassword, Class<?> configurationLocatorClass) {
Expand Down Expand Up @@ -578,7 +578,7 @@ public static void startSSLServer(ResourceConfig rcWork, URI mgrUri,
sslCon.setTrustStorePass(truststorePassword);
}
boolean verifyClient = !trustAllCerts;
GrizzlyHttpServerFactory.createHttpServer(mgrUri, rcWork, true,
GrizzlyHttpServerFactory.createHttpServer(mgrUri, resourceConfig, true,
new SSLEngineConfigurator(sslCon, false, verifyClient, false));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ public class CrossDomainFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext creq,
ContainerResponseContext cres) {
// cres.getHeaders().add("Access-Control-Allow-Origin", "*");
// cres.getHeaders().add("Access-Control-Allow-Headers",
// "origin, content-type, accept, authorization");
// cres.getHeaders().add("Access-Control-Allow-Credentials", "true");
// cres.getHeaders().add("Access-Control-Allow-Methods",
// "GET, POST, PUT, DELETE, OPTIONS, HEAD");
cres.getHeaders().add("Access-Control-Allow-Origin", "*");
cres.getHeaders().add("Access-Control-Allow-Headers",
"origin, content-type, accept, authorization");
"Host, User-Agent, Accept, Authorization, Accept-Language, Accept-Encoding, Content-Type, Referer, Content-Length, Origin, DNT, Connection");
cres.getHeaders().add("Access-Control-Allow-Credentials", "true");
cres.getHeaders().add("Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS, HEAD");
Expand Down
4 changes: 2 additions & 2 deletions projects/coordinator/src/org/batfish/coordinator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static void initPoolManager() {

ResourceConfig rcPool = new ResourceConfig(PoolMgrService.class)
.register(new JettisonFeature()).register(MultiPartFeature.class)
.register(org.batfish.coordinator.CrossDomainFilter.class);
.register(CrossDomainFilter.class);

if (_settings.getSslPoolDisable()) {
URI poolMgrUri = UriBuilder
Expand Down Expand Up @@ -109,7 +109,7 @@ private static void initPoolManager() {
private static void initWorkManager() {
ResourceConfig rcWork = new ResourceConfig(WorkMgrService.class)
.register(new JettisonFeature()).register(MultiPartFeature.class)
.register(org.batfish.coordinator.CrossDomainFilter.class);
.register(CrossDomainFilter.class);

if (_settings.getSslWorkDisable()) {
URI workMgrUri = UriBuilder
Expand Down
8 changes: 5 additions & 3 deletions projects/coordinator/src/org/batfish/coordinator/WorkMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,11 @@ public QueuedWork getWork(UUID workItemId) {
return _workQueueMgr.getWork(workItemId);
}

public String initContainer(String containerPrefix) throws Exception {

String containerName = containerPrefix + "_" + UUID.randomUUID();
public String initContainer(String containerName, String containerPrefix)
throws Exception {
if (containerName == null || containerName.equals("")) {
containerName = containerPrefix + "_" + UUID.randomUUID();
}

File containerDir = Paths
.get(Main.getSettings().getContainersLocation(), containerName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,25 +713,28 @@ public JSONArray getWorkStatus(
public JSONArray initContainer(
@FormDataParam(CoordConsts.SVC_KEY_API_KEY) String apiKey,
@FormDataParam(CoordConsts.SVC_KEY_VERSION) String clientVersion,
@FormDataParam(CoordConsts.SVC_KEY_CONTAINER_NAME) String containerName,
@FormDataParam(CoordConsts.SVC_KEY_CONTAINER_PREFIX) String containerPrefix) {
try {
_logger.info("WMS:initContainer " + containerPrefix + "\n");

checkStringParam(apiKey, "API key");
checkStringParam(clientVersion, "Client version");
checkStringParam(containerPrefix, "Container prefix");
if (containerName == null || containerName.equals("")) {
checkStringParam(containerPrefix, "Container prefix");
}

checkApiKeyValidity(apiKey);
checkClientVersion(clientVersion);

String containerName = Main.getWorkMgr()
.initContainer(containerPrefix);
String outputContainerName = Main.getWorkMgr()
.initContainer(containerName, containerPrefix);

Main.getAuthorizer().authorizeContainer(apiKey, containerName);
Main.getAuthorizer().authorizeContainer(apiKey, outputContainerName);

return new JSONArray(
Arrays.asList(CoordConsts.SVC_KEY_SUCCESS, (new JSONObject()
.put(CoordConsts.SVC_KEY_CONTAINER_NAME, containerName))));
return new JSONArray(Arrays.asList(CoordConsts.SVC_KEY_SUCCESS,
(new JSONObject().put(CoordConsts.SVC_KEY_CONTAINER_NAME,
outputContainerName))));
}
catch (FileExistsException | FileNotFoundException
| IllegalArgumentException | AccessControlException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -39,9 +39,6 @@ public class DbAuthorizer implements Authorizer {
private Connection _dbConn;
private BatfishLogger _logger;

private java.text.SimpleDateFormat DateFormatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");

public DbAuthorizer() throws SQLException {
_logger = Main.getLogger();
openDbConnection();
Expand All @@ -55,16 +52,19 @@ public void authorizeContainer(String apiKey, String containerName)

// add the container if it does not exist; update datelastaccessed if it
// does
Date now = new Date();
String insertQuery = String.format(
"INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s') "
+ " ON DUPLICATE KEY UPDATE %s = '%s'",
java.sql.Date now = new java.sql.Date(new Date().getTime());
String insertContainersString = String.format(
"INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?) "
+ " ON DUPLICATE KEY UPDATE %s = ?",
TABLE_CONTAINERS, COLUMN_CONTAINER_NAME, COLUMN_DATE_CREATED,
COLUMN_DATE_LAST_ACCESSED, containerName, DateFormatter.format(now),
DateFormatter.format(now), COLUMN_DATE_LAST_ACCESSED,
DateFormatter.format(now));

int numInsertRows = executeUpdate(insertQuery);
COLUMN_DATE_LAST_ACCESSED, COLUMN_DATE_LAST_ACCESSED);
PreparedStatement insertContainers = _dbConn
.prepareStatement(insertContainersString);
insertContainers.setString(1, containerName);
insertContainers.setDate(2, now);
insertContainers.setDate(3, now);
insertContainers.setDate(4, now);
int numInsertRows = executeUpdate(insertContainers);

// MySQL says 2 rows impacted when an old row is updated; otherwise it
// says 1
Expand All @@ -77,12 +77,14 @@ public void authorizeContainer(String apiKey, String containerName)
return;
}

String query = String.format(
"INSERT INTO %s (%s, %s) VALUES ('%s', '%s')", TABLE_PERMISSIONS,
COLUMN_APIKEY, COLUMN_CONTAINER_NAME, apiKey, containerName);

int numRows = executeUpdate(query);

String insertPermissionsString = String.format(
"INSERT INTO %s (%s, %s) VALUES (?, ?)", TABLE_PERMISSIONS,
COLUMN_APIKEY, COLUMN_CONTAINER_NAME);
PreparedStatement insertPermissions = _dbConn
.prepareStatement(insertPermissionsString);
insertPermissions.setString(1, apiKey);
insertPermissions.setString(2, containerName);
int numRows = executeUpdate(insertPermissions);
if (numRows > 0) {
String cacheKey = getPermsCacheKey(apiKey, containerName);
insertInCache(_cachePermissions, cacheKey);
Expand All @@ -91,18 +93,18 @@ public void authorizeContainer(String apiKey, String containerName)

}

private synchronized ResultSet executeQuery(String query) {
private synchronized ResultSet executeQuery(PreparedStatement query) {
int triesLeft = MAX_DB_TRIES;

String queryString = query.toString();
while (triesLeft > 0) {
triesLeft--;
try {
Statement stmt = _dbConn.createStatement();
return stmt.executeQuery(query);
_logger.debugf("Executing SQL query: %s\n", queryString);
return query.executeQuery();
}
catch (SQLException e) {
_logger.errorf("SQLException while executing query '%s': %s", query,
e.getMessage());
_logger.errorf("SQLException while executing query '%s': %s",
queryString, e.getMessage());
_logger.errorf("Tries left = %d\n", triesLeft);

if (triesLeft > 0) {
Expand All @@ -116,23 +118,26 @@ private synchronized ResultSet executeQuery(String query) {
}
}
}
catch (Exception e) {
throw new BatfishException("well shit");
}
}

return null;
}

private synchronized int executeUpdate(String query) {
private synchronized int executeUpdate(PreparedStatement update) {
int triesLeft = MAX_DB_TRIES;

String updateString = update.toString();
while (triesLeft > 0) {
triesLeft--;
try {
Statement stmt = _dbConn.createStatement();
return stmt.executeUpdate(query);
_logger.debugf("Executing SQL update: %s\n", updateString);
return update.executeUpdate();
}
catch (SQLException e) {
_logger.errorf("SQLException while executing query '%s': %s", query,
e.getMessage());
_logger.errorf("SQLException while executing query '%s': %s",
updateString, e.getMessage());
_logger.errorf("Tries left = %d\n", triesLeft);

if (triesLeft > 0) {
Expand Down Expand Up @@ -171,23 +176,28 @@ public boolean isAccessibleContainer(String apiKey, String containerName,
return true;
}

String query = String.format(
"SELECT * FROM %s WHERE %s = '%s' AND %s = '%s'", TABLE_PERMISSIONS,
COLUMN_APIKEY, apiKey, COLUMN_CONTAINER_NAME, containerName);
String selectPermittedContainerString = String.format(
"SELECT * FROM %s WHERE %s = ? AND %s = ?", TABLE_PERMISSIONS,
COLUMN_APIKEY, COLUMN_CONTAINER_NAME);
PreparedStatement ps = _dbConn
.prepareStatement(selectPermittedContainerString);
ps.setString(1, apiKey);
ps.setString(2, containerName);

ResultSet rs = executeQuery(query);
ResultSet rs = executeQuery(ps);

if (rs != null && rs.first()) {
insertInCache(_cachePermissions, cacheKey);

// a valid access was made; update datelastaccessed
Date now = new Date();
String updateQuery = String.format(
"UPDATE %s SET %s='%s' WHERE %s='%s'", TABLE_CONTAINERS,
COLUMN_DATE_LAST_ACCESSED, DateFormatter.format(now),
COLUMN_CONTAINER_NAME, containerName);

executeUpdate(updateQuery);
java.sql.Date now = new java.sql.Date(new Date().getTime());
String updatePsString = String.format("UPDATE %s SET %s=? WHERE %s=?",
TABLE_CONTAINERS, COLUMN_DATE_LAST_ACCESSED,
COLUMN_CONTAINER_NAME);
PreparedStatement updatePs = _dbConn.prepareStatement(updatePsString);
updatePs.setDate(1, now);
updatePs.setString(2, containerName);
executeUpdate(updatePs);

return true;
}
Expand Down Expand Up @@ -223,10 +233,12 @@ public boolean isValidWorkApiKey(String apiKey) throws Exception {
return true;
}

String query = String.format("SELECT * FROM %s WHERE %s = '%s'",
TABLE_USERS, COLUMN_APIKEY, apiKey);
String selectApiKeyRowString = String.format(
"SELECT * FROM %s WHERE %s = ?", TABLE_USERS, COLUMN_APIKEY);
PreparedStatement ps = _dbConn.prepareStatement(selectApiKeyRowString);
ps.setString(1, apiKey);

ResultSet rs = executeQuery(query);
ResultSet rs = executeQuery(ps);

if (rs != null && rs.first()) {
insertInCache(_cacheApiKeys, apiKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private void initConfigDefaults() {
CoordConsts.SVC_CFG_WORK_SSL_DISABLE);
setDefaultProperty(ARG_SSL_WORK_KEYSTORE_FILE, null);
setDefaultProperty(ARG_SSL_WORK_KEYSTORE_PASSWORD, null);
setDefaultProperty(ARG_SSL_WORK_TRUST_ALL_CERTS, false);
setDefaultProperty(ARG_SSL_WORK_TRUST_ALL_CERTS, true);
setDefaultProperty(ARG_SSL_WORK_TRUSTSTORE_FILE, null);
setDefaultProperty(ARG_SSL_WORK_TRUSTSTORE_PASSWORD, null);
// setDefaultProperty(ARG_SSL_KEYSTORE_FILE, "selfsigned.jks");
Expand Down
4 changes: 2 additions & 2 deletions test_rigs/parsing-tests/dc-as-reuse.ref
Original file line number Diff line number Diff line change
Expand Up @@ -4680,7 +4680,7 @@
]
}
},
"version" : "0.26.0"
"version" : "0.27.0"
},
{
"class" : "org.batfish.datamodel.answers.ConvertConfigurationAnswerElement",
Expand Down Expand Up @@ -4836,7 +4836,7 @@
}
}
},
"version" : "0.26.0"
"version" : "0.27.0"
},
{
"class" : "org.batfish.datamodel.answers.InitInfoAnswerElement",
Expand Down
4 changes: 2 additions & 2 deletions test_rigs/parsing-tests/disable-as-suppression.ref
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@
]
}
},
"version" : "0.26.0"
"version" : "0.27.0"
},
{
"class" : "org.batfish.datamodel.answers.ConvertConfigurationAnswerElement",
"version" : "0.26.0"
"version" : "0.27.0"
},
{
"class" : "org.batfish.datamodel.answers.InitInfoAnswerElement",
Expand Down
4 changes: 2 additions & 2 deletions test_rigs/parsing-tests/example-juniper.ref
Original file line number Diff line number Diff line change
Expand Up @@ -10365,7 +10365,7 @@
]
}
},
"version" : "0.26.0"
"version" : "0.27.0"
},
{
"class" : "org.batfish.datamodel.answers.ConvertConfigurationAnswerElement",
Expand Down Expand Up @@ -10494,7 +10494,7 @@
}
}
},
"version" : "0.26.0",
"version" : "0.27.0",
"warnings" : {
"as1border1" : {
"Red flags" : {
Expand Down

0 comments on commit 6453ed0

Please sign in to comment.