use of com.peterphi.servicemanager.service.db.entity.LetsEncryptAccountEntity 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;
}
Aggregations