Search in sources :

Example 1 with SCHEME_GPGKEY

use of com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GPGKEY 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();
    assertThat(keyMap.keySet()).named("keys returned by listGpgKeys()").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());
    assertThat(actualFps).named("external IDs in database").containsExactlyElementsIn(expectedFps);
    // Check raw stored keys.
    for (TestKey key : expected) {
        getOnlyKeyFromStore(key);
    }
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) AllUsersName(com.google.gerrit.server.config.AllUsersName) IGNORE_LABEL(com.google.gerrit.server.StarredChangesUtil.IGNORE_LABEL) Arrays(java.util.Arrays) SshMode(com.google.gerrit.testutil.SshMode) GlobalCapability(com.google.gerrit.common.data.GlobalCapability) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) TestAccount(com.google.gerrit.acceptance.TestAccount) 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) FluentIterable(com.google.common.collect.FluentIterable) GpgKeyInfo(com.google.gerrit.extensions.common.GpgKeyInfo) Map(java.util.Map) WatchConfig(com.google.gerrit.server.account.WatchConfig) EnumSet(java.util.EnumSet) TestKeys.allValidKeys(com.google.gerrit.gpg.testutil.TestKeys.allValidKeys) GitUtil.deleteRef(com.google.gerrit.acceptance.GitUtil.deleteRef) Set(java.util.Set) RefUpdate(org.eclipse.jgit.lib.RefUpdate) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) ExternalIds(com.google.gerrit.server.account.externalids.ExternalIds) RefNames(com.google.gerrit.reviewdb.client.RefNames) PushResult(org.eclipse.jgit.transport.PushResult) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) ExternalIdsUpdate(com.google.gerrit.server.account.externalids.ExternalIdsUpdate) GitUtil.fetch(com.google.gerrit.acceptance.GitUtil.fetch) Iterables(com.google.common.collect.Iterables) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Fingerprint(com.google.gerrit.gpg.Fingerprint) ArrayList(java.util.ArrayList) AccountByEmailCache(com.google.gerrit.server.account.AccountByEmailCache) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) Before(org.junit.Before) MagicBranch(com.google.gerrit.server.util.MagicBranch) TestRepository(org.eclipse.jgit.junit.TestRepository) BaseEncoding(com.google.common.io.BaseEncoding) Test(org.junit.Test) ANONYMOUS_USERS(com.google.gerrit.server.group.SystemGroupBackend.ANONYMOUS_USERS) TestKeys.validKeyWithoutExpiration(com.google.gerrit.gpg.testutil.TestKeys.validKeyWithoutExpiration) AccountIndexedListener(com.google.gerrit.extensions.events.AccountIndexedListener) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) DynamicSet(com.google.gerrit.extensions.registration.DynamicSet) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Assert(org.junit.Assert) Repository(org.eclipse.jgit.lib.Repository) TestKey(com.google.gerrit.gpg.testutil.TestKey) Inject(com.google.inject.Inject) REFS_GPG_KEYS(com.google.gerrit.gpg.PublicKeyStore.REFS_GPG_KEYS) ChangeRebuilderImpl(com.google.gerrit.server.notedb.rebuild.ChangeRebuilderImpl) AccountCreator(com.google.gerrit.acceptance.AccountCreator) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) DEFAULT_LABEL(com.google.gerrit.server.StarredChangesUtil.DEFAULT_LABEL) TestKeys.validKeyWithExpiration(com.google.gerrit.gpg.testutil.TestKeys.validKeyWithExpiration) Locale(java.util.Locale) After(org.junit.After) AuthException(com.google.gerrit.extensions.restapi.AuthException) Assert.fail(org.junit.Assert.fail) PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) AtomicLongMap(com.google.common.util.concurrent.AtomicLongMap) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Permission(com.google.gerrit.common.data.Permission) SCHEME_GPGKEY(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GPGKEY) NotifyType(com.google.gerrit.server.account.WatchConfig.NotifyType) TestKeys.validKeyWithSecondUserId(com.google.gerrit.gpg.testutil.TestKeys.validKeyWithSecondUserId) List(java.util.List) AddReviewerInput(com.google.gerrit.extensions.api.changes.AddReviewerInput) Ref(org.eclipse.jgit.lib.Ref) ConfigSuite(com.google.gerrit.testutil.ConfigSuite) RegistrationHandle(com.google.gerrit.extensions.registration.RegistrationHandle) RefPattern(com.google.gerrit.server.project.RefPattern) Message(com.google.gerrit.testutil.FakeEmailSender.Message) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) PublicKeyStore(com.google.gerrit.gpg.PublicKeyStore) SshKeyInfo(com.google.gerrit.extensions.common.SshKeyInfo) ImmutableList(com.google.common.collect.ImmutableList) UseSsh(com.google.gerrit.acceptance.UseSsh) Account(com.google.gerrit.reviewdb.client.Account) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) Truth.assert_(com.google.common.truth.Truth.assert_) Iterator(java.util.Iterator) Sandboxed(com.google.gerrit.acceptance.Sandboxed) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Truth.assertThat(com.google.common.truth.Truth.assertThat) TransportException(org.eclipse.jgit.api.errors.TransportException) ArmoredOutputStream(org.bouncycastle.bcpg.ArmoredOutputStream) Provider(com.google.inject.Provider) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) StarsInput(com.google.gerrit.extensions.api.changes.StarsInput) PushCertificateIdent(org.eclipse.jgit.transport.PushCertificateIdent) TestAccount(com.google.gerrit.acceptance.TestAccount) Account(com.google.gerrit.reviewdb.client.Account) TestKey(com.google.gerrit.gpg.testutil.TestKey) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) GpgKeyInfo(com.google.gerrit.extensions.common.GpgKeyInfo)

Example 2 with SCHEME_GPGKEY

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

the class PostGpgKeys method apply.

@Override
public Map<String, GpgKeyInfo> apply(AccountResource rsrc, Input input) throws ResourceNotFoundException, BadRequestException, ResourceConflictException, PGPException, OrmException, IOException, ConfigInvalidException {
    GpgKeys.checkVisible(self, rsrc);
    Collection<ExternalId> existingExtIds = externalIds.byAccount(rsrc.getUser().getAccountId(), SCHEME_GPGKEY);
    try (PublicKeyStore store = storeProvider.get()) {
        Set<Fingerprint> toRemove = readKeysToRemove(input, existingExtIds);
        List<PGPPublicKeyRing> newKeys = readKeysToAdd(input, toRemove);
        List<ExternalId> newExtIds = new ArrayList<>(existingExtIds.size());
        for (PGPPublicKeyRing keyRing : newKeys) {
            PGPPublicKey key = keyRing.getPublicKey();
            ExternalId.Key extIdKey = toExtIdKey(key.getFingerprint());
            Account account = getAccountByExternalId(extIdKey);
            if (account != null) {
                if (!account.getId().equals(rsrc.getUser().getAccountId())) {
                    throw new ResourceConflictException("GPG key already associated with another account");
                }
            } else {
                newExtIds.add(ExternalId.create(extIdKey, rsrc.getUser().getAccountId()));
            }
        }
        storeKeys(rsrc, newKeys, toRemove);
        List<ExternalId.Key> extIdKeysToRemove = toRemove.stream().map(fp -> toExtIdKey(fp.get())).collect(toList());
        externalIdsUpdateFactory.create().replace(rsrc.getUser().getAccountId(), extIdKeysToRemove, newExtIds);
        accountCache.evict(rsrc.getUser().getAccountId());
        return toJson(newKeys, toRemove, store, rsrc.getUser());
    }
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) OrmException(com.google.gwtorm.server.OrmException) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) ByteArrayInputStream(java.io.ByteArrayInputStream) GpgKeyInfo(com.google.gerrit.extensions.common.GpgKeyInfo) Map(java.util.Map) PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) PGPException(org.bouncycastle.openpgp.PGPException) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) RefUpdate(org.eclipse.jgit.lib.RefUpdate) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) SCHEME_GPGKEY(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GPGKEY) Sets(com.google.common.collect.Sets) ExternalIds(com.google.gerrit.server.account.externalids.ExternalIds) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) ExternalIdsUpdate(com.google.gerrit.server.account.externalids.ExternalIdsUpdate) Joiner(com.google.common.base.Joiner) Singleton(com.google.inject.Singleton) AccountCache(com.google.gerrit.server.account.AccountCache) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) PublicKeyStore(com.google.gerrit.gpg.PublicKeyStore) InternalAccountQuery(com.google.gerrit.server.query.account.InternalAccountQuery) GerritPublicKeyChecker(com.google.gerrit.gpg.GerritPublicKeyChecker) Fingerprint(com.google.gerrit.gpg.Fingerprint) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Account(com.google.gerrit.reviewdb.client.Account) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ArmoredInputStream(org.bouncycastle.bcpg.ArmoredInputStream) CheckResult(com.google.gerrit.gpg.CheckResult) Input(com.google.gerrit.gpg.server.PostGpgKeys.Input) CurrentUser(com.google.gerrit.server.CurrentUser) Logger(org.slf4j.Logger) BaseEncoding(com.google.common.io.BaseEncoding) UTF_8(java.nio.charset.StandardCharsets.UTF_8) AccountResource(com.google.gerrit.server.account.AccountResource) EmailException(com.google.gerrit.common.errors.EmailException) PublicKeyChecker(com.google.gerrit.gpg.PublicKeyChecker) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) PublicKeyStore.keyIdToString(com.google.gerrit.gpg.PublicKeyStore.keyIdToString) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) AddKeySender(com.google.gerrit.server.mail.send.AddKeySender) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AccountState(com.google.gerrit.server.account.AccountState) BcPGPObjectFactory(org.bouncycastle.openpgp.bc.BcPGPObjectFactory) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) InputStream(java.io.InputStream) PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) Account(com.google.gerrit.reviewdb.client.Account) Fingerprint(com.google.gerrit.gpg.Fingerprint) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) PublicKeyStore(com.google.gerrit.gpg.PublicKeyStore) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 BaseEncoding (com.google.common.io.BaseEncoding)2 GpgKeyInfo (com.google.gerrit.extensions.common.GpgKeyInfo)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 Fingerprint (com.google.gerrit.gpg.Fingerprint)2 PublicKeyStore (com.google.gerrit.gpg.PublicKeyStore)2 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)2 Joiner (com.google.common.base.Joiner)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 FluentIterable (com.google.common.collect.FluentIterable)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Truth.assert_ (com.google.common.truth.Truth.assert_)1 AtomicLongMap (com.google.common.util.concurrent.AtomicLongMap)1