Search in sources :

Example 31 with IdentityCertificate

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());
}
Also used : KeyPair(org.candlepin.model.KeyPair) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CertificateSerial(org.candlepin.model.CertificateSerial) CandlepinQuery(org.candlepin.model.CandlepinQuery) Rules(org.candlepin.model.Rules) ExportRules(org.candlepin.policy.js.export.ExportRules) Date(java.util.Date) Consumer(org.candlepin.model.Consumer) ConsumerType(org.candlepin.model.ConsumerType) File(java.io.File) Principal(org.candlepin.auth.Principal) IdentityCertificate(org.candlepin.model.IdentityCertificate) MockResultIterator(org.candlepin.test.MockResultIterator) Test(org.junit.Test)

Example 32 with IdentityCertificate

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;
}
Also used : Random(java.util.Random) CertificateSerial(org.candlepin.model.CertificateSerial) IdentityCertificate(org.candlepin.model.IdentityCertificate)

Example 33 with IdentityCertificate

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;
}
Also used : CertificateDTO(org.candlepin.dto.manifest.v1.CertificateDTO) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) ConsumerType(org.candlepin.model.ConsumerType) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ConsumerTypeDTO(org.candlepin.dto.manifest.v1.ConsumerTypeDTO) IdentityCertificate(org.candlepin.model.IdentityCertificate)

Example 34 with IdentityCertificate

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);
}
Also used : IdentityCertificate(org.candlepin.model.IdentityCertificate)

Example 35 with IdentityCertificate

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));
    }
}
Also used : CandlepinException(org.candlepin.common.exceptions.CandlepinException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Owner(org.candlepin.model.Owner) NoAuthPrincipal(org.candlepin.auth.NoAuthPrincipal) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Date(java.util.Date) GeneralSecurityException(java.security.GeneralSecurityException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) IseException(org.candlepin.common.exceptions.IseException) AutobindDisabledForOwnerException(org.candlepin.controller.AutobindDisabledForOwnerException) CandlepinException(org.candlepin.common.exceptions.CandlepinException) IOException(java.io.IOException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ExportCreationException(org.candlepin.sync.ExportCreationException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) PropertyValidationException(org.candlepin.util.PropertyValidationException) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) HypervisorId(org.candlepin.model.HypervisorId) IdentityCertificate(org.candlepin.model.IdentityCertificate)

Aggregations

IdentityCertificate (org.candlepin.model.IdentityCertificate)39 Consumer (org.candlepin.model.Consumer)25 Test (org.junit.Test)25 CertificateSerial (org.candlepin.model.CertificateSerial)16 Date (java.util.Date)14 Owner (org.candlepin.model.Owner)13 ConsumerType (org.candlepin.model.ConsumerType)10 File (java.io.File)8 ArrayList (java.util.ArrayList)8 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 HashMap (java.util.HashMap)7 ZipInputStream (java.util.zip.ZipInputStream)7 Principal (org.candlepin.auth.Principal)7 CandlepinQuery (org.candlepin.model.CandlepinQuery)7 List (java.util.List)6 Set (java.util.Set)6 KeyPair (org.candlepin.model.KeyPair)6 Rules (org.candlepin.model.Rules)6 VirtConsumerMap (org.candlepin.model.VirtConsumerMap)6