Line data Source code
1 : // Copyright (C) 2017 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.pgm.init; 16 : 17 : import static java.nio.charset.StandardCharsets.UTF_8; 18 : import static java.util.Objects.requireNonNull; 19 : 20 : import com.google.gerrit.entities.Account; 21 : import com.google.gerrit.entities.RefNames; 22 : import com.google.gerrit.pgm.init.api.AllUsersNameOnInitProvider; 23 : import com.google.gerrit.pgm.init.api.InitFlags; 24 : import com.google.gerrit.server.GerritPersonIdentProvider; 25 : import com.google.gerrit.server.account.AccountDelta; 26 : import com.google.gerrit.server.account.AccountProperties; 27 : import com.google.gerrit.server.account.Accounts; 28 : import com.google.gerrit.server.config.SitePaths; 29 : import com.google.inject.Inject; 30 : import java.io.File; 31 : import java.io.IOException; 32 : import java.nio.file.Path; 33 : import org.eclipse.jgit.dircache.DirCache; 34 : import org.eclipse.jgit.dircache.DirCacheEditor; 35 : import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit; 36 : import org.eclipse.jgit.dircache.DirCacheEntry; 37 : import org.eclipse.jgit.internal.storage.file.FileRepository; 38 : import org.eclipse.jgit.lib.CommitBuilder; 39 : import org.eclipse.jgit.lib.Config; 40 : import org.eclipse.jgit.lib.Constants; 41 : import org.eclipse.jgit.lib.FileMode; 42 : import org.eclipse.jgit.lib.ObjectId; 43 : import org.eclipse.jgit.lib.ObjectInserter; 44 : import org.eclipse.jgit.lib.PersonIdent; 45 : import org.eclipse.jgit.lib.RefUpdate; 46 : import org.eclipse.jgit.lib.RefUpdate.Result; 47 : import org.eclipse.jgit.lib.Repository; 48 : import org.eclipse.jgit.lib.RepositoryCache.FileKey; 49 : import org.eclipse.jgit.util.FS; 50 : 51 : public class AccountsOnInit { 52 : private final InitFlags flags; 53 : private final SitePaths site; 54 : private final String allUsers; 55 : 56 : @Inject 57 15 : public AccountsOnInit(InitFlags flags, SitePaths site, AllUsersNameOnInitProvider allUsers) { 58 15 : this.flags = flags; 59 15 : this.site = site; 60 15 : this.allUsers = allUsers.get(); 61 15 : } 62 : 63 : public Account insert(Account.Builder account) throws IOException { 64 0 : File path = getPath(); 65 0 : try (Repository repo = new FileRepository(path); 66 0 : ObjectInserter oi = repo.newObjectInserter()) { 67 0 : PersonIdent ident = 68 0 : new PersonIdent(new GerritPersonIdentProvider(flags.cfg).get(), account.registeredOn()); 69 : 70 0 : Config accountConfig = new Config(); 71 0 : AccountProperties.writeToAccountConfig( 72 0 : AccountDelta.builder() 73 0 : .setActive(!account.inactive()) 74 0 : .setFullName(account.fullName()) 75 0 : .setPreferredEmail(account.preferredEmail()) 76 0 : .setStatus(account.status()) 77 0 : .build(), 78 : accountConfig); 79 : 80 0 : DirCache newTree = DirCache.newInCore(); 81 0 : DirCacheEditor editor = newTree.editor(); 82 0 : final ObjectId blobId = oi.insert(Constants.OBJ_BLOB, accountConfig.toText().getBytes(UTF_8)); 83 0 : editor.add( 84 0 : new PathEdit(AccountProperties.ACCOUNT_CONFIG) { 85 : @Override 86 : public void apply(DirCacheEntry ent) { 87 0 : ent.setFileMode(FileMode.REGULAR_FILE); 88 0 : ent.setObjectId(blobId); 89 0 : } 90 : }); 91 0 : editor.finish(); 92 : 93 0 : ObjectId treeId = newTree.writeTree(oi); 94 : 95 0 : CommitBuilder cb = new CommitBuilder(); 96 0 : cb.setTreeId(treeId); 97 0 : cb.setCommitter(ident); 98 0 : cb.setAuthor(ident); 99 0 : cb.setMessage("Create Account"); 100 0 : ObjectId id = oi.insert(cb); 101 0 : oi.flush(); 102 : 103 0 : String refName = RefNames.refsUsers(account.id()); 104 0 : RefUpdate ru = repo.updateRef(refName); 105 0 : ru.setExpectedOldObjectId(ObjectId.zeroId()); 106 0 : ru.setNewObjectId(id); 107 0 : ru.setRefLogIdent(ident); 108 0 : ru.setRefLogMessage("Create Account", false); 109 0 : Result result = ru.update(); 110 0 : if (result != Result.NEW) { 111 0 : throw new IOException(String.format("Failed to update ref %s: %s", refName, result.name())); 112 : } 113 0 : account.setMetaId(id.name()); 114 : } 115 0 : return account.build(); 116 : } 117 : 118 : public boolean hasAnyAccount() throws IOException { 119 15 : File path = getPath(); 120 15 : if (path == null) { 121 0 : return false; 122 : } 123 : 124 15 : try (Repository repo = new FileRepository(path)) { 125 15 : return Accounts.hasAnyAccount(repo); 126 : } 127 : } 128 : 129 : private File getPath() { 130 15 : Path basePath = site.resolve(flags.cfg.getString("gerrit", null, "basePath")); 131 15 : requireNonNull(basePath, "gerrit.basePath must be configured"); 132 15 : File file = basePath.resolve(allUsers).toFile(); 133 15 : File resolvedFile = FileKey.resolve(file, FS.DETECTED); 134 15 : requireNonNull(resolvedFile, () -> String.format("%s does not exist", file.getAbsolutePath())); 135 15 : return resolvedFile; 136 : } 137 : }