| Next Page

App Engine Community Update

Posted 4 weeks, 1 day ago by The App Engine Team (noreply@blogger.com)


Over the past two months we've seen the App Engine community do some exciting things. To highlight community participation, we'll be posting regular App Engine community updates.

Applications

Below are some of the more popular ones we've seen:

  • Wordle - Create word clouds for user provided text
  • Guess-a-Sketch - Fans of Pictionary will love this sketching-guessing game!
  • BuddyPoke - An OpenSocial application for interacting with your friends

Developer Articles

Many developers have written useful articles tutorials about App Engine. This week, we published our first externally written article on the App Engine developer pages Configuring Eclipse on Windows to Use With Google App Engine. Below are some other contributions we've found out there on the world wide web:

Open Source Projects

Recently, we invited developers to list their Open Source projects on our group's pages. So far, we have 14 projects - these range from App Engine utility libraries, web frameworks, Open Source applications, and even an HTTP implementation of Map Reduce. Please continue to let us know what you are working on!

App Engine Group

Lastly, discussion continues to thrive on our Google group. Recently popular threads include discussions on full text search, session management, downloading apps, and executing common functionality before serving a request.

View Original

Good stuff (no post, just links)

Posted 1 month ago by The App Engine Fan (noreply@blogger.com)
This weekend, the weather is just too fine to sit inside blogging ;-) Therefore, just a few links I ran into over the weeks. They are not really that new, but I thought I'd mention them anyway in case anyone isn't aware of them:

Google App Engine tips&tricks; is a blog entry with some neat little hacks for the developer to get to some additional helpful data.

Google App Engine Open Source Projects, prominently featured on code.google.com.

How I Learned to Stop Worrying and Love Using a Lot of Disk Space to Scale, an oldie-but-goldie on the data store, updated several times and containing a couple of interesting links related to data modeling.

A list with links to some very good App Engine related videos from the Google I/O.

Now back to my patio chair -- I hope everyone is having a great weekend :-)

View Original

Google I/O session videos posted with slides

Posted 1 month, 1 week ago by The App Engine Team (noreply@blogger.com)


On May 28th and 29th the Google Developer Team hosted Google I/O, a developer gathering in San Francisco. For anyone who wasn't able to join us in person, you can now now catch videos of more than 70 talks, along with slides from each of the presentations, online. Among these are some great sessions from members of the Google App Engine Engineering team:

There are also a lot of interesting talks on Android, Gears, AJAX Search API, Gadgets, GData, OpenSocial, and many other Google Developer products. Feel free to discuss in our group!

View Original

Keeping private data private

Posted 1 month, 1 week ago by The App Engine Fan (noreply@blogger.com)
One of the things at Google that impresses me most (besides the quality of the food, of course) is the level of commitment and energy that the company puts into keeping its user's data secure. From the classic story about not handing out search data to its latest face blurring technology for street view, these guys do whatever they can to keep private data private. With the launch of App Engine, the company opens up its data centers to you and me, so I think it's only fair to strive for the same standard of quality. There are many small things we can do in our applications to decrease the chance of leaking data. In this post, I would like to talk about one of them.

Take a look at the following example application. Ignore the lack of transactions or that I hardcode my html and focus on the security aspect. Do you see anything wrong?

import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import login_required

# This app is an easy way for users to store their
# social security number online, in case they need to
# look it up

# I'm too lazy to use Django templates ;-)
SHOW_DATA = '<html><body>Your SSN: %s</body></html>'
NOT_FOUND = '<html><body>No data found</body></html>'
DATA_SAVED = """<html><body>
Your data was saved. You can get to it through <a
href='/?user=%s'>this link</a>.</body></html>"""
FORM = """<html><body><form method='GET'>
Enter your Social Security Number:
<input name="ssn"/><br/><input type="submit"/>
</form></body></html>"""

# Data model
class SSN(db.Model):
user = db.UserProperty(required=True)
ssn = db.StringProperty(required=True)

# Handler
class MainPage(webapp.RequestHandler):
# Get method (login required for security)
@login_required
def get(self):
html = ''
user = users.get_current_user()
# Case: show data
if self.request.get('user'):
user = users.User(self.request.get('user'))
data = SSN.gql('WHERE user = :1', user).get()
if data:
html = SHOW_DATA % data.ssn
else:
html = NOT_FOUND
# Case: modify data
else:
if self.request.get('ssn'):
data = SSN.gql('WHERE user = :1', user).get()
if data:
data.ssn = self.request.get('ssn')
data.put()
else:
data = SSN(user=user, ssn=self.request.get('ssn'))
data.put()
html = DATA_SAVED % user.email()
else:
html = FORM
# Render response
self.response.out.write(html)

# Main
def main():
application = webapp.WSGIApplication(
[('/.*', MainPage)], debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()


The author of this code (which is me, but I'd rather not point fingers ;-) tried to build a small database that people could use to store their social security number online. A user would log in and post its information. In return, he would get a unique URL back that pointed to his information. To make things secure, the programmer decided to require a login for data access. The problem is that the author neglected to check that the current user matched the owner of the user data. In other words, if Mitch Millionaire used the application and I somehow knew his email address, then I could get to his SSN by simply going to his URL and logging into my own gmail account.

On first sight, this example might seem a little bit contrived. Who the heck would be so stupid and forget that simple check? The reality however is that it could happen to anybody. We are all human, and real life applications are rarely as simple as what would fit into a single blog post. We usually have a hierarchy of data entities, all connected to each other. As an application traverses the entity relationships, it could easily happen that it stumbles upon information it is not intended to have access to. Unit tests may help to prevent some of these cases, but with growing complexity, it is not possible to foresee every turn of events. If we agree on the premise that we all make mistakes and that protecting our users is more important than protecting our pride, then this leads me to a simple conclusion: I'd rather have my application explode than hand out confidential data to the wrong person! My wounded pride will heal faster than the user's trust.

With that in mind, could we have prevented this bug from happening? Take a look at the following
addition to my original code:

class CurrentUserProperty(db.UserProperty):

def checkCurrentUser(self, value):
if value != users.get_current_user():
raise db.BadValueError(
'Property %s must be the current user' % self.name)
return value

def __init__(self, verbose_name=None, name=None):
super(CurrentUserProperty, self).__init__(
verbose_name, name, required=True,
validator=self.checkCurrentUser)



The class CurrentUserProperty is like a regular UserProperty, with a small addition: it assumes that only the current user is a valid input. This was achieved by creating a custom validator-function (checkCurrentUser) and passing it along with the constructor. The property's validation function is called whenever data is loaded from or stored in the database, so besides its original validation code (we set required=True, so validation will fail if no user was assigned) we will now also make sure that the user equals the person logged in. Since we did not modify anything else in the code, the new Property is a drop-in replacement in our model class:

class SSN(db.Model):
user = CurrentUserProperty()
ssn = db.StringProperty(required=True)



Did this fix our original bug? Absolutely not! Did it prevent somebody else having access to Mitch Millionaire's social? Absolutely, assumed that nobody hacks into his gmail account! The application will raise an exception and the data will not get served.

What does this mean for the application developer? Well, whenever a crash like this happens, the application console will log the exception and make it available in the logs. The developer can access this data in the dashboard, inspect the stack trace and detect where the data integrity violation came from. He or she can write a unit test, fix the leak and deploy an improved version.

Interested in more tips and tricks? Like I said before, there are other nifty things one can do to keep data secure. Why not posting some of them in the comments section? Go ahead and share your ideas!

View Original

Global Counters and other good stuff

Posted 1 month, 1 week ago by The App Engine Fan (noreply@blogger.com)
Have you ever not read a book because you were "waiting for the movie"?
10 days ago, I posted some mental notes from a talk by Brett Slatkin. Now, you can watch his original talk on youtube! Catch all the many tips and details I missed, mixed up or simply did not mention.

As a matter of fact, Google has released a site with pretty much all the sessions from the Google I/O -- and boy, are those great to watch! The site contains both the video and the original slides, so we can all learn more about App Engine, GWT or many of the other interesting APIs Google has to offer.

When I am not wearing my App-Engine-Fan-hat, I am mostly interested in Java-based development, so I would like to point out a couple of gems I found particularily interesting:


In addition to all these things mentioned above, there is tons of material about Android, GWT and more. Check it out and have fun!

View Original

Unit tests for Google App Engine apps

Posted 1 month, 2 weeks ago by The App Engine Fan (noreply@blogger.com)
I recently discovered a problem in my unit tests: I did not have any! Now don't worry -- I'm not climbing on my little soap box now to preach the importance of unit tests. There is enough documentation on the web, I do not need inflate this little article with all the same reasons over and over again. Besides, I would be way too busy wiping egg off my face... I was a little surprised at how
View Original

Unit tests for Google App Engine apps

Posted 1 month, 2 weeks ago by The App Engine Fan (noreply@blogger.com)
I recently discovered a problem in my unit tests: I did not have any! Now don't worry -- I'm not climbing on my little soap box now to preach the importance of unit tests. There is enough documentation on the web, I do not need inflate this little article with all the same reasons over and over again. Besides, I would be way too busy wiping egg off my face...

I was a little surprised at how much App Engine code I had written without a single test though. I usually am a firm believer, and my test coverage at work is usually quite decent. Granted, I am mostly a Java guy, but being able to hit F11 in Eclipse to run my tests is no excuse! I went ahead to correct that mistake and ran into a couple of obstacles, like how to simulate the database or the users API, and how to verify the output of my web handlers without parsing html. I figured it out eventually, so I thought I'd share my results.

Since what I'm coding at home is nobody's beeswax, I decided to unit test the guestbook app from the getting started guide instead. By the way, you can download the app and my unit-tests from this link. The archive (in unittest.py) contains a total of four unit tests (two for each handler). I will now take you through one of them; the others follow pretty much the same pattern.

Initial setup


Python comes out of the box with a unittest module, so I did not have to bother making a big decision here. Unfortunately, there were quite a few App Engine specific modules (such as the datastore or users) that had to either be configured or replaced for my tests to work. I decided to use a mocking framework and stub out most google-specific code instead of setting up and initializing a fake datastore, user objects and so on. The following code adjusts the python path and imports the necessary modules:

import unittest
import sys
import os.path


# Change the following line to reflect wherever your
# app engine installation and the mocker library are
APPENGINE_PATH = '../google_appengine'
MOCKER_PATH = '../mocker-0.10.1'


# Add app-engine related libraries to your path
paths = [
APPENGINE_PATH,
os.path.join(APPENGINE_PATH, 'lib', 'django'),
os.path.join(APPENGINE_PATH, 'lib', 'webob'),
os.path.join(APPENGINE_PATH, 'lib', 'yaml', 'lib'),
MOCKER_PATH,
]
for path in paths:
if not os.path.exists(path):
raise 'Path does not exist: %s' % path
sys.path = paths + sys.path
import mocker


Defining and running a test


We just set up our program with all the necessary components for a unit test.
So far so good -- now we are ready to define out test. I am subclassing from MockerTestCase, which provides me with a predefined reference to "self.mocker" (a factory object that controls my fake objects and makes sure that everything gets cleaned up at the end of the test). I create instances of both handlers I am about to test, but the request and response objects I populate them with are mocks. If they call a method that I do not expect, my test will automatically fail. I also replace the users- and templates module (why validating the generated html when I might as well just check the parameters?) and the Greetings datamodel:

import helloworld

class UnitTests(mocker.MockerTestCase):
"""Unit tests for the MainPage and GuestBook handler."""

def setUp(self):
self.request = self.mocker.mock()
self.response = self.mocker.mock()
self.handler1 = helloworld.MainPage()
self.handler2 = helloworld.Guestbook()
self.handler1.request = self.request
self.handler1.response = self.response
self.handler2.request = self.request
self.handler2.response = self.response
self.Greeting = self.mocker.replace('helloworld.Greeting')
self.users = self.mocker.replace('google.appengine.api.users')
self.template = self.mocker.replace('google.appengine.ext.webapp.template')


Now, everything is ready to write the first test. This test validates the behavior of MainPage.get() when a user is logged in (if you are not familiar with that method, check it out here). The easiest way of doing that is to record in our mocking framework each and every call that the application is going to make for our specific test case. The following code simulates a query to the datastore that orders the data and fetches the first 10 results. Note that we do not really return any model objects -- they are not relevant for this particular test. A simple string like Query result for template will do.

  def testGetWhenLoggedIn(self):
# What should happen in a regular request?
# First, we create a query on all greetings
all_query = self.mocker.mock()
self.Greeting.all()
self.mocker.result(all_query)
ordered_query = self.mocker.mock()
all_query.order('-date')
self.mocker.result(ordered_query)
ordered_query.fetch(10)
self.mocker.result('Query result for template')


We follow exactly the same pattern when the application requests the current user (aren't dynamic languages great?). In this example, we return a mock object instead of a random string. Technically, this is not necessary (or code does not call any methods on the user object), but it demonstrates how we could return a fake object instead if we needed to record more in-depth test behaviour.

    fake_user = self.mocker.mock()
self.users.get_current_user()
self.mocker.result(fake_user)
self.request.uri
self.mocker.result('fake uri')
self.users.create_logout_url('fake uri')
self.mocker.result('fake logout uri')


Now that our handler has loaded all the data, it would usually render a Django template. The following code demonstrates how to test this. Watch out for the checkArgs-function, which is used to verify our expectations regarding our template parameters. At the end, we return a small fake html-page and tell our mocker to verify that this is put into our fake output stream:

    out = self.mocker.mock()
self.response.out
self.mocker.result(out)
self.template.render(mocker.ANY, mocker.ANY)
def checkArgs(path, params):
template_values = {
'greetings': 'Query result for template',
'url': 'fake logout uri',
'url_linktext': 'Logout',
}
self.assert_(path.endswith('index.html'))
self.assertEqual(template_values, params)
return '<html/>'
self.mocker.call(checkArgs)
out.write('<html/>')


We have now modelled our expectations: assumed that we are logged into a user, what do we expect our handler to do? Now, all that remains is to switch into replay mode and exercise the function. If the handler behaves different in any unexpected way, our unit test will fail. We also call a prefabricated main method that will detect and run all of our tests.

    # Everything is recorded, so let's go into replay mode :-)
self.mocker.replay()
self.handler1.get()

# #####################
# LAUNCH ALL UNIT TESTS
# #####################
if __name__ == "__main__":
unittest.main()


So far so good?


When you download the full set of unit tests and take a look at one of the later tests, you will find a section where I left a TODO. This is an area where (instead of rendering a Django template) a browser redirect happens. My original code
for that test looked something like this:

    # Last but not least, store the post and redirect
# TODO: refactor test?
greeting.put()
self.handler2.redirect = lambda x: self.assertEquals('/', x)


Again, I forgot my soap box at home, but this is an example where (at least in Java) I would have preferred composition over inheritance. I replace the redirect-method in my handler with a lambda that validates the redirect-url. However, I have no guarantee that this redirect URL is ever called.
I can improve this by having the redirect invoking a mock object:

    # Last but not least, store the post and redirect
# TODO: refactor test?
greeting.put()
mock_redirect = self.mocker.mock()
self.handler2.redirect = lambda x: mock_redirect.redirect(x)
mock_redirect.redirect('/')



While this is definitely an improvement, it is still far from ideal. What if somebody changes the handler to additionally call the error()-method? That behaviour would get lost! Even worse, what if someone decides to overwrite the redirect-method in the handler's implementation? In that case, my test might even return false positives, since something in that new redirect method could have been broken.

I am not sure what is the ideal solution. For the moment, I have decided to call the post-method itself on a mock object, passing the mock as the "self"-reference to the Guestbook handler:

  def testPostWhenLoggedOut(self):
handler3 = self.mocker.mock(helloworld.Guestbook)
handler3.request
self.mocker.count(0,10)
self.mocker.result(self.request)
handler3.response
self.mocker.count(0,10)
self.mocker.result(self.response)

# First, a new greeting is created
greeting = self.mocker.mock()
self.Greeting()
self.mocker.result(greeting)

# The user is None and thus not assigned to the greeting
self.users.get_current_user()
self.mocker.result(None)

# Next, the content is fetched from a request parameter
self.request.get('content')
self.mocker.result('mock content')
greeting.content = 'mock content'

# Last but not least, store the post and redirect
# TODO: (check blog post for more information)
greeting.put()
mock_redirect = self.mocker.mock()
handler3.redirect('/')

# Everything is recorded, so let's go into replay mode :-)
self.mocker.replay()
helloworld.Guestbook.post(handler3)


This has the adavantage that the test fails if anything unexpected (like self.error()) is being called. For more complex handlers, however, it also would mean that one would need to record mock behavior for all potential helper methods that the class is actually expected to call.

Is there a better solution? Please post a comment to this article...
View Original

Google App Engine limitations and workarounds

Posted 1 month, 2 weeks ago by Roberto Saccon (noreply@blogger.com)
One of the current limitations of the Google App Engine is the lack of scheduled background processes. But early adapters are creative to circumnavigate this and other limitations. The most simple solution to this problem has William Vambenepe: he uses Google Reader to subscribe to a dummy RSS feed on his App Engine hosted test app, that results in some kind of regular polling with an interval
View Original

Efficient Global Counters

Posted 1 month, 3 weeks ago by The App Engine Fan (noreply@blogger.com)
I had the chance to spend some time at the Google I/O last week, and it was amazing -- great knowledge exchange and a couple of really interesting speakers. One of my favorite sessions was a talk by Brett Slatkin called "Building Scalable Web Applications with Google App Engine." He talked about quite a few gotchas to be aware of when trying to get most out of the datastore. One of his examples
View Original

Efficient Global Counters

Posted 1 month, 3 weeks ago by The App Engine Fan (noreply@blogger.com)
I had the chance to spend some time at the Google I/O last week, and it was amazing -- great knowledge exchange and a couple of really interesting speakers. One of my favorite sessions was a talk by Brett Slatkin called "Building Scalable Web Applications with Google App Engine." He talked about quite a few gotchas to be aware of when trying to get most out of the datastore.

One of his examples shows was a more efficient way of building a global counter: the naive approach I have been guilty of in the past (store the count in an entity and update it in a transaction) turns out to be a performance bottleneck, because this means that all transactions have to wait on a single lock for the entity. Instead, he suggested to split the counter into several shards, thus distributing the locking over more than one entity group. I don't recall the exact implementation from memory, but it went something like this:

from google.appengine.ext import db
import random

SHARDS_PER_COUNTER = 20

class CounterShard(db.Model):
name = db.StringProperty(required=True)
count = db.IntegerProperty(default=0)

def GetCount(nameOfCounter):
result = 0
for shard in CounterShard.gql('WHERE name=:1', nameOfCounter):
result += shard.count
return result

def ChangeCount(nameOfCounter, delta):
shard_id = '/%s/%s' % (
nameOfCounter, random.randint(1, SHARDS_PER_COUNTER))
def update():
shard = CounterShard.get_by_key_name(shard_id)
if shard:
shard.count += delta
else:
shard = CounterShard(
key_name=shard_id, name=nameOfCounter, count=delta)
shard.put()
db.run_in_transaction(update)

Pretty nice, and it gets even better: using the newly announced Memcache API, we can keep the counter in memory and only update it once in a while (which is acceptable for many use cases). The following modification keeps the counter in memory for a minute before reloading it:

def GetCount(nameOfCounter):
memcache_id = '/CounterShard/%s' % nameOfCounter
result = memcache.get(memcache_id)
if not (result == None):
return result
result = 0
for shard in CounterShard.gql('WHERE name=:1', nameOfCounter):
result += shard.count
memcache.set(memcache_id, result, 60)
return result

Brett, thanks for giving this talk. I'm looking forward to trying this out in my applications!
View Original
| Next Page
 Subscribe in a reader