Revert "CreateAccount: Fail early when invalid SSH key is given"

This reverts commit b5d2b319dad13ccd87dd41667d6b6b0ed3489779.

Reason for revert: This change breaks user creation, if a SSH key
is added during the creation from at least stable-2.16 onwards. Also
it does not resolve the original issue per se, since it does still not
ensure that the complete account creation is only happening, if all
given data is valid.

Bug: Issue 11370
Change-Id: Ibed66e7ec3d901de7fc56b54fd1e349b35c8506d
diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/accounts/AccountIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/accounts/AccountIT.java
index 4623675e..ac71e54 100644
--- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/accounts/AccountIT.java
+++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/accounts/AccountIT.java
@@ -280,21 +280,6 @@
   }
 
   @Test
-  public void createWithInvalidSshKey() throws Exception {
-    AccountInput input = new AccountInput();
-    input.username = name("test");
-    input.sshKey = "invalid key";
-
-    // Invalid key should cause the creation to fail
-    BadRequestException thrown =
-        assertThrows(BadRequestException.class, () -> gApi.accounts().create(input));
-    assertThat(thrown).hasMessageThat().isEqualTo("Invalid SSH Key");
-
-    // The account should not have been created
-    assertThrows(ResourceNotFoundException.class, () -> gApi.accounts().id(input.username).get());
-  }
-
-  @Test
   public void createWithInvalidEmailAddress() throws Exception {
     AccountInput input = new AccountInput();
     input.username = name("test");
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/account/CreateAccount.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/CreateAccount.java
index 3fa3de1..ef1e8cc 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/account/CreateAccount.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/CreateAccount.java
@@ -141,15 +141,6 @@
       }
     }
 
-    if (input.sshKey != null) {
-      try {
-        authorizedKeys.addKey(id, input.sshKey);
-        sshKeyCache.evict(username);
-      } catch (InvalidSshKeyException e) {
-        throw new BadRequestException(e.getMessage());
-      }
-    }
-
     List<ExternalId> extIds = new ArrayList<>();
     extIds.add(extUser);
     for (AccountExternalIdCreator c : externalIdCreators) {
@@ -193,6 +184,15 @@
       }
     }
 
+    if (input.sshKey != null) {
+      try {
+        authorizedKeys.addKey(id, input.sshKey);
+        sshKeyCache.evict(username);
+      } catch (InvalidSshKeyException e) {
+        throw new BadRequestException(e.getMessage());
+      }
+    }
+
     AccountLoader loader = infoLoader.create(true);
     AccountInfo info = loader.get(id);
     loader.fill();