use of org.candlepin.model.IdentityCertificate in project candlepin by candlepin.
the class ExporterTest method exportMetadata.
@Test
public void exportMetadata() throws ExportCreationException, IOException {
config.setProperty(ConfigProperties.SYNC_WORK_DIR, "/tmp/");
Date start = new Date();
Rules mrules = mock(Rules.class);
Consumer consumer = mock(Consumer.class);
Principal principal = mock(Principal.class);
IdentityCertificate idcert = new IdentityCertificate();
when(mrules.getRules()).thenReturn("foobar");
when(pki.getSHA256WithRSAHash(any(InputStream.class))).thenReturn("signature".getBytes());
when(rc.getRules()).thenReturn(mrules);
when(pprov.get()).thenReturn(principal);
when(principal.getUsername()).thenReturn("testUser");
idcert.setSerial(new CertificateSerial(10L, new Date()));
idcert.setKey("euh0876puhapodifbvj094");
idcert.setCert("hpj-08ha-w4gpoknpon*)&^%#");
idcert.setCreated(new Date());
idcert.setUpdated(new Date());
when(consumer.getIdCert()).thenReturn(idcert);
KeyPair keyPair = createKeyPair();
when(consumer.getKeyPair()).thenReturn(keyPair);
when(pki.getPemEncoded(keyPair.getPrivateKey())).thenReturn("privateKey".getBytes());
when(pki.getPemEncoded(keyPair.getPublicKey())).thenReturn("publicKey".getBytes());
CandlepinQuery cqmock = mock(CandlepinQuery.class);
when(cqmock.iterator()).thenReturn(Arrays.asList(new ConsumerType("system")).iterator());
when(ctc.listAll()).thenReturn(cqmock);
CandlepinQuery emptyIteratorMock = mock(CandlepinQuery.class);
when(emptyIteratorMock.iterate()).thenReturn(new MockResultIterator(Arrays.asList().iterator()));
when(cdnc.listAll()).thenReturn(emptyIteratorMock);
// FINALLY test this badboy
Exporter e = new Exporter(ctc, oc, me, ce, cte, re, ece, ecsa, pe, psa, pce, ec, ee, pki, config, exportRules, pprov, dvc, dve, cdnc, cdne, pc, su, exportExtensionAdapter, translator);
File export = e.getFullExport(consumer);
// VERIFY
assertNotNull(export);
assertTrue(export.exists());
verifyContent(export, "export/meta.json", new VerifyMetadata(start));
// cleanup the mess
FileUtils.deleteDirectory(export.getParentFile());
assertTrue(new File("/tmp/consumer_export.zip").delete());
assertTrue(new File("/tmp/meta.json").delete());
}
use of org.candlepin.model.IdentityCertificate in project candlepin by candlepin.
the class TestUtil method createIdCert.
public static IdentityCertificate createIdCert(Date expiration) {
IdentityCertificate idCert = new IdentityCertificate();
CertificateSerial serial = new CertificateSerial(expiration);
serial.setId(Long.valueOf(new Random().nextInt(1000000)));
// totally arbitrary
idCert.setId(String.valueOf(new Random().nextInt(1000000)));
idCert.setKey("uh0876puhapodifbvj094");
idCert.setCert("hpj-08ha-w4gpoknpon*)&^%#");
idCert.setSerial(serial);
return idCert;
}
use of org.candlepin.model.IdentityCertificate in project candlepin by candlepin.
the class Importer method importConsumer.
protected ConsumerDTO importConsumer(Owner owner, File consumerFile, File[] upstreamConsumer, ConflictOverrides forcedConflicts, Meta meta) throws IOException, SyncDataFormatException {
IdentityCertificate idcert = null;
for (File uc : upstreamConsumer) {
if (uc.getName().endsWith(".json")) {
log.debug("Import upstream consumeridentity certificate: {}", uc.getName());
try (Reader reader = new FileReader(uc)) {
CertificateDTO dtoCert = mapper.readValue(reader, CertificateDTO.class);
idcert = new IdentityCertificate();
populateEntity(idcert, dtoCert);
}
} else {
log.warn("Extra file found in upstream_consumer directory: {}", uc.getName());
}
}
ConsumerImporter importer = new ConsumerImporter(ownerCurator, idCertCurator, i18n, csCurator);
Reader reader = null;
ConsumerDTO consumer = null;
try {
reader = new FileReader(consumerFile);
consumer = importer.createObject(mapper, reader);
// we can not rely on the actual ConsumerType in the ConsumerDto
// because it could have an id not in our database. We need to
// stick with the label. Hence we need to lookup the ACTUAL type
// by label here before attempting to store the UpstreamConsumer
ConsumerType type = consumerTypeCurator.lookupByLabel(consumer.getType().getLabel());
consumer.setType(this.translator.translate(type, ConsumerTypeDTO.class));
// the metadata
if (StringUtils.isEmpty(consumer.getUrlWeb())) {
consumer.setUrlWeb(meta.getWebAppPrefix());
}
importer.store(owner, consumer, forcedConflicts, idcert);
} finally {
if (reader != null) {
reader.close();
}
}
return consumer;
}
use of org.candlepin.model.IdentityCertificate in project candlepin by candlepin.
the class DefaultIdentityCertServiceAdapter method regenerateIdentityCert.
@Override
public IdentityCertificate regenerateIdentityCert(Consumer consumer) throws GeneralSecurityException, IOException {
IdentityCertificate certificate = null;
if (consumer.getIdCert() != null) {
certificate = idCertCurator.find(consumer.getIdCert().getId());
}
if (certificate != null) {
consumer.setIdCert(null);
idCertCurator.delete(certificate);
}
return generate(consumer);
}
use of org.candlepin.model.IdentityCertificate in project candlepin by candlepin.
the class ConsumerResource method createConsumerFromDTO.
public Consumer createConsumerFromDTO(ConsumerDTO consumer, ConsumerType type, Principal principal, String userName, String ownerKey, String activationKeys, boolean identityCertCreation) throws BadRequestException {
// API:registerConsumer
Set<String> keyStrings = splitKeys(activationKeys);
// Only let NoAuth principals through if there are activation keys to consider:
if ((principal instanceof NoAuthPrincipal) && keyStrings.isEmpty()) {
throw new ForbiddenException(i18n.tr("Insufficient permissions"));
}
validateOnKeyStrings(keyStrings, ownerKey, userName);
Owner owner = setupOwner(principal, ownerKey);
// Raise an exception if none of the keys specified exist for this owner.
List<ActivationKey> keys = checkActivationKeys(principal, owner, keyStrings);
userName = setUserName(consumer, principal, userName);
checkConsumerName(consumer);
validateViaConsumerType(consumer, type, keys, owner, userName, principal);
if (type.isType(ConsumerTypeEnum.SHARE)) {
// Share consumers do not need identity certificates so refuse to create them.
identityCertCreation = false;
validateShareConsumer(consumer, principal, keys);
// if there exists a share consumer between the two orgs, return it.
Consumer existingShareConsumer = consumerCurator.getSharingConsumer(owner, consumer.getRecipientOwnerKey());
if (existingShareConsumer != null) {
return existingShareConsumer;
}
consumer.setAutoheal(false);
} else {
// this is the default
consumer.setAutoheal(true);
if (StringUtils.isNotEmpty(consumer.getRecipientOwnerKey())) {
throw new BadRequestException(i18n.tr("Only share consumers can specify recipient owners"));
}
}
if (consumer.getServiceLevel() == null) {
consumer.setServiceLevel("");
}
// Sanitize the inbound facts
this.sanitizeConsumerFacts(consumer);
// If no service level was specified, and the owner has a default set, use it:
if (consumer.getServiceLevel().equals("") && owner.getDefaultServiceLevel() != null && !type.isType(ConsumerTypeEnum.SHARE)) {
consumer.setServiceLevel(owner.getDefaultServiceLevel());
}
Consumer consumerToCreate = new Consumer();
consumerToCreate.setOwner(owner);
populateEntity(consumerToCreate, consumer);
consumerToCreate.setType(type);
if (!type.isType(ConsumerTypeEnum.SHARE)) {
consumerToCreate.setCanActivate(subAdapter.canActivateSubscription(consumerToCreate));
}
HypervisorId hvsrId = consumerToCreate.getHypervisorId();
if (hvsrId != null && hvsrId.getHypervisorId() != null && !hvsrId.getHypervisorId().isEmpty()) {
// If a hypervisorId is supplied, make sure the consumer and owner are correct
hvsrId.setConsumer(consumerToCreate);
hvsrId.setOwner(owner);
}
updateCapabilities(consumerToCreate, null);
logNewConsumerDebugInfo(consumerToCreate, keys, type);
validateContentAccessMode(consumerToCreate, owner);
consumerBindUtil.validateServiceLevel(owner.getId(), consumerToCreate.getServiceLevel());
try {
Date createdDate = consumerToCreate.getCreated();
Date lastCheckIn = consumerToCreate.getLastCheckin();
// create sets created to current time.
consumerToCreate = consumerCurator.create(consumerToCreate);
// If we sent in a created date, we want it persisted at the update below
if (createdDate != null) {
consumerToCreate.setCreated(createdDate);
}
if (lastCheckIn != null) {
log.info("Creating with specific last check-in time: {}", lastCheckIn);
consumerToCreate.setLastCheckin(lastCheckIn);
}
if (identityCertCreation) {
IdentityCertificate idCert = generateIdCert(consumerToCreate, false);
consumerToCreate.setIdCert(idCert);
}
sink.emitConsumerCreated(consumerToCreate);
if (keys.size() > 0) {
consumerBindUtil.handleActivationKeys(consumerToCreate, keys, owner.isAutobindDisabled());
}
// Don't allow complianceRules to update entitlementStatus, because we're about to perform
// an update unconditionally.
complianceRules.getStatus(consumerToCreate, null, false, false);
consumerCurator.update(consumerToCreate);
log.info("Consumer {} created in org {}", consumerToCreate.getUuid(), consumerToCreate.getOwnerId());
return consumerToCreate;
} catch (CandlepinException ce) {
// If it is one of ours, rethrow it.
throw ce;
} catch (Exception e) {
log.error("Problem creating unit:", e);
throw new BadRequestException(i18n.tr("Problem creating unit {0}", consumer));
}
}
Aggregations