mirror of
https://github.com/dsx-ai-factory/nvke.git
synced 2026-12-07 04:35:58 +08:00
An API describes each request as a Target, the action and the resource it acts on, and calls Client.Check. Check reads the caller from the authN principal, asks the backend, counts the decision, logs a refusal, and on an allow returns a context carrying the authorized Caller. Route resolution stays in each API. A missing object gets ErrNotFound without asking the backend: the answer a caller with a tenant gets for another tenant's object, so an ID doesn't reveal whether an object exists. Every existing object must have an owner tenant. NewClient takes a logger and a Recorder, either of which may be nil. A Recorder counts every decision, with OutcomeError for a request that couldn't be decided. Authorize becomes internal: an API never builds a Query or reads a Decision. ListTenant returns the tenant a list must be filtered to, or all tenants for an operator. On an error it returns no tenant and not all, so a caller that drops the error lists nothing. WriteError answers a refusal over HTTP: 403, 404 with no body, or 500, with a fixed body that never carries error text. authN and authZ share that writer, and authN's responses are unchanged. Signed-off-by: dbudris <dbudris@nvidia.com>
53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package authz
|
|
|
|
import (
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/dsx-ai-factory/nvke/auth/internal/httperror"
|
|
)
|
|
|
|
const (
|
|
// forbiddenMessage is the body of every 403. It gives no reason, so a
|
|
// refused caller learns nothing about the policy.
|
|
forbiddenMessage = "Not authorized"
|
|
)
|
|
|
|
// statusFor returns the HTTP status for err: 404 for ErrNotFound, 403 for
|
|
// ErrDenied or ErrNoTenant, and 500 for anything else, including nil. A
|
|
// request that couldn't be decided wraps ErrDenied, so it gets 403.
|
|
func statusFor(err error) int {
|
|
switch {
|
|
case errors.Is(err, ErrNotFound):
|
|
return http.StatusNotFound
|
|
case errors.Is(err, ErrDenied), errors.Is(err, ErrNoTenant):
|
|
return http.StatusForbidden
|
|
default:
|
|
return http.StatusInternalServerError
|
|
}
|
|
}
|
|
|
|
// WriteError answers an error from Client.Check over HTTP. The response never
|
|
// contains err's text: a 404 has no body, as elsewhere in the NVKE API, and a
|
|
// 403 or 500 has the API's error body, {"message": ...}. A 500 also logs err;
|
|
// logger may be nil. Check has already logged any refusal.
|
|
func WriteError(w http.ResponseWriter, r *http.Request, err error, logger *slog.Logger) {
|
|
if logger == nil {
|
|
logger = slog.Default()
|
|
}
|
|
status := statusFor(err)
|
|
var message string
|
|
switch status {
|
|
case http.StatusForbidden:
|
|
message = forbiddenMessage
|
|
case http.StatusInternalServerError:
|
|
logger.ErrorContext(r.Context(), "failed to authorize request", "error", err)
|
|
message = http.StatusText(status)
|
|
}
|
|
httperror.Write(w, r, status, message, logger)
|
|
}
|