Search in sources :

Example 1 with GpgKeyInfo

use of com.google.gerrit.extensions.common.GpgKeyInfo in project gerrit by GerritCodeReview.

the class GpgKeys method toJson.

public static GpgKeyInfo toJson(PGPPublicKey key, CheckResult checkResult) throws IOException {
    GpgKeyInfo info = new GpgKeyInfo();
    if (key != null) {
        info.id = PublicKeyStore.keyIdToString(key.getKeyID());
        info.fingerprint = Fingerprint.toString(key.getFingerprint());
        @SuppressWarnings("unchecked") Iterator<String> userIds = key.getUserIDs();
        info.userIds = ImmutableList.copyOf(userIds);
        try (ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
            ArmoredOutputStream aout = new ArmoredOutputStream(out)) {
            // This is not exactly the key stored in the store, but is equivalent. In
            // particular, it will have a Bouncy Castle version string. The armored
            // stream reader in PublicKeyStore doesn't give us an easy way to extract
            // the original ASCII armor.
            key.encode(aout);
            info.key = new String(out.toByteArray(), UTF_8);
        }
    }
    info.status = checkResult.getStatus();
    info.problems = checkResult.getProblems();
    return info;
}
Also used : ArmoredOutputStream(org.bouncycastle.bcpg.ArmoredOutputStream) IdString(com.google.gerrit.extensions.restapi.IdString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GpgKeyInfo(com.google.gerrit.extensions.common.GpgKeyInfo)

Example 2 with GpgKeyInfo

use of com.google.gerrit.extensions.common.GpgKeyInfo in project gerrit by GerritCodeReview.

the class PostGpgKeys method toJson.

private Map<String, GpgKeyInfo> toJson(Collection<PGPPublicKeyRing> keys, Set<Fingerprint> deleted, PublicKeyStore store, IdentifiedUser user) throws IOException {
    // Unlike when storing keys, include web-of-trust checks when producing
    // result JSON, so the user at least knows of any issues.
    PublicKeyChecker checker = checkerFactory.create(user, store);
    Map<String, GpgKeyInfo> infos = Maps.newHashMapWithExpectedSize(keys.size() + deleted.size());
    for (PGPPublicKeyRing keyRing : keys) {
        PGPPublicKey key = keyRing.getPublicKey();
        CheckResult result = checker.check(key);
        GpgKeyInfo info = GpgKeys.toJson(key, result);
        infos.put(info.id, info);
        info.id = null;
    }
    for (Fingerprint fp : deleted) {
        infos.put(keyIdToString(fp.getId()), new GpgKeyInfo());
    }
    return infos;
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) Fingerprint(com.google.gerrit.gpg.Fingerprint) CheckResult(com.google.gerrit.gpg.CheckResult) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) PublicKeyStore.keyIdToString(com.google.gerrit.gpg.PublicKeyStore.keyIdToString) GerritPublicKeyChecker(com.google.gerrit.gpg.GerritPublicKeyChecker) PublicKeyChecker(com.google.gerrit.gpg.PublicKeyChecker) GpgKeyInfo(com.google.gerrit.extensions.common.GpgKeyInfo)

Example 3 with GpgKeyInfo

use of com.google.gerrit.extensions.common.GpgKeyInfo 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 4 with GpgKeyInfo

use of com.google.gerrit.extensions.common.GpgKeyInfo in project gerrit by GerritCodeReview.

the class AccountIT method assertKeyMapContains.

private static void assertKeyMapContains(TestKey expected, Map<String, GpgKeyInfo> actualMap) {
    GpgKeyInfo actual = actualMap.get(expected.getKeyIdString());
    assertThat(actual).isNotNull();
    assertThat(actual.id).isNull();
    actual.id = expected.getKeyIdString();
    assertKeyEquals(expected, actual);
}
Also used : GpgKeyInfo(com.google.gerrit.extensions.common.GpgKeyInfo)

Example 5 with GpgKeyInfo

use of com.google.gerrit.extensions.common.GpgKeyInfo 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();
    GpgKeyInfo info = addGpgKey(armor(pk)).get(id);
    assertThat(info.userIds).hasSize(2);
    assertIteratorSize(2, getOnlyKeyFromStore(key).getUserIDs());
    pk = PGPPublicKey.removeCertification(pk, "foo:myId");
    info = addGpgKey(armor(pk)).get(id);
    assertThat(info.userIds).hasSize(1);
    assertIteratorSize(1, getOnlyKeyFromStore(key).getUserIDs());
}
Also used : TestKey(com.google.gerrit.gpg.testutil.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)

Aggregations

GpgKeyInfo (com.google.gerrit.extensions.common.GpgKeyInfo)7 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)5 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)4 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)3 Fingerprint (com.google.gerrit.gpg.Fingerprint)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 BaseEncoding (com.google.common.io.BaseEncoding)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 TestKey (com.google.gerrit.gpg.testutil.TestKey)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArmoredOutputStream (org.bouncycastle.bcpg.ArmoredOutputStream)2 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)2 Test (org.junit.Test)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