From 530cefa02cfc00c672403ab7ca9c8d2062e0f793 Mon Sep 17 00:00:00 2001 From: Edwin Kempin Date: Fri, 7 Jul 2017 13:27:37 +0200 Subject: [PATCH] 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 --- .../gerrit/pgm/init/AccountsOnInit.java | 70 +++++++++++++++---- .../gerrit/server/account/AccountConfig.java | 8 ++- .../gerrit/server/account/AccountsUpdate.java | 50 ------------- .../gerrit/server/schema/Schema_146.java | 34 +++++---- 4 files changed, 84 insertions(+), 78 deletions(-) diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/AccountsOnInit.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/AccountsOnInit.java index 81435e0ebd..9ffa9b0707 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/AccountsOnInit.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/AccountsOnInit.java @@ -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())); + } } } } diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountConfig.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountConfig.java index 70ed29eced..cd477c0f32 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountConfig.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountConfig.java @@ -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; } /** diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountsUpdate.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountsUpdate.java index 9c24d5c2d0..1a08090af1 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountsUpdate.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountsUpdate.java @@ -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); diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_146.java b/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_146.java index 6ba1b65adc..503ed7b0db 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_146.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_146.java @@ -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);