feat: add standalone IAM support in WebGUI

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/
This commit is contained in:
niksis02
2026-08-27 20:28:51 +04:00
parent 61f1d1c9c8
commit abb3b27149
37 changed files with 5854 additions and 307 deletions
+63
View File
@@ -0,0 +1,63 @@
// 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 (
"strings"
"github.com/gofiber/fiber/v3"
)
// corsMaxAge is how long a browser may reuse a preflight result, matching what
// IAM returns. Browsers clamp it to their own ceiling, so it is only a hint.
const corsMaxAge = "172800"
// corsExposeHeaders names the non-safelisted response headers a browser is
// allowed to read. The IAM API sets exactly two.
const corsExposeHeaders = HeaderAmznRequestID + ",Date"
// CORS answers browser preflights and stamps the CORS headers onto
// cross-origin responses, so the WebUI can call this API from its own origin.
// Register it only when the operator configured an allowed origin; left
// unregistered, the API stays usable by CLI and SDK clients but no browser.
func CORS(allowOrigin string) fiber.Handler {
return func(ctx fiber.Ctx) error {
if ctx.Get(fiber.HeaderOrigin) == "" {
// Not a browser call; IAM leaves these untouched.
return ctx.Next()
}
ctx.Response().Header.Set(fiber.HeaderAccessControlAllowOrigin, allowOrigin)
ctx.Response().Header.Set(fiber.HeaderAccessControlExposeHeaders, corsExposeHeaders)
ctx.Response().Header.Add(fiber.HeaderVary, fiber.HeaderOrigin)
if string(ctx.Request().Header.Method()) != fiber.MethodOptions {
return ctx.Next()
}
// Preflight. Echo back what was asked for rather than enumerating the
// SigV4 header set, which changes with every signing detail.
if reqMethod := ctx.Get(fiber.HeaderAccessControlRequestMethod); strings.TrimSpace(reqMethod) != "" {
ctx.Response().Header.Set(fiber.HeaderAccessControlAllowMethods, reqMethod)
}
if reqHeaders := ctx.Get(fiber.HeaderAccessControlRequestHeaders); strings.TrimSpace(reqHeaders) != "" {
ctx.Response().Header.Set(fiber.HeaderAccessControlAllowHeaders, reqHeaders)
}
ctx.Response().Header.Set(fiber.HeaderAccessControlMaxAge, corsMaxAge)
ctx.Status(fiber.StatusOK)
return nil
}
}
+151
View File
@@ -0,0 +1,151 @@
// 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)
}
}
+15
View File
@@ -19,6 +19,7 @@ import (
"net"
"net/http"
"os"
"strings"
"time"
"github.com/gofiber/fiber/v3"
@@ -61,6 +62,8 @@ type IAMApiServer struct {
// oidcThumbprintAutoFetchDisabled disables CreateOpenIDConnectProvider's
// TLS auto-fetch fallback; see WithOIDCThumbprintAutoFetchDisabled.
oidcThumbprintAutoFetchDisabled bool
// corsAllowOrigin is the single origin browsers may call this API from
corsAllowOrigin string
}
func New(store storage.Storer, root RootCredentials, opts ...Option) (*IAMApiServer, error) {
@@ -109,6 +112,10 @@ func New(store storage.Storer, root RootCredentials, opts ...Option) (*IAMApiSer
}))
}
if server.corsAllowOrigin != "" {
app.Use("*", iammiddleware.CORS(server.corsAllowOrigin))
}
app.Use("*", iammiddleware.RequestIDs())
if server.health != "" {
@@ -159,6 +166,14 @@ func WithSocketPerm(perm os.FileMode) Option {
return func(s *IAMApiServer) { s.socketPerm = perm }
}
// WithCORSAllowOrigin sets the Access-Control-Allow-Origin value returned to
// browsers, and enables preflight handling. Required for the WebUI, which
// never shares a port with the IAM API. Empty (the default) skips the CORS
// middleware, leaving the API usable by CLI and SDK clients only.
func WithCORSAllowOrigin(origin string) Option {
return func(s *IAMApiServer) { s.corsAllowOrigin = strings.TrimSpace(origin) }
}
func WithOnListen(fn func()) Option {
return func(s *IAMApiServer) { s.onListen = fn }
}