Search in sources :

Example 21 with Environment

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

the class EnvironmentResource method create.

@ApiOperation(notes = "Creates an Environment", value = "create")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@SecurityHole(noAuth = true)
@Path("/{env_id}/consumers")
public ConsumerDTO create(@PathParam("env_id") String envId, @ApiParam(name = "consumer", required = true) ConsumerDTO consumer, @Context Principal principal, @QueryParam("username") String userName, @QueryParam("owner") String ownerKey, @QueryParam("activation_keys") String activationKeys) throws BadRequestException {
    Environment e = lookupEnvironment(envId);
    consumer.setEnvironment(translator.translate(e, EnvironmentDTO.class));
    return this.consumerResource.create(consumer, principal, userName, e.getOwner().getKey(), activationKeys, true);
}
Also used : EnvironmentDTO(org.candlepin.dto.api.v1.EnvironmentDTO) Environment(org.candlepin.model.Environment) Path(javax.ws.rs.Path) SecurityHole(org.candlepin.common.auth.SecurityHole) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation)

Example 22 with Environment

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

the class OwnerResource method createEnv.

/**
 * Creates an Environment for an Owner
 *
 * @return an Environment object
 * @httpcode 404
 * @httpcode 200
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{owner_key}/environments")
@ApiOperation(notes = "Creates an Environment for an Owner", value = "Create environment")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public EnvironmentDTO createEnv(@PathParam("owner_key") @Verify(Owner.class) String ownerKey, @ApiParam(name = "environment", required = true) EnvironmentDTO envDTO) {
    Environment env = new Environment();
    OwnerDTO ownerDTO = new OwnerDTO().setKey(ownerKey);
    envDTO.setOwner(ownerDTO);
    populateEntity(env, envDTO);
    env = envCurator.create(env);
    return translator.translate(env, EnvironmentDTO.class);
}
Also used : OwnerDTO(org.candlepin.dto.api.v1.OwnerDTO) Environment(org.candlepin.model.Environment) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 23 with Environment

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

the class OwnerResource method listEnvironments.

/**
 * Retrieves a list of Environments for an Owner
 *
 * @param envName Optional environment name filter to search for.
 * @return a list of Environment objects
 * @httpcode 200
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{owner_key}/environments")
@Wrapped(element = "environments")
@ApiOperation(notes = "Retrieves a list of Environments for an Owner", value = "List environments")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public CandlepinQuery<EnvironmentDTO> listEnvironments(@PathParam("owner_key") @Verify(Owner.class) String ownerKey, @ApiParam("Environment name filter to search for.") @QueryParam("name") String envName) {
    Owner owner = findOwnerByKey(ownerKey);
    CandlepinQuery<Environment> query = envName == null ? envCurator.listForOwner(owner) : envCurator.listForOwnerByName(owner, envName);
    return translator.translateQuery(query, EnvironmentDTO.class);
}
Also used : Owner(org.candlepin.model.Owner) Environment(org.candlepin.model.Environment) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Wrapped(org.jboss.resteasy.annotations.providers.jaxb.Wrapped) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 24 with Environment

use of org.candlepin.model.Environment 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)

Example 25 with Environment

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

the class DefaultContentAccessCertServiceAdapter method createX509Certificate.

public X509Certificate createX509Certificate(Consumer consumer, Owner owner, BigInteger serialNumber, KeyPair keyPair, Date startDate, Date endDate) throws GeneralSecurityException, IOException {
    // fake a product dto as a container for the org content
    org.candlepin.model.dto.Product container = new org.candlepin.model.dto.Product();
    org.candlepin.model.dto.Content dContent = new org.candlepin.model.dto.Content();
    List<org.candlepin.model.dto.Content> dtoContents = new ArrayList<>();
    dtoContents.add(dContent);
    Environment environment = this.environmentCurator.getConsumerEnvironment(consumer);
    dContent.setPath(getContentPrefix(owner, environment));
    container.setContent(dtoContents);
    Set<X509ExtensionWrapper> extensions = prepareV3Extensions();
    Set<X509ByteExtensionWrapper> byteExtensions = prepareV3ByteExtensions(container);
    X509Certificate x509Cert = this.pki.createX509Certificate(createDN(consumer, owner), extensions, byteExtensions, startDate, endDate, keyPair, serialNumber, null);
    return x509Cert;
}
Also used : ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) X509Certificate(java.security.cert.X509Certificate) Content(org.candlepin.model.Content) EnvironmentContent(org.candlepin.model.EnvironmentContent) Environment(org.candlepin.model.Environment) X509ByteExtensionWrapper(org.candlepin.pki.X509ByteExtensionWrapper) X509ExtensionWrapper(org.candlepin.pki.X509ExtensionWrapper)

Aggregations

Environment (org.candlepin.model.Environment)29 EnvironmentContent (org.candlepin.model.EnvironmentContent)10 Test (org.junit.Test)9 HashSet (java.util.HashSet)8 Owner (org.candlepin.model.Owner)7 ApiOperation (io.swagger.annotations.ApiOperation)6 HashMap (java.util.HashMap)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 Content (org.candlepin.model.Content)6 ApiResponses (io.swagger.annotations.ApiResponses)5 Date (java.util.Date)5 Consumer (org.candlepin.model.Consumer)5 NotFoundException (org.candlepin.common.exceptions.NotFoundException)4 ConsumerCapability (org.candlepin.model.ConsumerCapability)4 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)4 ConsumerType (org.candlepin.model.ConsumerType)4 Release (org.candlepin.model.Release)4 Matchers.anyString (org.mockito.Matchers.anyString)4 KeyPair (java.security.KeyPair)3