Search in sources :

Example 1 with KvAttributeCollection

use of com.intel.mtwilson.datatypes.KvAttributeCollection in project OpenAttestation by OpenAttestation.

the class KvAttributeRepository method search.

@Override
public //    @RequiresPermissions("tag_kv_attributes:search")     
KvAttributeCollection search(KvAttributeFilterCriteria criteria) {
    log.debug("KvAttribute:Search - Got request to search for the KvAttributes.");
    KvAttributeCollection objCollection = new KvAttributeCollection();
    try (JooqContainer jc = TagJdbi.jooq()) {
        DSLContext jooq = jc.getDslContext();
        SelectQuery sql = jooq.select().from(MW_TAG_KVATTRIBUTE).getQuery();
        // all the data.
        if (criteria.filter) {
            if (criteria.id != null) {
                // when uuid is stored in database as the standard UUID string format (36 chars)
                sql.addConditions(MW_TAG_KVATTRIBUTE.ID.equalIgnoreCase(criteria.id.toString()));
            }
            if (criteria.nameEqualTo != null && criteria.nameEqualTo.length() > 0) {
                sql.addConditions(MW_TAG_KVATTRIBUTE.NAME.equalIgnoreCase(criteria.nameEqualTo));
            }
            if (criteria.nameContains != null && criteria.nameContains.length() > 0) {
                sql.addConditions(MW_TAG_KVATTRIBUTE.NAME.lower().contains(criteria.nameContains.toLowerCase()));
            }
            if (criteria.valueEqualTo != null && criteria.valueEqualTo.length() > 0) {
                sql.addConditions(MW_TAG_KVATTRIBUTE.VALUE.equalIgnoreCase(criteria.valueEqualTo));
            }
            if (criteria.valueContains != null && criteria.valueContains.length() > 0) {
                sql.addConditions(MW_TAG_KVATTRIBUTE.VALUE.lower().contains(criteria.valueContains.toLowerCase()));
            }
        }
        sql.addOrderBy(MW_TAG_KVATTRIBUTE.NAME, MW_TAG_KVATTRIBUTE.VALUE);
        log.debug("Opening tag-value dao");
        log.debug("Fetching records using JOOQ");
        Result<Record> result = sql.fetch();
        for (Record r : result) {
            KvAttribute obj = new KvAttribute();
            obj.setId(UUID.valueOf(r.getValue(MW_TAG_KVATTRIBUTE.ID)));
            //                obj.setId(r.getValue(MW_TAG_KVATTRIBUTE.ID.coerce(byte[].class), uuidConverter));
            //                obj.setId(r.getValue(MW_TAG_KVATTRIBUTE.ID, uuidConverter));
            //                obj.setId(r.getValue(MW_TAG_KVATTRIBUTE.ID));
            obj.setName(r.getValue(MW_TAG_KVATTRIBUTE.NAME));
            obj.setValue(r.getValue(MW_TAG_KVATTRIBUTE.VALUE));
            objCollection.getKvAttributes().add(obj);
        }
        sql.close();
    } catch (Exception ex) {
        log.error("KvAttribute:Search - Error during attribute search.", ex);
        throw new RepositorySearchException(ex, criteria);
    }
    log.debug("KvAttribute:Search - Returning back {} of results.", objCollection.getKvAttributes().size());
    return objCollection;
}
Also used : SelectQuery(org.jooq.SelectQuery) KvAttributeCollection(com.intel.mtwilson.datatypes.KvAttributeCollection) JooqContainer(com.intel.mtwilson.jooq.util.JooqContainer) KvAttribute(com.intel.mtwilson.datatypes.KvAttribute) DSLContext(org.jooq.DSLContext) Record(org.jooq.Record) RepositorySearchException(com.intel.mtwilson.datatypes.RepositorySearchException) RepositoryCreateException(com.intel.mtwilson.datatypes.RepositoryCreateException) RepositoryDeleteException(com.intel.mtwilson.datatypes.RepositoryDeleteException) RepositoryRetrieveException(com.intel.mtwilson.datatypes.RepositoryRetrieveException) RepositoryException(com.intel.mtwilson.datatypes.RepositoryException) RepositorySearchException(com.intel.mtwilson.datatypes.RepositorySearchException) RepositoryStoreConflictException(com.intel.mtwilson.datatypes.RepositoryStoreConflictException) RepositoryStoreException(com.intel.mtwilson.datatypes.RepositoryStoreException)

Example 2 with KvAttributeCollection

use of com.intel.mtwilson.datatypes.KvAttributeCollection in project OpenAttestation by OpenAttestation.

the class CertificateRepository method create.

@Override
public //    @RequiresPermissions("tag_certificates:create") 
void create(Certificate item) {
    log.debug("Certificate:Create - Got request to create a new Certificate {}.", item.getId().toString());
    CertificateLocator locator = new CertificateLocator();
    locator.id = item.getId();
    try (CertificateDAO dao = TagJdbi.certificateDao()) {
        Certificate newCert = dao.findById(item.getId());
        if (newCert == null) {
            newCert = Certificate.valueOf(item.getCertificate());
            dao.insert(item.getId(), newCert.getCertificate(), newCert.getSha1().toHexString(), newCert.getSha256().toHexString(), newCert.getSubject(), newCert.getIssuer(), newCert.getNotBefore(), newCert.getNotAfter());
            log.debug("Certificate:Create - Created the Certificate {} successfully.", item.getId().toString());
        } else {
            log.error("Certificate:Create - Certificate {} will not be created since a duplicate Certificate already exists.", item.getId().toString());
            throw new RepositoryCreateConflictException(locator);
        }
    } catch (RepositoryException re) {
        throw re;
    } catch (Exception ex) {
        log.error("Certificate:Create - Error during certificate creation.", ex);
        throw new RepositoryCreateException(ex, locator);
    }
    //Store tag values from Certificate
    try {
        log.info("Tags from certificate will now be stored");
        KvAttributeRepository repository = new KvAttributeRepository();
        KvAttribute kvAttrib = new KvAttribute();
        if (kvAttrib == null || repository == null)
            log.debug("kvAttrib or repository Obj is null, unable to store certificate tags");
        else {
            List<Attribute> certAttributes = X509AttributeCertificate.valueOf(item.getCertificate()).getAttribute();
            for (Attribute attr : certAttributes) {
                for (ASN1Encodable value : attr.getAttributeValues()) {
                    if (attr.getAttrType().toString().equals(UTF8NameValueMicroformat.OID)) {
                        UTF8NameValueMicroformat microformat = new UTF8NameValueMicroformat(DERUTF8String.getInstance(value));
                        // Check if that tag with same value already exists
                        KvAttributeFilterCriteria criteria = new KvAttributeFilterCriteria();
                        criteria.nameEqualTo = microformat.getName();
                        criteria.valueEqualTo = microformat.getValue();
                        KvAttributeCollection results = repository.search(criteria);
                        if (results.getDocuments().isEmpty()) {
                            kvAttrib.setId(new UUID());
                            kvAttrib.setName(microformat.getName());
                            kvAttrib.setValue(microformat.getValue());
                            repository.create(kvAttrib);
                        } else
                            log.debug("Tag with Name:{} & Value:{} is already stored.", microformat.getName(), microformat.getValue());
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("Certificate:Create - Error during attribute scan", e);
    }
}
Also used : CertificateLocator(com.intel.mtwilson.datatypes.CertificateLocator) CertificateDAO(com.intel.mtwilson.tag.dao.jdbi.CertificateDAO) KvAttribute(com.intel.mtwilson.datatypes.KvAttribute) Attribute(org.bouncycastle.asn1.x509.Attribute) KvAttribute(com.intel.mtwilson.datatypes.KvAttribute) RepositoryException(com.intel.mtwilson.tag.repository.RepositoryException) RepositoryCreateConflictException(com.intel.mtwilson.tag.repository.RepositoryCreateConflictException) RepositoryCreateException(com.intel.mtwilson.tag.repository.RepositoryCreateException) RepositoryDeleteException(com.intel.mtwilson.tag.repository.RepositoryDeleteException) RepositoryStoreException(com.intel.mtwilson.tag.repository.RepositoryStoreException) RepositoryStoreConflictException(com.intel.mtwilson.tag.repository.RepositoryStoreConflictException) RepositoryRetrieveException(com.intel.mtwilson.tag.repository.RepositoryRetrieveException) RepositoryException(com.intel.mtwilson.tag.repository.RepositoryException) RepositorySearchException(com.intel.mtwilson.tag.repository.RepositorySearchException) RepositoryCreateConflictException(com.intel.mtwilson.tag.repository.RepositoryCreateConflictException) KvAttributeCollection(com.intel.mtwilson.datatypes.KvAttributeCollection) KvAttributeRepository(com.intel.mtwilson.tag.repository.KvAttributeRepository) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) KvAttributeFilterCriteria(com.intel.mtwilson.datatypes.KvAttributeFilterCriteria) UUID(com.intel.mtwilson.util.io.UUID) UTF8NameValueMicroformat(com.intel.mtwilson.datatypes.UTF8NameValueMicroformat) Certificate(com.intel.mtwilson.datatypes.Certificate) X509AttributeCertificate(com.intel.mtwilson.datatypes.X509AttributeCertificate) RepositoryCreateException(com.intel.mtwilson.tag.repository.RepositoryCreateException)

Example 3 with KvAttributeCollection

use of com.intel.mtwilson.datatypes.KvAttributeCollection in project OpenAttestation by OpenAttestation.

the class KvAttributeRepository method delete.

@Override
public //    @RequiresPermissions("tag_kv_attributes:delete,search")     
void delete(KvAttributeFilterCriteria criteria) {
    log.debug("KvAttribute:Delete - Got request to delete KvAttribute by search criteria.");
    KvAttributeCollection objCollection = search(criteria);
    try {
        for (KvAttribute obj : objCollection.getKvAttributes()) {
            KvAttributeLocator locator = new KvAttributeLocator();
            locator.id = obj.getId();
            delete(locator);
        }
    } catch (RepositoryException re) {
        throw re;
    } catch (Exception ex) {
        log.error("KvAttribute:Delete - Error during KvAttribute deletion.", ex);
        throw new RepositoryDeleteException(ex);
    }
}
Also used : RepositoryDeleteException(com.intel.mtwilson.datatypes.RepositoryDeleteException) KvAttributeCollection(com.intel.mtwilson.datatypes.KvAttributeCollection) KvAttribute(com.intel.mtwilson.datatypes.KvAttribute) RepositoryException(com.intel.mtwilson.datatypes.RepositoryException) KvAttributeLocator(com.intel.mtwilson.datatypes.KvAttributeLocator) RepositoryCreateException(com.intel.mtwilson.datatypes.RepositoryCreateException) RepositoryDeleteException(com.intel.mtwilson.datatypes.RepositoryDeleteException) RepositoryRetrieveException(com.intel.mtwilson.datatypes.RepositoryRetrieveException) RepositoryException(com.intel.mtwilson.datatypes.RepositoryException) RepositorySearchException(com.intel.mtwilson.datatypes.RepositorySearchException) RepositoryStoreConflictException(com.intel.mtwilson.datatypes.RepositoryStoreConflictException) RepositoryStoreException(com.intel.mtwilson.datatypes.RepositoryStoreException)

Aggregations

KvAttribute (com.intel.mtwilson.datatypes.KvAttribute)3 KvAttributeCollection (com.intel.mtwilson.datatypes.KvAttributeCollection)3 RepositoryCreateException (com.intel.mtwilson.datatypes.RepositoryCreateException)2 RepositoryDeleteException (com.intel.mtwilson.datatypes.RepositoryDeleteException)2 RepositoryException (com.intel.mtwilson.datatypes.RepositoryException)2 RepositoryRetrieveException (com.intel.mtwilson.datatypes.RepositoryRetrieveException)2 RepositorySearchException (com.intel.mtwilson.datatypes.RepositorySearchException)2 RepositoryStoreConflictException (com.intel.mtwilson.datatypes.RepositoryStoreConflictException)2 RepositoryStoreException (com.intel.mtwilson.datatypes.RepositoryStoreException)2 Certificate (com.intel.mtwilson.datatypes.Certificate)1 CertificateLocator (com.intel.mtwilson.datatypes.CertificateLocator)1 KvAttributeFilterCriteria (com.intel.mtwilson.datatypes.KvAttributeFilterCriteria)1 KvAttributeLocator (com.intel.mtwilson.datatypes.KvAttributeLocator)1 UTF8NameValueMicroformat (com.intel.mtwilson.datatypes.UTF8NameValueMicroformat)1 X509AttributeCertificate (com.intel.mtwilson.datatypes.X509AttributeCertificate)1 JooqContainer (com.intel.mtwilson.jooq.util.JooqContainer)1 CertificateDAO (com.intel.mtwilson.tag.dao.jdbi.CertificateDAO)1 KvAttributeRepository (com.intel.mtwilson.tag.repository.KvAttributeRepository)1 RepositoryCreateConflictException (com.intel.mtwilson.tag.repository.RepositoryCreateConflictException)1 RepositoryCreateException (com.intel.mtwilson.tag.repository.RepositoryCreateException)1