Friday, August 24, 2018

Hibernate Search Not Indexing after change one field

Hi I am trying to solve Hibernate Search to index a column changed by classic Sql query as follows:

 @Override
    public boolean updateColumn(K entityId, String columnName, String columnValue) {
        String entityName = daoType.getSimpleName();
        ClassMetadata employeeMeta = currentSession().getSessionFactory().getClassMetadata(daoType);
        String primaryKey = employeeMeta.getIdentifierPropertyName();
        String queryString = "update " + entityName + " set " + columnName + "='" + columnValue + "' where " + primaryKey + "=" + entityId;
        org.hibernate.Query query = currentSession().createQuery(queryString);

        boolean result = query.executeUpdate() > 0;
        return result;
    }

Calling above method as follows:

belgeSatirService.updateColumn(1, "basvuruNo", "thgm");

After updating "basvuruNo" column, Hibernate does not automatically update basvuruNo column. The definition of this column is below:

@Field(store = Store.YES)
@Column(name = "BasvuruNo", length = 30)//13/95088973/0735/000001
@Analyzer(definition = "whitespaceanalyzer")
private String basvuruNo;

Solved

Hibernate Search doesn't support intercepting update changes applied using a query.

I would recommend to rewrite the DAO pattern to use Hibernate friendly patterns: by using actual objects and setters on your domain model.

You'll have several other benefits from it, such as:

  • make it possible to enable 2nd level caching.
  • benefit from dirty checking: avoid unnecessary database connections and operations.
  • high performance state processing, should you want to enable bytecode instrumentation or similar.
  • Hibernate Envers, Hibernate Search, and other tools properly integrated.
  • you'll also be saving loads of memory at runtime: much better performance.

A method like that would look like something like this:

public void updateBasvuruNo(K entityId, Class type, String newBasvuruNo) {
    E yourEntity = currentSession().load( type, entityId );
    yourEntity.setBasvuruNo( newBasvuruNo );
}

State on managed entities is flushed to the database as needed, only if needed, and controlled by your application: normally by the transaction scope. So you'll probably not want to use such an helper at all, it's just unnecessary boilerplate.


Monday, August 20, 2018

Tensorflow: Multi-GPU single input queue

In tensorflow's cifar10 multi-GPU example, it seems (correct me if I am wrong) that one queue of training images is created per GPU. Wouldn't the "right" way of doing things be to have a single queue feeding all of the towers? If so, is there an example available of a shared queue?

Solved

You're correct that the code for the CIFAR-10 model uses multiple input queues (through multiple calls to cifar10.distorted_inputs() via cifar10.tower_loss()).

The easiest way to use a shared queue between the GPUs would be to do the following:

  1. Increase the batch size by a factor of N, where N is the number of GPUs.

  2. Move the call to cifar10.distorted_inputs() out of cifar10.tower_loss() and outside the loop over GPUs.

  3. Split the images and labels tensors that are returned from cifar10.distorted_inputs() along the 0th (batch) dimension:

    images, labels = cifar10.distorted_inputs()
    split_images = tf.split(0, FLAGS.num_gpus, images)
    split_labels = tf.split(0, FLAGS.num_gpus, labels)
    
  4. Modify cifar10.tower_loss() to take images and labels arguments, and invoke it as follows:

    for i in xrange(FLAGS.num_gpus):
      with tf.device('/gpu:%d' % i):
        with tf.name_scope('%s_%d' % (cifar10.TOWER_NAME, i)) as scope:
    
          loss = tower_loss(scope, split_images[i], split_labels[i])
    

Sunday, August 19, 2018

Get complete certificate chain including the root certificate

How do I get complete certificate chain for a server? Though some claim one should be able to do just that with openssl s_client -showcerts, it turns not always to be the case.

echo | openssl s_client -CApath /etc/ssl/certs -connect www.ssllabs.com:443 \
                        -showcerts | grep -B2 BEGIN
depth=3 C = SE, O = AddTrust AB, OU = AddTrust External TTP Network, CN = AddTrust External CA Root
verify return:1
depth=2 C = GB, ST = Greater Manchester, L = Salford, O = COMODO CA Limited, CN = COMODO RSA Certification Authority
verify return:1
depth=1 C = GB, ST = Greater Manchester, L = Salford, O = COMODO CA Limited, CN = COMODO RSA Domain Validation Secure Server CA
verify return:1
depth=0 OU = Domain Control Validated, OU = PositiveSSL, CN = www.ssllabs.com
verify return:1
 0 s:/OU=Domain Control Validated/OU=PositiveSSL/CN=www.ssllabs.com
   i:/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Domain Validation Secure Server CA
-----BEGIN CERTIFICATE-----
--
 1 s:/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Domain Validation Secure Server CA
   i:/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Certification Authority
-----BEGIN CERTIFICATE-----
--
 2 s:/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Certification Authority
   i:/C=SE/O=AddTrust AB/OU=AddTrust External TTP Network/CN=AddTrust External CA Root
-----BEGIN CERTIFICATE-----
DONE

Here we have three certificates our of four. All except of the AddTrust External CA Root certificate. (Possibly because it is not included into the certificate bundle. And not like this is required. And yes, I can find the missing one at /etc/ssl/certs)

How do I get all certificates for a server in a fully automatic fashion?

Solved

You get the chain including the builtin trusted root certificate inside the verify_callback (see SSL_CTX_set_verify. With a small Perl program you can dump the chain like this:

#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket::SSL;

IO::Socket::SSL->new(
    PeerHost => 'www.google.com:443',
    SSL_verify_callback => sub {
        my $cert = $_[4];
        my $subject = Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_subject_name($cert));
        my $issuer  = Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_issuer_name($cert));
        print "# $subject (issuer=$issuer)\n";
        print Net::SSLeay::PEM_get_string_X509($cert),"\n";
        return 1;
    }
) or die $SSL_ERROR||$!;

Meta: I tried to answer this in superuser but you deleted it. Fortunately when I found this copy most of my work was still sitting in a scratch notepad I hadn't closed, otherwise I wouldn't have been willing to do the research work twice.

s_client -showcerts shows the certs sent by the server; according to the RFCs, this should be a valid chain in upward order except that the root MAY (in RFC2119 definition i.e. allowed but not particularly recommended) be omitted. However, not all servers are configured correctly, and some may send extra, missing, and/or out-of-order certs. Also depending on the CA used there may be more than one valid chain but the server can only send one. openssl currently will use only the chain sent, but this will change soon in 1.0.2, and other reliers already sometimes find a different chain than the one sent.

openssl: if the received chain is complete up to and maybe including a root which is in the truststore used (whose default location depends on system or build, and in any case can always be overridden) then openssl client will validate it as okay -- unless it is expired, or revoked and that info is available which usually it isn't. In that case you can write a client program that connects after setting a cert-verify callback function that outputs the full certs as processed by the validation loop, or other info from them you want, as opposed to s_client which uses a callback that logs (only) the subject name in the depth=n lines, which you can see in your example includes all 4 certs in the chain here. openssl is opensource, so a client program that does things mostly like s_client could be a modified copy of s_client (in this case specifically s_cb.c).

Java can also do this and is a good bit shorter to write, but requires Java be installed. If the received chain validates against an anchor in the truststore used (which defaults to a set of public roots but can be modified or overridden, and can have non-root anchors) you similarly can write a program (maybe 20 lines) to connect using a HandshakeCompletedListener which dumps the info from event.getPeerCertificates(). However if the chain doesn't validate, Java aborts the handshake with an exception and you get no certs at all, unlike the openssl case where you might get partial information before the error occurs -- plus openssl's checking, at least by default, isn't quite as strict anyway.

UPDATE: for completeness, in Java 7+, commandline keytool -printcert -sslserver displays the chain sent, in a rather cluttered format.

Among the browsers I can easily check, Firefox and Chrome on Windows (at least) can write out the chain they found and validated. ISTR but can't easily retest the Firefox error/exception dialog can also do this for a chain that fails to validate and may be incomplete. These are not automatic as-is, but I've seen ads for numerous "simulate GUI user input" tools that apparently could drive them as desired.


Saturday, August 18, 2018

Aligning div inside anchor

I want the

@Resource.AccordionStatus div to be aligned with the '@item.Title'. The div is currently getting centered vertically.

//Accordion-----------------------------------------------
$(document).ready(function() {
  $(".accordion-desc").fadeOut(0);
  $(".accordion").click(function() {
    $(".accordion-desc").not($(this).next()).slideUp('fast');
    $(this).next().slideToggle(400);
  });
});

$(".accordion").click(function() {
  $(".accordion").not(this).find(".rotate").removeClass("down");
  $(this).find(".rotate").toggleClass("down");
});
//-----------------------------------------------------------
body {
  background-color: #eee;
  font-family: "Open Sans", sans-serif;
}

header {
  background-color: #2cc185;
  color: #fff;
  padding: 2em 1em;
  margin-bottom: 1.5em;
}

h1 {
  font-weight: 300;
  text-align: center;
}

.container {
  position: relative;
  margin: 0 auto;
}

button {
  background-color: #2cc185;
  color: #fff;
  border: 0;
  padding: 1em 1.5em;
}

button:hover {
  background-color: #239768;
  color: #fff;
}

button:focus {
  background-color: #239768;
  color: #fff;
}

.accordion {
  position: relative;
  background-color: #fff;
  display: inline-block;
  width: 100%;
  border-top: 1px solid #f1f4f3;
  border-bottom: 1px solid #f1f4f3;
  font-weight: 700;
  color: #74777b;
  vertical-align: middle;
}


/*Rotation-------------------------------------*/

.accordion .fa {
  position: relative;
  float: right;
}

.rotate {
  -moz-transition: all 0.1s linear;
  -webkit-transition: all 0.1s linear;
  transition: all 0.1s linear;
}

.rotate.down {
  -moz-transform: rotate(90deg);
  -webkit-transform: rotate(90deg);
  transform: rotate(90deg);
}


/*------------------------------------------*/

.link {
  text-align: right;
  margin-bottom: 20px;
  margin-right: 30px;
}

.accordion h4 {
  position: relative;
  top: 0.8em;
  margin: 0;
  font-size: 14px;
  font-weight: 700;
}

.accordion a {
  position: relative;
  display: block;
  color: #74777b;
  padding: 1em 1em 2.5em 1em;
  text-decoration: none;
}

.accordion a:hover {
  text-decoration: none;
  color: #2cc185;
  background-color: #e7ecea;
  transition: 0.3s;
}

.accordion-desc {
  background-color: #f1f4f3;
  color: #74777b;
  z-index: 2;
  padding: 20px 15px;
}

@media (min-width:480px) {
  .container {
    max-width: 80%;
  }
}

@media (min-width:768px) {
  .container {
    max-width: 1000px;
  }
}

.accordion-desc p {
  word-break: break-all;
}

.status {
  position: relative;
  float: right;
  right: 20%;
  vertical-align: middle;
}

.btn {
  margin-top: 10px;
}

@item.Title

@Resource.AccordionProjectLead

Kay Wiberg

@Resource.AccordionDescription

@item.Description

Solved

Please update the following class.

.accordion h4 {
    position: relative;
    margin: 0;
    font-size: 14px;
    font-weight: 700;
    float: left;
}

Changes made:

Removed top: 0.8em; and just added float:left.

The issue is h4 tag occupied full width and div tag is logically set in new line aligned with right side.


You just need to use display:inline-block to align @item.Title to @Resource.AccordionStatus :

.accordion h4, .accordion .status {
    display:inline-block;
}

.accordion .status {
    top: 0.8em;
}

Try adding float left to your H4

.accordion h4 {
    position: relative;
    /* top: 0.8em; */
    margin: 0;
    font-size: 14px;
    font-weight: 700;
    float: left;
}

the problem is that h4 is displayed as a block element, then it push your floating right element to the bottom...


Correct with this :

   .accordion h4 {
        position: relative;
        margin: 0;
        font-size: 14px;
        font-weight: 700;
        display: inline-block;
    }