Ensure automatically generated job IDs meet the validation criteria (elastic/elasticsearch#586)

Original commit: elastic/x-pack-elasticsearch@34b67cb509
This commit is contained in:
David Roberts 2016-12-20 13:53:29 +00:00 committed by GitHub
parent 5b029ee595
commit 3383c67cf2
2 changed files with 16 additions and 1 deletions

View File

@ -693,7 +693,16 @@ public class Job extends AbstractDiffable<Job> implements Writeable, ToXContent
Date lastDataTime; Date lastDataTime;
String modelSnapshotId; String modelSnapshotId;
if (fromApi) { if (fromApi) {
id = this.id == null ? UUIDs.base64UUID().toLowerCase(Locale.ROOT): this.id; if (this.id != null) {
id = this.id;
} else {
// Base64 UUIDs are not necessarily valid job IDs
id = "auto-" + UUIDs.base64UUID().toLowerCase(Locale.ROOT).replaceAll("/\\+=", "_");
if (id.endsWith("_")) {
// Job IDs cannot end with underscores
id = id.substring(0, id.length() - 1) + "z";
}
}
createTime = this.createTime == null ? new Date() : this.createTime; createTime = this.createTime == null ? new Date() : this.createTime;
finishedTime = null; finishedTime = null;
lastDataTime = null; lastDataTime = null;

View File

@ -82,6 +82,12 @@ public class JobTests extends AbstractSerializingTestCase<Job> {
assertNotNull(buildJobBuilder(null).build(true).getId()); // test auto id generation assertNotNull(buildJobBuilder(null).build(true).getId()); // test auto id generation
} }
public void testNoIdStartsWithAuto() {
String autoId = buildJobBuilder(null).build(true).getId();
assertTrue(autoId, autoId.startsWith("auto-"));
assertFalse(autoId, autoId.endsWith("_"));
}
public void testEquals_GivenDifferentClass() { public void testEquals_GivenDifferentClass() {
Job job = buildJobBuilder("foo").build(); Job job = buildJobBuilder("foo").build();
assertFalse(job.equals("a string")); assertFalse(job.equals("a string"));