Search in sources :

Example 1 with TblHostsJpaController

use of com.intel.mtwilson.as.controller.TblHostsJpaController in project OpenAttestation by OpenAttestation.

the class HostBO method checkForDuplicate.

private void checkForDuplicate(TxtHost host) throws CryptographyException {
    TblHostsJpaController tblHostsJpaController = getHostsJpaController();
    TblHosts tblHosts1 = tblHostsJpaController.findByName(host.getHostName().toString());
    TblHosts tblHosts2 = tblHostsJpaController.findByIPAddress(host.getIPAddress().toString());
    if (tblHosts1 != null) {
        throw new ASException(ErrorCode.AS_HOST_EXISTS, host.getHostName());
    }
    if (tblHosts2 != null) {
        throw new ASException(ErrorCode.AS_IPADDRESS_EXISTS, host.getIPAddress().toString());
    }
}
Also used : TblHostsJpaController(com.intel.mtwilson.as.controller.TblHostsJpaController) TblHosts(com.intel.mtwilson.as.data.TblHosts) ASException(com.intel.mountwilson.as.common.ASException)

Example 2 with TblHostsJpaController

use of com.intel.mtwilson.as.controller.TblHostsJpaController in project OpenAttestation by OpenAttestation.

the class HostBO method getHostByName.

/**
	 * This is not a REST API method, it is public because it is used by
	 * HostTrustBO.
	 * 
	 * @param hostName
	 * @return
	 * @throws CryptographyException
	 */
public TblHosts getHostByName(Hostname hostName) throws CryptographyException {
    // datatype.Hostname
    TblHosts tblHosts = new TblHosts();
    try {
        InetAddress addr = InetAddress.getByName(hostName.toString());
        String hostname = addr.getHostName();
        String ip = addr.getHostAddress();
        tblHosts = new TblHostsJpaController(getEntityManagerFactory()).findByName(hostname);
        tblHosts = tblHosts != null ? tblHosts : new TblHostsJpaController(getEntityManagerFactory()).findByName(ip);
    } catch (UnknownHostException e) {
        log.error("Unknown host", e);
    }
    return tblHosts;
}
Also used : TblHostsJpaController(com.intel.mtwilson.as.controller.TblHostsJpaController) UnknownHostException(java.net.UnknownHostException) TblHosts(com.intel.mtwilson.as.data.TblHosts) InetAddress(java.net.InetAddress)

Example 3 with TblHostsJpaController

use of com.intel.mtwilson.as.controller.TblHostsJpaController in project OpenAttestation by OpenAttestation.

the class AssetTagCertBO method mapAssetTagCertToHost.

/**
     * This function would be used to associate a asset tag certificate with the host for which it is 
     * provisioned for.  It does not require you know the ID of the host you are associating to.  
     * Here you are giving the hash of the cert to the code and letting it find a matching host
     * @param atagObj
     * @return true if host was found, false if not
     */
public boolean mapAssetTagCertToHost(AssetTagCertAssociateRequest atagObj) throws CryptographyException {
    boolean result = false;
    log.debug("mapAssetTagCertToHost");
    AssetTagCertAssociateRequest request = new AssetTagCertAssociateRequest();
    if (atagObj.getSha1OfAssetCert() != null) {
        log.debug("trying to associate tag to existing host using " + Hex.encodeHexString(atagObj.getSha1OfAssetCert()));
        //List<MwAssetTagCertificate> atagCerts = My.jpa().mwAssetTagCertificate().findAssetTagCertificateBySha1Hash(atagObj.getSha1OfAssetCert());
        MwAssetTagCertificateJpaController mwAssetTagCertificateJpaController = new MwAssetTagCertificateJpaController(getEntityManagerFactory());
        List<MwAssetTagCertificate> atagCerts = mwAssetTagCertificateJpaController.findAssetTagCertificateBySha1Hash(atagObj.getSha1OfAssetCert());
        // List<MwAssetTagCertificate> atagCerts = My.jpa().mwAssetTagCertificate().findAssetTagCertificatesByHostUUID("494cb5dc-a3e1-4e46-9b52-e694349b1654");
        if (atagCerts.isEmpty()) {
            log.error("mapAssetTagCertToHost: The asset tag certificate does not exist");
            throw new ASException(ErrorCode.AS_INVALID_ASSET_TAG_CERTIFICATE_HASH);
        } else if (atagCerts.size() > 1) {
            log.error("mapAssetTagCertToHost: There were multiple matches for the specified hash");
            throw new ASException(ErrorCode.AS_INVALID_ASSET_TAG_CERTIFICATE_HASH);
        } else {
            MwAssetTagCertificate atagCert = atagCerts.get(0);
            request.setSha1OfAssetCert(atagCert.getSHA1Hash());
            String uuid = atagCert.getUuid().toLowerCase().trim();
            log.debug("searching using " + uuid);
            //TblHosts tblHost = My.jpa().mwHosts().findByHwUUID(uuid);
            TblHostsJpaController tblHostsJpaController = new TblHostsJpaController(getEntityManagerFactory());
            TblHosts tblHost = tblHostsJpaController.findByHwUUID(uuid);
            if (tblHost != null) {
                log.debug("found host matching uuid of cert, going to assoicate with host id = " + tblHost.getId());
                request.setHostID(tblHost.getId());
                //atagObj.setHostID(tblHost.getId());
                result = mapAssetTagCertToHostById(request);
            } else {
                log.debug("found no matching uuid of cert");
                result = false;
            }
        }
    }
    return result;
}
Also used : TblHostsJpaController(com.intel.mtwilson.as.controller.TblHostsJpaController) TblHosts(com.intel.mtwilson.as.data.TblHosts) MwAssetTagCertificateJpaController(com.intel.mtwilson.as.controller.MwAssetTagCertificateJpaController) MwAssetTagCertificate(com.intel.mtwilson.as.data.MwAssetTagCertificate) ASException(com.intel.mountwilson.as.common.ASException) AssetTagCertAssociateRequest(com.intel.mtwilson.datatypes.AssetTagCertAssociateRequest)

Example 4 with TblHostsJpaController

use of com.intel.mtwilson.as.controller.TblHostsJpaController in project OpenAttestation by OpenAttestation.

the class HostBO method queryForHosts.

/**
         * Author: Sudhir
         *
         * Searches for the hosts using the criteria specified.
         *
         * @param searchCriteria: If in case the user has not provided any
         * search criteria, then all the hosts would be returned back to the
         * caller
         * @param includeHardwareUuid: if this is set to true, it causes the resulting 
         * TxtHostRecord to include the hardware_uuid field from the tblHost
         * @return
         */
public List<TxtHostRecord> queryForHosts(String searchCriteria, boolean includeHardwareUuid) {
    log.debug("queryForHost " + searchCriteria + " includeHardwareUuid[" + includeHardwareUuid + "]");
    try {
        //TblHostsJpaController tblHostsJpaController = My.jpa().mwHosts(); //new TblHostsJpaController(getEntityManagerFactory());
        TblHostsJpaController tblHostsJpaController = new TblHostsJpaController(getEntityManagerFactory());
        List<TxtHostRecord> txtHostList = new ArrayList<TxtHostRecord>();
        List<TblHosts> tblHostList;
        if (searchCriteria != null && !searchCriteria.isEmpty()) {
            tblHostList = tblHostsJpaController.findHostsByNameSearchCriteria(searchCriteria);
        } else {
            tblHostList = tblHostsJpaController.findTblHostsEntities();
        }
        if (tblHostList != null) {
            log.debug(String.format("Found [%d] host results for search criteria [%s]", tblHostList.size(), searchCriteria));
            for (TblHosts tblHosts : tblHostList) {
                TxtHostRecord hostObj = createTxtHostFromDatabaseRecord(tblHosts, includeHardwareUuid);
                txtHostList.add(hostObj);
            }
        } else {
            log.debug(String.format("Found no hosts for search criteria [%s]", searchCriteria));
        }
        return txtHostList;
    } catch (ASException e) {
        throw e;
    } catch (Exception e) {
        // throw new ASException(e);
        // Bug: 1038 - prevent leaks in error messages to client
        log.error("Error during querying for registered hosts.", e);
        throw new ASException(ErrorCode.AS_QUERY_HOST_ERROR, e.getClass().getSimpleName());
    }
}
Also used : TblHostsJpaController(com.intel.mtwilson.as.controller.TblHostsJpaController) TblHosts(com.intel.mtwilson.as.data.TblHosts) ArrayList(java.util.ArrayList) 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 5 with TblHostsJpaController

use of com.intel.mtwilson.as.controller.TblHostsJpaController in project OpenAttestation by OpenAttestation.

the class HostBO method isHostRegistered.

public HostResponse isHostRegistered(String hostnameOrAddress) {
    try {
        TblHostsJpaController tblHostsJpaController = new TblHostsJpaController(getEntityManagerFactory());
        TblHosts tblHosts = tblHostsJpaController.findByName(hostnameOrAddress);
        if (tblHosts != null) {
            // host name exists in database
            return new HostResponse(ErrorCode.OK);
        }
        tblHosts = tblHostsJpaController.findByIPAddress(hostnameOrAddress);
        if (tblHosts != null) {
            // host IP address exists in database
            return new HostResponse(ErrorCode.OK);
        }
        return new HostResponse(ErrorCode.AS_HOST_NOT_FOUND);
    } catch (ASException e) {
        throw e;
    } catch (Exception e) {
        throw new ASException(e);
    }
}
Also used : TblHostsJpaController(com.intel.mtwilson.as.controller.TblHostsJpaController) 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)

Aggregations

TblHostsJpaController (com.intel.mtwilson.as.controller.TblHostsJpaController)7 TblHosts (com.intel.mtwilson.as.data.TblHosts)7 ASException (com.intel.mountwilson.as.common.ASException)5 UnknownHostException (java.net.UnknownHostException)4 IllegalOrphanException (com.intel.mtwilson.as.controller.exceptions.IllegalOrphanException)3 NonexistentEntityException (com.intel.mtwilson.as.controller.exceptions.NonexistentEntityException)3 CryptographyException (com.intel.mtwilson.crypto.CryptographyException)3 IOException (java.io.IOException)3 NoResultException (javax.persistence.NoResultException)3 ArrayList (java.util.ArrayList)2 MwAssetTagCertificateJpaController (com.intel.mtwilson.as.controller.MwAssetTagCertificateJpaController)1 MwAssetTagCertificate (com.intel.mtwilson.as.data.MwAssetTagCertificate)1 AssetTagCertAssociateRequest (com.intel.mtwilson.datatypes.AssetTagCertAssociateRequest)1 UUID (com.intel.mtwilson.util.io.UUID)1 InetAddress (java.net.InetAddress)1