Search in sources :

Example 1 with EmailException

use of com.google.gerrit.exceptions.EmailException in project gerrit by GerritCodeReview.

the class PostGpgKeys method tryStoreKeys.

private Void tryStoreKeys(AccountResource rsrc, List<PGPPublicKeyRing> keyRings, Collection<Fingerprint> toRemove) throws RestApiException, PGPException, IOException {
    try (PublicKeyStore store = storeProvider.get()) {
        List<String> addedKeys = new ArrayList<>();
        IdentifiedUser user = rsrc.getUser();
        for (PGPPublicKeyRing keyRing : keyRings) {
            PGPPublicKey key = keyRing.getPublicKey();
            // Don't check web of trust; admins can fill in certifications later.
            CheckResult result = checkerFactory.create(user, store).disableTrust().check(key);
            if (!result.isOk()) {
                throw new BadRequestException(String.format("Problems with public key %s:\n%s", keyToString(key), Joiner.on('\n').join(result.getProblems())));
            }
            addedKeys.add(PublicKeyStore.keyToString(key));
            store.add(keyRing);
        }
        for (Fingerprint fp : toRemove) {
            store.remove(fp.get());
        }
        CommitBuilder cb = new CommitBuilder();
        PersonIdent committer = serverIdent.get();
        cb.setAuthor(user.newCommitterIdent(committer));
        cb.setCommitter(committer);
        RefUpdate.Result saveResult = store.save(cb);
        switch(saveResult) {
            case NEW:
            case FAST_FORWARD:
            case FORCED:
                if (!addedKeys.isEmpty()) {
                    try {
                        addKeySenderFactory.create(user, addedKeys).send();
                    } catch (EmailException e) {
                        logger.atSevere().withCause(e).log("Cannot send GPG key added message to %s", rsrc.getUser().getAccount().preferredEmail());
                    }
                }
                if (!toRemove.isEmpty()) {
                    try {
                        deleteKeySenderFactory.create(user, toRemove.stream().map(Fingerprint::toString).collect(toList())).send();
                    } catch (EmailException e) {
                        logger.atSevere().withCause(e).log("Cannot send GPG key deleted message to %s", user.getAccount().preferredEmail());
                    }
                }
                break;
            case NO_CHANGE:
                break;
            case LOCK_FAILURE:
            case IO_FAILURE:
            case NOT_ATTEMPTED:
            case REJECTED:
            case REJECTED_CURRENT_BRANCH:
            case RENAMED:
            case REJECTED_MISSING_OBJECT:
            case REJECTED_OTHER_REASON:
            default:
                throw new StorageException(String.format("Failed to save public keys: %s", saveResult));
        }
    }
    return null;
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) Fingerprint(com.google.gerrit.gpg.Fingerprint) ArrayList(java.util.ArrayList) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) PublicKeyStore.keyIdToString(com.google.gerrit.gpg.PublicKeyStore.keyIdToString) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) CheckResult(com.google.gerrit.gpg.CheckResult) PublicKeyStore(com.google.gerrit.gpg.PublicKeyStore) EmailException(com.google.gerrit.exceptions.EmailException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) StorageException(com.google.gerrit.exceptions.StorageException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 2 with EmailException

use of com.google.gerrit.exceptions.EmailException in project gerrit by GerritCodeReview.

the class SmtpEmailSender method generateMultipartBoundary.

public static String generateMultipartBoundary(String textBody, String htmlBody) throws EmailException {
    byte[] bytes = new byte[8];
    ThreadLocalRandom rng = ThreadLocalRandom.current();
    // suffice, something is seriously wrong.
    for (int i = 0; i < 2; i++) {
        rng.nextBytes(bytes);
        String boundary = BaseEncoding.base64().encode(bytes);
        String encBoundary = "--" + boundary;
        if (textBody.contains(encBoundary) || htmlBody.contains(encBoundary)) {
            continue;
        }
        return boundary;
    }
    throw new EmailException("Gave up generating unique MIME boundary");
}
Also used : EmailException(com.google.gerrit.exceptions.EmailException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 3 with EmailException

use of com.google.gerrit.exceptions.EmailException in project gerrit by GerritCodeReview.

the class AddSshKey method apply.

public Response<SshKeyInfo> apply(IdentifiedUser user, SshKeyInput input) throws BadRequestException, IOException, ConfigInvalidException {
    if (input == null) {
        input = new SshKeyInput();
    }
    if (input.raw == null) {
        throw new BadRequestException("SSH public key missing");
    }
    final RawInput rawKey = input.raw;
    String sshPublicKey = new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return rawKey.getInputStream();
        }
    }.asCharSource(UTF_8).read();
    try {
        AccountSshKey sshKey = authorizedKeys.addKey(user.getAccountId(), sshPublicKey);
        try {
            addKeyFactory.create(user, sshKey).send();
        } catch (EmailException e) {
            logger.atSevere().withCause(e).log("Cannot send SSH key added message to %s", user.getAccount().preferredEmail());
        }
        user.getUserName().ifPresent(sshKeyCache::evict);
        return Response.created(GetSshKeys.newSshKeyInfo(sshKey));
    } catch (InvalidSshKeyException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : SshKeyInput(com.google.gerrit.extensions.api.accounts.SshKeyInput) InvalidSshKeyException(com.google.gerrit.exceptions.InvalidSshKeyException) AccountSshKey(com.google.gerrit.server.account.AccountSshKey) RawInput(com.google.gerrit.extensions.restapi.RawInput) EmailException(com.google.gerrit.exceptions.EmailException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ByteSource(com.google.common.io.ByteSource)

Example 4 with EmailException

use of com.google.gerrit.exceptions.EmailException in project gerrit by GerritCodeReview.

the class DeleteGpgKey method apply.

@Override
public Response<?> apply(GpgKey rsrc, Input input) throws RestApiException, PGPException, IOException, ConfigInvalidException {
    PGPPublicKey key = rsrc.getKeyRing().getPublicKey();
    String fingerprint = BaseEncoding.base16().encode(key.getFingerprint());
    Optional<ExternalId> extId = externalIds.get(externalIdKeyFactory.create(SCHEME_GPGKEY, fingerprint));
    if (!extId.isPresent()) {
        throw new ResourceNotFoundException(fingerprint);
    }
    accountsUpdateProvider.get().update("Delete GPG Key via API", rsrc.getUser().getAccountId(), u -> u.deleteExternalId(extId.get()));
    try (PublicKeyStore store = storeProvider.get()) {
        store.remove(rsrc.getKeyRing().getPublicKey().getFingerprint());
        CommitBuilder cb = new CommitBuilder();
        PersonIdent committer = serverIdent.get();
        cb.setAuthor(rsrc.getUser().newCommitterIdent(committer));
        cb.setCommitter(committer);
        cb.setMessage("Delete public key " + keyIdToString(key.getKeyID()));
        RefUpdate.Result saveResult = store.save(cb);
        switch(saveResult) {
            case NO_CHANGE:
            case FAST_FORWARD:
                try {
                    deleteKeySenderFactory.create(rsrc.getUser(), ImmutableList.of(PublicKeyStore.keyToString(key))).send();
                } catch (EmailException e) {
                    logger.atSevere().withCause(e).log("Cannot send GPG key deletion message to %s", rsrc.getUser().getAccount().preferredEmail());
                }
                break;
            case LOCK_FAILURE:
            case FORCED:
            case IO_FAILURE:
            case NEW:
            case NOT_ATTEMPTED:
            case REJECTED:
            case REJECTED_CURRENT_BRANCH:
            case RENAMED:
            case REJECTED_MISSING_OBJECT:
            case REJECTED_OTHER_REASON:
            default:
                throw new StorageException(String.format("Failed to delete public key: %s", saveResult));
        }
    }
    return Response.none();
}
Also used : PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) PublicKeyStore(com.google.gerrit.gpg.PublicKeyStore) EmailException(com.google.gerrit.exceptions.EmailException) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) PublicKeyStore.keyIdToString(com.google.gerrit.gpg.PublicKeyStore.keyIdToString) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) StorageException(com.google.gerrit.exceptions.StorageException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 5 with EmailException

use of com.google.gerrit.exceptions.EmailException in project gerrit by GerritCodeReview.

the class SetAccountCommand method addEmail.

private void addEmail(String email) throws UnloggedFailure, RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
    EmailInput in = new EmailInput();
    in.email = email;
    in.noConfirmation = true;
    try {
        createEmail.apply(rsrc, IdString.fromDecoded(email), in);
    } catch (EmailException e) {
        throw die(e.getMessage());
    }
}
Also used : EmailException(com.google.gerrit.exceptions.EmailException) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput)

Aggregations

EmailException (com.google.gerrit.exceptions.EmailException)12 StorageException (com.google.gerrit.exceptions.StorageException)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 UsedAt (com.google.gerrit.common.UsedAt)2 Address (com.google.gerrit.entities.Address)2 EmailInput (com.google.gerrit.extensions.api.accounts.EmailInput)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 PublicKeyStore (com.google.gerrit.gpg.PublicKeyStore)2 PublicKeyStore.keyIdToString (com.google.gerrit.gpg.PublicKeyStore.keyIdToString)2 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)2 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)2 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)2 IOException (java.io.IOException)2 AuthSMTPClient (org.apache.commons.net.smtp.AuthSMTPClient)2 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)2 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 ByteSource (com.google.common.io.ByteSource)1 Account (com.google.gerrit.entities.Account)1