Search in sources :

Example 36 with TblHosts

use of com.intel.mtwilson.as.data.TblHosts in project OpenAttestation by OpenAttestation.

the class HostBO method saveHostInDatabase.

private void saveHostInDatabase(TblHosts newRecordWithTlsPolicyAndKeystore, TxtHost host, String certificate, String location, HashMap<String, ? extends IManifest> pcrMap, List<TblHostSpecificManifest> tblHostSpecificManifests, TblMle biosMleId, TblMle vmmMleId) throws CryptographyException, IOException {
    // Building objects and validating that manifests are created ahead of create of host
    // new TblHosts();
    TblHosts tblHosts = newRecordWithTlsPolicyAndKeystore;
    log.info("saveHostInDatabase with tls policy {} and keystore size {}", tblHosts.getTlsPolicyName(), tblHosts.getTlsKeystore() == null ? "null" : tblHosts.getTlsKeystore().length);
    log.error("saveHostInDatabase with tls policy {} and keystore size {}", tblHosts.getTlsPolicyName(), tblHosts.getTlsKeystore() == null ? "null" : tblHosts.getTlsKeystore().length);
    TblHostsJpaController hostController = getHostsJpaController();
    tblHosts.setAddOnConnectionInfo(host.getAddOn_Connection_String());
    tblHosts.setBiosMleId(biosMleId);
    tblHosts.setDescription(host.getDescription());
    tblHosts.setEmail(host.getEmail());
    if (host.getIPAddress() != null) {
        // datatype.IPAddress
        tblHosts.setIPAddress(host.getIPAddress().toString());
    }
    // datatype.Hostname
    tblHosts.setName(host.getHostName().toString());
    if (host.getPort() != null) {
        tblHosts.setPort(host.getPort());
    }
    tblHosts.setVmmMleId(vmmMleId);
    // null is ok
    tblHosts.setAIKCertificate(certificate);
    if (location != null) {
        tblHosts.setLocation(location);
    }
    tblHosts.setBios_mle_uuid_hex(biosMleId.getUuid_hex());
    tblHosts.setVmm_mle_uuid_hex(vmmMleId.getUuid_hex());
    tblHosts.setUuid_hex(new UUID().toString());
    // create the host
    log.debug("COMMITING NEW HOST DO DATABASE");
    hostController.create(tblHosts);
    log.debug("Save host specific manifest if any");
    createHostSpecificManifest(tblHostSpecificManifests, tblHosts);
}
Also used : TblHostsJpaController(com.intel.mtwilson.as.controller.TblHostsJpaController) TblHosts(com.intel.mtwilson.as.data.TblHosts) UUID(com.intel.mtwilson.util.io.UUID)

Example 37 with TblHosts

use of com.intel.mtwilson.as.data.TblHosts in project OpenAttestation by OpenAttestation.

the class HostBO method addHost.

public String addHost(TxtHost host) {
    String certificate = null;
    String location = null;
    String ipAddress = null;
    HashMap<String, ? extends IManifest> pcrMap = null;
    try {
        ipAddress = InetAddress.getByName(host.getHostName().toString()).getHostAddress();
        if (!ipAddress.equalsIgnoreCase(host.getIPAddress().toString())) {
            throw new ASException(ErrorCode.AS_HOST_IPADDRESS_NOT_MATCHED, host.getHostName().toString(), host.getIPAddress().toString());
        }
        checkForDuplicate(host);
        getBiosAndVMM(host);
        log.info("Getting Server Identity.");
        TblHosts tblHosts = new TblHosts();
        tblHosts.setTlsPolicyName("TRUST_FIRST_CERTIFICATE");
        tblHosts.setTlsKeystore(null);
        tblHosts.setAddOnConnectionInfo(host.getAddOn_Connection_String());
        if (host.getHostName() != null) {
            tblHosts.setName(host.getHostName().toString());
        }
        if (host.getIPAddress() != null) {
            tblHosts.setIPAddress(host.getIPAddress().toString());
        }
        if (host.getPort() != null) {
            tblHosts.setPort(host.getPort());
        } else {
            throw new ASException(ErrorCode.PORT_MISSING, host.getHostName().toString(), host.getIPAddress().toString());
        }
        if (canFetchAIKCertificateForHost(host.getVmm().getName())) {
            // datatype.Vmm
            if (!host.getAddOn_Connection_String().toLowerCase().contains("citrix")) {
                certificate = getAIKCertificateForHost(tblHosts, host);
                // we have to check that the aik certificate was signed by a trusted privacy ca
                X509Certificate hostAikCert = X509Util.decodePemCertificate(certificate);
                hostAikCert.checkValidity();
                // read privacy ca certificate
                InputStream privacyCaIn = new FileInputStream(ResourceFinder.getFile("PrivacyCA.cer"));
                // XXX TODO currently we only support one privacy CA cert...
                // in the future we should read a PEM format file with possibly multiple trusted privacy ca certs
                X509Certificate privacyCaCert = X509Util.decodeDerCertificate(IOUtils.toByteArray(privacyCaIn));
                IOUtils.closeQuietly(privacyCaIn);
                privacyCaCert.checkValidity();
                // verify the trusted privacy ca signed this aik cert
                hostAikCert.verify(privacyCaCert.getPublicKey());
            // NoSuchAlgorithmException,InvalidKeyException,NoSuchProviderException,SignatureException
            }
        } else {
            // ESX host so get the location for the host and store in the table
            pcrMap = getHostPcrManifest(tblHosts, host);
            // BUG #497 sending both the new TblHosts record and the TxtHost object just to get the TlsPolicy into
            // the initial call so that with the trust_first_certificate policy we will obtain the host certificate now while adding it
            log.info("Getting location for host from VCenter");
            location = getLocation(pcrMap);
        }
        HostAgentFactory factory = new HostAgentFactory();
        HostAgent agent = factory.getHostAgent(tblHosts);
        log.info("Saving Host in database with TlsPolicyName {} and TlsKeystoreLength {}", tblHosts.getTlsPolicyName(), tblHosts.getTlsKeystore() == null ? "null" : tblHosts.getTlsKeystore().length);
        Map<String, String> attributes = agent.getHostAttributes();
        String hostUuidAttr = attributes.get("Host_UUID");
        //if ((attributes != null) && (!attributes.isEmpty()) && (hostUuidAttr != null))
        if (!attributes.isEmpty() && hostUuidAttr != null)
            tblHosts.setHardwareUuid(hostUuidAttr.toLowerCase().trim());
        //                        
        log.debug("Saving the host details in the DB");
        // retrieve the complete manifest and get module info inserted into database
        // We only handle module info for PCR 19
        HashMap<String, ? extends IManifest> pcrs = getHostPcrManifest(tblHosts, host);
        List<TblHostSpecificManifest> tblHostSpecificManifests = null;
        if (vmmMleId.getRequiredManifestList().contains(MODULE_PCR)) {
            if (pcrs != null) {
                PcrManifest pcr19 = (PcrManifest) pcrs.get(MODULE_PCR);
                addModuleWhiteList(pcr19, tblHosts, host, hostUuidAttr);
                log.info("Host specific modules would be retrieved from the host that extends into PCR 19.");
                String hostType = host.getVendor();
                tblHostSpecificManifests = createHostSpecificManifestRecords(vmmMleId, pcrs, hostType);
            }
        } else {
            log.info("Host specific modules will not be configured since PCR 19 is not selected for attestation");
        }
        //saveHostInDatabase(tblHosts, host, certificate, location, pcrMap);
        biosMleId = findBiosMleForHost(host);
        vmmMleId = findVmmMleForHost(host);
        saveHostInDatabase(tblHosts, host, certificate, location, pcrMap, tblHostSpecificManifests, biosMleId, vmmMleId);
        // Now that the host has been registered successfully, let us see if there is an asset tag certificated configured for the host
        // to which the host has to be associated
        //attributes);
        associateAssetTagCertForHost(host, agent.getHostAttributes(), tblHosts);
    } catch (ASException ase) {
        throw ase;
    } catch (CryptographyException e) {
        throw new ASException(e, ErrorCode.AS_ENCRYPTION_ERROR, e.getCause() == null ? e.getMessage() : e.getCause().getMessage());
    } catch (Exception e) {
        log.debug("beggining stack trace --------------");
        e.printStackTrace();
        log.debug("end stack trace --------------");
        throw new ASException(e);
    }
    return "true";
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) X509Certificate(java.security.cert.X509Certificate) FileInputStream(java.io.FileInputStream) ASException(com.intel.mountwilson.as.common.ASException) NoResultException(javax.persistence.NoResultException) NonexistentEntityException(com.intel.mtwilson.as.controller.exceptions.NonexistentEntityException) IllegalOrphanException(com.intel.mtwilson.as.controller.exceptions.IllegalOrphanException) CryptographyException(com.intel.mtwilson.crypto.CryptographyException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) CryptographyException(com.intel.mtwilson.crypto.CryptographyException) PcrManifest(com.intel.mountwilson.manifest.data.PcrManifest) TblHosts(com.intel.mtwilson.as.data.TblHosts) TblHostSpecificManifest(com.intel.mtwilson.as.data.TblHostSpecificManifest) HostAgent(com.intel.mtwilson.agent.HostAgent) HostAgentFactory(com.intel.mtwilson.agent.HostAgentFactory) ASException(com.intel.mountwilson.as.common.ASException)

Example 38 with TblHosts

use of com.intel.mtwilson.as.data.TblHosts in project OpenAttestation by OpenAttestation.

the class HostBO method deleteHost.

public String deleteHost(Hostname hostName) {
    try {
        TblHosts tblHosts = getHostByName(hostName);
        if (tblHosts == null) {
            throw new ASException(ErrorCode.AS_HOST_NOT_FOUND, hostName);
        }
        log.info("Deleting Host from database");
        deleteHostAssetTagMapping(tblHosts);
        deleteHostSpecificManifest(tblHosts);
        deleteModulesForMLE(createTxtHostFromDatabaseRecord(tblHosts));
        deleteTALogs(tblHosts.getId());
        deleteSAMLAssertions(tblHosts);
        getHostsJpaController().destroy(tblHosts.getId());
        unmapAssetTagCertFromHost(tblHosts.getId(), tblHosts.getName());
    } catch (ASException ase) {
        throw ase;
    } catch (CryptographyException e) {
        throw new ASException(ErrorCode.SYSTEM_ERROR, e.getCause() == null ? e.getMessage() : e.getCause().getMessage(), e);
    } catch (Exception e) {
        throw new ASException(e);
    }
    // return new HostResponse(ErrorCode.OK);
    return "true";
}
Also used : CryptographyException(com.intel.mtwilson.crypto.CryptographyException) TblHosts(com.intel.mtwilson.as.data.TblHosts) ASException(com.intel.mountwilson.as.common.ASException) ASException(com.intel.mountwilson.as.common.ASException) NoResultException(javax.persistence.NoResultException) NonexistentEntityException(com.intel.mtwilson.as.controller.exceptions.NonexistentEntityException) IllegalOrphanException(com.intel.mtwilson.as.controller.exceptions.IllegalOrphanException) CryptographyException(com.intel.mtwilson.crypto.CryptographyException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 39 with TblHosts

use of com.intel.mtwilson.as.data.TblHosts in project OpenAttestation by OpenAttestation.

the class HostTrustBO method getHostLocation.

// PREMIUM FEATURE ? 
/**
     * Gets the location of the host from db table tblHosts
     *
     * @param hostName
     * @return {@link HostLocation}
     */
public HostLocation getHostLocation(Hostname hostName) {
    try {
        TblHosts tblHosts = getHostByName(hostName);
        if (tblHosts == null) {
            throw new ASException(ErrorCode.AS_HOST_NOT_FOUND, String.format("%s", hostName));
        }
        HostLocation location = new HostLocation(tblHosts.getLocation());
        return location;
    } catch (ASException e) {
        throw e;
    } catch (Exception e) {
        throw new ASException(e);
    }
}
Also used : TblHosts(com.intel.mtwilson.as.data.TblHosts) ASException(com.intel.mountwilson.as.common.ASException) ASException(com.intel.mountwilson.as.common.ASException) WebApplicationException(javax.ws.rs.WebApplicationException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) CryptographyException(com.intel.mtwilson.crypto.CryptographyException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 40 with TblHosts

use of com.intel.mtwilson.as.data.TblHosts in project OpenAttestation by OpenAttestation.

the class HostTrustBOTest method mockGetHostByIpAddress.

public TblHosts mockGetHostByIpAddress() {
    TblHosts tblHosts = new TblHosts();
    tblHosts.setId(1);
    tblHosts.setName(SERVER_NAME);
    tblHosts.setIPAddress(SERVER_NAME);
    String biosName = "DELL";
    String biosVersion = "A08";
    String oemName = "DELL";
    TblMle biosMle = new TblMle();
    biosMle.setId(1);
    biosMle.setName(biosName);
    biosMle.setVersion(biosVersion);
    TblOem oem = new TblOem();
    oem.setId(1);
    oem.setName(oemName);
    biosMle.setOemId(oem);
    Collection<TblPcrManifest> tblPcrManifestCollection = new ArrayList<TblPcrManifest>();
    TblPcrManifest tblPcrManifest = new TblPcrManifest();
    tblPcrManifest.setId(1);
    tblPcrManifest.setName("0");
    tblPcrManifest.setValue("31B97D97B4679917EC3C1D943635693FFBAB4143");
    tblPcrManifestCollection.add(tblPcrManifest);
    biosMle.setTblPcrManifestCollection(tblPcrManifestCollection);
    tblHosts.setBiosMleId(biosMle);
    String vmmName = "XEN";
    String vmmVersion = "4.3";
    String osName = "Fedora";
    String osVersion = "20";
    TblMle vmmMle = new TblMle();
    vmmMle.setId(1);
    vmmMle.setName(vmmName);
    vmmMle.setVersion(vmmVersion);
    TblOs os = new TblOs();
    os.setId(1);
    os.setName(osName);
    os.setVersion(osVersion);
    vmmMle.setOsId(os);
    Collection<TblPcrManifest> tblPcrManifestCollectionVMM = new ArrayList<TblPcrManifest>();
    TblPcrManifest tblPcrManifestVMM = new TblPcrManifest();
    tblPcrManifestVMM.setId(2);
    tblPcrManifestVMM.setName("18");
    tblPcrManifestVMM.setValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
    tblPcrManifestCollection.add(tblPcrManifestVMM);
    vmmMle.setTblPcrManifestCollection(tblPcrManifestCollectionVMM);
    tblHosts.setVmmMleId(vmmMle);
    return tblHosts;
}
Also used : TblHosts(com.intel.mtwilson.as.data.TblHosts) TblMle(com.intel.mtwilson.as.data.TblMle) TblOem(com.intel.mtwilson.as.data.TblOem) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) TblPcrManifest(com.intel.mtwilson.as.data.TblPcrManifest) TblOs(com.intel.mtwilson.as.data.TblOs)

Aggregations

TblHosts (com.intel.mtwilson.as.data.TblHosts)42 ASException (com.intel.mountwilson.as.common.ASException)17 CryptographyException (com.intel.mtwilson.crypto.CryptographyException)15 EntityManager (javax.persistence.EntityManager)14 NonexistentEntityException (com.intel.mtwilson.as.controller.exceptions.NonexistentEntityException)13 IOException (java.io.IOException)12 UnknownHostException (java.net.UnknownHostException)11 IllegalOrphanException (com.intel.mtwilson.as.controller.exceptions.IllegalOrphanException)10 ArrayList (java.util.ArrayList)10 TblMle (com.intel.mtwilson.as.data.TblMle)9 TblPcrManifest (com.intel.mtwilson.as.data.TblPcrManifest)9 NoResultException (javax.persistence.NoResultException)8 TblHostsJpaController (com.intel.mtwilson.as.controller.TblHostsJpaController)7 TblTaLog (com.intel.mtwilson.as.data.TblTaLog)6 EntityNotFoundException (javax.persistence.EntityNotFoundException)6 Query (javax.persistence.Query)6 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)6 Hostname (com.intel.mtwilson.util.net.Hostname)5 Matchers.anyString (org.mockito.Matchers.anyString)5 ASDataException (com.intel.mtwilson.as.controller.exceptions.ASDataException)4