Fix key checking in associate_key

This also includes renaming 'key_name' variable to 'key_id' for more
clarity.