Search in sources :

Example 1 with VersionedAuthorizedKeys

use of com.google.gerrit.server.account.VersionedAuthorizedKeys in project gerrit by GerritCodeReview.

the class Schema_124 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    ListMultimap<Account.Id, AccountSshKey> imports = MultimapBuilder.hashKeys().arrayListValues().build();
    try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
        ResultSet rs = stmt.executeQuery("SELECT " + "account_id, " + "seq, " + "ssh_public_key, " + "valid " + "FROM account_ssh_keys")) {
        while (rs.next()) {
            Account.Id accountId = new Account.Id(rs.getInt(1));
            int seq = rs.getInt(2);
            String sshPublicKey = rs.getString(3);
            AccountSshKey key = new AccountSshKey(new AccountSshKey.Id(accountId, seq), sshPublicKey);
            boolean valid = toBoolean(rs.getString(4));
            if (!valid) {
                key.setInvalid();
            }
            imports.put(accountId, key);
        }
    }
    if (imports.isEmpty()) {
        return;
    }
    try (Repository git = repoManager.openRepository(allUsersName);
        RevWalk rw = new RevWalk(git)) {
        BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
        for (Map.Entry<Account.Id, Collection<AccountSshKey>> e : imports.asMap().entrySet()) {
            try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git, bru)) {
                md.getCommitBuilder().setAuthor(serverUser);
                md.getCommitBuilder().setCommitter(serverUser);
                VersionedAuthorizedKeys authorizedKeys = new VersionedAuthorizedKeys(new SimpleSshKeyCreator(), e.getKey());
                authorizedKeys.load(md);
                authorizedKeys.setKeys(fixInvalidSequenceNumbers(e.getValue()));
                authorizedKeys.commit(md);
            }
        }
        bru.execute(rw, NullProgressMonitor.INSTANCE);
    } catch (ConfigInvalidException | IOException ex) {
        throw new OrmException(ex);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) Statement(java.sql.Statement) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) SimpleSshKeyCreator(com.google.gerrit.server.account.VersionedAuthorizedKeys.SimpleSshKeyCreator) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) ResultSet(java.sql.ResultSet) Collection(java.util.Collection) VersionedAuthorizedKeys(com.google.gerrit.server.account.VersionedAuthorizedKeys) Map(java.util.Map) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Aggregations

Account (com.google.gerrit.reviewdb.client.Account)1 AccountSshKey (com.google.gerrit.reviewdb.client.AccountSshKey)1 VersionedAuthorizedKeys (com.google.gerrit.server.account.VersionedAuthorizedKeys)1 SimpleSshKeyCreator (com.google.gerrit.server.account.VersionedAuthorizedKeys.SimpleSshKeyCreator)1 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)1 OrmException (com.google.gwtorm.server.OrmException)1 IOException (java.io.IOException)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Collection (java.util.Collection)1 Map (java.util.Map)1 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)1 BatchRefUpdate (org.eclipse.jgit.lib.BatchRefUpdate)1 Repository (org.eclipse.jgit.lib.Repository)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1