Skip to content

Commit cdc0ffe

Browse files
authored
Use filename regex in CopyDetailReportsAction (#3102)
No registrars have an underscore in their name, but as far as I'm aware there's nothing explicitly preventing that.
1 parent 7c23413 commit cdc0ffe

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

core/src/main/java/google/registry/reporting/billing/CopyDetailReportsAction.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
2121
import static java.util.stream.Collectors.joining;
2222

2323
import com.google.cloud.storage.BlobId;
24-
import com.google.common.base.Splitter;
2524
import com.google.common.collect.ImmutableList;
2625
import com.google.common.collect.ImmutableMultimap;
27-
import com.google.common.collect.Iterables;
2826
import com.google.common.flogger.FluentLogger;
2927
import com.google.common.io.ByteStreams;
3028
import com.google.common.net.MediaType;
@@ -41,6 +39,8 @@
4139
import java.io.IOException;
4240
import java.io.InputStream;
4341
import java.util.Optional;
42+
import java.util.regex.Matcher;
43+
import java.util.regex.Pattern;
4444

4545
/** Copy all registrar detail reports in a given bucket's subdirectory from GCS to Drive. */
4646
@Action(
@@ -54,6 +54,9 @@ public final class CopyDetailReportsAction implements Runnable {
5454

5555
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
5656

57+
private static final Pattern FILENAME_PATTERN =
58+
Pattern.compile("^invoice_details_[0-9]{4}-[0-9]{2}_(.+)_.+\\.csv$");
59+
5760
private final String billingBucket;
5861
private final String invoiceDirectoryPrefix;
5962
private final DriveConnection driveConnection;
@@ -101,8 +104,13 @@ public void run() {
101104
new ImmutableMultimap.Builder<>();
102105
for (String detailReportName : detailReportObjectNames) {
103106
// The standard report format is "invoice_details_yyyy-MM_registrarId_tld.csv
104-
// TODO(larryruili): Determine a safer way of enforcing this.
105-
String registrarId = Iterables.get(Splitter.on('_').split(detailReportName), 3);
107+
Matcher matcher = FILENAME_PATTERN.matcher(detailReportName);
108+
if (!matcher.matches()) {
109+
logger.atWarning().log(
110+
"Detail report filename '%s' does not match the expected pattern.", detailReportName);
111+
continue;
112+
}
113+
String registrarId = matcher.group(1);
106114
Optional<Registrar> registrar = Registrar.loadByRegistrarId(registrarId);
107115
if (registrar.isEmpty()) {
108116
logger.atWarning().log(

core/src/test/java/google/registry/reporting/billing/CopyDetailReportsActionTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,38 @@ void testFail_noRegistrarFolderId_doesntCopy() throws IOException {
248248
action.run();
249249
verifyNoInteractions(driveConnection);
250250
}
251+
252+
@Test
253+
void testSuccess_registrarIdWithUnderscores() throws IOException {
254+
persistResource(
255+
loadRegistrar("TheRegistrar")
256+
.asBuilder()
257+
.setRegistrarId("The_Registrar")
258+
.setDriveFolderId("0B-99999")
259+
.build());
260+
261+
gcsUtils.createFromBytes(
262+
BlobId.of("test-bucket", "results/invoice_details_2017-10_The_Registrar_test.csv"),
263+
"hello,world\n1,2".getBytes(UTF_8));
264+
265+
action.run();
266+
verify(driveConnection)
267+
.createOrUpdateFile(
268+
"invoice_details_2017-10_The_Registrar_test.csv",
269+
MediaType.CSV_UTF_8,
270+
"0B-99999",
271+
"hello,world\n1,2".getBytes(UTF_8));
272+
assertThat(response.getStatus()).isEqualTo(SC_OK);
273+
}
274+
275+
@Test
276+
void testSuccess_invalidFilenamePattern_skipped() throws IOException {
277+
gcsUtils.createFromBytes(
278+
BlobId.of("test-bucket", "results/invoice_details_2017-10.csv"),
279+
"hello,world\n1,2".getBytes(UTF_8));
280+
281+
action.run();
282+
verifyNoInteractions(driveConnection);
283+
assertThat(response.getStatus()).isEqualTo(SC_OK);
284+
}
251285
}

0 commit comments

Comments
 (0)