Skip to content

Commit 6cd544d

Browse files
author
=
committed
Added migration scripts and documentation
1 parent 01c0673 commit 6cd544d

File tree

11 files changed

+420
-21
lines changed

11 files changed

+420
-21
lines changed

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,99 @@
11
# `accessmap-api`: AccessMap's user/profiles/auth API
2+
3+
## Why?
4+
5+
AccessMap users want to securely store and access settings, routing profiles, and
6+
other information across browsers and devices.
7+
8+
## How?
9+
10+
`accessmap-api` is a fairly boilerplate Flask application (a popular Python web
11+
framework) with roughly RESTful interfaces: it uses HTTP verbs appropriately for GET,
12+
POST, PUT, etc. `accessmap-api` is only compatible with Python 3.6+.
13+
14+
## Installation
15+
16+
`accessmap-api` is developed using `poetry`, which makes development, releases, and
17+
installation more simple and reproducible. The best way to install `accessmap-api`
18+
into a development environment is to use the poetry tool
19+
20+
poetry install
21+
22+
A guide for installing `poetry` itself can be found here:
23+
https://poetry.eustace.io/docs/
24+
25+
This command will create an isolated virtual environment for all of `accessmap-api`'s
26+
dependencies. You can now run any commands in this environment by prepending
27+
`poetry run` to the command. We'll set up a testing database to show how it works, but
28+
first we need to set up our configuration.
29+
30+
## Configuration
31+
32+
Configuration of `accessmap-api` is done with environment variables. Flask supports the
33+
use of a .env file that contains `ENV_VAR=value` lines or using the default environment
34+
variables available on the system. Note that system environment variables will
35+
supercede entries in the .env file.
36+
37+
*Please note that all of the following environment variables that are marked "required"
38+
are necessary for `accessmap-api` to function correctly and securetly.*
39+
40+
- `SECRET_KEY` (required): This is a secret used to sign / secure sessions during a
41+
request context. It is very imporatnt to keep this value secure, particularly if your
42+
application is exposed to the internet. It is a best practice to generate the value of
43+
most secrets using a secure hashing algorithm with a good source of entropy - something
44+
like `ssh-keygen`.
45+
46+
- `JWT_SECRET_KEY` (required): This is a secret used to sign JWTs issued by
47+
`accessmap-api` and is the most important secret to get correct, as JWTs will be
48+
publicly sent to clients (over HTTPS). It must be kept private and generated using best
49+
practices (like `ssh-keygen`).
50+
51+
- `SQLALCHEMY_DATABASE_URI`: An SQLAlchemy-compatible database URI. This defauls to
52+
an SQLite3 database stored at `/tmp/accessmap-api.db` (for development), but can be
53+
any valid SQLAlchemy URI for postgres, mysql, etc.
54+
55+
- `OAUTH_CACHE_DIR`: A path used to cache OAuth data - i.e. user-authorized
56+
token information. This makes auth workflows more efficient. This defaults to
57+
`/tmp/accessmap-api-cache`.
58+
59+
- `OSM_CLIENT_ID` (required): AccessMap currently uses OpenStreetMap for
60+
authentication. This is the OAuth 1.0a client ID of your registered application.
61+
62+
- `OSM_CLIENT_SECRET` (required): AccessMap currently uses OpenStreetMap for
63+
authentication. This is the OAuth 1.0a client secret of your registered application.
64+
65+
- `OSM_URI`: The base API path to use when talking to OpenStreetMap for authentication.
66+
It is important to use the testing/development server(s) when trying out new features
67+
that might impact the data on OpenStreetMap or expose a client's credentials, so this
68+
is set to the primary OpenStreetMap testing URI by default. For production applications
69+
using HTTPS and secure secrets, use `https://api.openstreetmap.org`. Keep in mind that
70+
you will need to separately register OAuth 1.0a applications for the testing
71+
OpenStreetMap API vs. the main one, so they will have different `OSM_CLIENT_ID` and
72+
`OSM_CLIENT_SECRET` credentials.
73+
74+
- `CONSUMER_CALLBACK_URI` (required): as a security precaution, this API will currently
75+
only send OAuth callback redirects to a URI (URL) defined by this environment variable.
76+
The callback URI defined by this variable will be appended with `access_token` and
77+
`refresh_token` URL parameters defining a JWT access token and a JWT refresh token for
78+
use with protected `accessmap-api` endpoints. `CONSUMER_CALLBACK_URI` should therefore
79+
be a client URI such as an instance of `accessmap-webapp`, e.g.
80+
`http://localhost:3000/callback` in development mode.
81+
82+
## Creating and migrating the database
83+
84+
`accessmap-api` uses `Flask-Migrate`, which uses the `alembic` library to manage
85+
database migrations. To initialize a database, run:
86+
87+
poetry run flask db upgrade
88+
89+
If you make changes to the database and need to create a new migration, run:
90+
91+
poetry run flask db migrate
92+
93+
## Running `accessmap-api`
94+
95+
By default, `accessmap-api` runs a `werkzeug` development server. For a production
96+
system, you should use a WSGI framework. `accessmap-api` comes with a script to assist
97+
deployments using a WSGI runner: `wsgi.py`. It is important to note that it is
98+
currently hard-coded to assume that you are running `accessmap-api` at the `/api`
99+
subdirectory of your production host, for example `https://example.com/api`.

accessmapapi/__init__.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,62 @@
22

33
from dotenv import load_dotenv
44
from flask import Flask
5-
from flask_sqlalchemy import SQLAlchemy
5+
from flask_migrate import Migrate
66

77
from . import auth
88
from . import jwt
9-
from .models import init_app as db_init_app
9+
from .exceptions import MissingConfigError
10+
from .models import db, init_app as db_init_app
1011
from . import blueprints
1112

1213

14+
REQUIRED = [
15+
"SECRET_KEY",
16+
"SQLALCHEMY_DATABASE_URI",
17+
"OAUTH_CACHE_DIR",
18+
"JWT_SECRET_KEY",
19+
"OSM_CLIENT_ID",
20+
"OSM_CLIENT_SECRET",
21+
"OSM_URI",
22+
"CONSUMER_CALLBACK_URI"
23+
]
24+
25+
26+
DEFAULTS = {
27+
"SQLALCHEMY_DATABASE_URI": "sqlite:////tmp/accessmap-api.db",
28+
"OAUTH_CACHE_DIR": "/tmp/accessmap-api-cache",
29+
"OSM_URI": "https://master.apis.dev.openstreetmap.org/"
30+
}
31+
32+
1333
def create_app():
1434
app = Flask(__name__)
1535

1636
# Config
1737
# TODO: do checks on env variable inputs
1838
load_dotenv() # TODO: make optional / part of dev only? Try/except/fail?
1939
app.config.from_mapping(
20-
SECRET_KEY=os.getenv("FLASK_SECRET"),
21-
SQLALCHEMY_DATABASE_URI=os.getenv("SQLALCHEMY_DATABASE_URI"),
40+
SECRET_KEY=os.getenv("SECRET_KEY"),
41+
SQLALCHEMY_DATABASE_URI=os.getenv("SQLALCHEMY_DATABASE_URI", DEFAULTS["SQLALCHEMY_DATABASE_URI"]),
2242
SQLALCHEMY_TRACK_MODIFICATIONS=False,
23-
OAUTH_CACHE_DIR=os.getenv("OAUTH_CACHE_DIR"),
24-
JWT_SECRET_KEY=os.getenv("JWT_SECRET"),
43+
OAUTH_CACHE_DIR=os.getenv("OAUTH_CACHE_DIR", DEFAULTS["OAUTH_CACHE_DIR"]),
44+
JWT_SECRET_KEY=os.getenv("JWT_SECRET_KEY"),
2545
JWT_IDENTITY_CLAIM="sub",
2646
OSM_CLIENT_ID=os.getenv("OSM_CLIENT_ID"),
2747
OSM_CLIENT_SECRET=os.getenv("OSM_CLIENT_SECRET"),
28-
OSM_URI=os.getenv("OSM_URI"),
48+
OSM_URI=os.getenv("OSM_URI", DEFAULTS["OSM_URI"]),
2949
CONSUMER_CALLBACK_URI=os.getenv("CONSUMER_CALLBACK_URI")
3050
)
51+
for env_var in REQUIRED:
52+
env = app.config.get(env_var, None)
53+
if env is None:
54+
raise MissingConfigError("{} environment variable not set.".format(env_var))
3155

3256
# Attach database
33-
db_init_app(app)
57+
db.init_app(app)
58+
59+
# Attach migration scripts (Alembic / Flask-Migrate)
60+
migrate = Migrate(app, db)
3461

3562
# Add oauth interface
3663
auth.init_app(app)

accessmapapi/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Exceptions for accessmap-api."""
2+
3+
4+
class MissingConfigError(Exception):
5+
pass

accessmapapi/models.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,7 @@
99
db = SQLAlchemy()
1010

1111

12-
def init_db():
13-
db.create_all()
14-
15-
16-
@click.command("init-db")
17-
@with_appcontext
18-
def init_db_command():
19-
init_db()
20-
click.echo("Initialized the database.")
21-
22-
23-
def init_app(app):
12+
def init_app(app, db):
2413
app.cli.add_command(init_db_command)
2514
db.init_app(app)
2615

migrations/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

migrations/alembic.ini

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# template used to generate migration files
5+
# file_template = %%(rev)s_%%(slug)s
6+
7+
# set to 'true' to run the environment during
8+
# the 'revision' command, regardless of autogenerate
9+
# revision_environment = false
10+
11+
12+
# Logging configuration
13+
[loggers]
14+
keys = root,sqlalchemy,alembic
15+
16+
[handlers]
17+
keys = console
18+
19+
[formatters]
20+
keys = generic
21+
22+
[logger_root]
23+
level = WARN
24+
handlers = console
25+
qualname =
26+
27+
[logger_sqlalchemy]
28+
level = WARN
29+
handlers =
30+
qualname = sqlalchemy.engine
31+
32+
[logger_alembic]
33+
level = INFO
34+
handlers =
35+
qualname = alembic
36+
37+
[handler_console]
38+
class = StreamHandler
39+
args = (sys.stderr,)
40+
level = NOTSET
41+
formatter = generic
42+
43+
[formatter_generic]
44+
format = %(levelname)-5.5s [%(name)s] %(message)s
45+
datefmt = %H:%M:%S

migrations/env.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from __future__ import with_statement
2+
3+
import logging
4+
from logging.config import fileConfig
5+
6+
from sqlalchemy import engine_from_config
7+
from sqlalchemy import pool
8+
9+
from alembic import context
10+
11+
# this is the Alembic Config object, which provides
12+
# access to the values within the .ini file in use.
13+
config = context.config
14+
15+
# Interpret the config file for Python logging.
16+
# This line sets up loggers basically.
17+
fileConfig(config.config_file_name)
18+
logger = logging.getLogger('alembic.env')
19+
20+
# add your model's MetaData object here
21+
# for 'autogenerate' support
22+
# from myapp import mymodel
23+
# target_metadata = mymodel.Base.metadata
24+
from flask import current_app
25+
config.set_main_option('sqlalchemy.url',
26+
current_app.config.get('SQLALCHEMY_DATABASE_URI'))
27+
target_metadata = current_app.extensions['migrate'].db.metadata
28+
29+
# other values from the config, defined by the needs of env.py,
30+
# can be acquired:
31+
# my_important_option = config.get_main_option("my_important_option")
32+
# ... etc.
33+
34+
35+
def run_migrations_offline():
36+
"""Run migrations in 'offline' mode.
37+
38+
This configures the context with just a URL
39+
and not an Engine, though an Engine is acceptable
40+
here as well. By skipping the Engine creation
41+
we don't even need a DBAPI to be available.
42+
43+
Calls to context.execute() here emit the given string to the
44+
script output.
45+
46+
"""
47+
url = config.get_main_option("sqlalchemy.url")
48+
context.configure(
49+
url=url, target_metadata=target_metadata, literal_binds=True
50+
)
51+
52+
with context.begin_transaction():
53+
context.run_migrations()
54+
55+
56+
def run_migrations_online():
57+
"""Run migrations in 'online' mode.
58+
59+
In this scenario we need to create an Engine
60+
and associate a connection with the context.
61+
62+
"""
63+
64+
# this callback is used to prevent an auto-migration from being generated
65+
# when there are no changes to the schema
66+
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
67+
def process_revision_directives(context, revision, directives):
68+
if getattr(config.cmd_opts, 'autogenerate', False):
69+
script = directives[0]
70+
if script.upgrade_ops.is_empty():
71+
directives[:] = []
72+
logger.info('No changes in schema detected.')
73+
74+
connectable = engine_from_config(
75+
config.get_section(config.config_ini_section),
76+
prefix='sqlalchemy.',
77+
poolclass=pool.NullPool,
78+
)
79+
80+
with connectable.connect() as connection:
81+
context.configure(
82+
connection=connection,
83+
target_metadata=target_metadata,
84+
process_revision_directives=process_revision_directives,
85+
**current_app.extensions['migrate'].configure_args
86+
)
87+
88+
with context.begin_transaction():
89+
context.run_migrations()
90+
91+
92+
if context.is_offline_mode():
93+
run_migrations_offline()
94+
else:
95+
run_migrations_online()

migrations/script.py.mako

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
${imports if imports else ""}
11+
12+
# revision identifiers, used by Alembic.
13+
revision = ${repr(up_revision)}
14+
down_revision = ${repr(down_revision)}
15+
branch_labels = ${repr(branch_labels)}
16+
depends_on = ${repr(depends_on)}
17+
18+
19+
def upgrade():
20+
${upgrades if upgrades else "pass"}
21+
22+
23+
def downgrade():
24+
${downgrades if downgrades else "pass"}

0 commit comments

Comments
 (0)