Friday, April 27, 2012

Securing Data in iOS

There are numerous ways to secure data that you are storing on an iOS device.

The Simple/Built-in Way

The simplest way is to take advantage of the iOS Data Protection (iOS 4+). This can be accomplished by setting an attribute on a file like this:

    [[NSFileManager defaultManager] createFileAtPath:[self filePath]
                    contents:[@"super secret file contents" dataUsingEncoding:NSUTF8StringEncoding]
                    attributes:[NSDictionary dictionaryWithObject:NSFileProtectionComplete
                                                           forKey:NSFileProtectionKey]];

There are several different levels of file protection. The following was taken from the NSFileManager class reference from Apple:

  • NSFileProtectionNone - no file protection
  • NSFileProtectionComplete - file is encrypted when the device is locked or booting
  • NSFileProtectionCompleteUnlessOpen - file is encrypted and can only be opened when the device is unlocked. Once open, the file can continue to accessed even if the user locks the device.
  • NSFileProtectionCompleteUntilFirstUserAuthentication - The file is stored in an encrypted format on disk and cannot be accessed until after the device has booted. After the user unlocks the device for the first time, your application can access the file and continue to access it even if the user subsequently locks the device.

A Core Data sqlite store can also be encrypted by setting the NSFileProtectionKey file attribute to one of the above values (after you create your persistent store coordinator).

However, an important thing to realize is that this type of data protection requires the device to have a passcode set on it (http://support.apple.com/kb/HT4175).

CommonCrypto

What if you really need to insure that your data is protected regardless of whether the device has a passcode set? One way is to use the CommonCrypto libraries from Apple. This can be fairly complex. There is a great write up here from Rob Napier on what's involved. Fortunately, he has also provided a wrapper that greatly simplifies this process here. Using this library (or writing your own), you can encrypt your data and store it wherever you need. If you are using Core Data, you could write an NSValueTransformer for the Core Data entity attributes that require encryption using CommonCrypto or the RNCrypto library to encrypt/decrypt the attribute values.

SQLCipher

One more way to protect your data, specifically data that you want to store in a SQLite database, would be to use SQLCipher. SQLCipher encrypts/decrypts data at the page level and is transparent to your application code. You still use the standard SQLite APIs, with one additional method call when accessing the database (passing your key to sqlite). There are excellent instructions on setting it up for use in an iOS project here.

You can build SQLCipher as a set of static libraries by following the same iOS instructions with some modifications and additions. Here are the high level steps:

  1. Instead of creating a view based iOS project, create a static library project
  2. Follow the balance of the steps for including SQLCipher in your project
  3. Add a new "Aggregate" build target
  4. Add a "Run Script" build phase and paste the following script into it. This will build both the simulator and iOS based targets for the libcrypto, libsqlcipher, and libssl libraries.
  5. xcodebuild -project ${PROJECT_NAME}.xcodeproj -sdk iphonesimulator -target ${PROJECT_NAME} -configuration ${CONFIGURATION} clean build CONFIGURATION_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphonesimulator
    
    xcodebuild -project ${PROJECT_NAME}.xcodeproj -sdk iphoneos -target ${PROJECT_NAME} -configuration ${CONFIGURATION} clean build CONFIGURATION_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphoneos
    
  6. Add another "Run Script" build phase and paste the following script into it. This will merge the simulator and iOS builds into one for each library. These three libraries are what would be added to a project using SQLCipher.
  7. CRYPTO_LIB="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/libcrypto.a" &&
    SQLCIPHER_LIB="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/libsqlcipher.a" &&
    SSL_LIB="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/libssl.a" &&
    
    DEVICE_CRYPTO_LIB="${BUILD_DIR}/${CONFIGURATION}-iphoneos/libcrypto.a" &&
    DEVICE_SQLCIPHER_LIB="${BUILD_DIR}/${CONFIGURATION}-iphoneos/libsqlcipher.a" &&
    DEVICE_SSL_LIB="${BUILD_DIR}/${CONFIGURATION}-iphoneos/libssl.a" &&
    
    UNIVERSAL_LIBRARY_DIR="${BUILD_DIR}/${CONFIGURATION}-iphoneuniversal" &&
    
    
    # Create framework directory structure.
    rm -rf "${UNIVERSAL_LIBRARY_DIR}" &&
    mkdir -p "${UNIVERSAL_LIBRARY_DIR}" &&
    
    # Generate universal binary for the device and simulator.
    lipo "${CRYPTO_LIB}" "${DEVICE_CRYPTO_LIB}" -create -output "${UNIVERSAL_LIBRARY_DIR}/libcrypto" &&
    lipo "${SQLCIPHER_LIB}" "${DEVICE_SQLCIPHER_LIB}" -create -output "${UNIVERSAL_LIBRARY_DIR}/libsqlcipher" &&
    lipo "${SSL_LIB}" "${DEVICE_SSL_LIB}" -create -output "${UNIVERSAL_LIBRARY_DIR}/libssl"
    

Once completed, you can build your new aggregate target and there should be 3 libraries located in a subdirectory within your project build directory. Just add these to your XCode project as you would any other library and you are good to go.

Sunday, April 1, 2012

Notes from the Phorum -- Cloud means something a little different to everyone

Enterprise

Enterprise users get dinged for cloud-washing: buying VMware and saying they're running a cloud.  But many of them do want to offer a "private cloud".  Some of the differences?  Automation, standard cloud "machine" configurations, self-service provisioning, a single cloud for all business units, calculating runtime costs and charging projects for them.  Some of the obstacles?  Lack of automation in existing processes, trying to bring complex legacy applications on board, trying to migrate existing staff and tools "to the cloud".  A key recommendation was when building a private cloud, start it as a totally separate effort that offers capabilities to the business, and strive to reach the point where they will go to you instead of a third-party cloud.

But why run a private cloud?  The number one reason the business prefers the cloud is that it facilitates speed and agility -- effectively taking much of the budgeting and procurement out of a project.  And if you try a project and it doesn't work, you shut down the cloud instances and wash your hands with much less regret.  As for IT, a well-implemented private cloud can further increase the utilization of hardware over virtualization alone, and the automation allows fewer administrators for the same quantity of hardware.  Plus you're offering the business capabilities, instead of hurdles.

Along those lines, there was significant discussion of the changing role of IT as a business moves more toward the cloud.  Instead of the detailed management of servers, virtualization consoles, and execution environments, it becomes managing multiple sets of capabilities -- one-off hardware required for legacy applications, virtual machines, private cloud, public cloud, etc.  While you might promote the cloud for mainstream projects, there will always be some with special requirements that place it somewhere else along that spectrum, and the goal is more to shepherd each project to the right home.

With all that said, there was a lot of enthusiasm for public cloud offerings.  While the security of a public cloud service is still the number one concern, even among businesses that are using the cloud, there were many arguments made that such concerns are no longer insurmountable.  Cloud vendors have fewer hardware and software platforms to maintain, and more motivation to do it right (given the cost of a failure).  Plus there's no guarantee that internal employees are less likely to make security mistakes.  "Do you truly believe your company manages their machines better than the next company?  How about, better than a vendor whose business is the cloud?"

Startups

On the other hand, one attendee quipped, "the only thing Enterprise is to me is a rental car company."  The advice to startups came from a totally different direction.  Use the cloud in every possible way -- only be smart about it.

On Software as a Service: Why buy a box of software and a machine to run it, when you can use a lower-cost online service that you don't have to manage and gives you regular feature updates automatically?

On Infrastructure as a Service: No VC will fund you if your business plan shows boxes instead of Amazon.

On Platform as a Service: You could come up with specific requirements, build the perfect software, find the ways to customize it just like you need, and integrate it with other systems.  But we've done that already and poof! this great platform emerged.  Why not just take advantage of that instead?  (Granted, this was SalesForce.com talking.)

In fact, one of the challenges quoted was that there are so many good-looking services that are easy to adopt, you may find yourselves three months down the road before you realize it's not really what you want after all.  So the pearl of wisdom was to define your requirements well in advance, and then find the right combination of services to support them.  Another was to think carefully before you start custom-integrating all these services.  You're better off changing your process to fit the service than spending truckloads of time on custom integration.  Plus there are companies out there who do that already.

Developers

Of course you can build applications for the cloud.  But one point was to realize that's what you're doing going in.  You won't necessarily have multiple machines on the same network segment (or even with multicast capability at all!), different "identical" machines may perform differently, machines or resources may go down at any time, common services like e-mail might not be available, you may be limited in what database it's convenient to use, and so on.

And the cloud isn't automatically cheaper -- in many cases you have to be careful about shutting machines down whenever they're not really in use in order to realize the potential cost savings.  (As an aside, one blog breathlessly proclaims that Amazon has reduced the EC2 price-per-compute-unit by 15% over the course of six years.  Conveniently overlooked: Moore's Law suggests that you should receive 1600% more hardware power per dollar over the same period of time.)  In other cases, the time and cost savings comes from adopting a runtime platform instead of cloud infrastructure (a VM).

Final Thoughts

Overall though, the message was largely the same for everyone -- there are benefits to be reaped from the cloud, but you have to be wise about it.  Plan first, don't just meander toward the cloud and assume you'll get to the right place in time.  you may, but it may be a lot more time than you're counting on.

Monday, March 26, 2012

Breaking: Android Philly ETE Application Released

We kept the functionality you love and gave the app a fresh new look. We'd love to hear what you think!





Wednesday, March 14, 2012

What is Chariot Day?

No, Chariot Day is not a group of Ben Hur enthusiasts reenacting Roman-era racing.  Chariot Day is a one day technical conference put on by the techies at my company, Chariot Solutions, on a Saturday.  To borrow a phrase, Chariot Day is of the people, by the people, for the people.  It is loose, it is informative and it is fun.  It's a chance to catch up with all of the other Chariot Solutions consultants who are spread out at clients throughout the greater Philadelphia area.

Some of the things that make Chariot Day a big success are:

Content -- The sessions range from highly polished, conference-ready presentations to less polished, more impromptu "stunt coding", but the one thing that is common is the high quality of the content.  The reason the content is so good is that it is very evident that the developers are passionate about the technologies they are talking about.  I get excited about things at Chariot Day because the presenters are so excited.  This year's topics were:
  • Building Real-Time Web Applications
  • Real world Hadoop
  • Programming Environments Designed for Children
  • jQuery Mobile + Backbone.js
  • Griffon - Making Java Desktop Development Groovy
  • Scala
  • Sencha Touch
  • Tech Radar
  • ByteBuffers and Off Heap Memory
  • One month with Arduino
  • NoSQL: Cool vs. Real
  • Andriod 101 (lab)
  • Partitioning & Scaling Batch Processing to the Cloud
  • CPU Caches
  • Introducing KIF
I came out of Chariot Day wanting to build a system that had a Griffon front end with a Hadoop data store using a Redis cache that was written in Scala and tested using KIF all running on an Ardiuno processor.  Of course that stack isn't actually possible, but that's how diverse the presentations were.

Participation -- We had 38 attendees and 15 presentations.  We ran 3 concurrent tracks of presentations all day.  That represents about 90% of the consultants attending and a little less than half presenting.  It is exciting that that many people came and were willing to present.  It is really valuable to get up in front of your peers and tell them about technologies you think are important.

Fun Factor -- There was breakfast, lunch, and snacks served and the food was quite good.  We have a special "limited edition" Chariot Day t-shirt designed and printed each year.  This year's shirt was really cool.  There is a book buy where you can order any technical book you want and pick it up on Chariot Day.  There is always some kind of interesting mad scientist project demonstrated; this year's Arduino talk featured a fully automatic Nerf gun that could be controlled via a web browser.  We have a crazy Quizzo game at the end of the day with questions about topics discussed during the day.  In short, it is a lot of fun.

It is easy to brag about Chariot Day because that is really bragging about my colleagues who truly are top notch.  Their creativity and passion for continual learning is what makes Chariot Day a success.

Friday, March 2, 2012

Android 101: A Crash Course

Tomorrow is Chariot Day! Charioteers will give 45 minute presentations on a broad spectrum of topics, including enterprise Java techniques, mobile, and Arduinos.

I'm planning a dive into Android and teaching concepts along the way. At the end of my session, students will away with a simple Doctor Who app and learn about these foundation topics:
  • XML layouts
  • Themes, styles, drawables, & strings
  • Click/event handling
  • Starting a new activity
  • Specifying an icon for the app
  • Fragments & tablet version (if time)
Read more here!

Wednesday, February 22, 2012

Using jQueryMobile and Backbone.js for handling forms

Introduction

In this post, I continue the development of my basic "exercise app" that I started (and enhanced) in these posts:

Let's add the ability to create and edit exercise records.

Adding a new activity

Just like in the previous post, the first thing we need is a jQueryMobile page to hold the form content. Add the following to index.html:

    
Save

New Activity

In the code above, we see the typical jQueryMobile page. The main things of interest here are with respect to the anchor tag in the header div:

  • Specify data-rel="back" to navigate to the previous page once the save is complete. This way whether we enter the form page from "Add" or "Edit", navigation will be handled.
  • The class="ui-btn-right" moves the button to the right side of the header bar.
  • The data-theme="b" causes the save button to use the blue theme.
The next step is to define the template that the view will use to render the content for this page. Lets start with a simple template with an input for each attribute of an activity. To vary the UI slightly, we can use a drop down for the activity type. The basic template will look like this:

    

Notice that we are using the HTML 5 input types for some of the fields. Browser support for these will vary, but worst case they will deprecate to a text input type. For example, on iOS, the date will present a native date picker, whereas on Android (or Chrome, etc.) the field will behave as a standard text input.

Now we need to navigate to the form page. This requires modifying the "Add" button on the list page.

    

Activities

Add

In order to transition to the form page, the href attribute on the anchor tag (i.e. button) needs to be changed to the id of the form page, as shown in line 4 above. The final step in presenting the form page to support adding exercise activities is modifying the add button click handler.

    $('#add-button').live('click', function(){
        var activity = new exercise.Activity(),
            activityForm = $('#activity-form-form'),
            activityFormView;

        activityFormView = new exercise.ActivityFormView({model: activity, viewContainer: activityForm});
        activityFormView.render();
    });

Here we create an empty model, grab the form node, create a new exercise.ActivityFormView with these objects, and render the form. Testing at this point reveals a problem. The screen shot below doesn't look right ;)

Investigating the browsers console reveals an error: Uncaught ReferenceError: distance is not defined. This is due to the underscore template trying to access an undefined attribute of the model. The issue is described here with some work arounds. I found the easiest workaround is to provide defaults for the model, as shown below.

     exercise.Activity = Backbone.Model.extend({
        defaults: {
            date: '',
            type: '',
            distance: '',
            comments: '',
            minutes: ''
        }
     });

Now the form renders properly. With the help of some CSS, we can tighten things up a little and end up with the version on the right (the CSS is part of the source code, see the links at the end of this post).

Editing an existing activity

With the template and the view already implemented, adding edit functionality is pretty straightforward. The first step is to update the href attribute of the edit button on the activity-details page.

    
Edit

Activity Details

As we did with the add button, this should reference the activity-form page. Now we need to add the click event handler for the edit button.

    $('#edit-activity-button').live('click', function() {
        var activityId = $('#activity-details').jqmData('activityId'),
            activityModel = exercise.activities.get(activityId),
            activityForm = $('#activity-form-form'),
            activityFormView;
        
        activityFormView = new exercise.ActivityFormView({model: activityModel, viewContainer: activityForm});
        activityFormView.render();
    });

If you recall, the activityId is passed to the details page view the list item click event handler. We reuse that fact here to retrieve the activity from the collection.

Time to test. Selecting an activity renders the detail page. Clicking the edit button renders the form, pre-filled with the activity details. Everything looks great.

But wait.....that screenshot was from the browser on which I am testing. We should always test on devices as well. Earlier I mentioned that we are using some of the HTML 5 input types (i.e. date). The nice thing is that iOS 5 will render a nice date picker for date types. The challenge is that the date needs to be in a specific format for that to work. The image below shows what the form page looks like on an iOS device.


Fixing the Date

No date value is showing up on our iOS device. This is a known issue. So, for iOS we need the date format as yyyy-mm-dd, while for other platforms, I prefer the date to be shown as mm/dd/yyyy. I do realize that date formats should be localized, but for the purpose of this example I would like to keep it straightforward and specify mm/dd/yyyy as the display date format. There are many options that could be pursued here, but to demonstrate some more capabilities of Backbone.js, we will modify our model to help meet our date requirements.

First, the date attribute in our JSON feed is a string. Lets convert this to a date when the data is retrieved from the server, then when we need the date, we can just manipulate it as needed. This can be accomplished several ways.

  • Implement a parse method on the collection that converts the String to a Date as the data is being fetched from the server.
  • Implement a method on the model that converts the date String to a Date and sets it on the model. This would then require the calling code to know to call this method depending upon the circumstance.
  • Override the set method on the model, look for the date attribute, then convert it to a date as needed.
The last option is the most seamless approach. Add the following method to the Activity model.

        set: function(attributes, options) {
            var aDate;
            if (attributes.date){
                //TODO future version - make sure date is valid format during input
                aDate = new Date(attributes.date);
                if ( Object.prototype.toString.call(aDate) === "[object Date]" && !isNaN(aDate.getTime()) ){
                    attributes.date = aDate;
                }
            }
            Backbone.Model.prototype.set.call(this, attributes, options);
        }

Also, update the defaults so that the date attribute is now a Date instead of a String.

        defaults: {
            date: new Date(),
            type: '',
            distance: '',
            comments: '',
            minutes: ''
        },

While this clearly isn't the most robust date handling, it is fine for now. In a future version, this should be improved.

Next, add attributes to our model that will format the date in the ways we need it (i.e. mm/dd/yyyy and yyyy-mm-dd).

        dateInputType: function(){
            return exercise.formatDate(this.get('date'), "yyyy-mm-dd"); //https://github.com/jquery/jquery-mobile/issues/2755
        },
        
        displayDate: function(){
            return exercise.formatDate(this.get('date'), "mm/dd/yyyy");
        }

The formatDate function is a simple date formatter to meet our specific needs and can be found in the source code that accompanied this post (see link at the bottom of the post).

Now, how do we use these new methods in our view. The first thing to realize is that we pass the template JSON. The default implementation of the toJSON method of a Backbone.js model will not include these functions. Therefore, we need to override the toJSON method.

        toJSON: function(){
            var json = Backbone.Model.prototype.toJSON.call(this);
            return _.extend(json, {dateInputType : this.dateInputType(), displayDate: this.displayDate()});
        }

Here, we are using the Underscore.js extend to add our attributes to the standard Backbone JSON. Now, we need to modify our view templates to use the appropriate JSON attributes.

    
    
    
    
    

Notice that lines 16-21 include some conditional logic. This is a very basic device detection check to determine which date format to use. There are plug-ins,etc. that provide more robust alternatives, but to keep things clear, this will serve our needs. The screen shots below show the desktop browser and the iOS versions of the form with the appropriate date handling.

Saving

The final step is to implement the save functionality.

    $('#save-activity-button').live('click', function(){
        var activityId = $('#activity-details').jqmData('activityId'),
            activity,
            dateComponents,
            formJSON = $('#activity-form-form').formParams();
        
        //if we are on iOS and we have a date...convert it from yyyy-mm-dd back to mm/dd/yyyy
        //TODO future version - for non-iOS, we would need to validate the date is in the expected format (mm/dd/yyyy)
        if (formJSON.date && ((navigator.userAgent.indexOf('iPhone') >= 0 || navigator.userAgent.indexOf('iPad') >= 0)) ){
            dateComponents = formJSON.date.split("-");
            formJSON.date = dateComponents[1] + "/" + dateComponents[2] + "/" + dateComponents[0];
        }
        
        if (activityId){
            //editing
            activity = exercise.activities.get(activityId);
            activity.set(formJSON); //not calling save since we have no REST backend...save in memory
        }else{
            //new (since we have no REST backend, create a new model and add to collection to prevent Backbone making REST calls)
            activity = new exercise.Activity(formJSON);
            activity.set({'id': new Date().getTime()});  //create some identifier
            exercise.activities.add(activity);
        }
    });

I used the JavascriptMVC formParams jQuery plugin (found here) to convert my HTML form into a JSON object. Then, the date is converted into the appropriate format. One thing to note here is that since we have no real back end server for Backbone to interface with, we don't call save on the model or create on the collection. These methods would cause the appropriate REST calls to the server.

Since the click event on the list view passes the activityId to the details page, we can use this to determine if we are adding or editing. One thing to consider here is that the user may click an existing activity, then go back to the list, then click add. In this case, the activityId from the previously selected activity is still attached to the activity-details view. This will cause our save implementation to think we are editing. To prevent this, we should remove the activityId from the activity-details page when adding a new activity. This can be accomplished by adding the highlighted line below to the add handler.

    $('#add-button').live('click', function(){
        var activity = new exercise.Activity(),
            activityForm = $('#activity-form-form'),
            activityFormView;
    
        //clear any existing id attribute from the form page
        $('#activity-details').jqmRemoveData('activityId');
        activityFormView = new exercise.ActivityFormView({model: activity, viewContainer: activityForm});
        activityFormView.render();
    });

The final step is to make sure our views are updated accordingly when we add or modify activities. Since the details view retrieves the activity before the page loads, no change is required here. But our list view only renders during the initial page load. This is where we can take advantage of Backbone event binding. A few small additions to the ActivityListView and things will be handled.

        initialize: function() {
            this.collection.bind('add', this.render, this);
            this.collection.bind('change', this.changeItem, this);
            this.collection.bind('reset', this.render, this);
            this.template = _.template($('#activity-list-item-template').html());
        },

        ...

        changeItem: function(item){
            this.collection.sort();
        }

The change event will bubble up from the model and fires as a result of the set being called. In the changeItem method, we sort the collection to handle any changes in the date attribute. The sort method call will cause the reset event to fire, which then results in the render method invocation, causing the list to be re-rendered. This keeps everything in the correct order. The add event will be fired when we add a new activity to the collection. Since we implemented the comparator method on the collection, models added to the collection will be added in the appropriate order.

We can now add and edit activities. Start exercising :)

The source code for this post can be found here