Search in sources :

Example 6 with ExternalId

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

the class ExternalIdIT method readExternalIdWithAccountIdThatCanBeExpressedInKiB.

@Test
public void readExternalIdWithAccountIdThatCanBeExpressedInKiB() throws Exception {
    ExternalId.Key extIdKey = ExternalId.Key.parse("foo:bar");
    Account.Id accountId = new Account.Id(1024 * 100);
    extIdsUpdate.create().insert(ExternalId.create(extIdKey, accountId));
    ExternalId extId = externalIds.get(extIdKey);
    assertThat(extId.accountId()).isEqualTo(accountId);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ObjectId(org.eclipse.jgit.lib.ObjectId) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 7 with ExternalId

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

the class ExternalIdIT method addExtId.

private void addExtId(TestRepository<?> testRepo, ExternalId... extIds) throws IOException, OrmDuplicateKeyException, ConfigInvalidException {
    ObjectId rev = ExternalIdReader.readRevision(testRepo.getRepository());
    try (ObjectInserter ins = testRepo.getRepository().newObjectInserter()) {
        NoteMap noteMap = ExternalIdReader.readNoteMap(testRepo.getRevWalk(), rev);
        for (ExternalId extId : extIds) {
            ExternalIdsUpdate.insert(testRepo.getRevWalk(), ins, noteMap, extId);
        }
        ExternalIdsUpdate.commit(testRepo.getRepository(), testRepo.getRevWalk(), ins, rev, noteMap, "Add external ID", admin.getIdent(), admin.getIdent());
    }
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) NoteMap(org.eclipse.jgit.notes.NoteMap)

Example 8 with ExternalId

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

the class GpgKeys method parseFingerprint.

static byte[] parseFingerprint(String str, Iterable<ExternalId> existingExtIds) throws ResourceNotFoundException {
    str = CharMatcher.whitespace().removeFrom(str).toUpperCase();
    if ((str.length() != 8 && str.length() != 40) || !CharMatcher.anyOf("0123456789ABCDEF").matchesAllOf(str)) {
        throw new ResourceNotFoundException(str);
    }
    byte[] fp = null;
    for (ExternalId extId : existingExtIds) {
        String fpStr = extId.key().id();
        if (!fpStr.endsWith(str)) {
            continue;
        } else if (fp != null) {
            throw new ResourceNotFoundException("Multiple keys found for " + str);
        }
        fp = BaseEncoding.base16().decode(fpStr);
        if (str.length() == 40) {
            break;
        }
    }
    if (fp == null) {
        throw new ResourceNotFoundException(str);
    }
    return fp;
}
Also used : ExternalId(com.google.gerrit.server.account.externalids.ExternalId) IdString(com.google.gerrit.extensions.restapi.IdString) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 9 with ExternalId

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

the class Schema_144 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    Set<ExternalId> toAdd = new HashSet<>();
    try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
        ResultSet rs = stmt.executeQuery("SELECT " + "account_id, " + "email_address, " + "password, " + "external_id " + "FROM account_external_ids")) {
        while (rs.next()) {
            Account.Id accountId = new Account.Id(rs.getInt(1));
            String email = rs.getString(2);
            String password = rs.getString(3);
            String externalId = rs.getString(4);
            toAdd.add(ExternalId.create(ExternalId.Key.parse(externalId), accountId, email, password));
        }
    }
    try {
        try (Repository repo = repoManager.openRepository(allUsersName);
            RevWalk rw = new RevWalk(repo);
            ObjectInserter ins = repo.newObjectInserter()) {
            ObjectId rev = ExternalIdReader.readRevision(repo);
            NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);
            for (ExternalId extId : toAdd) {
                ExternalIdsUpdate.upsert(rw, ins, noteMap, extId);
            }
            ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, COMMIT_MSG, serverIdent, serverIdent);
        }
    } catch (IOException | ConfigInvalidException e) {
        throw new OrmException("Failed to migrate external IDs to NoteDb", e);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ObjectId(org.eclipse.jgit.lib.ObjectId) Statement(java.sql.Statement) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) NoteMap(org.eclipse.jgit.notes.NoteMap) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) OrmException(com.google.gwtorm.server.OrmException) ResultSet(java.sql.ResultSet) ObjectId(org.eclipse.jgit.lib.ObjectId) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) HashSet(java.util.HashSet)

Example 10 with ExternalId

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

the class Schema_148 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    try (Repository repo = repoManager.openRepository(allUsersName);
        RevWalk rw = new RevWalk(repo);
        ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId rev = ExternalIdReader.readRevision(repo);
        NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);
        boolean dirty = false;
        for (Note note : noteMap) {
            byte[] raw = rw.getObjectReader().open(note.getData(), OBJ_BLOB).getCachedBytes(ExternalIdReader.MAX_NOTE_SZ);
            try {
                ExternalId extId = ExternalId.parse(note.getName(), raw);
                if (needsUpdate(extId)) {
                    ExternalIdsUpdate.upsert(rw, ins, noteMap, extId);
                    dirty = true;
                }
            } catch (ConfigInvalidException e) {
                ui.message(String.format("Warning: Ignoring invalid external ID note %s", note.getName()));
            }
        }
        if (dirty) {
            ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, COMMIT_MSG, serverUser, serverUser);
        }
    } catch (IOException e) {
        throw new OrmException("Failed to update external IDs", e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ObjectId(org.eclipse.jgit.lib.ObjectId) OrmException(com.google.gwtorm.server.OrmException) Note(org.eclipse.jgit.notes.Note) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) NoteMap(org.eclipse.jgit.notes.NoteMap) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Aggregations

ExternalId (com.google.gerrit.server.account.externalids.ExternalId)34 Account (com.google.gerrit.reviewdb.client.Account)12 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)8 Test (org.junit.Test)8 OrmException (com.google.gwtorm.server.OrmException)7 ArrayList (java.util.ArrayList)7 ObjectId (org.eclipse.jgit.lib.ObjectId)7 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)6 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)6 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)6 NoteMap (org.eclipse.jgit.notes.NoteMap)6 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)5 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)5 ExternalIdsUpdate (com.google.gerrit.server.account.externalids.ExternalIdsUpdate)5 HashSet (java.util.HashSet)5 AccountExternalIdInfo (com.google.gerrit.extensions.common.AccountExternalIdInfo)4 AuthException (com.google.gerrit.extensions.restapi.AuthException)4 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)4 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)4 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)4