Search in sources :

Example 11 with ExternalIdNotes

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

the class OnlineExternalIdCaseSensivityMigratorIT method shouldThrowExceptionWhenDuplicateKeys.

@Test
@GerritConfig(name = "auth.userNameCaseInsensitive", value = "true")
@GerritConfig(name = "auth.userNameCaseInsensitiveMigrationMode", value = "true")
public void shouldThrowExceptionWhenDuplicateKeys() throws IOException, ConfigInvalidException {
    try (Repository allUsersRepo = repoManager.openRepository(allUsers);
        MetaDataUpdate md = metaDataUpdateFactory.create(allUsers)) {
        ExternalIdNotes extIdNotes = externalIdNotesFactory.load(allUsersRepo);
        createExternalId(md, extIdNotes, SCHEME_USERNAME, "JonDoe", accountId, isUserNameCaseInsensitive);
        createExternalId(md, extIdNotes, SCHEME_USERNAME, "jondoe", Account.id(67), isUserNameCaseInsensitive);
        assertThat(getExactExternalId(extIdNotes, SCHEME_USERNAME, "JonDoe").isPresent()).isTrue();
        assertThat(getExactExternalId(extIdNotes, SCHEME_USERNAME, "jondoe").isPresent()).isTrue();
        assertThrows(DuplicateExternalIdKeyException.class, () -> objectUnderTest.migrate());
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 12 with ExternalIdNotes

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

the class OnlineExternalIdCaseSensivityMigratorIT method shouldSkipMigrationWhenUserNameCaseInsensitiveMigrationModeIsSetToFalse.

@Test
@GerritConfig(name = "auth.userNameCaseInsensitive", value = "true")
@GerritConfig(name = "auth.userNameCaseInsensitiveMigrationMode", value = "false")
public void shouldSkipMigrationWhenUserNameCaseInsensitiveMigrationModeIsSetToFalse() throws RepositoryNotFoundException, IOException, ConfigInvalidException {
    try (Repository allUsersRepo = repoManager.openRepository(allUsers);
        MetaDataUpdate md = metaDataUpdateFactory.create(allUsers)) {
        ExternalIdNotes extIdNotes = externalIdNotesFactory.load(allUsersRepo);
        createExternalId(md, extIdNotes, SCHEME_USERNAME, "JonDoe", accountId, isUserNameCaseInsensitive);
        objectUnderTest.migrate();
        extIdNotes = externalIdNotesFactory.load(allUsersRepo);
        assertThat(getExactExternalId(extIdNotes, SCHEME_USERNAME, "jondoe").isPresent()).isFalse();
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 13 with ExternalIdNotes

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

the class OnlineExternalIdCaseSensivityMigratorIT method shouldMigrateExternalId.

@Test
@GerritConfig(name = "auth.userNameCaseInsensitive", value = "true")
@GerritConfig(name = "auth.userNameCaseInsensitiveMigrationMode", value = "true")
public void shouldMigrateExternalId() throws IOException, ConfigInvalidException {
    try (Repository allUsersRepo = repoManager.openRepository(allUsers);
        MetaDataUpdate md = metaDataUpdateFactory.create(allUsers)) {
        ExternalIdNotes extIdNotes = externalIdNotesFactory.load(allUsersRepo);
        createExternalId(md, extIdNotes, SCHEME_GERRIT, "JonDoe", accountId, isUserNameCaseInsensitive);
        createExternalId(md, extIdNotes, SCHEME_USERNAME, "JonDoe", accountId, isUserNameCaseInsensitive);
        assertThat(getExactExternalId(extIdNotes, SCHEME_GERRIT, "JonDoe").isPresent()).isTrue();
        assertThat(getExactExternalId(extIdNotes, SCHEME_GERRIT, "jondoe").isPresent()).isFalse();
        assertThat(getExactExternalId(extIdNotes, SCHEME_USERNAME, "JonDoe").isPresent()).isTrue();
        assertThat(getExactExternalId(extIdNotes, SCHEME_USERNAME, "jondoe").isPresent()).isFalse();
        objectUnderTest.migrate();
        extIdNotes = externalIdNotesFactory.load(allUsersRepo);
        assertThat(getExactExternalId(extIdNotes, SCHEME_GERRIT, "JonDoe").isPresent()).isFalse();
        assertThat(getExactExternalId(extIdNotes, SCHEME_GERRIT, "jondoe").isPresent()).isTrue();
        assertThat(getExactExternalId(extIdNotes, SCHEME_USERNAME, "JonDoe").isPresent()).isFalse();
        assertThat(getExactExternalId(extIdNotes, SCHEME_USERNAME, "jondoe").isPresent()).isTrue();
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 14 with ExternalIdNotes

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

the class AccountIT method stalenessChecker.

@Test
// Instants
@SuppressWarnings("JdkObsolete")
public void stalenessChecker() throws Exception {
    // Newly created account is not stale.
    AccountInfo accountInfo = gApi.accounts().create(name("foo")).get();
    Account.Id accountId = Account.id(accountInfo._accountId);
    assertThat(stalenessChecker.check(accountId).isStale()).isFalse();
    // Manually updating the user ref makes the index document stale.
    String userRef = RefNames.refsUsers(accountId);
    try (Repository repo = repoManager.openRepository(allUsers);
        ObjectInserter oi = repo.newObjectInserter();
        RevWalk rw = new RevWalk(repo)) {
        RevCommit commit = rw.parseCommit(repo.exactRef(userRef).getObjectId());
        PersonIdent ident = new PersonIdent(serverIdent.get(), Date.from(TimeUtil.now()));
        CommitBuilder cb = new CommitBuilder();
        cb.setTreeId(commit.getTree());
        cb.setCommitter(ident);
        cb.setAuthor(ident);
        cb.setMessage(commit.getFullMessage());
        ObjectId emptyCommit = oi.insert(cb);
        oi.flush();
        RefUpdate updateRef = repo.updateRef(userRef);
        updateRef.setExpectedOldObjectId(commit.toObjectId());
        updateRef.setNewObjectId(emptyCommit);
        assertThat(updateRef.forceUpdate()).isEqualTo(RefUpdate.Result.FORCED);
    }
    assertStaleAccountAndReindex(accountId);
    // stale.
    try (Repository repo = repoManager.openRepository(allUsers)) {
        ExternalIdNotes extIdNotes = ExternalIdNotes.load(allUsers, repo, externalIdFactory, authConfig.isUserNameCaseInsensitiveMigrationMode());
        ExternalId.Key key = externalIdKeyFactory.create("foo", "foo");
        extIdNotes.insert(externalIdFactory.create(key, accountId));
        try (MetaDataUpdate update = metaDataUpdateFactory.create(allUsers)) {
            extIdNotes.commit(update);
        }
        assertStaleAccountAndReindex(accountId);
        extIdNotes = ExternalIdNotes.load(allUsers, repo, externalIdFactory, authConfig.isUserNameCaseInsensitiveMigrationMode());
        extIdNotes.upsert(externalIdFactory.createWithEmail(key, accountId, "foo@example.com"));
        try (MetaDataUpdate update = metaDataUpdateFactory.create(allUsers)) {
            extIdNotes.commit(update);
        }
        assertStaleAccountAndReindex(accountId);
        extIdNotes = ExternalIdNotes.load(allUsers, repo, externalIdFactory, authConfig.isUserNameCaseInsensitiveMigrationMode());
        extIdNotes.delete(accountId, key);
        try (MetaDataUpdate update = metaDataUpdateFactory.create(allUsers)) {
            extIdNotes.commit(update);
        }
        assertStaleAccountAndReindex(accountId);
    }
    // Manually delete account
    try (Repository repo = repoManager.openRepository(allUsers);
        RevWalk rw = new RevWalk(repo)) {
        RevCommit commit = rw.parseCommit(repo.exactRef(userRef).getObjectId());
        RefUpdate updateRef = repo.updateRef(userRef);
        updateRef.setExpectedOldObjectId(commit.toObjectId());
        updateRef.setNewObjectId(ObjectId.zeroId());
        updateRef.setForceUpdate(true);
        assertThat(updateRef.delete()).isEqualTo(RefUpdate.Result.FORCED);
    }
    assertStaleAccountAndReindex(accountId);
}
Also used : TestAccount(com.google.gerrit.acceptance.TestAccount) Account(com.google.gerrit.entities.Account) ObjectId(org.eclipse.jgit.lib.ObjectId) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PersonIdent(org.eclipse.jgit.lib.PersonIdent) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 15 with ExternalIdNotes

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

the class LocalUsernamesToLowerCase method run.

@Override
public int run() throws Exception {
    Injector dbInjector = createDbInjector();
    manager.add(dbInjector, dbInjector.createChildInjector(NoteDbSchemaVersionCheck.module()));
    manager.start();
    dbInjector.createChildInjector(new FactoryModule() {

        @Override
        protected void configure() {
            bind(GitReferenceUpdated.class).toInstance(GitReferenceUpdated.DISABLED);
            factory(MetaDataUpdate.InternalFactory.class);
            // The LocalUsernamesToLowerCase program needs to access all external IDs only
            // once to update them. After the update they are not accessed again. Hence the
            // LocalUsernamesToLowerCase program doesn't benefit from caching external IDs and
            // the external ID cache can be disabled.
            install(DisabledExternalIdCache.module());
        }
    }).injectMembers(this);
    Collection<ExternalId> todo = externalIds.all();
    monitor.beginTask("Converting local usernames", todo.size());
    try (Repository repo = repoManager.openRepository(allUsersName)) {
        ExternalIdNotes extIdNotes = externalIdNotesFactory.load(repo);
        for (ExternalId extId : todo) {
            convertLocalUserToLowerCase(extIdNotes, extId);
            monitor.update(1);
        }
        try (MetaDataUpdate metaDataUpdate = metaDataUpdateServerFactory.get().create(allUsersName)) {
            metaDataUpdate.setMessage("Convert local usernames to lower case");
            extIdNotes.commit(metaDataUpdate);
        }
    }
    monitor.endTask();
    int exitCode = reindexAccounts();
    manager.stop();
    return exitCode;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Injector(com.google.inject.Injector) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Aggregations

ExternalIdNotes (com.google.gerrit.server.account.externalids.ExternalIdNotes)31 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)29 Repository (org.eclipse.jgit.lib.Repository)28 Test (org.junit.Test)23 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)18 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)14 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)13 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)12 TestRepository (org.eclipse.jgit.junit.TestRepository)12 Account (com.google.gerrit.entities.Account)11 LightweightPluginDaemonTest (com.google.gerrit.acceptance.LightweightPluginDaemonTest)5 TestAccount (com.google.gerrit.acceptance.TestAccount)5 FactoryModule (com.google.gerrit.extensions.config.FactoryModule)2 GitReferenceUpdated (com.google.gerrit.server.extensions.events.GitReferenceUpdated)2 GitRepositoryManager (com.google.gerrit.server.git.GitRepositoryManager)2 Injector (com.google.inject.Injector)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 DuplicateKeyException (com.google.gerrit.exceptions.DuplicateKeyException)1 StorageException (com.google.gerrit.exceptions.StorageException)1 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)1