Skip to content

Commit

Permalink
Improve environment variables naming
Browse files Browse the repository at this point in the history
  • Loading branch information
jirik committed Oct 31, 2019
1 parent af6ae39 commit bdb404a
Show file tree
Hide file tree
Showing 25 changed files with 98 additions and 101 deletions.
7 changes: 4 additions & 3 deletions .env.demo
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ LAYMAN_REDIS_URL=redis://redis:6379/0
# skip importing data into Redis
LAYMAN_SKIP_REDIS_LOADING=false

# docker names
LAYMAN_DOCKER_MAIN_SERVICE=layman
LAYMAN_CLIENT_DOCKER_SERVICE=layman_client
# Flask-style server name (domain and port)
LAYMAN_SERVER_NAME=layman:8000

# internal settings
LAYMAN_PRELOAD_MODULES=false
LAYMAN_TIMGEN_URL=http://hslayers:8080/
LAYMAN_CLIENT_URL=http://layman_client:3000/client/

# client version
LAYMAN_CLIENT_VERSION=213ffaefda53f514da16bd8f850109fd701b51d7
Expand Down
7 changes: 4 additions & 3 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ LAYMAN_REDIS_URL=redis://redis:6379/0
# skip importing data into Redis
LAYMAN_SKIP_REDIS_LOADING=false

# docker names
LAYMAN_DOCKER_MAIN_SERVICE=layman_dev
LAYMAN_CLIENT_DOCKER_SERVICE=layman_client
# Flask-style server name (domain and port)
LAYMAN_SERVER_NAME=layman_dev:8000

# internal settings
LAYMAN_PRELOAD_MODULES=true
LAYMAN_TIMGEN_URL=http://hslayers:8080/
LAYMAN_CLIENT_URL=http://layman_client:3000/client/

# client version
LAYMAN_CLIENT_VERSION=213ffaefda53f514da16bd8f850109fd701b51d7
Expand Down
7 changes: 4 additions & 3 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ LAYMAN_REDIS_URL=redis://redis:6379/15
# skip importing data into Redis
LAYMAN_SKIP_REDIS_LOADING=false

# docker names
LAYMAN_DOCKER_MAIN_SERVICE=layman_test_run_1
LAYMAN_CLIENT_DOCKER_SERVICE=layman_client_test
# Flask-style server name (domain and port)
LAYMAN_SERVER_NAME=layman_test_run_1:8000

# internal settings
LAYMAN_PRELOAD_MODULES=false
LAYMAN_TIMGEN_URL=http://hslayers:8080/
LAYMAN_CLIENT_URL=http://layman_client_test:3000/client/

# client version
LAYMAN_CLIENT_VERSION=213ffaefda53f514da16bd8f850109fd701b51d7
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ Files at all three levels are suffixed with strings that indicates what they are

When you are switching between different contexts (e.g. between demo and dev), always check that you are using settings intended for your context, especially
- `.env*` file (check `env_file` properties in `docker-compose.*.yml` file)
- `layman_settings*` file (check `LAYMAN_SETTINGS_MODULE` environment variable in `env*` file)
- `layman_settings*` file (check [LAYMAN_SETTINGS_MODULE](doc/env-settings.md#LAYMAN_SETTINGS_MODULE) environment variable in `env*` file)

Also, anytime you change `.env` file, remember to rebuild docker images as some environemnt variables affect build stage of docker images. Particularly these environment settings:
- UID_GID
- LAYMAN_GS_HOST
- LAYMAN_GS_PORT
- LAYMAN_DOCKER_MAIN_SERVICE
- LAYMAN_SERVER_NAME
- LAYMAN_CLIENT_VERSION


Expand Down Expand Up @@ -157,10 +157,10 @@ Configure Layman using [environment settings](doc/env-settings.md). Demo configu
- FLASK_SECRET_KEY

Last, start layman and necessary services:
- hslayers using npm
- layman client using npm
- layman using you deployment server
- celery worker using python
- thumbnail image generator (TIMGEN, also referred to as hslayers) using npm (see startup command of `hslayers` docker-compose service)
- Layman client using npm (see startup command of `layman_client` docker-compose service)
- Layman using your deployment server (see startup command of `layman` docker-compose service)
- Layman celery worker using python (see startup command of `celery_worker` docker-compose service)

## Run in development
Suitable for **development only**.
Expand Down
24 changes: 18 additions & 6 deletions doc/env-settings.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
# Environment Settings

## LAYMAN_GS_HOST
URL host of GeoServer instance.

## LAYMAN_GS_PORT
URL port of GeoServer instance.

## LAYMAN_GS_USER
Name of [GeoServer user](https://docs.geoserver.org/stable/en/user/security/webadmin/ugr.html#add-user) that Layman uses for authentication and communication with GeoServer. The LAYMAN_GS_USER must be another user than default `admin` user. The LAYMAN_GS_USER user must have at least the [LAYMAN_GS_ROLE](#LAYMAN_GS_ROLE) and default [`ADMIN`](https://docs.geoserver.org/stable/en/user/security/usergrouprole/roleservices.html#mapping-roles-to-system-roles) role (defined by `adminRoleName`).

## LAYMAN_GS_ROLE
Name of [GeoServer role](https://docs.geoserver.org/stable/en/user/security/webadmin/ugr.html#edit-role-service) of [LAYMAN_GS_USER](#LAYMAN_GS_USER). The role is used to create explicit [access rule](https://docs.geoserver.org/stable/en/user/security/layer.html) for all layers published by Layman. The LAYMAN_GS_ROLE must be another role than default [`ADMIN`](https://docs.geoserver.org/stable/en/user/security/usergrouprole/roleservices.html#mapping-roles-to-system-roles) role (defined by `adminRoleName`)! See default development configuration of [roles](deps/geoserver/sample/geoserver_data/security/role/default/roles.xml) and [layer access rights](deps/geoserver/sample/geoserver_data/security/layers.properties).

## LAYMAN_PG_USER
Name of [PostgreSQL user](https://www.postgresql.org/docs/9.5/sql-createuser.html) that Layman uses for authentication and communication with PostgreSQL. The user needs enough privileges to create new schemas in [LAYMAN_PG_DBNAME](#LAYMAN_PG_DBNAME) database. The LAYMAN_PG_USER must be another user than default `postgres` user! The user also needs access to `public` schema where PostGIS must be installed.

## LAYMAN_PG_DBNAME
## LAYMAN_PG_DBNAME
Name of [PostgreSQL database](https://www.postgresql.org/docs/9.5/sql-createdatabase.html) in which Layman publishes layer vector data.

## LAYMAN_REDIS_URL
## LAYMAN_PG_USER
Name of [PostgreSQL user](https://www.postgresql.org/docs/9.5/sql-createuser.html) that Layman uses for authentication and communication with PostgreSQL. The user needs enough privileges to create new schemas in [LAYMAN_PG_DBNAME](#LAYMAN_PG_DBNAME) database. The LAYMAN_PG_USER must be another user than default `postgres` user! The user also needs access to `public` schema where PostGIS must be installed.

## LAYMAN_REDIS_URL
URL of [Redis logical database](https://redis.io/commands/select) including database number where Layman stores internal data about publications, users, etc.

## LTC_REDIS_URL
## LAYMAN_SETTINGS_MODULE
Dotted path to a Python module with Layman settings for Python level.

## LTC_REDIS_URL
URL of [Redis logical database](https://redis.io/commands/select) including database number where Layman Test Client stores user sessions including authentication credentials.

## UID_GID
String with unix-like user identifier and group identifier `<UID>:<GID>`, e.g. `1000:1000`. Suitable for [mounting some volumes as non-root user](./../README.md#mount-some-volumes-as-non-root-user).

2 changes: 1 addition & 1 deletion docker-compose.demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ services:
args:
- LAYMAN_GS_HOST=${LAYMAN_GS_HOST}
- LAYMAN_GS_PORT=${LAYMAN_GS_PORT}
- LAYMAN_DOCKER_MAIN_SERVICE=${LAYMAN_DOCKER_MAIN_SERVICE}
- LAYMAN_SERVER_NAME=${LAYMAN_SERVER_NAME}
image: hslayers:latest
user: ${UID_GID}
command: sh -c "node proxy.js & ./node_modules/.bin/http-server dist/"
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ services:
args:
- LAYMAN_GS_HOST=${LAYMAN_GS_HOST}
- LAYMAN_GS_PORT=${LAYMAN_GS_PORT}
- LAYMAN_DOCKER_MAIN_SERVICE=${LAYMAN_DOCKER_MAIN_SERVICE}
- LAYMAN_SERVER_NAME=${LAYMAN_SERVER_NAME}
image: hslayers:latest
user: ${UID_GID}
command: sh -c "node proxy.js & ./node_modules/.bin/http-server dist/"
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ services:
args:
- LAYMAN_GS_HOST=${LAYMAN_GS_HOST}
- LAYMAN_GS_PORT=${LAYMAN_GS_PORT}
- LAYMAN_DOCKER_MAIN_SERVICE=${LAYMAN_DOCKER_MAIN_SERVICE}
- LAYMAN_SERVER_NAME=${LAYMAN_SERVER_NAME}
image: hslayers:latest
user: ${UID_GID}
command: sh -c "node proxy.js & ./node_modules/.bin/http-server dist/"
Expand Down
6 changes: 3 additions & 3 deletions hslayers/Dockerfile.production
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ RUN npm ci

ARG LAYMAN_GS_HOST
ARG LAYMAN_GS_PORT
ARG LAYMAN_DOCKER_MAIN_SERVICE
ARG LAYMAN_SERVER_NAME

# https://stackoverflow.com/a/42144033
RUN test -n "$LAYMAN_GS_HOST"
RUN test -n "$LAYMAN_GS_PORT"
RUN test -n "LAYMAN_DOCKER_MAIN_SERVICE"
RUN test -n "$LAYMAN_SERVER_NAME"

ENV LAYMAN_GS_HOST $LAYMAN_GS_HOST
ENV LAYMAN_GS_PORT $LAYMAN_GS_PORT
ENV LAYMAN_DOCKER_MAIN_SERVICE $LAYMAN_DOCKER_MAIN_SERVICE
ENV LAYMAN_SERVER_NAME $LAYMAN_SERVER_NAME

RUN npm run build
8 changes: 4 additions & 4 deletions hslayers/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const LAYMAN_GS_HOST=process.env.LAYMAN_GS_HOST;
export const LAYMAN_GS_PORT=process.env.LAYMAN_GS_PORT;
export const LAYMAN_GS_HOSTPORT=`${LAYMAN_GS_HOST}:${LAYMAN_GS_PORT}`;
export const LAYMAN_DOCKER_MAIN_SERVICE=process.env.LAYMAN_DOCKER_MAIN_SERVICE;
export const LAYMAN_GS_HOST = process.env.LAYMAN_GS_HOST;
export const LAYMAN_GS_PORT = process.env.LAYMAN_GS_PORT;
export const LAYMAN_GS_HOSTPORT = LAYMAN_GS_HOST && LAYMAN_GS_HOST ? `${LAYMAN_GS_HOST}:${LAYMAN_GS_PORT}` : '';
export const LAYMAN_SERVER_NAME = process.env.LAYMAN_SERVER_NAME || '';
6 changes: 3 additions & 3 deletions hslayers/src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ const proxify = (requested_url) => {

export const adjust_map_url = (requested_url) => {
requested_url = decodeURIComponent(requested_url);
if(requested_url.startsWith(`http://localhost:8000`)) {
if(config.LAYMAN_SERVER_NAME && requested_url.startsWith(`http://localhost:8000`)) {
const old = requested_url;
requested_url = requested_url.replace(`http://localhost:8000`, `http://${config.LAYMAN_DOCKER_MAIN_SERVICE}:8000`);
requested_url = requested_url.replace(`http://localhost:8000`, `http://${config.LAYMAN_SERVER_NAME}`);
console.log(`replaced map URL ${old} with ${requested_url}`);
}
return proxify(requested_url);
Expand All @@ -64,7 +64,7 @@ export const adjust_map_url = (requested_url) => {

const adjust_layer_url = (requested_url) => {
requested_url = decodeURIComponent(requested_url);
if(requested_url.startsWith(`http://localhost:8600`)) {
if(config.LAYMAN_GS_HOSTPORT && requested_url.startsWith(`http://localhost:8600`)) {
const old = requested_url;
requested_url = requested_url.replace(`http://localhost:8600`, `http://${config.LAYMAN_GS_HOSTPORT}`);
console.log(`replaced layer URL ${old} with ${requested_url}`);
Expand Down
8 changes: 4 additions & 4 deletions src/layman/authn/oauth2/liferay_mock_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def create_server(port, env='development'):
},
'app_config': {
'ENV': env,
'SERVER_NAME': f'{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{port}',
'SESSION_COOKIE_DOMAIN': f'{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{port}',
'SERVER_NAME': f"{settings.LAYMAN_SERVER_NAME.split(':')[0]}:{port}",
'SESSION_COOKIE_DOMAIN': f"{settings.LAYMAN_SERVER_NAME.split(':')[0]}:{port}",
},
'host': '0.0.0.0',
'port': port,
Expand Down Expand Up @@ -61,7 +61,7 @@ def server2():

@pytest.mark.usefixtures('server', 'server2')
def test_mock():
url1 = f'http://{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{PORT1}/rest/test-oauth2/user-profile'
url1 = f"http://{settings.LAYMAN_SERVER_NAME.split(':')[0]}:{PORT1}/rest/test-oauth2/user-profile"
rv = requests.get(url1, headers={
f'{ISS_URL_HEADER}': 'http://localhost:8082/o/oauth2/authorize',
f'{TOKEN_HEADER}': 'Bearer abc'
Expand All @@ -70,7 +70,7 @@ def test_mock():
resp_json = rv.json()
assert resp_json['FLASK_ENV'] == 'development'

url2 = f'http://{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{PORT2}/rest/test-oauth2/user-profile'
url2 = f"http://{settings.LAYMAN_SERVER_NAME.split(':')[0]}:{PORT2}/rest/test-oauth2/user-profile"
rv = requests.get(url2, headers={
f'{ISS_URL_HEADER}': 'http://localhost:8082/o/oauth2/authorize',
f'{TOKEN_HEADER}': 'Bearer abc'
Expand Down
18 changes: 8 additions & 10 deletions src/layman/authn/oauth2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def liferay_mock():
},
'app_config': {
'ENV': 'development',
'SERVER_NAME': f'{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{LIFERAY_PORT}',
'SESSION_COOKIE_DOMAIN': f'{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{LIFERAY_PORT}',
'SERVER_NAME': f"{settings.LAYMAN_SERVER_NAME.split(':')[0]}:{LIFERAY_PORT}",
'SESSION_COOKIE_DOMAIN': f"{settings.LAYMAN_SERVER_NAME.split(':')[0]}:{LIFERAY_PORT}",
},
'host': '0.0.0.0',
'port': LIFERAY_PORT,
Expand All @@ -48,8 +48,6 @@ def liferay_mock():
server.join()


PORT = 8000

num_layers_before_test = 0


Expand All @@ -74,23 +72,23 @@ def unexisting_introspection_url():
@pytest.fixture()
def inactive_token_introspection_url(liferay_mock):
introspection_url = liferay.INTROSPECTION_URL
liferay.INTROSPECTION_URL = f'http://{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{LIFERAY_PORT}/rest/test-oauth2/introspection'
liferay.INTROSPECTION_URL = f"http://{settings.LAYMAN_SERVER_NAME.split(':')[0]}:{LIFERAY_PORT}/rest/test-oauth2/introspection"
yield
liferay.INTROSPECTION_URL = introspection_url


@pytest.fixture()
def active_token_introspection_url(liferay_mock):
introspection_url = liferay.INTROSPECTION_URL
liferay.INTROSPECTION_URL = f'http://{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{LIFERAY_PORT}/rest/test-oauth2/introspection?is_active=true'
liferay.INTROSPECTION_URL = f"http://{settings.LAYMAN_SERVER_NAME.split(':')[0]}:{LIFERAY_PORT}/rest/test-oauth2/introspection?is_active=true"
yield
liferay.INTROSPECTION_URL = introspection_url


@pytest.fixture()
def user_profile_url(liferay_mock):
user_profile_url = liferay.USER_PROFILE_URL
liferay.USER_PROFILE_URL = f'http://{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{LIFERAY_PORT}/rest/test-oauth2/user-profile'
liferay.USER_PROFILE_URL = f"http://{settings.LAYMAN_SERVER_NAME.split(':')[0]}:{LIFERAY_PORT}/rest/test-oauth2/user-profile"
yield
liferay.USER_PROFILE_URL = user_profile_url

Expand All @@ -101,8 +99,8 @@ def client():

app.config['TESTING'] = True
app.config['DEBUG'] = True
app.config['SERVER_NAME'] = f'{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{PORT}'
app.config['SESSION_COOKIE_DOMAIN'] = f'{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{PORT}'
app.config['SERVER_NAME'] = settings.LAYMAN_SERVER_NAME
app.config['SESSION_COOKIE_DOMAIN'] = settings.LAYMAN_SERVER_NAME

with app.app_context() as ctx:
publs_by_type = uuid.check_redis_consistency()
Expand All @@ -115,7 +113,7 @@ def client():
def server():
server = Process(target=app.run, kwargs={
'host': '0.0.0.0',
'port': PORT,
'port': settings.LAYMAN_SERVER_NAME.split(':')[1],
'debug': False,
})
server.start()
Expand Down
8 changes: 3 additions & 5 deletions src/layman/authz/read_everyone_write_owner_auth2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
from layman.authn.oauth2.util import TOKEN_HEADER, ISS_URL_HEADER


PORT = 8000

num_layers_before_test = 0


Expand All @@ -41,16 +39,16 @@ def client():
client = app.test_client()
server = Process(target=app.run, kwargs={
'host': '0.0.0.0',
'port': PORT,
'port': settings.LAYMAN_SERVER_NAME.split(':')[1],
'debug': False,
})
server.start()
time.sleep(1)

app.config['TESTING'] = True
app.config['DEBUG'] = True
app.config['SERVER_NAME'] = f'{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{PORT}'
app.config['SESSION_COOKIE_DOMAIN'] = f'{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{PORT}'
app.config['SERVER_NAME'] = settings.LAYMAN_SERVER_NAME
app.config['SESSION_COOKIE_DOMAIN'] = settings.LAYMAN_SERVER_NAME

with app.app_context() as ctx:
publs_by_type = uuid.check_redis_consistency()
Expand Down
8 changes: 3 additions & 5 deletions src/layman/authz/read_everyone_write_owner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
from layman import uuid


PORT = 8000

num_layers_before_test = 0


Expand All @@ -32,16 +30,16 @@ def client():
client = app.test_client()
server = Process(target=app.run, kwargs={
'host': '0.0.0.0',
'port': PORT,
'port': settings.LAYMAN_SERVER_NAME.split(':')[1],
'debug': False,
})
server.start()
time.sleep(1)

app.config['TESTING'] = True
app.config['DEBUG'] = True
app.config['SERVER_NAME'] = f'{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{PORT}'
app.config['SESSION_COOKIE_DOMAIN'] = f'{settings.LAYMAN_DOCKER_MAIN_SERVICE}:{PORT}'
app.config['SERVER_NAME'] = settings.LAYMAN_SERVER_NAME
app.config['SESSION_COOKIE_DOMAIN'] = settings.LAYMAN_SERVER_NAME

with app.app_context() as ctx:
publs_by_type = uuid.check_redis_consistency()
Expand Down
Loading

0 comments on commit bdb404a

Please sign in to comment.