feat: add NEXT_PUBLIC_API_PREFIX for reverse-proxy support#109
Open
thijs-s wants to merge 1 commit intorustfs:mainfrom
Open
feat: add NEXT_PUBLIC_API_PREFIX for reverse-proxy support#109thijs-s wants to merge 1 commit intorustfs:mainfrom
thijs-s wants to merge 1 commit intorustfs:mainfrom
Conversation
Allow the console to be deployed behind a reverse proxy at a path prefix (e.g. /rustfs/api) by adding a NEXT_PUBLIC_API_PREFIX build-time env var. When set, the prefix is added to: - siteConfig.api.baseURL (lib/config.ts, lib/config-helpers.ts) so the custom AwsClient sends admin requests to the prefixed origin. - The wire URL of every AWS SDK request via a finalizeRequest middleware in lib/api-prefix-middleware.ts, registered in S3Provider and STS. The SigV4 signer (lib/aws4fetch.ts) strips the prefix from the canonical URI BEFORE computing the canonical string. The wire request keeps the prefix; the signature is computed against the un-prefixed path. A reverse proxy strips the prefix before forwarding to rustfs, which then verifies the signature against the same un-prefixed path. No server-side change is required. Defaults to empty (backward-compatible). Documented in .env.example. Also refactored module-level API_PREFIX constants into getApiPrefix() accessor functions in the three relevant files so process.env stubbing in tests is straightforward. Tests: - tests/lib/api-prefix-middleware.test.ts - tests/lib/aws4fetch.prefix.test.ts - tests/lib/config.prefix.test.ts
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request
Description
Adds a build-time env var
NEXT_PUBLIC_API_PREFIXthat lets the console operate behind a reverse proxy at a path prefix (e.g./rustfs/api). When set:siteConfig.api.baseURL(inlib/config.tsandlib/config-helpers.ts)is prefixed, so the custom
AwsClientsends admin requests to${origin}${API_PREFIX}/rustfs/admin/v3.by a
finalizeRequestmiddleware in a newlib/api-prefix-middleware.ts,registered in
S3Provider(contexts/s3-context.tsx) and the STS clientfactory (
lib/sts.ts). The middleware runs after the SDK's authmiddleware signs the request, so the signature is computed against the
un-prefixed canonical URI.
lib/aws4fetch.tsstrips the prefix fromthis.url.pathnamebefore it buildsthis.encodedPath(thecanonical URI used in the signature). The wire request still carries
the prefix; only the canonical-string-being-signed has it stripped.
A reverse proxy that strips the prefix before forwarding to rustfs
(e.g.
nginx rewrite ^/rustfs/api/(.*) /$1 break;) forwards a requestwhose signature matches what an unmodified rustfs server verifies. No
server-side change is required. Default is empty, so existing
deployments are unaffected.
Also refactored module-level
API_PREFIXconstants in three files intogetApiPrefix()accessor functions soprocess.envis read on eachcall rather than at module load — cleaner for tests and zero runtime
cost.
Related: see #108 for the full motivation, alternatives, and
SigV4 contract diagram. The patch was validated end-to-end against a
real rustfs
1.0.0-alpha.99deployed behind nginx with the rewriterule above (login + STS
AssumeRole+ bucket list + path-styleobject operations + multipart upload all succeed).
Type of Change
Testing
Three new test files under
tests/lib/, written in the samenode:teststyle as
bucket-cors.test.tsanderror-handler.test.ts:tests/lib/api-prefix-middleware.test.ts— middleware registersrelation: "after" / toMiddleware: "awsAuthMiddleware", prepends theprefix to
request.path, normalizes/, no-ops when env is empty,is idempotent on re-invocation. (5 tests)
tests/lib/aws4fetch.prefix.test.ts—AwsV4Signer.encodedPathisstripped of the prefix when set (root, path-style bucket+key);
unchanged when empty (backward-compat) or when path doesn't start
with the prefix;
this.url.pathname(the wire URL) stays untouched;trailing slash on the prefix is normalized. (6 tests)
tests/lib/config.prefix.test.ts—loadRuntimeConfigandcreateDefaultConfigcomposeapi.baseURLwith the prefix and leaves3.endpointclean; backward-compat path unchanged when empty;trailing slash normalized. (5 tests)
Plus a manual end-to-end run against a real rustfs
1.0.0-alpha.99behind nginx (login flow /
ListBuckets/ListObjectsV2/CreateMultipartUpload+UploadPart+CompleteMultipartUploadallsucceed; SigV4 signatures verify on the server side).