Search in sources :

Example 1 with EmailException

use of com.google.gerrit.common.errors.EmailException in project gerrit by GerritCodeReview.

the class CreateEmail method apply.

public Response<EmailInfo> apply(IdentifiedUser user, EmailInput input) throws AuthException, BadRequestException, ResourceConflictException, ResourceNotFoundException, OrmException, EmailException, MethodNotAllowedException, IOException, ConfigInvalidException, PermissionBackendException {
    if (input.email != null && !email.equals(input.email)) {
        throw new BadRequestException("email address must match URL");
    }
    EmailInfo info = new EmailInfo();
    info.email = email;
    if (input.noConfirmation || isDevMode) {
        if (isDevMode) {
            log.warn("skipping email validation in developer mode");
        }
        try {
            accountManager.link(user.getAccountId(), AuthRequest.forEmail(email));
        } catch (AccountException e) {
            throw new ResourceConflictException(e.getMessage());
        }
        if (input.preferred) {
            putPreferred.apply(new AccountResource.Email(user, email), null);
            info.preferred = true;
        }
    } else {
        try {
            RegisterNewEmailSender sender = registerNewEmailFactory.create(email);
            if (!sender.isAllowed()) {
                throw new MethodNotAllowedException("Not allowed to add email address " + email);
            }
            sender.send();
            info.pendingConfirmation = true;
        } catch (EmailException | RuntimeException e) {
            log.error("Cannot send email verification message to " + email, e);
            throw e;
        }
    }
    return Response.created(info);
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) RegisterNewEmailSender(com.google.gerrit.server.mail.send.RegisterNewEmailSender) EmailException(com.google.gerrit.common.errors.EmailException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) EmailInfo(com.google.gerrit.extensions.common.EmailInfo)

Example 2 with EmailException

use of com.google.gerrit.common.errors.EmailException in project gerrit by GerritCodeReview.

the class OutgoingEmail method velocifyFile.

protected String velocifyFile(String name) throws EmailException {
    try {
        RuntimeInstance runtime = args.velocityRuntime;
        if (runtime.getLoaderNameForResource(name) == null) {
            name = "com/google/gerrit/server/mail/" + name;
        }
        Template template = runtime.getTemplate(name, UTF_8.name());
        StringWriter w = new StringWriter();
        template.merge(velocityContext, w);
        return w.toString();
    } catch (Exception e) {
        throw new EmailException("Cannot format velocity template " + name, e);
    }
}
Also used : StringWriter(java.io.StringWriter) EmailException(com.google.gerrit.common.errors.EmailException) RuntimeInstance(org.apache.velocity.runtime.RuntimeInstance) OrmException(com.google.gwtorm.server.OrmException) ValidationException(com.google.gerrit.server.validators.ValidationException) MalformedURLException(java.net.MalformedURLException) EmailException(com.google.gerrit.common.errors.EmailException) Template(org.apache.velocity.Template)

Example 3 with EmailException

use of com.google.gerrit.common.errors.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.common.errors.EmailException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 4 with EmailException

use of com.google.gerrit.common.errors.EmailException in project gerrit by GerritCodeReview.

the class OutgoingEmail method velocify.

protected String velocify(String template) throws EmailException {
    try {
        RuntimeInstance runtime = args.velocityRuntime;
        String templateName = "OutgoingEmail";
        SimpleNode tree = runtime.parse(new StringReader(template), templateName);
        InternalContextAdapterImpl ica = new InternalContextAdapterImpl(velocityContext);
        ica.pushCurrentTemplateName(templateName);
        try {
            tree.init(ica, runtime);
            StringWriter w = new StringWriter();
            tree.render(ica, w);
            return w.toString();
        } finally {
            ica.popCurrentTemplateName();
        }
    } catch (Exception e) {
        throw new EmailException("Cannot format velocity template: " + template, e);
    }
}
Also used : InternalContextAdapterImpl(org.apache.velocity.context.InternalContextAdapterImpl) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) EmailException(com.google.gerrit.common.errors.EmailException) RuntimeInstance(org.apache.velocity.runtime.RuntimeInstance) OrmException(com.google.gwtorm.server.OrmException) ValidationException(com.google.gerrit.server.validators.ValidationException) MalformedURLException(java.net.MalformedURLException) EmailException(com.google.gerrit.common.errors.EmailException) SimpleNode(org.apache.velocity.runtime.parser.node.SimpleNode)

Example 5 with EmailException

use of com.google.gerrit.common.errors.EmailException in project gerrit by GerritCodeReview.

the class PostGpgKeys method storeKeys.

private void storeKeys(AccountResource rsrc, List<PGPPublicKeyRing> keyRings, Set<Fingerprint> toRemove) throws BadRequestException, ResourceConflictException, PGPException, IOException {
    try (PublicKeyStore store = storeProvider.get()) {
        List<String> addedKeys = new ArrayList<>();
        for (PGPPublicKeyRing keyRing : keyRings) {
            PGPPublicKey key = keyRing.getPublicKey();
            // Don't check web of trust; admins can fill in certifications later.
            CheckResult result = checkerFactory.create(rsrc.getUser(), 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(rsrc.getUser().newCommitterIdent(committer.getWhen(), committer.getTimeZone()));
        cb.setCommitter(committer);
        RefUpdate.Result saveResult = store.save(cb);
        switch(saveResult) {
            case NEW:
            case FAST_FORWARD:
            case FORCED:
                try {
                    addKeyFactory.create(rsrc.getUser(), addedKeys).send();
                } catch (EmailException e) {
                    log.error("Cannot send GPG key added message to " + rsrc.getUser().getAccount().getPreferredEmail(), e);
                }
                break;
            case NO_CHANGE:
                break;
            case IO_FAILURE:
            case LOCK_FAILURE:
            case NOT_ATTEMPTED:
            case REJECTED:
            case REJECTED_CURRENT_BRANCH:
            case RENAMED:
            default:
                // TODO(dborowitz): Backoff and retry on LOCK_FAILURE.
                throw new ResourceConflictException("Failed to save public keys: " + saveResult);
        }
    }
}
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) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) 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.common.errors.EmailException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Aggregations

EmailException (com.google.gerrit.common.errors.EmailException)10 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 OrmException (com.google.gwtorm.server.OrmException)3 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 ValidationException (com.google.gerrit.server.validators.ValidationException)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 MalformedURLException (java.net.MalformedURLException)2 Date (java.util.Date)2 AuthSMTPClient (org.apache.commons.net.smtp.AuthSMTPClient)2 RuntimeInstance (org.apache.velocity.runtime.RuntimeInstance)2 ByteSource (com.google.common.io.ByteSource)1 InvalidSshKeyException (com.google.gerrit.common.errors.InvalidSshKeyException)1 EmailInput (com.google.gerrit.extensions.api.accounts.EmailInput)1 EmailInfo (com.google.gerrit.extensions.common.EmailInfo)1 SshKeyInfo (com.google.gerrit.extensions.common.SshKeyInfo)1 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)1 RawInput (com.google.gerrit.extensions.restapi.RawInput)1 CheckResult (com.google.gerrit.gpg.CheckResult)1 Fingerprint (com.google.gerrit.gpg.Fingerprint)1