Skip to content

Commit 570b941

Browse files
madolearymichielbdejong
authored andcommitted
make user-defined network from db
implements custom filtering for user service customized auth to work with phoenix fixes bug in non-incognito browsers creates user in h at moment of auth if it does not exist canonical determination of authority Create .gitlab-ci.yml Add proxy prefix Add hypothesis base url to assets fetches user based on default authority, not hard-coded authority hack to authenticate user a second time when building session replaces request.authenticated_userid client expects userid, not username resolves root session issue in security policy, removes hack solution in session model dynamically manages tosdr url via store for dev env (to-do : staging, prod) adds tosdr base domain to client settings removes print statement fixes error that is interpreting every h request as api request Update .gitlab-ci.yml exposes es with a network queries annotations in es based on strict uri determines asset path based on env forces username compliance for tosdr users created in h Build stable image relies on phoenix docker-compose for db, es
1 parent 67d1ad9 commit 570b941

File tree

14 files changed

+119
-40
lines changed

14 files changed

+119
-40
lines changed

.gitlab-ci.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# You can override the included template(s) by including variable overrides
2+
# SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings
3+
# Secret Detection customization: https://docs.gitlab.com/ee/user/application_security/secret_detection/#customizing-settings
4+
# Dependency Scanning customization: https://docs.gitlab.com/ee/user/application_security/dependency_scanning/#customizing-the-dependency-scanning-settings
5+
# Container Scanning customization: https://docs.gitlab.com/ee/user/application_security/container_scanning/#customizing-the-container-scanning-settings
6+
# Note that environment variables can be set in several places
7+
# See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence
8+
9+
stages:
10+
- publish
11+
12+
13+
docker-nightly:
14+
stage: publish
15+
image:
16+
name: gcr.io/kaniko-project/executor:v1.9.0-debug
17+
entrypoint:
18+
- ''
19+
script:
20+
- /kaniko/executor --context "${CI_PROJECT_DIR}" --dockerfile "${CI_PROJECT_DIR}/Dockerfile"
21+
--destination "${CI_REGISTRY_IMAGE}:nightly"
22+
rules:
23+
- if: $CI_COMMIT_BRANCH == "phoenix-integration"
24+
25+
26+
docker-prod:
27+
stage: publish
28+
image:
29+
name: gcr.io/kaniko-project/executor:v1.9.0-debug
30+
entrypoint:
31+
- ''
32+
script:
33+
- /kaniko/executor --context "${CI_PROJECT_DIR}" --dockerfile "${CI_PROJECT_DIR}/Dockerfile"
34+
--destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}" --destination "${CI_REGISTRY_IMAGE}:stable"
35+
rules:
36+
- if: $CI_COMMIT_TAG

conf/app.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use: call:h.app:create_app
88

99
[filter:proxy-prefix]
1010
use: egg:PasteDeploy#prefix
11+
prefix = /hypothesis
12+
1113

1214
[server:main]
1315
use: egg:gunicorn#main

conf/development-app.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ h.bouncer_url: http://localhost:8000
1414
h.client_rpc_allowed_origins: http://localhost:8001 https://localhost:48001
1515
h.client_url: {current_scheme}://{current_host}:3001/hypothesis
1616
h.websocket_url: ws://localhost:5001/ws
17+
h.tosdr: http://localhost:9090
1718

1819
h.debug: True
1920
h.reload_assets: True

docker-compose.yml

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
version: '3'
22
services:
3-
postgres:
4-
image: postgres:11.5-alpine
5-
ports:
6-
- '127.0.0.1:5432:5432'
7-
healthcheck:
8-
test: ["CMD", "pg_isready", "-U", "postgres"]
9-
interval: 1s
10-
networks:
11-
- dbs
12-
elasticsearch:
13-
image: hypothesis/elasticsearch:latest
14-
ports:
15-
- '127.0.0.1:9200:9200'
16-
environment:
17-
- discovery.type=single-node
183
rabbit:
194
image: rabbitmq:3.6-management-alpine
205
ports:
@@ -26,4 +11,9 @@ networks:
2611
# To avoid having unnecessary dependencies between the projects
2712
# the network is created with `docker network crate dbs` in each project's Makefile (make services)
2813
dbs:
14+
name: dbs
15+
external: true
16+
elasticsearch:
17+
name: elasticsearch
2918
external: true
19+

h/assets.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
"""View for serving static assets under `/assets`."""
22

3+
import os
34
import importlib_resources
45
from h_assets import Environment, assets_view
56
from pyramid.settings import asbool
67

78

9+
810
def includeme(config):
911
auto_reload = asbool(config.registry.settings.get("h.reload_assets", False))
1012
h_files = importlib_resources.files("h")
1113

14+
asset_path = "/hypothesis/assets"
15+
if "ASSET_PATH" in os.environ:
16+
asset_path = os.environ["ASSET_PATH"]
17+
1218
assets_env = Environment(
13-
assets_base_url="/assets",
19+
assets_base_url=asset_path,
1420
bundle_config_path=h_files / "assets.ini",
1521
manifest_path=h_files / "../build/manifest.json",
1622
auto_reload=auto_reload,

h/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def configure(environ=None, settings=None): # pylint: disable=too-many-statemen
2828
environ = os.environ
2929
if settings is None:
3030
settings = {}
31+
3132
settings_manager = SettingsManager(settings, environ)
3233
# Configuration for external components
3334
settings_manager.set("broker_url", "BROKER_URL")
@@ -87,6 +88,9 @@ def configure(environ=None, settings=None): # pylint: disable=too-many-statemen
8788
# secret.
8889
settings_manager.set("h.client_oauth_id", "CLIENT_OAUTH_ID")
8990

91+
# Base domain for tosdr
92+
settings_manager.set("h.tosdr", "TOSDR_URL")
93+
9094
# The list of origins that the client will respond to cross-origin RPC
9195
# requests from.
9296
settings_manager.set(

h/search/query.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,16 @@ def __call__(self, search, params):
272272
[u for u in wildcard_uris if wildcard_uri_is_valid(u)],
273273
normalize_method=self._wildcard_uri_normalized,
274274
)
275+
strict_uri = uris[0]
275276
uris = self._normalize_uris(uris)
276277

277278
queries = []
278279
if wildcard_uris:
279280
queries = [Q("wildcard", **{"target.scope": u}) for u in wildcard_uris]
280281
if uris:
281282
queries.append(Q("terms", **{"target.scope": uris}))
282-
return search.query("bool", should=queries)
283+
# TOSDR
284+
return search.query(Q("bool", must=[Q("match", uri=strict_uri)]))
283285

284286
def _normalize_uris(self, query_uris, normalize_method=uri.normalize):
285287
uris = set()

h/security/policy/combined.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def _call_sub_policies(self, method, request, *args, **kwargs):
7474
return getattr(self._ui_policy, method)(request, *args, **kwargs)
7575

7676
# Then we try the bearer header (or `access_token` GET param)
77+
7778
result = getattr(self._bearer_token_policy, method)(request, *args, **kwargs)
7879

7980
if not result and self._http_basic_auth_policy.handles(request):
@@ -87,7 +88,9 @@ def _call_sub_policies(self, method, request, *args, **kwargs):
8788

8889
@staticmethod
8990
def _is_api_request(request):
90-
return request.path.startswith("/api") and request.path not in [
91+
return (request.path.startswith("/api") or request.path.startswith("/hypothesis/api")) and request.path not in [
9192
"/api/token",
9293
"/api/badge",
94+
"/hypothesis/api/token",
95+
"/hypothesis/api/badge"
9396
]

h/services/user.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sqlalchemy as sa
2+
from sqlalchemy.sql import text
23

34
from h.models import User, UserIdentity
45
from h.util.db import on_transaction_end
@@ -50,6 +51,7 @@ def fetch(self, userid_or_username, authority=None):
5051
:rtype: h.models.User or None
5152
5253
"""
54+
5355
if authority is not None:
5456
username = userid_or_username
5557
else:
@@ -65,12 +67,19 @@ def fetch(self, userid_or_username, authority=None):
6567
self._cache[cache_key] = (
6668
self.session.query(User)
6769
.filter_by(username=username)
68-
.filter_by(authority=authority)
70+
# comment for tosdr
71+
# .filter_by(authority=authority)
6972
.one_or_none()
7073
)
7174

7275
return self._cache[cache_key]
7376

77+
def fetch_from_tosdr(self, h_key):
78+
statement = text("SELECT * FROM users WHERE h_key =:x")
79+
statement = statement.bindparams(x=h_key)
80+
user_tosdr = self.session.execute(statement).one_or_none()
81+
return user_tosdr
82+
7483
def fetch_all(self, userids):
7584
"""
7685
Fetch a list of users by their userids.

h/session.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@ def profile(request, authority=None):
2929
authority = user.authority
3030
else:
3131
authority = authority or request.default_authority
32+
33+
userid = request.authenticated_userid
34+
35+
if not userid:
36+
userid = user.userid
3237

3338
return dict(
3439
{
35-
"userid": request.authenticated_userid,
40+
"userid": userid,
3641
"authority": authority,
3742
"groups": _current_groups(request, authority),
3843
"features": request.feature.all(),

0 commit comments

Comments
 (0)