Search in sources :

Example 1 with AcmeConflictException

use of org.shredzone.acme4j.exception.AcmeConflictException in project stdlib by petergeneric.

the class LetsEncryptService method getRegistration.

public Registration getRegistration() {
    if (_registration == null) {
        LetsEncryptAccountEntity existing = accountDao.getById(LetsEncryptAccountEntity.MAIN_ACCOUNT_ID);
        final KeyPair keypair;
        try {
            if (existing != null) {
                ByteArrayInputStream bis = new ByteArrayInputStream(existing.getKeypair());
                InputStreamReader r = new InputStreamReader(bis, StandardCharsets.UTF_8);
                keypair = KeyPairUtils.readKeyPair(r);
            } else {
                keypair = KeyPairUtils.createKeyPair(REGISTRATION_KEY_SIZE);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                OutputStreamWriter w = new OutputStreamWriter(bos, StandardCharsets.UTF_8);
                KeyPairUtils.writeKeyPair(keypair, w);
                existing = new LetsEncryptAccountEntity();
                existing.setId(LetsEncryptAccountEntity.MAIN_ACCOUNT_ID);
                existing.setKeypair(bos.toByteArray());
                // Save the generated keypair
                accountDao.save(existing);
            }
        } catch (IOException e) {
            throw new RuntimeException("Error creating/loading/saving Let's Encrypt Registration Keypair", e);
        }
        Session session = new Session(acmeServerUri, keypair);
        Registration registration;
        {
            try {
                try {
                    final RegistrationBuilder registrationBuilder = new RegistrationBuilder();
                    registration = registrationBuilder.create(session);
                } catch (AcmeConflictException ex) {
                    registration = Registration.bind(session, ex.getLocation());
                }
                // Automatically accept any agreement updates
                registration.modify().setAgreement(registration.getAgreement()).commit();
            } catch (Exception e) {
                throw new RuntimeException("Unexpected error registering with ACME CA", e);
            }
        }
        _registration = registration;
    }
    return _registration;
}
Also used : KeyPair(java.security.KeyPair) InputStreamReader(java.io.InputStreamReader) LetsEncryptAccountEntity(com.peterphi.servicemanager.service.db.entity.LetsEncryptAccountEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AcmeConflictException(org.shredzone.acme4j.exception.AcmeConflictException) IOException(java.io.IOException) AcmeException(org.shredzone.acme4j.exception.AcmeException) RegistrationBuilder(org.shredzone.acme4j.RegistrationBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) Registration(org.shredzone.acme4j.Registration) OutputStreamWriter(java.io.OutputStreamWriter) AcmeConflictException(org.shredzone.acme4j.exception.AcmeConflictException) Session(org.shredzone.acme4j.Session)

Aggregations

LetsEncryptAccountEntity (com.peterphi.servicemanager.service.db.entity.LetsEncryptAccountEntity)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 KeyPair (java.security.KeyPair)1 Registration (org.shredzone.acme4j.Registration)1 RegistrationBuilder (org.shredzone.acme4j.RegistrationBuilder)1 Session (org.shredzone.acme4j.Session)1 AcmeConflictException (org.shredzone.acme4j.exception.AcmeConflictException)1 AcmeException (org.shredzone.acme4j.exception.AcmeException)1