mirror of
https://github.com/versity/versitygw.git
synced 2026-09-19 06:32:10 +00:00
Gates bucket listing behind an identity policy, lets browsers reach the standalone IAM API, and turns the WebUI into a dashboard for S3, IAM, or both.
**Bucket listing.** `ListBuckets` is now gated by the new `s3:ListAllMyBuckets` action, evaluated against `arn:aws:s3:::*`. The request names no bucket, so only identity policies apply — there is no resource policy to combine with, which is the same shape `CreateBucket` already had, so both now share one identity-only evaluation path. Root and admin bypass it, and backends with no identity-policy layer keep listing as before since their listing is already narrowed to the caller's own buckets. The action is IAM-only and is deliberately absent from the bucket-policy action list.
**Fixed bucket ownership.** The standalone IAM client has no per-user ownership to express — accounts are all plain users, cannot be enumerated, and access is decided by policy rather than ACL — so it now implements `auth.FixedBucketOwner` and every bucket is owned by root. Bucket creation stops resolving an owner, `ListBuckets` returns every bucket to every caller (what they may then do with one stays a per-request policy decision), and the admin `ChangeBucketOwner` reports method-not-supported. Other IAM backends are untouched.
**IAM service CORS.** `--cors-allow-origin` now applies to the `iam` command: it answers preflights and stamps the CORS headers, mirroring back the requested method and headers rather than enumerating the SigV4 header set. Without it no browser can reach the IAM API at all, so setting `--webui` without it falls back to `*` with a warning. The chart gets `iamServer.corsAllowOrigin`.
**WebUI.** New IAM pages for users, roles and OIDC providers, signing IAM/STS query-form requests directly from the browser. Navigation is capability-gated rather than role-gated: on sign-in the session probes the S3, admin and IAM endpoints independently and each page shows only what those credentials actually reach, so one build serves an IAM-only dashboard, an S3-only dashboard, and a combined one. The login page takes an optional IAM endpoint, seeded from the new `--webui-iam-gateways` (chart: `webui.iamGateways`) — never auto-detected, since the IAM service is a separate process. The WebUI can also be hosted by `versitygw iam` itself, for deployments with no S3 gateway behind it.
**The admin API is ignored once an IAM endpoint is in play.** The IAM service is then the user directory and bucket ownership is fixed, which leaves the admin API no job: the session is given no admin endpoint at all, its login field is hidden, `users.html` redirects to its IAM counterpart, and every admin-only surface stays off screen. Dashboard and Buckets remain available to any S3 session in such a deployment, running on the S3 and IAM APIs alone and surfacing each denial per action instead of redirecting.
Also fixes two WebUI bugs: embedded assets went out with a zero modification time and no `Cache-Control`, so browsers treated them as fresh for centuries and an upgraded gateway served new HTML against stale JS — they now revalidate against an ETag; and the login page's advanced-options section clipped its last field, since it animated to a height named in the stylesheet rather than the one it measures now.
**Usage**
IAM-only dashboard, served by the IAM service:
versitygw iam --port :7076 --webui :8080 --cors-allow-origin http://localhost:8080/
IAM + S3, dashboard served by the IAM service — point it at the gateway with `--webui-gateways`, and let the gateway accept the dashboard's origin:
versitygw iam --port :7076 --webui :8080 --webui-gateways http://localhost:7070/ --cors-allow-origin http://localhost:8080/
versitygw --port :7070 --cors-allow-origin http://localhost:8080/ posix /data
IAM + S3, dashboard served by the S3 gateway — point it at the IAM service with `--webui-iam-gateways`, and let the IAM service accept the dashboard's origin:
versitygw --port :7070 --webui :8080 --webui-iam-gateways http://localhost:7076/ posix /data
versitygw iam --port :7076 --cors-allow-origin http://localhost:8080/
152 lines
4.8 KiB
Go
152 lines
4.8 KiB
Go
// Copyright 2026 Versity Software
|
|
// This file is licensed under the Apache License, Version 2.0
|
|
// (the "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing,
|
|
// software distributed under the License is distributed on an
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
// KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
package iammiddleware
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
const testOrigin = "https://webui.example.com"
|
|
|
|
func TestCORSPreflightMirrorsRequest(t *testing.T) {
|
|
resp := corsRequest(t, http.MethodOptions, map[string]string{
|
|
"Origin": "https://some-other.example",
|
|
"Access-Control-Request-Method": "POST",
|
|
"Access-Control-Request-Headers": "authorization,x-amz-date,content-type",
|
|
})
|
|
|
|
// IAM answers a preflight 200 with an empty body, not 204.
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatalf("read body: %v", err)
|
|
}
|
|
if len(body) != 0 {
|
|
t.Fatalf("expected empty preflight body, got %q", body)
|
|
}
|
|
|
|
// The configured origin is returned, not the one the browser sent.
|
|
assertHeader(t, resp, "Access-Control-Allow-Origin", testOrigin)
|
|
assertHeader(t, resp, "Access-Control-Allow-Methods", "POST")
|
|
assertHeader(t, resp, "Access-Control-Allow-Headers", "authorization,x-amz-date,content-type")
|
|
assertHeader(t, resp, "Access-Control-Expose-Headers", corsExposeHeaders)
|
|
assertHeader(t, resp, "Access-Control-Max-Age", corsMaxAge)
|
|
assertHeader(t, resp, "Vary", "Origin")
|
|
}
|
|
|
|
// A preflight without Access-Control-Request-Method is still short-circuited,
|
|
// it just carries no allow-methods.
|
|
func TestCORSPreflightWithoutRequestMethod(t *testing.T) {
|
|
resp := corsRequest(t, http.MethodOptions, map[string]string{
|
|
"Origin": testOrigin,
|
|
})
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
assertHeader(t, resp, "Access-Control-Allow-Origin", testOrigin)
|
|
assertHeader(t, resp, "Access-Control-Max-Age", corsMaxAge)
|
|
assertNoHeader(t, resp, "Access-Control-Allow-Methods")
|
|
assertNoHeader(t, resp, "Access-Control-Allow-Headers")
|
|
}
|
|
|
|
func TestCORSActualRequestOmitsPreflightHeaders(t *testing.T) {
|
|
resp := corsRequest(t, http.MethodPost, map[string]string{
|
|
"Origin": testOrigin,
|
|
})
|
|
|
|
if resp.StatusCode != http.StatusTeapot {
|
|
t.Fatalf("expected the handler to run, got status %d", resp.StatusCode)
|
|
}
|
|
assertHeader(t, resp, "Access-Control-Allow-Origin", testOrigin)
|
|
assertHeader(t, resp, "Access-Control-Expose-Headers", corsExposeHeaders)
|
|
assertHeader(t, resp, "Vary", "Origin")
|
|
// Preflight-only headers must not leak onto an actual response.
|
|
assertNoHeader(t, resp, "Access-Control-Allow-Methods")
|
|
assertNoHeader(t, resp, "Access-Control-Allow-Headers")
|
|
assertNoHeader(t, resp, "Access-Control-Max-Age")
|
|
}
|
|
|
|
// Without an Origin the request is not a browser call: no CORS headers.
|
|
func TestCORSWithoutOriginIsUntouched(t *testing.T) {
|
|
resp := corsRequest(t, http.MethodPost, nil)
|
|
|
|
if resp.StatusCode != http.StatusTeapot {
|
|
t.Fatalf("expected the handler to run, got status %d", resp.StatusCode)
|
|
}
|
|
for _, hdr := range []string{
|
|
"Access-Control-Allow-Origin",
|
|
"Access-Control-Expose-Headers",
|
|
"Access-Control-Max-Age",
|
|
"Vary",
|
|
} {
|
|
assertNoHeader(t, resp, hdr)
|
|
}
|
|
}
|
|
|
|
// An OPTIONS without an Origin is not a preflight and must reach the router.
|
|
func TestCORSOptionsWithoutOriginIsRouted(t *testing.T) {
|
|
resp := corsRequest(t, http.MethodOptions, nil)
|
|
|
|
if resp.StatusCode != http.StatusTeapot {
|
|
t.Fatalf("expected the handler to run, got status %d", resp.StatusCode)
|
|
}
|
|
assertNoHeader(t, resp, "Access-Control-Allow-Origin")
|
|
}
|
|
|
|
func corsRequest(t *testing.T, method string, headers map[string]string) *http.Response {
|
|
t.Helper()
|
|
|
|
app := fiber.New()
|
|
app.Use("*", CORS(testOrigin))
|
|
app.All("/*", func(ctx fiber.Ctx) error {
|
|
return ctx.SendStatus(http.StatusTeapot)
|
|
})
|
|
|
|
req := httptest.NewRequest(method, "/", nil)
|
|
for key, val := range headers {
|
|
req.Header.Set(key, val)
|
|
}
|
|
|
|
resp, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatalf("app.Test: %v", err)
|
|
}
|
|
t.Cleanup(func() { resp.Body.Close() })
|
|
|
|
return resp
|
|
}
|
|
|
|
func assertHeader(t *testing.T, resp *http.Response, key, want string) {
|
|
t.Helper()
|
|
if got := resp.Header.Get(key); got != want {
|
|
t.Errorf("%s: expected %q, got %q", key, want, got)
|
|
}
|
|
}
|
|
|
|
func assertNoHeader(t *testing.T, resp *http.Response, key string) {
|
|
t.Helper()
|
|
if got := resp.Header.Get(key); got != "" {
|
|
t.Errorf("%s: expected absent, got %q", key, got)
|
|
}
|
|
}
|