Skip to content

Commit af28a98

Browse files
phoenix-integration
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 e6aa687 commit af28a98

File tree

14 files changed

+118
-46
lines changed

14 files changed

+118
-46
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/development.ini

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

1718
h.debug: True
1819
h.reload_assets: True

conf/production.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
[loggers]
1315
keys = root, alembic, gunicorn.error, h

docker-compose.yml

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
11
services:
2-
postgres:
3-
image: postgres:15.6-alpine
4-
ports:
5-
- '127.0.0.1:5432:5432'
6-
healthcheck:
7-
test: ["CMD", "pg_isready", "-U", "postgres"]
8-
interval: 1s
9-
environment:
10-
POSTGRES_HOST_AUTH_METHOD: trust
11-
networks:
12-
- dbs
13-
elasticsearch:
14-
image: hypothesis/elasticsearch:latest
15-
ports:
16-
- '127.0.0.1:9200:9200'
17-
healthcheck:
18-
test: curl --fail --silent http://localhost:9200 >/dev/null
19-
interval: 3s
20-
start_period: 1m
21-
environment:
22-
- discovery.type=single-node
232
rabbit:
243
image: rabbitmq:3.12-management-alpine
254
ports:
@@ -31,4 +10,9 @@ networks:
3110
# To avoid having unnecessary dependencies between the projects
3211
# the network is created with `docker network create dbs` in each project's Makefile (make services)
3312
dbs:
13+
name: dbs
14+
external: true
15+
elasticsearch:
16+
name: elasticsearch
3417
external: true
18+

h/assets.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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
@@ -9,8 +10,12 @@ def includeme(config): # pragma: no cover
910
auto_reload = asbool(config.registry.settings.get("h.reload_assets", False))
1011
h_files = importlib_resources.files("h")
1112

13+
asset_path = "/hypothesis/assets"
14+
if "ASSET_PATH" in os.environ:
15+
asset_path = os.environ["ASSET_PATH"]
16+
1217
assets_env = Environment(
13-
assets_base_url="/assets",
18+
assets_base_url=asset_path,
1419
bundle_config_path=h_files / "assets.ini",
1520
manifest_path=h_files / "../build/manifest.json",
1621
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: # pragma: no cover
3030
settings = {}
31+
3132
settings_manager = SettingsManager(settings, environ)
3233
# Configuration for external components
3334
settings_manager.set("broker_url", "BROKER_URL")
@@ -88,6 +89,9 @@ def configure(environ=None, settings=None): # pylint: disable=too-many-statemen
8889
# secret.
8990
settings_manager.set("h.client_oauth_id", "CLIENT_OAUTH_ID")
9091

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

h/search/query.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,16 @@ def __call__(self, search, params):
268268
[u for u in wildcard_uris if wildcard_uri_is_valid(u)],
269269
normalize_method=self._wildcard_uri_normalized,
270270
)
271+
strict_uri = uris[0]
271272
uris = self._normalize_uris(uris)
272273

273274
queries = []
274275
if wildcard_uris:
275276
queries = [Q("wildcard", **{"target.scope": u}) for u in wildcard_uris]
276277
if uris:
277278
queries.append(Q("terms", **{"target.scope": uris}))
278-
return search.query("bool", should=queries)
279+
# TOSDR
280+
return search.query(Q("bool", must=[Q("match", uri=strict_uri)]))
279281

280282
def _normalize_uris(self, query_uris, normalize_method=uri.normalize):
281283
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
@@ -31,10 +31,15 @@ def profile(request, authority=None):
3131
authority = user.authority
3232
else:
3333
authority = authority or request.default_authority
34+
35+
userid = request.authenticated_userid
36+
37+
if not userid:
38+
userid = user.userid
3439

3540
return dict(
3641
{
37-
"userid": request.authenticated_userid,
42+
"userid": userid,
3843
"authority": authority,
3944
"groups": _current_groups(request, authority),
4045
"features": request.feature.all(),

0 commit comments

Comments
 (0)