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();
}
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;
}
Aggregations