Merge branch 'stable-2.14' into stable-2.15

* stable-2.14:
  ElasticQueryAdapter: Move isV6 method to ElasticVersion and simplify
  Account.java: introduce compiled pattern and use where applicable
  Optimize USER_NAME_PATTERN string and its usage
  ElasticContainer: Test against Elasticsearch version 5.6.11

Change-Id: I9dc62b986007c3c06fd8efd2331aaab61f9c2dc7
This commit is contained in:
David Pursehouse
2018-09-04 11:16:23 +09:00
12 changed files with 44 additions and 26 deletions

View File

@@ -31,8 +31,8 @@ public class ElasticQueryAdapter {
ElasticQueryAdapter(ElasticVersion version) { ElasticQueryAdapter(ElasticVersion version) {
this.ignoreUnmapped = version == ElasticVersion.V2_4; this.ignoreUnmapped = version == ElasticVersion.V2_4;
this.usePostV5Type = isV6(version); this.usePostV5Type = version.isV6();
this.versionDiscoveryUrl = isV6(version) ? "%s*" : "%s*/_aliases"; this.versionDiscoveryUrl = version.isV6() ? "%s*" : "%s*/_aliases";
switch (version) { switch (version) {
case V5_6: case V5_6:
@@ -55,10 +55,6 @@ public class ElasticQueryAdapter {
} }
} }
private boolean isV6(ElasticVersion version) {
return version == ElasticVersion.V6_2 || version == ElasticVersion.V6_3;
}
void setIgnoreUnmapped(JsonObject properties) { void setIgnoreUnmapped(JsonObject properties) {
if (ignoreUnmapped) { if (ignoreUnmapped) {
properties.addProperty("ignore_unmapped", true); properties.addProperty("ignore_unmapped", true);

View File

@@ -54,6 +54,10 @@ public enum ElasticVersion {
return Joiner.on(", ").join(ElasticVersion.values()); return Joiner.on(", ").join(ElasticVersion.values());
} }
public boolean isV6() {
return version.startsWith("6.");
}
@Override @Override
public String toString() { public String toString() {
return version; return version;

View File

@@ -45,7 +45,7 @@ public class ElasticContainer<SELF extends ElasticContainer<SELF>> extends Gener
case V2_4: case V2_4:
return "elasticsearch:2.4.6-alpine"; return "elasticsearch:2.4.6-alpine";
case V5_6: case V5_6:
return "docker.elastic.co/elasticsearch/elasticsearch:5.6.10"; return "docker.elastic.co/elasticsearch/elasticsearch:5.6.11";
case V6_2: case V6_2:
return "docker.elastic.co/elasticsearch/elasticsearch-oss:6.2.4"; return "docker.elastic.co/elasticsearch/elasticsearch-oss:6.2.4";
case V6_3: case V6_3:

View File

@@ -46,4 +46,11 @@ public class ElasticVersionTest {
"Invalid version: [4.0.0]. Supported versions: " + ElasticVersion.supportedVersions()); "Invalid version: [4.0.0]. Supported versions: " + ElasticVersion.supportedVersions());
ElasticVersion.forVersion("4.0.0"); ElasticVersion.forVersion("4.0.0");
} }
@Test
public void version6() throws Exception {
assertThat(ElasticVersion.V6_2.isV6()).isTrue();
assertThat(ElasticVersion.V6_3.isV6()).isTrue();
assertThat(ElasticVersion.V5_6.isV6()).isFalse();
}
} }

View File

@@ -16,6 +16,7 @@ gwt_module(
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [ deps = [
"//gerrit-extension-api:client", "//gerrit-extension-api:client",
"//lib:guava",
"//lib:gwtorm-client", "//lib:gwtorm-client",
"//lib:gwtorm-client_src", "//lib:gwtorm-client_src",
], ],

View File

@@ -18,11 +18,13 @@ import static com.google.gerrit.reviewdb.client.RefNames.REFS_DRAFT_COMMENTS;
import static com.google.gerrit.reviewdb.client.RefNames.REFS_STARRED_CHANGES; import static com.google.gerrit.reviewdb.client.RefNames.REFS_STARRED_CHANGES;
import static com.google.gerrit.reviewdb.client.RefNames.REFS_USERS; import static com.google.gerrit.reviewdb.client.RefNames.REFS_USERS;
import com.google.common.annotations.GwtIncompatible;
import com.google.gerrit.extensions.client.DiffPreferencesInfo; import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import com.google.gerrit.extensions.client.GeneralPreferencesInfo; import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
import com.google.gwtorm.client.Column; import com.google.gwtorm.client.Column;
import com.google.gwtorm.client.IntKey; import com.google.gwtorm.client.IntKey;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.regex.Pattern;
/** /**
* Information about a single user. * Information about a single user.
@@ -46,15 +48,14 @@ import java.sql.Timestamp;
* </ul> * </ul>
*/ */
public final class Account { public final class Account {
public static final String USER_NAME_PATTERN_FIRST = "[a-zA-Z0-9]"; private static final String USER_NAME_COMMON_PATTERN = "a-zA-Z0-9";
public static final String USER_NAME_PATTERN_REST = "[a-zA-Z0-9._@-]"; public static final String USER_NAME_PATTERN_FIRST = "[" + USER_NAME_COMMON_PATTERN + "]";
public static final String USER_NAME_PATTERN_LAST = "[a-zA-Z0-9]"; public static final String USER_NAME_PATTERN_REST = "[" + USER_NAME_COMMON_PATTERN + "._@-]";
public static final String USER_NAME_PATTERN_LAST = USER_NAME_PATTERN_FIRST;
/** Regular expression that {@link #userName} must match. */ /** Regular expression that {@link #userName} must match. */
public static final String USER_NAME_PATTERN = public static final String USER_NAME_PATTERN =
"^" "^("
+ //
"("
+ // + //
USER_NAME_PATTERN_FIRST USER_NAME_PATTERN_FIRST
+ // + //
@@ -67,9 +68,10 @@ public final class Account {
+ // + //
USER_NAME_PATTERN_FIRST USER_NAME_PATTERN_FIRST
+ // + //
")" ")$";
+ //
"$"; @GwtIncompatible("Unemulated class java.util.regex.Pattern")
public static final Pattern USER_NAME_PATTERN_COMPILED = Pattern.compile(USER_NAME_PATTERN);
/** Key local to Gerrit to identify a user. */ /** Key local to Gerrit to identify a user. */
public static class Id extends IntKey<com.google.gwtorm.client.Key<?>> { public static class Id extends IntKey<com.google.gwtorm.client.Key<?>> {

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.server.account; package com.google.gerrit.server.account;
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN_COMPILED;
import static java.util.stream.Collectors.toSet; import static java.util.stream.Collectors.toSet;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
@@ -109,7 +110,7 @@ public class AccountResolver {
return Collections.emptySet(); return Collections.emptySet();
} }
if (nameOrEmail.matches(Account.USER_NAME_PATTERN)) { if (USER_NAME_PATTERN_COMPILED.matcher(nameOrEmail).matches()) {
AccountState who = byId.getByUsername(nameOrEmail); AccountState who = byId.getByUsername(nameOrEmail);
if (who != null) { if (who != null) {
return Collections.singleton(who.getAccount().getId()); return Collections.singleton(who.getAccount().getId());

View File

@@ -14,11 +14,11 @@
package com.google.gerrit.server.account; package com.google.gerrit.server.account;
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN_COMPILED;
import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME; import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME;
import com.google.gerrit.common.Nullable; import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.errors.NameAlreadyUsedException; import com.google.gerrit.common.errors.NameAlreadyUsedException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.IdentifiedUser; import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.externalids.ExternalId; import com.google.gerrit.server.account.externalids.ExternalId;
import com.google.gerrit.server.account.externalids.ExternalIds; import com.google.gerrit.server.account.externalids.ExternalIds;
@@ -32,7 +32,6 @@ import com.google.inject.assistedinject.Assisted;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.regex.Pattern;
import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.errors.ConfigInvalidException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -40,7 +39,6 @@ import org.slf4j.LoggerFactory;
/** Operation to change the username of an account. */ /** Operation to change the username of an account. */
public class ChangeUserName implements Callable<VoidResult> { public class ChangeUserName implements Callable<VoidResult> {
private static final Logger log = LoggerFactory.getLogger(ChangeUserName.class); private static final Logger log = LoggerFactory.getLogger(ChangeUserName.class);
private static final Pattern USER_NAME_PATTERN = Pattern.compile(Account.USER_NAME_PATTERN);
public static final String USERNAME_CANNOT_BE_CHANGED = "Username cannot be changed."; public static final String USERNAME_CANNOT_BE_CHANGED = "Username cannot be changed.";
@@ -84,7 +82,7 @@ public class ChangeUserName implements Callable<VoidResult> {
ExternalIdsUpdate externalIdsUpdate = externalIdsUpdateFactory.create(); ExternalIdsUpdate externalIdsUpdate = externalIdsUpdateFactory.create();
if (newUsername != null && !newUsername.isEmpty()) { if (newUsername != null && !newUsername.isEmpty()) {
if (!USER_NAME_PATTERN.matcher(newUsername).matches()) { if (!USER_NAME_PATTERN_COMPILED.matcher(newUsername).matches()) {
throw new InvalidUserNameException(); throw new InvalidUserNameException();
} }

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.account; package com.google.gerrit.server.account;
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN;
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN_COMPILED;
import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_MAILTO; import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_MAILTO;
import com.google.gerrit.common.Nullable; import com.google.gerrit.common.Nullable;
@@ -120,9 +122,9 @@ public class CreateAccount implements RestModifyView<TopLevelResource, AccountIn
throw new BadRequestException("username must match URL"); throw new BadRequestException("username must match URL");
} }
if (!username.matches(Account.USER_NAME_PATTERN)) { if (!USER_NAME_PATTERN_COMPILED.matcher(username).matches()) {
throw new BadRequestException( throw new BadRequestException(
"Username '" + username + "' must contain only letters, numbers, _, - or ."); "Username '" + username + "' must comply with [" + USER_NAME_PATTERN + "] pattern.");
} }
Set<AccountGroup.UUID> groups = parseGroups(input.groups); Set<AccountGroup.UUID> groups = parseGroups(input.groups);

View File

@@ -16,7 +16,10 @@ package com.google.gerrit.server.account;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
/** Error indicating the SSH user name does not match {@link Account#USER_NAME_PATTERN} pattern. */ /**
* Error indicating the SSH user name does not match {@link Account#USER_NAME_PATTERN_COMPILED}
* pattern.
*/
public class InvalidUserNameException extends Exception { public class InvalidUserNameException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.args4j; package com.google.gerrit.server.args4j;
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN_COMPILED;
import com.google.gerrit.extensions.client.AuthType; import com.google.gerrit.extensions.client.AuthType;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.account.AccountException; import com.google.gerrit.server.account.AccountException;
@@ -90,7 +92,7 @@ public class AccountIdHandler extends OptionHandler<Account.Id> {
} }
private Account.Id createAccountByLdap(String user) throws CmdLineException, IOException { private Account.Id createAccountByLdap(String user) throws CmdLineException, IOException {
if (!user.matches(Account.USER_NAME_PATTERN)) { if (!USER_NAME_PATTERN_COMPILED.matcher(user).matches()) {
throw new CmdLineException(owner, "user \"" + user + "\" not found"); throw new CmdLineException(owner, "user \"" + user + "\" not found");
} }

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.group; package com.google.gerrit.server.group;
import static com.google.gerrit.reviewdb.client.Account.USER_NAME_PATTERN_COMPILED;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
@@ -181,7 +183,7 @@ public class AddMembers implements RestModifyView<GroupResource, Input> {
} }
private Account createAccountByLdap(String user) throws IOException { private Account createAccountByLdap(String user) throws IOException {
if (!user.matches(Account.USER_NAME_PATTERN)) { if (!USER_NAME_PATTERN_COMPILED.matcher(user).matches()) {
return null; return null;
} }