Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bxc-4358 manage links service #1644

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class SingleUseKeyService {
*/
public String generate(String id) {
var key = getKey();
var expirationInMilliseconds = System.currentTimeMillis() + DAY_MILLISECONDS;
lock.lock();
sharonluong marked this conversation as resolved.
Show resolved Hide resolved
var expirationInMilliseconds = System.currentTimeMillis() + DAY_MILLISECONDS;
try (var csvPrinter = createCsvPrinter(CSV_HEADERS, csvPath)) {
csvPrinter.printRecord(id, key, expirationInMilliseconds);
} catch (Exception e) {
Expand All @@ -49,16 +49,19 @@ public String generate(String id) {
* Determines if a key is valid by seeing if it is in the CSV and if the expiration timestamp has not passed
* @param key access key for single use link
* @return true if key is in the CSV, otherwise false
* @throws IOException
*/
public boolean keyIsValid(String key) throws IOException {
var csvRecords = parseCsv(CSV_HEADERS, csvPath);
var currentMilliseconds = System.currentTimeMillis();
for (CSVRecord record : csvRecords) {
if (key.equals(record.get(ACCESS_KEY))) {
var expirationTimestamp = Long.parseLong(record.get(TIMESTAMP));
return currentMilliseconds <= expirationTimestamp;
public boolean keyIsValid(String key) {
try {
var csvRecords = parseCsv(CSV_HEADERS, csvPath);
var currentMilliseconds = System.currentTimeMillis();
for (CSVRecord record : csvRecords) {
if (key.equals(record.get(ACCESS_KEY))) {
var expirationTimestamp = Long.parseLong(record.get(TIMESTAMP));
return currentMilliseconds <= expirationTimestamp;
}
}
} catch (IOException e) {
throw new RepositoryException("Failed to determine if key is valid in Single Use Key CSV", e);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ public static List<CSVRecord> parseCsv(String[] headers, Path csvPath) throws IO
* @throws IOException
*/
public static CSVPrinter createCsvPrinter(String[] headers, Path csvPath) throws IOException {
var file = csvPath.toFile();
if (file.exists()) {
if (Files.exists(csvPath)) {
var writer = Files.newBufferedWriter(csvPath, StandardOpenOption.APPEND);
return new CSVPrinter(writer, CSVFormat.DEFAULT
.withHeader(headers));
//return new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(headers));
sharonluong marked this conversation as resolved.
Show resolved Hide resolved
return new CSVPrinter(writer, CSVFormat.DEFAULT.withSkipHeaderRecord());
} else {
return createNewCsvPrinter(headers, csvPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public void setup() throws IOException {
public void testGenerateNoCsvExists() throws IOException {
var key = singleUseKeyService.generate(UUID_TEST);
var newRecords = parseCsv(CSV_HEADERS, csvPath);
var currentMilliseconds = System.currentTimeMillis();
assertCsvContainsCorrectEntry(newRecords, UUID_TEST, key, currentMilliseconds);
assertCsvContainsCorrectEntry(newRecords, UUID_TEST, key);
assertEquals(1, newRecords.size());
}

@Test
Expand All @@ -56,10 +56,10 @@ public void testGenerateMultipleCallsForDifferentIds() throws IOException {
var key3 = singleUseKeyService.generate(UUID_3);

var newRecords = parseCsv(CSV_HEADERS, csvPath);
var currentMilliseconds = System.currentTimeMillis();
assertCsvContainsCorrectEntry(newRecords, UUID_1, key1, currentMilliseconds);
assertCsvContainsCorrectEntry(newRecords, UUID_2, key2, currentMilliseconds);
assertCsvContainsCorrectEntry(newRecords, UUID_3, key3, currentMilliseconds);
assertCsvContainsCorrectEntry(newRecords, UUID_1, key1);
assertCsvContainsCorrectEntry(newRecords, UUID_2, key2);
assertCsvContainsCorrectEntry(newRecords, UUID_3, key3);
assertEquals(3, newRecords.size());
}

@Test
Expand All @@ -69,10 +69,10 @@ public void testGenerateMultipleCallsForSameId() throws IOException {
var key3 = singleUseKeyService.generate(UUID_1);

var newRecords = parseCsv(CSV_HEADERS, csvPath);
var currentMilliseconds = System.currentTimeMillis();
assertCsvContainsCorrectEntry(newRecords, UUID_1, key1, currentMilliseconds);
assertCsvContainsCorrectEntry(newRecords, UUID_1, key2, currentMilliseconds);
assertCsvContainsCorrectEntry(newRecords, UUID_1, key3, currentMilliseconds);
assertCsvContainsCorrectEntry(newRecords, UUID_1, key1);
assertCsvContainsCorrectEntry(newRecords, UUID_1, key2);
assertCsvContainsCorrectEntry(newRecords, UUID_1, key3);
assertEquals(3, newRecords.size());

}

Expand Down Expand Up @@ -109,6 +109,7 @@ public void testInvalidate() throws IOException {

var records = parseCsv(CSV_HEADERS, csvPath);
assertDoesNotContainValue(records, SingleUseKeyService.ACCESS_KEY, key);
assertEquals(3, records.size());
}

@Test
Expand All @@ -120,6 +121,22 @@ public void testInvalidateWhenKeyIsNotPresent() throws IOException {

var records = parseCsv(CSV_HEADERS, csvPath);
assertDoesNotContainValue(records, SingleUseKeyService.ACCESS_KEY, key);
assertEquals(3, records.size());
}

@Test
public void testInvalidateMultipleTimes() throws IOException {
var key = SingleUseKeyService.getKey();
var expirationTimestamp = System.currentTimeMillis() + DAY_MILLISECONDS;
generateDefaultCsv(key, expirationTimestamp);
var key2 = singleUseKeyService.generate(UUID_TEST);
singleUseKeyService.invalidate(key);
singleUseKeyService.invalidate(key2);

var records = parseCsv(CSV_HEADERS, csvPath);
assertDoesNotContainValue(records, SingleUseKeyService.ACCESS_KEY, key);
assertDoesNotContainValue(records, SingleUseKeyService.ACCESS_KEY, key2);
assertEquals(3, records.size());
}

private void assertDoesNotContainValue(List<CSVRecord> csvRecords, String column, String value) {
Expand All @@ -131,15 +148,11 @@ private void assertDoesNotContainValue(List<CSVRecord> csvRecords, String column
}
}

private void assertCsvContainsCorrectEntry(List<CSVRecord> csvRecords,
String id, String key, long currentTimestamp) {
private void assertCsvContainsCorrectEntry(List<CSVRecord> csvRecords, String id, String key) {
for (CSVRecord record : csvRecords) {
if (key.equals(record.get(SingleUseKeyService.ACCESS_KEY))) {
assertEquals(id, record.get(SingleUseKeyService.ID));
// timestamp must not be null and must be in the future
var expirationTimestamp = record.get(SingleUseKeyService.TIMESTAMP);
assertNotNull(expirationTimestamp);
assertTrue(Long.parseLong(expirationTimestamp) > currentTimestamp);
sharonluong marked this conversation as resolved.
Show resolved Hide resolved
assertNotNull(record.get(SingleUseKeyService.TIMESTAMP));
return;
}
}
Expand Down
Loading