Write account.config for initial admin user

When an initial admin user is created by the InitAdminUser init step we
only created a user branch in the All-Users repository, but we forgot to
write the account.config file to it.

Change-Id: I09cfc058ab3aaeaa2fe2adf38c779f6f672eef18
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin 2017-07-07 13:27:37 +02:00
parent e86e4d370c
commit 530cefa02c
4 changed files with 84 additions and 78 deletions

View File

@ -15,25 +15,36 @@
package com.google.gerrit.pgm.init;
import static com.google.common.base.Preconditions.checkArgument;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.pgm.init.api.InitFlags;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.GerritPersonIdentProvider;
import com.google.gerrit.server.account.AccountConfig;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.account.AccountsUpdate;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheEditor;
import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.RefUpdate.Result;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryCache.FileKey;
import org.eclipse.jgit.util.FS;
@ -57,17 +68,48 @@ public class AccountsOnInit {
if (path != null) {
try (Repository repo = new FileRepository(path);
ObjectInserter oi = repo.newObjectInserter()) {
PersonIdent serverIdent = new GerritPersonIdentProvider(flags.cfg).get();
AccountsUpdate.createUserBranch(
repo,
new Project.NameKey(allUsers),
GitReferenceUpdated.DISABLED,
null,
oi,
serverIdent,
serverIdent,
account.getId(),
account.getRegisteredOn());
PersonIdent ident =
new PersonIdent(
new GerritPersonIdentProvider(flags.cfg).get(), account.getRegisteredOn());
Config accountConfig = new Config();
AccountConfig.writeToConfig(account, accountConfig);
DirCache newTree = DirCache.newInCore();
DirCacheEditor editor = newTree.editor();
final ObjectId blobId =
oi.insert(Constants.OBJ_BLOB, accountConfig.toText().getBytes(UTF_8));
editor.add(
new PathEdit(AccountConfig.ACCOUNT_CONFIG) {
@Override
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.REGULAR_FILE);
ent.setObjectId(blobId);
}
});
editor.finish();
ObjectId treeId = newTree.writeTree(oi);
CommitBuilder cb = new CommitBuilder();
cb.setTreeId(treeId);
cb.setCommitter(ident);
cb.setAuthor(ident);
cb.setMessage("Create Account");
ObjectId id = oi.insert(cb);
oi.flush();
String refName = RefNames.refsUsers(account.getId());
RefUpdate ru = repo.updateRef(refName);
ru.setExpectedOldObjectId(ObjectId.zeroId());
ru.setNewObjectId(id);
ru.setRefLogIdent(ident);
ru.setRefLogMessage("Create Account", false);
Result result = ru.update();
if (result != Result.NEW) {
throw new IOException(
String.format("Failed to update ref %s: %s", refName, result.name()));
}
}
}
}

View File

@ -177,12 +177,16 @@ public class AccountConfig extends VersionedMetaData implements ValidationError.
}
Config cfg = readConfig(ACCOUNT_CONFIG);
writeToConfig(account, cfg);
saveConfig(ACCOUNT_CONFIG, cfg);
return true;
}
public static void writeToConfig(Account account, Config cfg) {
setActive(cfg, account.isActive());
set(cfg, KEY_FULL_NAME, account.getFullName());
set(cfg, KEY_PREFERRED_EMAIL, account.getPreferredEmail());
set(cfg, KEY_STATUS, account.getStatus());
saveConfig(ACCOUNT_CONFIG, cfg);
return true;
}
/**

View File

@ -37,14 +37,10 @@ import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.function.Consumer;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
@ -333,52 +329,6 @@ public class AccountsUpdate {
deleteUserBranch(accountId);
}
public static void createUserBranch(
Repository repo,
Project.NameKey project,
GitReferenceUpdated gitRefUpdated,
@Nullable IdentifiedUser user,
ObjectInserter oi,
PersonIdent committerIdent,
PersonIdent authorIdent,
Account.Id accountId,
Timestamp registeredOn)
throws IOException {
ObjectId id = createInitialEmptyCommit(oi, committerIdent, authorIdent, registeredOn);
String refName = RefNames.refsUsers(accountId);
RefUpdate ru = repo.updateRef(refName);
ru.setExpectedOldObjectId(ObjectId.zeroId());
ru.setNewObjectId(id);
ru.setRefLogIdent(committerIdent);
ru.setRefLogMessage("Create Account", false);
Result result = ru.update();
if (result != Result.NEW) {
throw new IOException(String.format("Failed to update ref %s: %s", refName, result.name()));
}
gitRefUpdated.fire(project, ru, user != null ? user.getAccount() : null);
}
private static ObjectId createInitialEmptyCommit(
ObjectInserter oi,
PersonIdent committerIdent,
PersonIdent authorIdent,
Timestamp registrationDate)
throws IOException {
CommitBuilder cb = new CommitBuilder();
cb.setTreeId(emptyTree(oi));
cb.setCommitter(new PersonIdent(committerIdent, registrationDate));
cb.setAuthor(new PersonIdent(authorIdent, registrationDate));
cb.setMessage("Create Account");
ObjectId id = oi.insert(cb);
oi.flush();
return id;
}
private static ObjectId emptyTree(ObjectInserter oi) throws IOException {
return oi.insert(Constants.OBJ_TREE, new byte[] {});
}
private void deleteUserBranch(Account.Id accountId) throws IOException {
try (Repository repo = repoManager.openRepository(allUsersName)) {
deleteUserBranch(repo, allUsersName, gitRefUpdated, currentUser, committerIdent, accountId);

View File

@ -18,9 +18,7 @@ import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.account.AccountsUpdate;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
@ -88,16 +86,7 @@ public class Schema_146 extends SchemaVersion {
if (ref != null) {
rewriteUserBranch(repo, rw, oi, emptyTree, ref, e.getValue());
} else {
AccountsUpdate.createUserBranch(
repo,
allUsersName,
GitReferenceUpdated.DISABLED,
null,
oi,
serverIdent,
serverIdent,
e.getKey(),
e.getValue());
createUserBranch(repo, oi, emptyTree, e.getKey(), e.getValue());
}
}
} catch (IOException e) {
@ -151,6 +140,27 @@ public class Schema_146 extends SchemaVersion {
}
}
public void createUserBranch(
Repository repo,
ObjectInserter oi,
ObjectId emptyTree,
Account.Id accountId,
Timestamp registeredOn)
throws IOException {
ObjectId id = createInitialEmptyCommit(oi, emptyTree, registeredOn);
String refName = RefNames.refsUsers(accountId);
RefUpdate ru = repo.updateRef(refName);
ru.setExpectedOldObjectId(ObjectId.zeroId());
ru.setNewObjectId(id);
ru.setRefLogIdent(serverIdent);
ru.setRefLogMessage(CREATE_ACCOUNT_MSG, false);
Result result = ru.update();
if (result != Result.NEW) {
throw new IOException(String.format("Failed to update ref %s: %s", refName, result.name()));
}
}
private ObjectId createInitialEmptyCommit(
ObjectInserter oi, ObjectId emptyTree, Timestamp registrationDate) throws IOException {
PersonIdent ident = new PersonIdent(serverIdent, registrationDate);