@Test public void testValidKey() { assertThat(_authorizer.isValidWorkApiKey(KEY1), is(true)); assertThat(_authorizer.isValidWorkApiKey(KEY3), is(false)); }
@Override public boolean isValidWorkApiKey(String apiKey) { if (_cacheApiKeys.getIfPresent(apiKey) != null) { return true; } String selectApiKeyRowString = String.format("SELECT COUNT(*) FROM %s WHERE %s = ?", TABLE_USERS, COLUMN_APIKEY); boolean authorized; try (PreparedStatement ps = _dbConn.prepareStatement(selectApiKeyRowString)) { ps.setString(1, apiKey); try (ResultSet rs = executeQuery(ps)) { if (rs == null) { return false; } rs.next(); authorized = rs.getInt(1) == 1; } } catch (SQLException e) { throw new BatfishException("Could not query users table", e); } if (authorized) { _cacheApiKeys.put(apiKey, Boolean.TRUE); return true; } _logger.infof("Authorizer: %s is NOT a valid key\n", apiKey); return false; }
DbAuthorizer implements Authorizer { @Override public boolean isValidWorkApiKey(String apiKey) { if (_cacheApiKeys.getIfPresent(apiKey) != null) { return true; } String selectApiKeyRowString = String.format("SELECT COUNT(*) FROM %s WHERE %s = ?", TABLE_USERS, COLUMN_APIKEY); boolean authorized; try (PreparedStatement ps = _dbConn.prepareStatement(selectApiKeyRowString)) { ps.setString(1, apiKey); try (ResultSet rs = executeQuery(ps)) { if (rs == null) { return false; } rs.next(); authorized = rs.getInt(1) == 1; } } catch (SQLException e) { throw new BatfishException("Could not query users table", e); } if (authorized) { _cacheApiKeys.put(apiKey, Boolean.TRUE); return true; } _logger.infof("Authorizer: %s is NOT a valid key\n", apiKey); return false; } }
DbAuthorizer implements Authorizer { @Override public boolean isValidWorkApiKey(String apiKey) { if (_cacheApiKeys.getIfPresent(apiKey) != null) { return true; } String selectApiKeyRowString = String.format("SELECT COUNT(*) FROM %s WHERE %s = ?", TABLE_USERS, COLUMN_APIKEY); boolean authorized; try (PreparedStatement ps = _dbConn.prepareStatement(selectApiKeyRowString)) { ps.setString(1, apiKey); try (ResultSet rs = executeQuery(ps)) { if (rs == null) { return false; } rs.next(); authorized = rs.getInt(1) == 1; } } catch (SQLException e) { throw new BatfishException("Could not query users table", e); } if (authorized) { _cacheApiKeys.put(apiKey, Boolean.TRUE); return true; } _logger.infof("Authorizer: %s is NOT a valid key\n", apiKey); return false; } DbAuthorizer(String connectionString, @Nullable String driverClass, long cacheTimeout); }
DbAuthorizer implements Authorizer { @Override public boolean isValidWorkApiKey(String apiKey) { if (_cacheApiKeys.getIfPresent(apiKey) != null) { return true; } String selectApiKeyRowString = String.format("SELECT COUNT(*) FROM %s WHERE %s = ?", TABLE_USERS, COLUMN_APIKEY); boolean authorized; try (PreparedStatement ps = _dbConn.prepareStatement(selectApiKeyRowString)) { ps.setString(1, apiKey); try (ResultSet rs = executeQuery(ps)) { if (rs == null) { return false; } rs.next(); authorized = rs.getInt(1) == 1; } } catch (SQLException e) { throw new BatfishException("Could not query users table", e); } if (authorized) { _cacheApiKeys.put(apiKey, Boolean.TRUE); return true; } _logger.infof("Authorizer: %s is NOT a valid key\n", apiKey); return false; } DbAuthorizer(String connectionString, @Nullable String driverClass, long cacheTimeout); static DbAuthorizer createFromSettings(Settings settings); @Override void authorizeContainer(String apiKey, String containerName); @Override boolean isAccessibleNetwork(String apiKey, String containerName, boolean logError); @Override boolean isValidWorkApiKey(String apiKey); @Override String toString(); }
DbAuthorizer implements Authorizer { @Override public boolean isValidWorkApiKey(String apiKey) { if (_cacheApiKeys.getIfPresent(apiKey) != null) { return true; } String selectApiKeyRowString = String.format("SELECT COUNT(*) FROM %s WHERE %s = ?", TABLE_USERS, COLUMN_APIKEY); boolean authorized; try (PreparedStatement ps = _dbConn.prepareStatement(selectApiKeyRowString)) { ps.setString(1, apiKey); try (ResultSet rs = executeQuery(ps)) { if (rs == null) { return false; } rs.next(); authorized = rs.getInt(1) == 1; } } catch (SQLException e) { throw new BatfishException("Could not query users table", e); } if (authorized) { _cacheApiKeys.put(apiKey, Boolean.TRUE); return true; } _logger.infof("Authorizer: %s is NOT a valid key\n", apiKey); return false; } DbAuthorizer(String connectionString, @Nullable String driverClass, long cacheTimeout); static DbAuthorizer createFromSettings(Settings settings); @Override void authorizeContainer(String apiKey, String containerName); @Override boolean isAccessibleNetwork(String apiKey, String containerName, boolean logError); @Override boolean isValidWorkApiKey(String apiKey); @Override String toString();  }