Search in sources :

Example 21 with TestKey

use of com.google.gerrit.gpg.testing.TestKey in project gerrit by GerritCodeReview.

the class AccountIT method assertKeys.

private void assertKeys(Iterable<TestKey> expectedKeys) throws Exception {
    // Check via API.
    FluentIterable<TestKey> expected = FluentIterable.from(expectedKeys);
    Map<String, GpgKeyInfo> keyMap = gApi.accounts().self().listGpgKeys();
    assertWithMessage("keys returned by listGpgKeys()").that(keyMap.keySet()).containsExactlyElementsIn(expected.transform(TestKey::getKeyIdString));
    for (TestKey key : expected) {
        assertKeyEquals(key, gApi.accounts().self().gpgKey(key.getKeyIdString()).get());
        assertKeyEquals(key, gApi.accounts().self().gpgKey(Fingerprint.toString(key.getPublicKey().getFingerprint())).get());
        assertKeyMapContains(key, keyMap);
    }
    // Check raw external IDs.
    Account.Id currAccountId = atrScope.get().getUser().getAccountId();
    Iterable<String> expectedFps = expected.transform(k -> BaseEncoding.base16().encode(k.getPublicKey().getFingerprint()));
    Iterable<String> actualFps = externalIds.byAccount(currAccountId, SCHEME_GPGKEY).stream().map(e -> e.key().id()).collect(toSet());
    assertWithMessage("external IDs in database").that(actualFps).containsExactlyElementsIn(expectedFps);
    // Check raw stored keys.
    for (TestKey key : expected) {
        getOnlyKeyFromStore(key);
    }
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) ExternalIdKeyFactory(com.google.gerrit.server.account.externalids.ExternalIdKeyFactory) KeyPair(java.security.KeyPair) Arrays(java.util.Arrays) OBJ_BLOB(org.eclipse.jgit.lib.Constants.OBJ_BLOB) GlobalCapability(com.google.gerrit.common.data.GlobalCapability) CommentInfo(com.google.gerrit.extensions.common.CommentInfo) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) TestAccount(com.google.gerrit.acceptance.TestAccount) TestProjectUpdate.deny(com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.deny) REGISTERED_USERS(com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Config(org.eclipse.jgit.lib.Config) TestKey(com.google.gerrit.gpg.testing.TestKey) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) FluentIterable(com.google.common.collect.FluentIterable) GpgKeyInfo(com.google.gerrit.extensions.common.GpgKeyInfo) Map(java.util.Map) RetryHelper(com.google.gerrit.server.update.RetryHelper) GroupOperations(com.google.gerrit.acceptance.testsuite.group.GroupOperations) TestProjectUpdate.allow(com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.allow) DeleteDraftCommentsInput(com.google.gerrit.extensions.api.accounts.DeleteDraftCommentsInput) DeletedDraftCommentInfo(com.google.gerrit.extensions.api.accounts.DeletedDraftCommentInfo) GitUtil.deleteRef(com.google.gerrit.acceptance.GitUtil.deleteRef) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) AccessSection(com.google.gerrit.entities.AccessSection) ConsistencyCheckInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo) Set(java.util.Set) RefUpdate(org.eclipse.jgit.lib.RefUpdate) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) TestKeys.validKeyWithoutExpiration(com.google.gerrit.gpg.testing.TestKeys.validKeyWithoutExpiration) BranchNameKey(com.google.gerrit.entities.BranchNameKey) ExternalIds(com.google.gerrit.server.account.externalids.ExternalIds) RequestScopeOperations(com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations) PersonIdent(org.eclipse.jgit.lib.PersonIdent) UseClockStep(com.google.gerrit.acceptance.UseClockStep) AccountProperties(com.google.gerrit.server.account.AccountProperties) Emails(com.google.gerrit.server.account.Emails) PushResult(org.eclipse.jgit.transport.PushResult) PluginSetContext(com.google.gerrit.server.plugincontext.PluginSetContext) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) GerritJUnit.assertThrows(com.google.gerrit.testing.GerritJUnit.assertThrows) GitUtil.fetch(com.google.gerrit.acceptance.GitUtil.fetch) Mockito.mock(org.mockito.Mockito.mock) EmailInfo(com.google.gerrit.extensions.common.EmailInfo) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) ClientProtocolException(org.apache.http.client.ClientProtocolException) Iterables(com.google.common.collect.Iterables) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) AccountOperations(com.google.gerrit.acceptance.testsuite.account.AccountOperations) Fingerprint(com.google.gerrit.gpg.Fingerprint) ArrayList(java.util.ArrayList) AccountInput(com.google.gerrit.extensions.api.accounts.AccountInput) ReviewerInput(com.google.gerrit.extensions.api.changes.ReviewerInput) TestKeys.validKeyWithExpiration(com.google.gerrit.gpg.testing.TestKeys.validKeyWithExpiration) Runnables(com.google.common.util.concurrent.Runnables) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) Address(com.google.gerrit.entities.Address) ExtensionRegistry(com.google.gerrit.acceptance.ExtensionRegistry) Before(org.junit.Before) AccountGroup(com.google.gerrit.entities.AccountGroup) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) TestRepository(org.eclipse.jgit.junit.TestRepository) Sequences(com.google.gerrit.server.notedb.Sequences) BaseEncoding(com.google.common.io.BaseEncoding) ValidationException(com.google.gerrit.server.validators.ValidationException) StorageException(com.google.gerrit.exceptions.StorageException) AccountActivationListener(com.google.gerrit.extensions.events.AccountActivationListener) IOException(java.io.IOException) Test(org.junit.Test) ANONYMOUS_USERS(com.google.gerrit.server.group.SystemGroupBackend.ANONYMOUS_USERS) StalenessChecker(com.google.gerrit.server.index.account.StalenessChecker) Correspondence(com.google.common.truth.Correspondence) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) AccountApi(com.google.gerrit.extensions.api.accounts.AccountApi) CacheBasedWebSession(com.google.gerrit.httpd.CacheBasedWebSession) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) Project(com.google.gerrit.entities.Project) HttpResponse(org.apache.http.HttpResponse) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) AccountState(com.google.gerrit.server.account.AccountState) ObjectReader(org.eclipse.jgit.lib.ObjectReader) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) Date(java.util.Date) Inject(com.google.inject.Inject) REFS_GPG_KEYS(com.google.gerrit.gpg.PublicKeyStore.REFS_GPG_KEYS) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) AccountDetailInfo(com.google.gerrit.extensions.common.AccountDetailInfo) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) DEFAULT_LABEL(com.google.gerrit.server.StarredChangesUtil.DEFAULT_LABEL) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CheckAccountsInput(com.google.gerrit.extensions.api.config.ConsistencyCheckInput.CheckAccountsInput) Locale(java.util.Locale) After(org.junit.After) RefNames(com.google.gerrit.entities.RefNames) AuthException(com.google.gerrit.extensions.restapi.AuthException) PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) AtomicLongMap(com.google.common.util.concurrent.AtomicLongMap) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) ImmutableSet(com.google.common.collect.ImmutableSet) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) SshSessionFactory(com.google.gerrit.acceptance.testsuite.request.SshSessionFactory) Account(com.google.gerrit.entities.Account) SCHEME_GPGKEY(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GPGKEY) AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) Collectors(java.util.stream.Collectors) TestSshKeys(com.google.gerrit.acceptance.testsuite.account.TestSshKeys) GroupReference(com.google.gerrit.entities.GroupReference) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) StopStrategies(com.github.rholder.retry.StopStrategies) HttpGet(org.apache.http.client.methods.HttpGet) Ref(org.eclipse.jgit.lib.Ref) ConfigSubject.assertThat(com.google.gerrit.truth.ConfigSubject.assertThat) Optional(java.util.Optional) AuthConfig(com.google.gerrit.server.config.AuthConfig) RefPattern(com.google.gerrit.server.project.RefPattern) AccountIndexer(com.google.gerrit.server.index.account.AccountIndexer) TestProjectUpdate.block(com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.block) VersionedAuthorizedKeys(com.google.gerrit.server.account.VersionedAuthorizedKeys) DraftInput(com.google.gerrit.extensions.api.changes.DraftInput) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) PublicKeyStore(com.google.gerrit.gpg.PublicKeyStore) InternalAccountQuery(com.google.gerrit.server.query.account.InternalAccountQuery) SshKeyInfo(com.google.gerrit.extensions.common.SshKeyInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ConfigSuite(com.google.gerrit.testing.ConfigSuite) HashSet(java.util.HashSet) GitReferenceUpdatedListener(com.google.gerrit.extensions.events.GitReferenceUpdatedListener) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) ImmutableList(com.google.common.collect.ImmutableList) UseSsh(com.google.gerrit.acceptance.UseSsh) ExceptionHook(com.google.gerrit.server.ExceptionHook) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) Objects.requireNonNull(java.util.Objects.requireNonNull) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ExternalIdFactory(com.google.gerrit.server.account.externalids.ExternalIdFactory) Change(com.google.gerrit.entities.Change) LockFailureException(com.google.gerrit.git.LockFailureException) Truth8.assertThat(com.google.common.truth.Truth8.assertThat) ConsistencyCheckInput(com.google.gerrit.extensions.api.config.ConsistencyCheckInput) AccountActivationValidationListener(com.google.gerrit.server.validators.AccountActivationValidationListener) AccountsUpdate(com.google.gerrit.server.account.AccountsUpdate) TestProjectUpdate.allowCapability(com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.allowCapability) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Iterator(java.util.Iterator) Sandboxed(com.google.gerrit.acceptance.Sandboxed) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Permission(com.google.gerrit.entities.Permission) HttpServletResponse(javax.servlet.http.HttpServletResponse) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) ObjectId(org.eclipse.jgit.lib.ObjectId) TransportException(org.eclipse.jgit.api.errors.TransportException) TestProjectUpdate.permissionKey(com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.permissionKey) ArmoredOutputStream(org.bouncycastle.bcpg.ArmoredOutputStream) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) Message(com.google.gerrit.testing.FakeEmailSender.Message) ProjectOperations(com.google.gerrit.acceptance.testsuite.project.ProjectOperations) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ServerInitiated(com.google.gerrit.server.ServerInitiated) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) NullAwareCorrespondence(com.google.gerrit.truth.NullAwareCorrespondence) DuplicateExternalIdKeyException(com.google.gerrit.server.account.externalids.DuplicateExternalIdKeyException) Git(org.eclipse.jgit.api.Git) TestKeys.allValidKeys(com.google.gerrit.gpg.testing.TestKeys.allValidKeys) TestKeys.validKeyWithSecondUserId(com.google.gerrit.gpg.testing.TestKeys.validKeyWithSecondUserId) PushCertificateIdent(org.eclipse.jgit.transport.PushCertificateIdent) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Action(com.google.gerrit.entities.PermissionRule.Action) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) TestAccount(com.google.gerrit.acceptance.TestAccount) Account(com.google.gerrit.entities.Account) TestKey(com.google.gerrit.gpg.testing.TestKey) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) GpgKeyInfo(com.google.gerrit.extensions.common.GpgKeyInfo)

Example 22 with TestKey

use of com.google.gerrit.gpg.testing.TestKey in project gerrit by GerritCodeReview.

the class AccountIT method addOtherUsersGpgKey_Conflict.

@Test
public void addOtherUsersGpgKey_Conflict() throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        // Both users have a matching external ID for this key.
        addExternalIdEmail(admin, "test5@example.com");
        accountIndexedCounter.clear();
        accountsUpdateProvider.get().update("Add External ID", user.id(), u -> u.addExternalId(externalIdFactory.create("foo", "myId", user.id())));
        accountIndexedCounter.assertReindexOf(user);
        TestKey key = validKeyWithSecondUserId();
        addGpgKey(key.getPublicKeyArmored());
        requestScopeOperations.setApiUser(user.id());
        ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> addGpgKey(user, key.getPublicKeyArmored()));
        assertThat(thrown).hasMessageThat().contains("GPG key already associated with another account");
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) TestKey(com.google.gerrit.gpg.testing.TestKey) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 23 with TestKey

use of com.google.gerrit.gpg.testing.TestKey in project gerrit by GerritCodeReview.

the class AccountIT method adminCannotAddGpgKeyToOtherAccount.

@Test
public void adminCannotAddGpgKeyToOtherAccount() throws Exception {
    TestKey key = validKeyWithoutExpiration();
    addExternalIdEmail(user, "test1@example.com");
    sender.clear();
    requestScopeOperations.setApiUser(admin.id());
    assertThrows(ResourceNotFoundException.class, () -> addGpgKey(user, key.getPublicKeyArmored()));
}
Also used : TestKey(com.google.gerrit.gpg.testing.TestKey) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 24 with TestKey

use of com.google.gerrit.gpg.testing.TestKey in project gerrit by GerritCodeReview.

the class AccountIT method reAddExistingGpgKey.

@Test
public void reAddExistingGpgKey() throws Exception {
    addExternalIdEmail(admin, "test5@example.com");
    TestKey key = validKeyWithSecondUserId();
    String id = key.getKeyIdString();
    PGPPublicKey pk = key.getPublicKey();
    sender.clear();
    GpgKeyInfo info = addGpgKey(armor(pk)).get(id);
    assertThat(info.userIds).hasSize(2);
    assertIteratorSize(2, getOnlyKeyFromStore(key).getUserIDs());
    assertThat(sender.getMessages()).hasSize(1);
    assertThat(sender.getMessages().get(0).body()).contains("new GPG keys have been added");
    pk = PGPPublicKey.removeCertification(pk, "foo:myId");
    sender.clear();
    info = addGpgKeyNoReindex(armor(pk)).get(id);
    assertThat(info.userIds).hasSize(1);
    assertIteratorSize(1, getOnlyKeyFromStore(key).getUserIDs());
    // TODO: Issue 10769: Adding an already existing key should not result in a notification email
    assertThat(sender.getMessages()).hasSize(1);
    assertThat(sender.getMessages().get(0).body()).contains("new GPG keys have been added");
}
Also used : TestKey(com.google.gerrit.gpg.testing.TestKey) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) GpgKeyInfo(com.google.gerrit.extensions.common.GpgKeyInfo) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 25 with TestKey

use of com.google.gerrit.gpg.testing.TestKey in project gerrit by GerritCodeReview.

the class AccountIT method deleteGpgKey.

@Test
public void deleteGpgKey() throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        TestKey key = validKeyWithoutExpiration();
        String id = key.getKeyIdString();
        addExternalIdEmail(admin, "test1@example.com");
        addGpgKey(key.getPublicKeyArmored());
        assertKeys(key);
        accountIndexedCounter.clear();
        sender.clear();
        gApi.accounts().self().gpgKey(id).delete();
        accountIndexedCounter.assertReindexOf(admin);
        assertKeys();
        assertThat(sender.getMessages()).hasSize(1);
        assertThat(sender.getMessages().get(0).body()).contains("GPG keys have been deleted");
        ResourceNotFoundException thrown = assertThrows(ResourceNotFoundException.class, () -> gApi.accounts().self().gpgKey(id).get());
        assertThat(thrown).hasMessageThat().contains(id);
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) TestKey(com.google.gerrit.gpg.testing.TestKey) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

TestKey (com.google.gerrit.gpg.testing.TestKey)44 Test (org.junit.Test)41 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)9 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)9 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)7 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)6 AccountIndexedCounter (com.google.gerrit.acceptance.AccountIndexedCounter)5 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)5 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)3 PushCertificate (org.eclipse.jgit.transport.PushCertificate)3 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)2 GpgKeyInfo (com.google.gerrit.extensions.common.GpgKeyInfo)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 PublicKeyStore.keyIdToString (com.google.gerrit.gpg.PublicKeyStore.keyIdToString)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 ObjectReader (org.eclipse.jgit.lib.ObjectReader)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 StopStrategies (com.github.rholder.retry.StopStrategies)1 FluentIterable (com.google.common.collect.FluentIterable)1