Mount An Encrypted Volume From The Command-Line

I backup my personal laptop manually using encrypted Linux volumes. The file systems therein were created using Ubuntu’s disk management UI because it was easier to do it that way than to learn how to accomplish the same with the command line. I generally do not take that road more travelled, but hey, I’m getting older and have less time for knowing everything.  As usual, however, that attitude has come back to bite me in the backside.  There I was at my desk with my shiny new MacBook Air and no more Ubuntu Linux desktop computer. How was I going to get my backup drives mounted? It’s admittedly been hard enough to get ext4 to mount on a Mac, so I wasn’t about to try getting an encrypted Linux volume to mount.  However, I do have a CentOS 7 box sitting under my desk. No X Windows running.  Now you can understand the title of this post.

The main concept is to tell the system’s device mapper how to create a pseudo-device through which applications can access the real, encrypted device in an unencrypted fashion.  So, first create the mapping:

# cryptesetup open /dev/sdX# sdXx --type luks

This assumption here is that your encrypted volume exists on /dev/sdX# , e.g. /dev/sdb1, where ‘X’ represents a drive letter and ‘#’ represents a volume number. The type of encryption used when Ubuntu created these filesystem was “luks”.  The parameter before ‘–type’ is the name of the pseudo-device.  Following the example of /dev/sdb1, I would name the pseudo-device sdbx.  This naming pattern is not required, it’s just my way of doing it.

Once the mapping is in place, you can mount it as a normal drive:

# mount /dev/mapper/sdbx /mnt

Drupal: Adding Views Filter Criteria Programmatically With A Query Alter

I am developing a Drupal site which uses the Nodequeue module and Views to create a block of “featured” (handpicked) nodes to display in a sidebar. When viewing the detail page of any given node, this sidebar of featured nodes was to appear on the right. You can imagine that when viewing the detail page of one of the featured nodes, you’d end up with that node in the main content and also see it within the sidebar list as a featured item. My client wanted to make sure the current node never appeared within in the sidebar. At first I was going to tackle this at the theme layer. But my conscience got the best of me and I decided to figure out the proper way to do it.

Unfortunately, there is no such filter that meets those requirements, that I know, within Views or any contributed submodules. After googling and encountering various hacks on this topic, I found the cleanest way create a Views criterion was by using hook_views_query_alter(). This hook allows you to modify the database query through Views data structures (as opposed to modifying raw SQL) before the query is executed. Let’s look at the code.

function mymodule_views_query_alter(&$view, &$query) {
    if ($view->name == 'featured_resources_sidebar') {
        $main_content_node = _get_node_by_path($_GET['q']);
        $query->where[1]['conditions'][0] = array(
            'field' => 'node.nid',
            'value' => $main_content_node->nid,
            'operator' => '!='
        );
    }
}

The if() is to avoid wasting cycles by making sure I only run this code when the hook encounters the Views display in question, my right hand sidebar. I find it’s best to give your displays a thoughtful machine name instead of the default, such as  “block” or “page”.

Next I run a custom function, _get_node_by_path($path), which I wrote to take a given path and return the object of the node found at that path by Drupal. In our case, I’m using the path of the currently viewed page, the detail page for a node. If there are better ways of finding the node of the current browsing context, please let me know. I’m not going to get into the details of that function in this post, but know that it gives me access to the node (and so the nid) of the currently displayed node within the main content pane.

Now I can get to the business of altering the query. How did I know to write this data structure with “field”, “value” and “operator”? I went to the configuration for this Views display and added a filter criterion on the node id of the query results, using some gibberish number as an example nid. At the beginning of this function I debug printed the $query parameter and inspected $query->where to see how Drupal created the data structure for query conditions. Once I knew what to do, I removed the filter using and mimicked it within the code, using my discerned current node nid as the value for my condition.

Now, when viewing details pages, I never show the current node within the featured nodes list.  Happy client.

Salesforce Custom Buttons: No Mess, No Fuss

After trying a few methods, I found the following to be the cleanest, easiest way to add an Apex-executing custom button to a Salesforce detail page.  Essentially, you’re using Force.com’s AJAX library to make a function call from a VisualForce page to an Apex method. If you’re not familiar with Javascript, you might be doing some educated copying and pasting.  Knowing some simple JSON notation will be very helpful.  It might sound cumbersome, but this pattern really is quite quick and easy.

I like to create my Apex method first. This way I’ve defined what I need from the button’s page before writing any JavaScript.

global class exampleClass {
    WebService static String exampleMethod(String id, String name, String accountId) {

        String result = '{"type":"url", "data":"/'+accountId+'"}';
        
        try {
            List contacts = [SELECT Id FROM Contact WHERE Id = :id];
            if (contacts.size() < 1) {
                result = '{"type":"msg", "data":"Unable to find contact."}';
            }
            contacts[0].Name_And_Id__c = name+' '+id;
            update contacts;
        } catch (Exception e) {
            result = '{"type":"msg", "data":"An error occured: '+e.getMessage()+'"}';
        }
        
        return result;
    }
}

This code does little more than provide an example for creating a backend for custom buttons. Given a Contact id, a Contact name and Contact’s Account id, concatenate the Contact name and id into the “Name_And_Id__c” field. Then return a response to the calling JavaScript. If all goes well, the response will be a URL to the detail page of the Contact’s parent Account. If things go wrong, one of two different messages will be returned. May you find more important things to do with Apex.

Notice the method declaration. It should come as no surprise that we declare it as a WebService. Also, all WebService methods must be declared static. Lastly, any class which contains WebService methods must be declared global.

Next, take a look at the method’s parameters. You can, of course, have far more meaningful names. Remember these parameter names as they will be supplied in your Javascript for the passing of information into the method. The parameters must be strings, but remember that you can use JSON to embed all sorts of complex information.

Finally, the method ought to return something. It might not need to, but consider the user experience. When a button is clicked there ought to be some sort of feedback. What I prefer to do is to return a self-describing JSON object. The “type” member tells the Javascript which type of information it’s getting and the “data” is some data relevant to the processing. Compare the types of messages I return in the code, and see how the response is parsed (and used) within the Javascript below.

To begin creating the button, go to the “Buttons, Links, and Actions” section for your object and do some pointing and clicking. Click on “New Button or Link”. Give a label and a name, and generally I leave “Behavior” as “Display in new window”. Change “Content source” to “OnClick JavaScript”. Here’s what the JavaScript code will look like:

{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

try {
  var response = sforce.apex.execute(
    "exampleClass",
    "exampleMethod",
    {
      id:"{!Contact.Id}",
      name:"{!Contact.Name}",
      accountId:"{!Account.Id}"
    }
  );
  response = JSON.parse(response);
  if (response.type == 'url') {
    location.href=response.data;
  } else {
    alert(response.data);
  }
} catch(e) {
  alert('An unknown error has occurred');
}

The Javascript code starts out including the Force.com AJAX toolkit libraries, with REQUIRESCRIPT directives. Moving on to the actual remote execution function, sforce.apex.execute, the first parameter is the name of the Apex class which contains the method to execute, and then the name of the method itself. The third parameter is a JSON description of the name/value pairs of the Apex method’s parameters. Notice the names of the pairs match the names of the parameters in the Apex code.

In my example, when the Apex call returns, you’re getting one of those JSON strings. I use the browser’s built in JSON parser to create a navigable object from the response. Now, based upon the response.type, I can decide what to do with the response.data information, whether to redirect the browser or pop up a message. The possibilities are endless.

How To Pass Salesforce Certification Maintenance Exams, Every Time.

The following is a set of steps I use to study for Salesforce certification maintenance exams, or in my case more specifically the developers’ maintenance exams.  When following this prescription, I’ve never failed.  I originally posted this information to the LinkedIn Salesforce.com Certification Group.  I highly recommend joining it.

The certification folks at SFDC should be including, within the certification maintenance notification emails, links to the release notes and educational videos. If you get a notice without these links contact the certification team. In my experience, they’re a very helpful bunch of people.

Also within the notification email you’ll get very specific pointers on what and how to study. I’ve never gotten a notice that didn’t somehow tell me explicitly which topics would be directly tested. I find that 90% of a maintenance exam is an exercise in following those directions. Here’s what I do:

1. Do a quick overview of the “Contents” of the release notes PDF, peeking further in on topics that are unfamiliar.  I do pay particular attention to the specific topics for my exam’s section as given in the notification e-mail, but I also do this to make myself generally aware of the all changes in this present release.

2. Watch every one of the recommended video topics for your exam type (dev, admin, etc). If time permits I watch the others, more for rounding out my knowledge rather than the exam itself.

3. As I watch the videos for my topics I type out every spoken word in a text editor, just to help commit things to memory. Also, each video has its presenter’s script under the video. It helps me keep up my typing with what’s being spoken.

4. Go to WebAssessor, login etc, and get to the point just before starting the exam. Then I open a Salesforce.com help/search page, the release notes PDF as given in the notice, and our good friend Google. I don’t think I’ve ever needed reference materials open for maintenance exams (but I wish I had it on the original certification exam! ;). Begin!

5. As each question comes I give an initially quick but thoughtful answer and print it out right after answering. Then on to the next question. I mark for review the ones that give me immediate trouble or concern.  (Although, as I mentioned, I’ve never failed since adopting this method, were I to fail an exam I would take the print outs and use them to mine the docs, release notes and reference material to find out where I went wrong. )

6. When I’ve gone through all of the questions I go back and give each question one more check, looking through release notes if have to, sometimes even scrambling to find the right spot in one of the videos. Don’t stress, there’s ample time if you’ve studied.

I’ve noticed that the majority of questions are intentionally trick questions. They are not necessarily difficult with regard to the material, but certainly are intended to snag people who haven’t done sufficient studying. My entire studying time tends to run 1 to 1.5 hours, with another half hour to take the exam.

I hope this helps.

Day 0

It was November 5, 2013.  I had to show up at 7:30am for surgery at 9:30am. Craig Wong was already there. I was really pleased. I wouldn’t have been surprised to have been the first one there, its my surgery after all. But it was as though the Lord preceded me. (Don’t let that get to your head, Craig). Soon after arriving in the registration/waiting room Dr. Gaudiani came out, and as he’s usually excited about technology stuff, he wanted to show me that 3D rendering of the CT scan of my heart and its connected bits. It was mind-blowing. The aneurysm in my proximal arch was flabbergastingly huge. It rivaled the dimensions of the heart itself.

The top of my body (from laying down) was shaved from shoulders to toes, though my legs all around. The nurse was trying to enter some information into the computer, but it was taking to long. The hospital just started their transition to a new computer system — something I would hear a hundred times over the next few days. Finally, tired of waiting, Dr. Egrie came in and just walked me to the ER.

It wasn’t frightening. Though, as I sat on the table to lay down, the enormity of what was about to happen took over a bit. I should mention that the last few weeks were a living hell, trying to choose which valve to get, and worrying about whatever else I could find to worry about. The last few days were particularly tense. Many of the valuable lessons I felt I had learned, about what’s valuable in life, over the weeks since realize I need surgery seemed to fade. Once again, I had been losing my temper a lot easier. Everything I knew told me that I would come through this surgery just fine. But just in case, I had to prepare for the possibility that I wouldn’t be around. I made videos with my kids and one for them by myself. I printed out all my passwords for my wife. And so on. It was like living with one foot in the grave. But after having been prayed for and anointed by the church’s elders the night before, the presence of God was immensely clear. So, as I sat on that table, I brought all this with me. This had to be done. I knew I had some top notch people standing over me. I knew I had, truly, thousands of people praying for me the world round. I knew that no matter what happened, and I mean no matter what, God can and will use it all for his good.

The anesthesiologist said I would feel like I had a good glass of wine. I don’t remember passing out.

Surgery went longer than expected. The valve replacement was common, but the arterial graft was very complex. After being on bypass for so long, it took some extra zaps to get my heart restarted.

I was woken up at around 7pm, still intubated of course. Initially it was scheduled for me to wake up at 10pm, but I seemed to be doing so well they moved it up. I didn’t freak out about the tubes down my throat. Prayer #1, answered.  I remember my wife holding my hand.  I began wiggling my hand around, finally someone figured out I wanted to write. On my wife’s hand, with my index finger, I wrote the  “ALIVE!”.  Apparently, later on,  there was some question as to whether I had written “ALIVE!” or “ALIVE?”.  I eventually cleared this up some days later explaining that I was quite sure that Heaven would not have included intubation.

Somehow I asked to have the tubes take out of my throat, but was told that I had to prove I could breath on my own.  This meant making my lungs and diaphragm out do the machine.  So, I began to breath as deeply and with as much calm measure as possible.  After a couple of minutes the nurse was convinced and they pulled the tubes out.  Not too bad over all, really.  But my throat hurt longer than it did after the two transesophageal echocardiograms I’ve had in the past.

I don’t remember the rest of the first evening in ICU. I do remember that my wife, my dad and my older sister were there.  At some point, after a few ice chips, I was able to talk a bit.  I fell asleep at some point.