def add(fn, key):
    """
    Add a key to an `authorized_keys` file.
    """
    if hasattr(fn, 'read'):
        fh = fn
        fh.seek(0, 2)
        close_fh = False
    else:
        fh = open(fn, mode='a+', encoding='utf-8')
        close_fh = True
    try:
        fh.write(key.getvalue())
        fh.write('\n')
    finally:
        if close_fh:
            fh.close()
