Skip to content

Commit

Permalink
gateway/lambda_function: redirect to documentation for crawlers
Browse files Browse the repository at this point in the history
just surfing to https://rpmrepo.osbuild.org/ what also
crawlers do, should result in something other than a 4xx error.
  • Loading branch information
schuellerf committed Nov 21, 2024
1 parent 23cbca7 commit c089d9d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ help:
@echo
@echo " help: Print this usage information."
@echo " snapshot-configs: Regenerate all snapshot configs from definitions."
@echo " test: Run unit-tests."

$(BUILDDIR)/:
$(BIN_MKDIR) -p "$@"
Expand Down Expand Up @@ -124,3 +125,7 @@ gateway-zip: $(BUILDDIR)/gateway/rpmrepo-gateway-main.zip
snapshot-configs:
rm -f $(SRCDIR)/repo/*.json
./gen-all-repos.py --definitions $(SRCDIR)/repo-definitions.yaml --output $(SRCDIR)/repo/

.PHONY: test
test:
pytest src/gateway/lambda_function.py
22 changes: 22 additions & 0 deletions src/gateway/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"rhvpn": "https://rpmrepo-storage.bucket.vpce-08d3201af28373567-o10c2v4q.s3.us-east-1.vpce.amazonaws.com/data/rhvpn",
}

_documentation_url = "https://osbuild.org/docs/developer-guide/projects/rpmrepo/"


def _error(code=500):
"""Synthesize API Error Reply"""
Expand Down Expand Up @@ -72,6 +74,9 @@ def _parse_proxy(stage, proxy):
# arguments (this also means you cannot have trailing slashes for now,
# but API-Gateway drops those silently, anyway).

if proxy is None:
return None

elements = proxy.split("/")
if len(elements) < 1:
return None
Expand Down Expand Up @@ -336,6 +341,8 @@ def lambda_handler(event, _context):
if request is None:
if proxy == "robots.txt":
return _success(_robots_txt)
if proxy is None or proxy in ["", "/"]:
return _redirect(_documentation_url)
return _error(400)

if "enumerate" in request:
Expand Down Expand Up @@ -691,3 +698,18 @@ def test_psi():
)
assert r["statusCode"] == 301
assert r["headers"]["Location"] == "https://rhos-d.infra.prod.upshift.rdu2.redhat.com:13808/v1/AUTH_95e858620fb34bcc9162d9f52367a560/manifestdb/rpmrepo/rpm/a/Packages/c/d"


def test_basics():
"""Tests for the basic redirects"""
documentation_events = [{"pathParameters": {"proxy": "/"}},
{"pathParameters": {"proxy": ""}},
{}]

for event in documentation_events:
r = lambda_handler(event
,
None,
)
assert r["statusCode"] == 301
assert r["headers"]["Location"] == _documentation_url

0 comments on commit c089d9d

Please sign in to comment.