Search in sources :

Example 1 with OwnerEnvContentAccess

use of org.candlepin.model.OwnerEnvContentAccess in project candlepin by candlepin.

the class DefaultContentAccessCertServiceAdapter method hasCertChangedSince.

public boolean hasCertChangedSince(Consumer consumer, Date date) {
    if (date == null) {
        return true;
    }
    Environment env = this.environmentCurator.getConsumerEnvironment(consumer);
    OwnerEnvContentAccess oeca = ownerEnvContentAccessCurator.getContentAccess(consumer.getOwnerId(), env == null ? null : env.getId());
    return oeca == null || consumer.getContentAccessCert() == null || oeca.getUpdated().getTime() > date.getTime();
}
Also used : Environment(org.candlepin.model.Environment) OwnerEnvContentAccess(org.candlepin.model.OwnerEnvContentAccess)

Example 2 with OwnerEnvContentAccess

use of org.candlepin.model.OwnerEnvContentAccess in project candlepin by candlepin.

the class DefaultContentAccessCertServiceAdapter method getCertificate.

@Transactional
public ContentAccessCertificate getCertificate(Consumer consumer) throws GeneralSecurityException, IOException {
    Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId());
    // appropriate cert generation
    if (!ORG_ENV_ACCESS_MODE.equals(owner.getContentAccessMode()) || !this.consumerIsCertV3Capable(consumer)) {
        return null;
    }
    ContentAccessCertificate existing = consumer.getContentAccessCert();
    ContentAccessCertificate result = new ContentAccessCertificate();
    String pem = "";
    if (existing != null && existing.getSerial().getExpiration().getTime() < (new Date()).getTime()) {
        consumer.setContentAccessCert(null);
        contentAccessCertificateCurator.delete(existing);
        existing = null;
    }
    if (existing == null) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, -1);
        Date startDate = cal.getTime();
        cal.add(Calendar.YEAR, 1);
        Date endDate = cal.getTime();
        CertificateSerial serial = new CertificateSerial(endDate);
        // We need the sequence generated id before we create the Certificate,
        // otherwise we could have used cascading create
        serialCurator.create(serial);
        KeyPair keyPair = keyPairCurator.getConsumerKeyPair(consumer);
        byte[] pemEncodedKeyPair = pki.getPemEncoded(keyPair.getPrivate());
        X509Certificate x509Cert = createX509Certificate(consumer, owner, BigInteger.valueOf(serial.getId()), keyPair, startDate, endDate);
        existing = new ContentAccessCertificate();
        existing.setSerial(serial);
        existing.setKeyAsBytes(pemEncodedKeyPair);
        existing.setConsumer(consumer);
        log.info("Setting PEM encoded cert.");
        pem = new String(this.pki.getPemEncoded(x509Cert));
        existing.setCert(pem);
        consumer.setContentAccessCert(existing);
        contentAccessCertificateCurator.create(existing);
        consumerCurator.merge(consumer);
    } else {
        pem = existing.getCert();
    }
    Environment env = this.environmentCurator.getConsumerEnvironment(consumer);
    // we need to see if this is newer than the previous result
    OwnerEnvContentAccess oeca = ownerEnvContentAccessCurator.getContentAccess(owner.getId(), env == null ? null : env.getId());
    if (oeca == null) {
        String contentJson = createPayloadAndSignature(owner, env);
        oeca = new OwnerEnvContentAccess(owner, env, contentJson);
        ownerEnvContentAccessCurator.saveOrUpdate(oeca);
    }
    pem += oeca.getContentJson();
    result.setCert(pem);
    result.setCreated(existing.getCreated());
    result.setUpdated(existing.getUpdated());
    result.setId(existing.getId());
    result.setConsumer(existing.getConsumer());
    result.setKey(existing.getKey());
    result.setSerial(existing.getSerial());
    return result;
}
Also used : Owner(org.candlepin.model.Owner) KeyPair(java.security.KeyPair) Calendar(java.util.Calendar) CertificateSerial(org.candlepin.model.CertificateSerial) Environment(org.candlepin.model.Environment) ContentAccessCertificate(org.candlepin.model.ContentAccessCertificate) OwnerEnvContentAccess(org.candlepin.model.OwnerEnvContentAccess) Date(java.util.Date) X509Certificate(java.security.cert.X509Certificate) Transactional(com.google.inject.persist.Transactional)

Aggregations

Environment (org.candlepin.model.Environment)2 OwnerEnvContentAccess (org.candlepin.model.OwnerEnvContentAccess)2 Transactional (com.google.inject.persist.Transactional)1 KeyPair (java.security.KeyPair)1 X509Certificate (java.security.cert.X509Certificate)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 CertificateSerial (org.candlepin.model.CertificateSerial)1 ContentAccessCertificate (org.candlepin.model.ContentAccessCertificate)1 Owner (org.candlepin.model.Owner)1