From f94ca378fd6aff379627d07ecac87fa2fed102a9 Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Tue, 13 Oct 2026 10:21:57 +0000 Subject: [PATCH 01/26] fix(keystone): authorize the identity management surface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Comprehensive review 2026-10-13, findings C-1/C-2/C-3/H-4/M-5 (also found independently as N1/N2/N5): the identity management API was authentication-only. Every route under /v3 (except /auth/*) honored any valid token, so a plain member could: - reset any user's password — including the admin's — via PATCH /users/:id (which, unlike ChangePassword, never asks for the original) [C-1/N1, account takeover] - grant itself the admin role via PUT /projects/:id/users/:uid/roles/:rid and revoke anyone else's [C-2/N2, privilege escalation] - dump every tenant's recoverable EC2/S3 secret via GET /credentials, and mint/overwrite/delete credentials for other users [C-3] - mint unrestricted application credentials for any user, receiving the plaintext secret in the 201 [H-4/N5] - enumerate users, projects, roles, assignments, groups, domains, services, endpoints [M-5] Fix: the same self-or-admin check the EC2-credentials API and ChangePassword already use, plus an admin-only variant, applied across the surface (new internal/keystone/authz.go helpers): - users: List/Create admin-only; Get/GetProjects/GetGroups self-or-admin; PATCH self-or-admin, and password/enabled stay admin-only even for self (ChangePassword keeps its original-password contract) - role assignment: Assign/Unassign/List/Check admin-only, with existence checks for project/user/role (the old code 204'd assignments referencing nonexistent rows) - projects, groups, roles, role assignments, domains, services, endpoints: admin-only management; catalog reads admin-only - generic credentials: List self-scoped for members (admin sees all), Create self-or-admin, Get/Update/Delete owner-or-admin - application credentials: all four user-scoped routes self-or-admin; the id-only lookup 404s for foreign owners (no existence leak) Convention kept (cloud-storage#515, TestIdentityAuthzPrecedence): gated routes with path ids moved from the uuid-validating 'val' group to 'mgmt' and validate ids in-handler AFTER the authz check, so 403 precedes the verbatim 404. routes_wiring_test and identity_notfound_test updated to the new wiring (admin caller, same 404 shapes); new identity_authz_gates_test pins 19 member denials, the self-service allowances, and the unchanged admin path. Unit suite (incl. DB-backed): PASS. Contract suite: no regressions (the 5 failures in this environment are the docker-exec-dependent tests that cannot pass without docker, unchanged from baseline). Signed-off-by: Uwe Schwaeke --- internal/keystone/application_credentials.go | 31 ++ internal/keystone/authz.go | 42 +++ internal/keystone/credentials.go | 51 ++- internal/keystone/domains.go | 54 +++ internal/keystone/groups.go | 80 +++++ internal/keystone/handlers.go | 335 ++++++++++++++---- .../keystone/identity_authz_gates_test.go | 176 +++++++++ internal/keystone/identity_notfound_test.go | 21 +- internal/keystone/routes_wiring_test.go | 60 +++- internal/keystone/services.go | 72 ++++ 10 files changed, 838 insertions(+), 84 deletions(-) create mode 100644 internal/keystone/authz.go create mode 100644 internal/keystone/identity_authz_gates_test.go diff --git a/internal/keystone/application_credentials.go b/internal/keystone/application_credentials.go index 79ef7810..d60d8444 100644 --- a/internal/keystone/application_credentials.go +++ b/internal/keystone/application_credentials.go @@ -18,6 +18,12 @@ import ( func (svc *Service) ListApplicationCredentials(c *gin.Context) { userID := c.Param("id") + // Self-or-admin (review 2026-10-13 H-4): credential metadata and role + // lists are footprint information. + if denyIfNotSelfOrAdmin(c, userID) { + return + } + rows, err := svc.pool.Query(c.Request.Context(), ` SELECT id, user_id, project_id, name, description, expires_at, unrestricted, created_at FROM application_credentials @@ -101,6 +107,13 @@ func (svc *Service) ListApplicationCredentials(c *gin.Context) { func (svc *Service) CreateApplicationCredential(c *gin.Context) { userID := c.Param("id") + // Self-or-admin (review 2026-10-13 H-4): the 201 response hands back the + // plaintext secret, and requested roles are stored verbatim — minting one + // for another user would be both secret disclosure and impersonation. + if denyIfNotSelfOrAdmin(c, userID) { + return + } + var req struct { ApplicationCredential struct { Name string `json:"name" binding:"required"` @@ -200,6 +213,11 @@ func (svc *Service) GetApplicationCredential(c *gin.Context) { userID := c.Param("id") credID := c.Param("cred_id") + // Self-or-admin (review 2026-10-13 H-4). + if denyIfNotSelfOrAdmin(c, userID) { + return + } + var id, userIDVal, name string var projectID, description *string var expiresAt *time.Time @@ -271,6 +289,12 @@ func (svc *Service) DeleteApplicationCredential(c *gin.Context) { userID := c.Param("id") credID := c.Param("cred_id") + // Self-or-admin (review 2026-10-13 H-4): deleting another user's + // credential is an availability attack on their automation. + if denyIfNotSelfOrAdmin(c, userID) { + return + } + result, err := svc.pool.Exec(c.Request.Context(), "DELETE FROM application_credentials WHERE id = $1 AND user_id = $2", credID, userID) @@ -314,6 +338,13 @@ func (svc *Service) GetApplicationCredentialByID(c *gin.Context) { return } + // Owner-or-admin (review 2026-10-13 H-4): a foreign credential answers + // 404 rather than 403 so the id-only lookup does not leak existence. + if userID != c.GetString("user_id") && !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewIdentityNotFoundError("Application Credential", credID)) + return + } + credential := map[string]any{ "id": id, "user_id": userID, diff --git a/internal/keystone/authz.go b/internal/keystone/authz.go new file mode 100644 index 00000000..9d915cd0 --- /dev/null +++ b/internal/keystone/authz.go @@ -0,0 +1,42 @@ +package keystone + +import ( + "github.com/cobaltcore-dev/lightstack/internal/common" + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +// The identity management API is authentication-only by transport: every route +// under /v3 (except /auth/*) requires a valid token. What it historically did +// NOT require is authorization — the management surface (users, projects, +// roles, assignments, groups, domains, catalog, credentials) honored any +// authenticated caller, so a plain member could reset the admin's password, +// self-grant the admin role, or dump another tenant's S3 secret. These helpers +// put the same check the EC2-credentials API and ChangePassword already use +// (self-or-admin) plus the admin-only variant across that surface. +// +// Convention (cloud-storage#515, asserted by TestIdentityAuthzPrecedence): +// on a route with an authz check, the check runs in-handler BEFORE id +// validation, so a caller without permission gets 403 even for a malformed id. +// Such routes therefore sit on `mgmt` (no uuidParamMiddleware) and validate +// their path ids with denyIfMalformedID below, after the authz decision. + +// denyIfNotAdmin writes a 403 and returns true unless the caller holds the +// admin role. +func denyIfNotAdmin(c *gin.Context) bool { + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("admin role required")) + return true + } + return false +} + +// denyIfMalformedID writes the Keystone-verbatim 404 and returns true when id +// is not a well-formed UUID. Call it after the authz check (see above). +func denyIfMalformedID(c *gin.Context, kind, id string) bool { + if uuid.Validate(id) != nil { + common.SendError(c, common.NewIdentityNotFoundError(kind, id)) + return true + } + return false +} diff --git a/internal/keystone/credentials.go b/internal/keystone/credentials.go index af2a8033..46ab3208 100644 --- a/internal/keystone/credentials.go +++ b/internal/keystone/credentials.go @@ -13,11 +13,16 @@ import ( // ListCredentials returns all credentials func (svc *Service) ListCredentials(c *gin.Context) { + // Review 2026-10-13 C-3: the generic credentials API returns the + // recoverable EC2/S3 secret blob, so it is scoped like the EC2 API — + // a non-admin sees only their own credentials; an admin sees all. + callerID := c.GetString("user_id") rows, err := svc.pool.Query(c.Request.Context(), ` SELECT id, user_id, project_id, type, blob, created_at FROM credentials + WHERE user_id = $1 OR $2 ORDER BY created_at DESC - `) + `, callerID, common.HasRole(c, common.RoleAdmin)) if err != nil { log.Error().Err(err).Str("operation", "list_credentials").Msg("Failed to query credentials") common.SendError(c, common.NewInternalServerError("failed to query credentials")) @@ -74,6 +79,14 @@ func (svc *Service) CreateCredential(c *gin.Context) { return } + // Self-or-admin (review 2026-10-13 C-3): a credential's blob is the + // recoverable secret consumed by S3/EC2 signature auth — minting one for + // another user (with a known secret) would defeat that auth. + if c.GetString("user_id") != req.Credential.UserID && !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("cannot create credentials for another user")) + return + } + credID := uuid.New() now := time.Now() @@ -107,10 +120,38 @@ func (svc *Service) CreateCredential(c *gin.Context) { c.JSON(201, gin.H{"credential": credential}) } +// credentialOwnerOrViewer enforces the owner-or-admin rule on a single +// credential (review 2026-10-13 C-3): 404 when it does not exist, 403 when it +// belongs to another user and the caller is not admin. +func (svc *Service) credentialOwnerOrViewer(c *gin.Context, credID string) bool { + var owner string + err := svc.pool.QueryRow(c.Request.Context(), + "SELECT user_id FROM credentials WHERE id = $1", credID, + ).Scan(&owner) + if errors.Is(err, pgx.ErrNoRows) { + common.SendError(c, common.NewIdentityNotFoundError("credential", credID)) + return false + } + if err != nil { + log.Error().Err(err).Str("operation", "credential_owner").Str("cred_id", credID).Msg("Failed to query credential") + common.SendError(c, common.NewInternalServerError("failed to query credential")) + return false + } + if c.GetString("user_id") != owner && !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("cannot manage another user's credentials")) + return false + } + return true +} + // GetCredential returns a specific credential by ID func (svc *Service) GetCredential(c *gin.Context) { credID := c.Param("id") + if !svc.credentialOwnerOrViewer(c, credID) { + return + } + var id, userID, credType, blob string var projectID *string @@ -148,6 +189,10 @@ func (svc *Service) GetCredential(c *gin.Context) { func (svc *Service) UpdateCredential(c *gin.Context) { credID := c.Param("id") + if !svc.credentialOwnerOrViewer(c, credID) { + return + } + var req struct { Credential map[string]any `json:"credential"` } @@ -192,6 +237,10 @@ func (svc *Service) UpdateCredential(c *gin.Context) { func (svc *Service) DeleteCredential(c *gin.Context) { credID := c.Param("id") + if !svc.credentialOwnerOrViewer(c, credID) { + return + } + result, err := svc.pool.Exec(c.Request.Context(), "DELETE FROM credentials WHERE id = $1", credID) diff --git a/internal/keystone/domains.go b/internal/keystone/domains.go index f7965220..7ead3272 100644 --- a/internal/keystone/domains.go +++ b/internal/keystone/domains.go @@ -17,6 +17,13 @@ import ( // domain instead of the one they asked for (SPEC-022 FR-001a). Names are matched // exactly — upstream treats domain names as case-sensitive. func (svc *Service) ListDomains(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + query := `SELECT id, name, description, enabled FROM domains` args := []any{} if name := c.Query("name"); name != "" { @@ -69,6 +76,13 @@ func (svc *Service) ListDomains(c *gin.Context) { // CreateDomain handles POST /v3/domains func (svc *Service) CreateDomain(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + var req struct { Domain struct { Name string `json:"name" binding:"required"` @@ -120,6 +134,13 @@ func (svc *Service) CreateDomain(c *gin.Context) { func (svc *Service) GetDomain(c *gin.Context) { domainID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): domain records enumerate the + // deployment's trust structure. Checked before the alias resolution + // below, which doubles as this route's in-handler id validation (#515). + if denyIfNotAdmin(c) { + return + } + // This route is registered on the non-validating group precisely so the `default` // alias reaches here (see the note in RegisterRoutes). Resolve it to a real id // first; the resolver also rejects any other non-identifier value without a query, @@ -168,6 +189,17 @@ func (svc *Service) GetDomain(c *gin.Context) { func (svc *Service) UpdateDomain(c *gin.Context) { domainID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "domain", domainID) { + return + } + var req struct { Domain map[string]any `json:"domain" binding:"required"` } @@ -257,6 +289,17 @@ func (svc *Service) UpdateDomain(c *gin.Context) { func (svc *Service) DeleteDomain(c *gin.Context) { domainID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "domain", domainID) { + return + } + // Check if domain is enabled var enabled bool err := svc.pool.QueryRow(c.Request.Context(), ` @@ -296,6 +339,17 @@ func (svc *Service) DeleteDomain(c *gin.Context) { func (svc *Service) GetDomainConfig(c *gin.Context) { domainID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "domain", domainID) { + return + } + // Verify domain exists var exists bool err := svc.pool.QueryRow(c.Request.Context(), ` diff --git a/internal/keystone/groups.go b/internal/keystone/groups.go index 2b830883..c171f707 100644 --- a/internal/keystone/groups.go +++ b/internal/keystone/groups.go @@ -13,6 +13,13 @@ import ( // ListGroups handles GET /v3/groups func (svc *Service) ListGroups(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + var f listFilter f.eq(c, "name", "name") f.eqDomain(c, "domain_id", "domain_id") @@ -60,6 +67,13 @@ func (svc *Service) ListGroups(c *gin.Context) { // CreateGroup handles POST /v3/groups func (svc *Service) CreateGroup(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + var req struct { Group struct { Name string `json:"name" binding:"required"` @@ -124,6 +138,17 @@ func (svc *Service) CreateGroup(c *gin.Context) { func (svc *Service) GetGroup(c *gin.Context) { groupID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "group", groupID) { + return + } + var name, domainID, description string var createdAt time.Time @@ -157,6 +182,17 @@ func (svc *Service) GetGroup(c *gin.Context) { func (svc *Service) UpdateGroup(c *gin.Context) { groupID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "group", groupID) { + return + } + var req struct { Group struct { Name *string `json:"name"` @@ -220,6 +256,17 @@ func (svc *Service) UpdateGroup(c *gin.Context) { func (svc *Service) DeleteGroup(c *gin.Context) { groupID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "group", groupID) { + return + } + result, err := svc.pool.Exec(c.Request.Context(), "DELETE FROM groups WHERE id = $1", groupID, @@ -243,6 +290,17 @@ func (svc *Service) DeleteGroup(c *gin.Context) { func (svc *Service) ListGroupUsers(c *gin.Context) { groupID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "group", groupID) { + return + } + rows, err := svc.pool.Query(c.Request.Context(), `SELECT u.id, u.name, u.domain_id, u.enabled FROM users u @@ -292,6 +350,17 @@ func (svc *Service) AddUserToGroup(c *gin.Context) { groupID := c.Param("id") userID := c.Param("user_id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "group", groupID) || denyIfMalformedID(c, "user", userID) { + return + } + _, err := svc.pool.Exec(c.Request.Context(), `INSERT INTO group_members (group_id, user_id) VALUES ($1, $2) @@ -312,6 +381,17 @@ func (svc *Service) RemoveUserFromGroup(c *gin.Context) { groupID := c.Param("id") userID := c.Param("user_id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "group", groupID) || denyIfMalformedID(c, "user", userID) { + return + } + result, err := svc.pool.Exec(c.Request.Context(), "DELETE FROM group_members WHERE group_id = $1 AND user_id = $2", groupID, userID, diff --git a/internal/keystone/handlers.go b/internal/keystone/handlers.go index 1b4cb832..108fc2cb 100644 --- a/internal/keystone/handlers.go +++ b/internal/keystone/handlers.go @@ -63,19 +63,22 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup, authMiddleware gin.Handle val := mgmt.Group("") val.Use(uuidParamMiddleware) - // Users - val.GET("/users", svc.ListUsers) - val.POST("/users", svc.CreateUser) - val.GET("/users/:id", svc.GetUser) - val.PATCH("/users/:id", svc.UpdateUser) - mgmt.DELETE("/users/:id", svc.DeleteUser) // authz-gated (admin) - mgmt.POST("/users/:id/password", svc.ChangePassword) // authz-gated (self-or-admin) - val.GET("/users/:id/projects", svc.GetUserProjects) - val.GET("/users/:id/groups", svc.GetUserGroups) - val.POST("/users/:id/application_credentials", svc.CreateApplicationCredential) - val.GET("/users/:id/application_credentials", svc.ListApplicationCredentials) - val.GET("/users/:id/application_credentials/:cred_id", svc.GetApplicationCredential) - val.DELETE("/users/:id/application_credentials/:cred_id", svc.DeleteApplicationCredential) + // Users. Authz (review 2026-10-13 C-1/M-5): reads and writes of the user + // collection are admin-only; user-scoped subresources are self-or-admin. + // Routes with an authz check sit on `mgmt` and validate ids in-handler so + // 403 precedes the 404 (#515, asserted by TestIdentityAuthzPrecedence). + val.GET("/users", svc.ListUsers) // admin-only (in-handler) + val.POST("/users", svc.CreateUser) // admin-only (in-handler) + mgmt.GET("/users/:id", svc.GetUser) // self-or-admin + mgmt.PATCH("/users/:id", svc.UpdateUser) // self-or-admin, admin-only fields + mgmt.DELETE("/users/:id", svc.DeleteUser) // authz-gated (admin) + mgmt.POST("/users/:id/password", svc.ChangePassword) // authz-gated (self-or-admin) + mgmt.GET("/users/:id/projects", svc.GetUserProjects) // self-or-admin + mgmt.GET("/users/:id/groups", svc.GetUserGroups) // self-or-admin + mgmt.POST("/users/:id/application_credentials", svc.CreateApplicationCredential) // self-or-admin + mgmt.GET("/users/:id/application_credentials", svc.ListApplicationCredentials) // self-or-admin + mgmt.GET("/users/:id/application_credentials/:cred_id", svc.GetApplicationCredential) // self-or-admin + mgmt.DELETE("/users/:id/application_credentials/:cred_id", svc.DeleteApplicationCredential) // self-or-admin // EC2/S3 credentials — authz-gated (self-or-admin via denyIfNotSelfOrAdmin); // the :id (user) is validated in-handler after that check (SPEC-014 FR-005). mgmt.POST("/users/:id/credentials/OS-EC2", svc.CreateEC2Credential) @@ -83,36 +86,38 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup, authMiddleware gin.Handle mgmt.GET("/users/:id/credentials/OS-EC2/:access", svc.GetEC2Credential) mgmt.DELETE("/users/:id/credentials/OS-EC2/:access", svc.DeleteEC2Credential) - // Projects (role assignments must come before /projects/:id to avoid conflicts) - val.GET("/projects", svc.ListProjects) - val.POST("/projects", svc.CreateProject) - val.PUT("/projects/:id/users/:user_id/roles/:role_id", svc.AssignRole) - val.DELETE("/projects/:id/users/:user_id/roles/:role_id", svc.UnassignRole) - val.GET("/projects/:id/users/:user_id/roles", svc.ListUserProjectRoles) - val.GET("/projects/:id/users/:user_id/roles/:role_id", svc.CheckRoleAssignment) - val.GET("/projects/:id", svc.GetProject) - val.PATCH("/projects/:id", svc.UpdateProject) - val.DELETE("/projects/:id", svc.DeleteProject) - - // Groups - val.GET("/groups", svc.ListGroups) - val.POST("/groups", svc.CreateGroup) - val.GET("/groups/:id", svc.GetGroup) - val.PATCH("/groups/:id", svc.UpdateGroup) - val.DELETE("/groups/:id", svc.DeleteGroup) - val.GET("/groups/:id/users", svc.ListGroupUsers) - val.PUT("/groups/:id/users/:user_id", svc.AddUserToGroup) - val.DELETE("/groups/:id/users/:user_id", svc.RemoveUserFromGroup) - - // Roles - val.GET("/roles", svc.ListRoles) - val.POST("/roles", svc.CreateRole) - val.GET("/roles/:id", svc.GetRole) - val.PATCH("/roles/:id", svc.UpdateRole) - val.DELETE("/roles/:id", svc.DeleteRole) - - // Role Assignments - val.GET("/role_assignments", svc.ListRoleAssignments) + // Projects (role assignments must come before /projects/:id to avoid conflicts). + // Authz (review 2026-10-13 C-2/M-5): project management and role assignment + // are admin-only — the assignment pair is the privilege-escalation primitive. + val.GET("/projects", svc.ListProjects) // admin-only (in-handler) + val.POST("/projects", svc.CreateProject) // admin-only (in-handler) + mgmt.PUT("/projects/:id/users/:user_id/roles/:role_id", svc.AssignRole) // admin-only + mgmt.DELETE("/projects/:id/users/:user_id/roles/:role_id", svc.UnassignRole) // admin-only + mgmt.GET("/projects/:id/users/:user_id/roles", svc.ListUserProjectRoles) // admin-only + mgmt.GET("/projects/:id/users/:user_id/roles/:role_id", svc.CheckRoleAssignment) // admin-only + mgmt.GET("/projects/:id", svc.GetProject) // admin-only + mgmt.PATCH("/projects/:id", svc.UpdateProject) // admin-only + mgmt.DELETE("/projects/:id", svc.DeleteProject) // admin-only + + // Groups — admin-only (review 2026-10-13 C-2). + val.GET("/groups", svc.ListGroups) // admin-only (in-handler) + val.POST("/groups", svc.CreateGroup) // admin-only (in-handler) + mgmt.GET("/groups/:id", svc.GetGroup) + mgmt.PATCH("/groups/:id", svc.UpdateGroup) + mgmt.DELETE("/groups/:id", svc.DeleteGroup) + mgmt.GET("/groups/:id/users", svc.ListGroupUsers) + mgmt.PUT("/groups/:id/users/:user_id", svc.AddUserToGroup) + mgmt.DELETE("/groups/:id/users/:user_id", svc.RemoveUserFromGroup) + + // Roles — admin-only (review 2026-10-13 C-2). + val.GET("/roles", svc.ListRoles) // admin-only (in-handler) + val.POST("/roles", svc.CreateRole) // admin-only (in-handler) + mgmt.GET("/roles/:id", svc.GetRole) + mgmt.PATCH("/roles/:id", svc.UpdateRole) + mgmt.DELETE("/roles/:id", svc.DeleteRole) + + // Role Assignments — admin-only (review 2026-10-13 M-5). + val.GET("/role_assignments", svc.ListRoleAssignments) // admin-only (in-handler) // Domains. // @@ -125,24 +130,26 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup, authMiddleware gin.Handle // The exception is one route, one method, one alias. Every other non-identifier // value on it still gets #515's verbatim 404, produced in-handler; and PATCH, // DELETE and /config stay on `val`, where the middleware still answers. - val.GET("/domains", svc.ListDomains) - val.POST("/domains", svc.CreateDomain) - mgmt.GET("/domains/:id", svc.GetDomain) // alias-aware (see above) - val.PATCH("/domains/:id", svc.UpdateDomain) - val.DELETE("/domains/:id", svc.DeleteDomain) - val.GET("/domains/:id/config", svc.GetDomainConfig) - - // Services (catalog management) - val.GET("/services", svc.ListServices) - val.POST("/services", svc.CreateService) - val.GET("/services/:id", svc.GetService) - val.PATCH("/services/:id", svc.UpdateService) - val.DELETE("/services/:id", svc.DeleteService) - - // Endpoints - val.GET("/endpoints", svc.ListEndpoints) - val.POST("/endpoints", svc.CreateEndpoint) - val.DELETE("/endpoints/:id", svc.DeleteEndpoint) + // + // Domain management is admin-only (review 2026-10-13 C-2). + val.GET("/domains", svc.ListDomains) // admin-only (in-handler) + val.POST("/domains", svc.CreateDomain) // admin-only (in-handler) + mgmt.GET("/domains/:id", svc.GetDomain) // alias-aware (see above), admin-only + mgmt.PATCH("/domains/:id", svc.UpdateDomain) // admin-only + mgmt.DELETE("/domains/:id", svc.DeleteDomain) // admin-only + mgmt.GET("/domains/:id/config", svc.GetDomainConfig) // admin-only + + // Services (catalog management) — admin-only (review 2026-10-13 C-2). + val.GET("/services", svc.ListServices) // admin-only (in-handler) + val.POST("/services", svc.CreateService) // admin-only (in-handler) + mgmt.GET("/services/:id", svc.GetService) + mgmt.PATCH("/services/:id", svc.UpdateService) + mgmt.DELETE("/services/:id", svc.DeleteService) + + // Endpoints — admin-only (review 2026-10-13 C-2). + val.GET("/endpoints", svc.ListEndpoints) // admin-only (in-handler) + val.POST("/endpoints", svc.CreateEndpoint) // admin-only (in-handler) + mgmt.DELETE("/endpoints/:id", svc.DeleteEndpoint) // Credentials val.GET("/credentials", svc.ListCredentials) @@ -390,6 +397,11 @@ func (svc *Service) ListAuthProjects(c *gin.Context) { // ListUsers lists all users func (svc *Service) ListUsers(c *gin.Context) { + // Admin-only (review 2026-10-13 M-5): the user collection exposes every + // account's name, enabled state and domain — identity enumeration. + if denyIfNotAdmin(c) { + return + } var f listFilter f.eq(c, "name", "name") f.eqDomain(c, "domain_id", "domain_id") @@ -434,6 +446,12 @@ func (svc *Service) ListUsers(c *gin.Context) { func (svc *Service) GetUser(c *gin.Context) { userID := c.Param("id") + // Self-or-admin (review 2026-10-13 M-5): user records (name, email, + // enabled) are only for the user itself or an admin. + if denyIfNotSelfOrAdmin(c, userID) { + return + } + var id, name, domainID string var enabled bool err := svc.pool.QueryRow(c.Request.Context(), @@ -457,6 +475,13 @@ func (svc *Service) GetUser(c *gin.Context) { // ListProjects lists all projects func (svc *Service) ListProjects(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + var f listFilter f.eq(c, "name", "name") f.eqDomain(c, "domain_id", "domain_id") @@ -531,6 +556,17 @@ func identityLink(c *gin.Context, collection, id string) gin.H { func (svc *Service) GetProject(c *gin.Context) { projectID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "project", projectID) { + return + } + var id, name string var description, domainID sql.NullString // nullable, as in ListProjects var enabled bool @@ -556,6 +592,13 @@ func (svc *Service) GetProject(c *gin.Context) { // CreateProject handles POST /v3/projects func (svc *Service) CreateProject(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + var req struct { Project struct { Name string `json:"name" binding:"required"` @@ -630,6 +673,17 @@ func (svc *Service) CreateProject(c *gin.Context) { func (svc *Service) UpdateProject(c *gin.Context) { projectID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "project", projectID) { + return + } + var req struct { Project struct { Name *string `json:"name"` @@ -697,6 +751,17 @@ func (svc *Service) UpdateProject(c *gin.Context) { func (svc *Service) DeleteProject(c *gin.Context) { projectID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "project", projectID) { + return + } + result, err := svc.pool.Exec(c.Request.Context(), "DELETE FROM projects WHERE id = $1", projectID, @@ -718,6 +783,13 @@ func (svc *Service) DeleteProject(c *gin.Context) { // ListRoles lists all roles func (svc *Service) ListRoles(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + var f listFilter f.eq(c, "name", "name") rows, err := svc.pool.Query(c.Request.Context(), @@ -757,6 +829,17 @@ func (svc *Service) ListRoles(c *gin.Context) { func (svc *Service) GetRole(c *gin.Context) { roleID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "role", roleID) { + return + } + var id, name string err := svc.pool.QueryRow(c.Request.Context(), "SELECT id, name FROM roles WHERE id = $1", @@ -777,6 +860,13 @@ func (svc *Service) GetRole(c *gin.Context) { // CreateRole handles POST /v3/roles func (svc *Service) CreateRole(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + var req struct { Role struct { Name string `json:"name" binding:"required"` @@ -816,6 +906,17 @@ func (svc *Service) CreateRole(c *gin.Context) { func (svc *Service) UpdateRole(c *gin.Context) { roleID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "role", roleID) { + return + } + var req struct { Role struct { Name *string `json:"name"` @@ -861,6 +962,17 @@ func (svc *Service) UpdateRole(c *gin.Context) { func (svc *Service) DeleteRole(c *gin.Context) { roleID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "role", roleID) { + return + } + result, err := svc.pool.Exec(c.Request.Context(), "DELETE FROM roles WHERE id = $1", roleID, @@ -886,6 +998,32 @@ func (svc *Service) AssignRole(c *gin.Context) { userID := c.Param("user_id") roleID := c.Param("role_id") + // Admin-only (review 2026-10-13 C-2): role assignment is the privilege + // escalation primitive — without this check any authenticated user could + // grant itself (or anyone) the admin role on any project. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "project", projectID) || denyIfMalformedID(c, "user", userID) || + denyIfMalformedID(c, "role", roleID) { + return + } + + // The ids must name real rows: ON CONFLICT DO NOTHING would otherwise + // silently insert an assignment referencing nothing and answer 204. + for _, tc := range []struct{ kind, id string }{ + {"project", projectID}, {"user", userID}, {"role", roleID}, + } { + var exists bool + err := svc.pool.QueryRow(c.Request.Context(), + "SELECT EXISTS(SELECT 1 FROM "+tc.kind+"s WHERE id = $1)", tc.id, + ).Scan(&exists) + if existsOr404(c, err, exists, tc.kind, tc.id) { + return + } + } + // Insert role assignment (idempotent) _, err := svc.pool.Exec(c.Request.Context(), `INSERT INTO role_assignments (id, user_id, project_id, role_id, created_at) @@ -908,6 +1046,17 @@ func (svc *Service) UnassignRole(c *gin.Context) { userID := c.Param("user_id") roleID := c.Param("role_id") + // Admin-only (review 2026-10-13 C-2): revoking roles is the inverse + // escalation — any user could strip an admin's role and lock the + // deployment out. + if denyIfNotAdmin(c) { + return + } + if denyIfMalformedID(c, "project", projectID) || denyIfMalformedID(c, "user", userID) || + denyIfMalformedID(c, "role", roleID) { + return + } + result, err := svc.pool.Exec(c.Request.Context(), "DELETE FROM role_assignments WHERE user_id = $1 AND project_id = $2 AND role_id = $3", userID, projectID, roleID, @@ -932,6 +1081,17 @@ func (svc *Service) ListUserProjectRoles(c *gin.Context) { projectID := c.Param("id") userID := c.Param("user_id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "project", projectID) || denyIfMalformedID(c, "user", userID) { + return + } + rows, err := svc.pool.Query(c.Request.Context(), `SELECT r.id, r.name FROM roles r @@ -978,6 +1138,17 @@ func (svc *Service) CheckRoleAssignment(c *gin.Context) { userID := c.Param("user_id") roleID := c.Param("role_id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "project", projectID) || denyIfMalformedID(c, "user", userID) || denyIfMalformedID(c, "role", roleID) { + return + } + var exists bool err := svc.pool.QueryRow(c.Request.Context(), `SELECT EXISTS( @@ -1003,6 +1174,13 @@ func (svc *Service) CheckRoleAssignment(c *gin.Context) { // ListRoleAssignments handles GET /v3/role_assignments func (svc *Service) ListRoleAssignments(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Parse query parameters userID := c.Query("user.id") projectID := c.Query("scope.project.id") @@ -1170,6 +1348,21 @@ func (svc *Service) CreateUser(c *gin.Context) { func (svc *Service) UpdateUser(c *gin.Context) { userID := c.Param("id") + // Self-or-admin (review 2026-10-13 C-1): without this check any + // authenticated caller could reset any user's password — including the + // admin's — or disable any account, because this handler (unlike + // ChangePassword) never asked for the original password. + isAdmin := common.HasRole(c, common.RoleAdmin) + if c.GetString("user_id") != userID && !isAdmin { + common.SendError(c, common.NewForbiddenError("cannot update another user")) + return + } + // Authorization passed; validate the id after it so 403 precedes the 404 (#515). + if uuid.Validate(userID) != nil { + common.SendError(c, common.NewIdentityNotFoundError("user", userID)) + return + } + var req struct { User struct { Name *string `json:"name"` @@ -1185,6 +1378,15 @@ func (svc *Service) UpdateUser(c *gin.Context) { return } + // A non-admin may only edit their own profile fields. Setting a password + // without the original (ChangePassword's contract) or flipping `enabled` + // stays admin-only — otherwise this route would be a weaker password + // change and a self-enable. + if !isAdmin && (req.User.Password != nil || req.User.Enabled != nil) { + common.SendError(c, common.NewForbiddenError("admin role required to set password or enabled")) + return + } + // Check if user exists var exists bool err := svc.pool.QueryRow(c.Request.Context(), @@ -1384,6 +1586,12 @@ func (svc *Service) ChangePassword(c *gin.Context) { func (svc *Service) GetUserProjects(c *gin.Context) { userID := c.Param("id") + // Self-or-admin (review 2026-10-13 M-5): a user's project membership is + // footprint information. + if denyIfNotSelfOrAdmin(c, userID) { + return + } + // Check if user exists var exists bool err := svc.pool.QueryRow(c.Request.Context(), @@ -1444,6 +1652,11 @@ func (svc *Service) GetUserProjects(c *gin.Context) { func (svc *Service) GetUserGroups(c *gin.Context) { userID := c.Param("id") + // Self-or-admin (review 2026-10-13 M-5). + if denyIfNotSelfOrAdmin(c, userID) { + return + } + // Check if user exists var exists bool err := svc.pool.QueryRow(c.Request.Context(), diff --git a/internal/keystone/identity_authz_gates_test.go b/internal/keystone/identity_authz_gates_test.go new file mode 100644 index 00000000..4b77538d --- /dev/null +++ b/internal/keystone/identity_authz_gates_test.go @@ -0,0 +1,176 @@ +package keystone + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// TestIdentityManagementAuthz_Gates pins the review-2026-10-13 fixes (C-1, C-2, +// M-5): the identity management surface — user updates, role assignment, +// project/role/group/domain/catalog management — must not honor a plain +// member. Before the fix every one of these answered 2xx to any authenticated +// caller, making PATCH /users/:id a passwordless account takeover and +// PUT .../roles/:role_id a one-request privilege escalation. +func TestIdentityManagementAuthz_Gates(t *testing.T) { + requireDB(t) + r := newIdentityTestRouter() + + const adminUID = "00000000-0000-0000-0000-000000000001" // seeded admin + const projectID = "00000000-0000-0000-0000-000000000002" + const memberUID = "00000000-0000-0000-0000-0000000000aa" + member := map[string]string{"X-Test-Roles": "member", "X-Test-User": memberUID} + + // A valid target id, so these failures are the authz check and not the 404. + takeover := `{"user":{"password":"attacker-controlled"}}` + + for _, tc := range []struct { + name, method, path, body string + }{ + {"member cannot reset another user's password (C-1)", "PATCH", "/v3/users/" + adminUID, takeover}, + {"member cannot disable another user (C-1)", "PATCH", "/v3/users/" + adminUID, `{"user":{"enabled":false}}`}, + {"member cannot self-grant a role (C-2)", "PUT", "/v3/projects/" + projectID + "/users/" + memberUID + "/roles/00000000-0000-0000-0000-000000000003", ""}, + {"member cannot revoke a role (C-2)", "DELETE", "/v3/projects/" + projectID + "/users/" + adminUID + "/roles/00000000-0000-0000-0000-000000000003", ""}, + {"member cannot create users", "POST", "/v3/users", `{"user":{"name":"x"}}`}, + {"member cannot enumerate users (M-5)", "GET", "/v3/users", ""}, + {"member cannot read another user's record (M-5)", "GET", "/v3/users/" + adminUID, ""}, + {"member cannot create projects", "POST", "/v3/projects", `{"project":{"name":"x"}}`}, + {"member cannot update projects", "PATCH", "/v3/projects/" + projectID, `{"project":{"name":"x"}}`}, + {"member cannot delete projects", "DELETE", "/v3/projects/" + projectID, ""}, + {"member cannot create roles", "POST", "/v3/roles", `{"role":{"name":"x"}}`}, + {"member cannot list role assignments (M-5)", "GET", "/v3/role_assignments", ""}, + {"member cannot create groups", "POST", "/v3/groups", `{"group":{"name":"x"}}`}, + {"member cannot list domains", "GET", "/v3/domains", ""}, + {"member cannot create services", "POST", "/v3/services", `{"service":{"name":"x","type":"compute"}}`}, + {"member cannot list endpoints", "GET", "/v3/endpoints", ""}, + {"member cannot mint credentials for another user (C-3)", "POST", "/v3/credentials", `{"credential":{"user_id":"` + adminUID + `","type":"ec2","blob":"{}"}}`}, + {"member cannot mint an application credential for another user (H-4)", "POST", "/v3/users/" + adminUID + "/application_credentials", `{"application_credential":{"name":"x"}}`}, + {"member cannot list another user's application credentials (H-4)", "GET", "/v3/users/" + adminUID + "/application_credentials", ""}, + } { + t.Run(tc.name, func(t *testing.T) { + req := httptest.NewRequest(tc.method, tc.path, strings.NewReader(tc.body)) + if tc.body != "" { + req.Header.Set("Content-Type", "application/json") + } + for k, v := range member { + req.Header.Set(k, v) + } + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + if w.Code != http.StatusForbidden { + t.Errorf("%s %s: code=%d body=%s; want 403", tc.method, tc.path, w.Code, w.Body.String()) + } + }) + } +} + +// TestIdentitySelfServiceStillWorks is the complement: the gates must not lock +// a member out of their own record — PATCH of one's own profile fields (name, +// email) stays allowed, while the admin-only fields on the same route +// (password, enabled) stay denied even for self. +func TestIdentitySelfServiceStillWorks(t *testing.T) { + requireDB(t) + r := newIdentityTestRouter() + + // A real member row — the authz pass must not lock a user out of their + // own record. + req := httptest.NewRequest("POST", "/v3/users", strings.NewReader( + `{"user":{"name":"authz-self","password":"Scratch-1","domain_id":"default"}}`)) + req.Header.Set("Content-Type", "application/json") + for k, v := range adminHdr { + req.Header.Set(k, v) + } + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + if w.Code != http.StatusCreated { + t.Fatalf("create scratch user: code=%d body=%s", w.Code, w.Body.String()) + } + var created struct { + User struct { + ID string `json:"id"` + } `json:"user"` + } + if err := json.Unmarshal(w.Body.Bytes(), &created); err != nil || created.User.ID == "" { + t.Fatalf("create scratch user: no id in %s", w.Body.String()) + } + t.Cleanup(func() { + req := httptest.NewRequest("DELETE", "/v3/users/"+created.User.ID, nil) + for k, v := range adminHdr { + req.Header.Set(k, v) + } + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + }) + member := map[string]string{"X-Test-Roles": "member", "X-Test-User": created.User.ID} + + doPatch := func(body string) int { + req := httptest.NewRequest("PATCH", "/v3/users/"+created.User.ID, strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + for k, v := range member { + req.Header.Set(k, v) + } + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + return w.Code + } + + if code := doPatch(`{"user":{"email":"member@example.com"}}`); code != http.StatusOK { + t.Errorf("self PATCH of profile fields: code=%d; want 200", code) + } + if code := doPatch(`{"user":{"password":"new-password"}}`); code != http.StatusForbidden { + t.Errorf("self PATCH of password: code=%d; want 403 (use ChangePassword, which requires the original)", code) + } + if code := doPatch(`{"user":{"enabled":false}}`); code != http.StatusForbidden { + t.Errorf("self PATCH of enabled: code=%d; want 403", code) + } +} + +// TestUpdateUserAdminPathUnchanged pins the admin side of the C-1 fix: an +// admin can still set password/enabled through PATCH /users/:id. +func TestUpdateUserAdminPathUnchanged(t *testing.T) { + requireDB(t) + r := newIdentityTestRouter() + + do := func(method, path, body string, hdr map[string]string) (*httptest.ResponseRecorder, string) { + var reader *strings.Reader + if body != "" { + reader = strings.NewReader(body) + } else { + reader = strings.NewReader("") + } + req := httptest.NewRequest(method, path, reader) + if body != "" { + req.Header.Set("Content-Type", "application/json") + } + for k, v := range hdr { + req.Header.Set(k, v) + } + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + return w, w.Body.String() + } + + // Create a scratch user as admin. + w, body := do("POST", "/v3/users", `{"user":{"name":"authz-scratch","password":"Scratch-1","domain_id":"default"}}`, adminHdr) + if w.Code != http.StatusCreated { + t.Fatalf("create scratch user: code=%d body=%s", w.Code, body) + } + var created struct { + User struct { + ID string `json:"id"` + } `json:"user"` + } + if err := json.Unmarshal([]byte(body), &created); err != nil || created.User.ID == "" { + t.Fatalf("create scratch user: no id in %s", body) + } + t.Cleanup(func() { do("DELETE", "/v3/users/"+created.User.ID, "", adminHdr) }) + + // Admin PATCH with password + enabled must still succeed. + w, body = do("PATCH", "/v3/users/"+created.User.ID, + `{"user":{"password":"Scratch-2","enabled":true}}`, adminHdr) + if w.Code != http.StatusOK { + t.Errorf("admin PATCH password/enabled: code=%d body=%s; want 200", w.Code, body) + } +} diff --git a/internal/keystone/identity_notfound_test.go b/internal/keystone/identity_notfound_test.go index b6017035..7ac9c5be 100644 --- a/internal/keystone/identity_notfound_test.go +++ b/internal/keystone/identity_notfound_test.go @@ -13,17 +13,30 @@ import ( // TestIdentityNotFound_ReadPaths (US1) covers the ticket's core resources: a // malformed OR a valid-but-unknown id returns the identical Keystone-verbatim // 404 ("Could not find : ."), while an existing id still 200s. -// DB-backed (requireDB); routes are wired via the real RegisterRoutes. +// DB-backed (requireDB); routes are wired via the real RegisterRoutes. These +// reads are admin/self-gated (review 2026-10-13), so the caller is admin — +// the 404 shape is what this test pins, not who may see it. func TestIdentityNotFound_ReadPaths(t *testing.T) { requireDB(t) gin.SetMode(gin.TestMode) r := gin.New() svc := NewService(testPool, nil, nil) - svc.RegisterRoutes(&r.RouterGroup, func(c *gin.Context) { c.Next() }) + svc.RegisterRoutes(&r.RouterGroup, func(c *gin.Context) { + if roles := c.GetHeader("X-Test-Roles"); roles != "" { + c.Set("roles", strings.Split(roles, ",")) + } + if uid := c.GetHeader("X-Test-User"); uid != "" { + c.Set("user_id", uid) + } + c.Next() + }) serve := func(path string) (int, string) { + req := httptest.NewRequest(http.MethodGet, path, nil) + req.Header.Set("X-Test-Roles", "admin") + req.Header.Set("X-Test-User", "00000000-0000-0000-0000-00000000a0a0") w := httptest.NewRecorder() - r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil)) + r.ServeHTTP(w, req) return w.Code, w.Body.String() } @@ -40,7 +53,7 @@ func TestIdentityNotFound_ReadPaths(t *testing.T) { t.Fatalf("no seeded %s row to test the 200 case: %v", res.noun, err) } - // malformed id -> verbatim 404 (via middleware, before the DB) + // malformed id -> verbatim 404 (after the authz check, before the DB) if code, body := serve(res.base + "not-a-uuid"); code != http.StatusNotFound || !strings.Contains(body, "Could not find "+res.noun+": not-a-uuid.") { t.Errorf("malformed: code=%d body=%s", code, body) diff --git a/internal/keystone/routes_wiring_test.go b/internal/keystone/routes_wiring_test.go index 5dd73746..b13c7e12 100644 --- a/internal/keystone/routes_wiring_test.go +++ b/internal/keystone/routes_wiring_test.go @@ -24,15 +24,23 @@ func TestRegisterRoutesMiddlewareWiring(t *testing.T) { // cannot. Registering a management route on `v3` instead of `mgmt` is a one-token // slip that would leave the resource readable without a token. authCalls := 0 - stubAuth := func(c *gin.Context) { authCalls++; c.Next() } // authenticated, no roles + stubAuth := func(c *gin.Context) { // authenticated; admin only via header + authCalls++ + if c.GetHeader("X-Test-Admin") != "" { + c.Set("roles", []string{"admin"}) + } + c.Next() + } svc := NewService(testPool, nil, nil) svc.RegisterRoutes(&r.RouterGroup, stubAuth) // Non-gated route (on val): malformed id → middleware 404, handler never runs. + // (Credentials keep their :id on val: the id-only lookup 404s for foreign owners + // anyway, so the middleware 404 does not preempt an authz decision there.) w := httptest.NewRecorder() - r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/v3/projects/not-a-uuid", nil)) - if w.Code != http.StatusNotFound || !strings.Contains(w.Body.String(), "Could not find project: not-a-uuid.") { - t.Fatalf("GET /projects/not-a-uuid: code=%d body=%s; want 404 verbatim", w.Code, w.Body.String()) + r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/v3/credentials/not-a-uuid", nil)) + if w.Code != http.StatusNotFound || !strings.Contains(w.Body.String(), "Could not find credential: not-a-uuid.") { + t.Fatalf("GET /credentials/not-a-uuid: code=%d body=%s; want 404 verbatim", w.Code, w.Body.String()) } // Authz-gated route (on mgmt, NOT behind the uuid middleware): a non-admin @@ -44,39 +52,55 @@ func TestRegisterRoutesMiddlewareWiring(t *testing.T) { t.Fatalf("DELETE /users/not-a-uuid (non-admin): code=%d body=%s; want 403 (authz precedes)", w.Code, w.Body.String()) } - // GET /domains/:id also lives on mgmt, for a different reason: it must resolve - // the "default" alias, which the uuid middleware would reject before the handler - // ran (SPEC-022 FR-001a). The narrow exception is that one alias — every other - // non-identifier value still gets #515's verbatim 404, now produced in-handler. - // No database needed: this asserts the 404 shape, which precedes any query. + // GET /domains/:id lives on `mgmt` so it can resolve the `default` alias + // (SPEC-022 FR-001a), and it is admin-only like the rest of the domain + // management surface (review 2026-10-13). A non-admin gets 403 before the + // resolver runs; an admin still gets the #515 verbatim 404 for any other + // non-identifier value, now produced in-handler. w = httptest.NewRecorder() authBefore := authCalls r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/v3/domains/not-a-uuid", nil)) + if w.Code != http.StatusForbidden { + t.Fatalf("GET /domains/not-a-uuid (non-admin): code=%d body=%s; want 403 (admin-only)", w.Code, w.Body.String()) + } + w = httptest.NewRecorder() + adminReq := httptest.NewRequest(http.MethodGet, "/v3/domains/not-a-uuid", nil) + adminReq.Header.Set("X-Test-Admin", "1") + r.ServeHTTP(w, adminReq) if w.Code != http.StatusNotFound || !strings.Contains(w.Body.String(), "Could not find domain: not-a-uuid.") { - t.Fatalf("GET /domains/not-a-uuid: code=%d body=%s; want 404 verbatim from the handler", w.Code, w.Body.String()) + t.Fatalf("GET /domains/not-a-uuid (admin): code=%d body=%s; want 404 verbatim from the handler", w.Code, w.Body.String()) } // Moving the route off `val` must not have moved it off the authenticated tree as // well: registered on the public `v3` group by mistake, it would answer the same - // 404 above while leaving every domain readable without a token. The status alone - // cannot see that, so assert the middleware ran. This piggybacks on the malformed - // id deliberately — resolveDomainRef answers it without a query, so the assertion - // needs no database (TestResolveDomainRefMalformedNeedsNoDatabase pins that). + // 403/404 above while leaving every domain readable without a token. The status + // alone cannot see that, so assert the middleware ran. This piggybacks on the + // malformed id deliberately — resolveDomainRef answers it without a query, so the + // assertion needs no database (TestResolveDomainRefMalformedNeedsNoDatabase pins that). if authCalls == authBefore { t.Fatal("GET /v3/domains/:id bypassed the auth middleware — it must be on mgmt, not the public v3 group") } - // The sibling domain routes stay on val, so the middleware still answers for - // them — the exception is one route and one method, not the whole resource. + // The domain write/config routes moved to `mgmt` with the admin gate + // (review 2026-10-13 C-2), so their malformed-id 404 is likewise produced + // in-handler, after the authz decision — same shape, different producer. for _, tc := range []struct{ method, path string }{ {http.MethodPatch, "/v3/domains/not-a-uuid"}, {http.MethodDelete, "/v3/domains/not-a-uuid"}, {http.MethodGet, "/v3/domains/not-a-uuid/config"}, } { w = httptest.NewRecorder() - r.ServeHTTP(w, httptest.NewRequest(tc.method, tc.path, nil)) + req := httptest.NewRequest(tc.method, tc.path, nil) + r.ServeHTTP(w, req) + if w.Code != http.StatusForbidden { + t.Fatalf("%s %s (non-admin): code=%d body=%s; want 403 (admin-only)", tc.method, tc.path, w.Code, w.Body.String()) + } + w = httptest.NewRecorder() + req = httptest.NewRequest(tc.method, tc.path, nil) + req.Header.Set("X-Test-Admin", "1") + r.ServeHTTP(w, req) if w.Code != http.StatusNotFound || !strings.Contains(w.Body.String(), "Could not find domain: not-a-uuid.") { - t.Fatalf("%s %s: code=%d body=%s; want the middleware's 404", tc.method, tc.path, w.Code, w.Body.String()) + t.Fatalf("%s %s (admin): code=%d body=%s; want the in-handler 404", tc.method, tc.path, w.Code, w.Body.String()) } } } diff --git a/internal/keystone/services.go b/internal/keystone/services.go index bdf7760a..1fd8a6f1 100644 --- a/internal/keystone/services.go +++ b/internal/keystone/services.go @@ -14,6 +14,13 @@ import ( // ListServices returns all services in the catalog func (svc *Service) ListServices(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + var f listFilter f.eq(c, "type", "type") f.eq(c, "name", "name") @@ -63,6 +70,13 @@ func (svc *Service) ListServices(c *gin.Context) { // CreateService creates a new service in the catalog func (svc *Service) CreateService(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + var req struct { Service struct { Type string `json:"type" binding:"required"` @@ -114,6 +128,17 @@ func (svc *Service) CreateService(c *gin.Context) { func (svc *Service) GetService(c *gin.Context) { serviceID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "service", serviceID) { + return + } + var id, svcType, name string var description *string var enabled bool @@ -152,6 +177,17 @@ func (svc *Service) GetService(c *gin.Context) { func (svc *Service) DeleteService(c *gin.Context) { serviceID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "service", serviceID) { + return + } + result, err := svc.pool.Exec(c.Request.Context(), "DELETE FROM services WHERE id = $1", serviceID) @@ -174,6 +210,17 @@ func (svc *Service) DeleteService(c *gin.Context) { func (svc *Service) UpdateService(c *gin.Context) { serviceID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "service", serviceID) { + return + } + // Parse request body var req struct { Service struct { @@ -256,6 +303,13 @@ func (svc *Service) UpdateService(c *gin.Context) { // ListEndpoints returns all endpoints in the catalog func (svc *Service) ListEndpoints(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + var f listFilter f.eq(c, "interface", "e.interface") f.eqID(c, "service_id", "e.service_id") @@ -308,6 +362,13 @@ func (svc *Service) ListEndpoints(c *gin.Context) { // CreateEndpoint creates a new endpoint in the catalog func (svc *Service) CreateEndpoint(c *gin.Context) { + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + var req struct { Endpoint struct { ServiceID string `json:"service_id" binding:"required"` @@ -360,6 +421,17 @@ func (svc *Service) CreateEndpoint(c *gin.Context) { func (svc *Service) DeleteEndpoint(c *gin.Context) { endpointID := c.Param("id") + // Admin-only (review 2026-10-13 C-2): the identity management surface + // must not honor a plain member — this is the role/assignment/catalog + // authority. + if denyIfNotAdmin(c) { + return + } + // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). + if denyIfMalformedID(c, "endpoint", endpointID) { + return + } + result, err := svc.pool.Exec(c.Request.Context(), "DELETE FROM endpoints WHERE id = $1", endpointID) From ec7fe60a9c033a1a3c21b29f8df0f5eaca429b15 Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Tue, 13 Oct 2026 10:24:21 +0000 Subject: [PATCH 02/26] fix(glance): image-poisoning, checksum-of-stored-bytes, task/cache authz, upload caps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Comprehensive review 2026-10-13 (H-1, M-1, M-2, M-3, N-3): H-1 — upload authorization was visibility-based: the gate was `visibility = 'public' OR project_id = caller`, so any tenant that could SEE a public image could overwrite its bytes while queued — image poisoning of an operator-staged base that every tenant boots. Now the same owner-or-admin rule delete already uses (imageDeleteAuthz): 403 for a visible-but-foreign image, 404 for an invisible one, admin override. Pinned by TestUploadImageDataOwnership_DB. N-3 — the recorded checksum was MD5 of the upload stream, but normalizing backends (local, local,rbd, local,s3) rewrite the bytes (qcow2 → raw base): MD5(raw replica) != MD5(original qcow2), so EnsureLocalBase's integrity check failed on every node without the local base and boot-from-image errored — the exact multi-node case local,s3 exists for. New ImageStore.StoredChecksum hashes the stored object, and UploadImageData records it whenever the backend normalizes (stream hash stays correct for as-is backends: s3, stub). Pinned by TestStoredChecksumMatchesStoredBytesNotUploadBytes (real qemu-img, real qcow2). M-1 — glance tasks leaked cross-tenant input/result/message and echoed internal errors: ListTasks/GetTask are now owner-scoped (admin sees all), GetTask 404s for foreign tasks, and no debug_error / err.Error() in response bodies. M-2 — cache management (prefetch/evict/clear) is admin-only (upstream admin_api): any member could flush the deployment-wide cache with one request. Pinned by TestImageCacheManagementAdminOnly_DB. M-3 — unbounded bodies: PUT /images/:id/file and POST stage now wrap the request in http.MaxBytesReader with a 10 GiB cap (upstream Glance's image_size_cap default). Unit suite (incl. DB-backed): PASS. Contract suite: unchanged (only the 5 docker-exec-dependent tests fail, same as baseline). Signed-off-by: Uwe Schwaeke --- internal/glance/cache.go | 26 +++++++- internal/glance/images.go | 63 ++++++++++++++++-- internal/glance/import.go | 5 +- internal/glance/tasks.go | 24 +++++-- internal/glance/upload_authz_test.go | 97 ++++++++++++++++++++++++++++ pkg/storage/image_store.go | 24 +++++++ pkg/storage/image_store_test.go | 59 +++++++++++++++++ 7 files changed, 283 insertions(+), 15 deletions(-) create mode 100644 internal/glance/upload_authz_test.go diff --git a/internal/glance/cache.go b/internal/glance/cache.go index f7a5c967..ca1fa23d 100644 --- a/internal/glance/cache.go +++ b/internal/glance/cache.go @@ -61,10 +61,17 @@ func (svc *Service) ListCachedImages(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"cached_images": cachedImages}) } -// PrefetchImage prefetches an image into the cache +// PrefetchImage prefetches an image into the cache. Admin-only (review +// 2026-10-13 M-2): cache management is an operator surface (upstream +// `admin_api`) — it moves shared disk state for every tenant. func (svc *Service) PrefetchImage(c *gin.Context) { imageID := c.Param("id") + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("admin role required to manage the image cache")) + return + } + // Verify image exists var exists bool err := svc.pool.QueryRow(c.Request.Context(), @@ -93,10 +100,16 @@ func (svc *Service) PrefetchImage(c *gin.Context) { c.Status(http.StatusAccepted) } -// DeleteCachedImage removes an image from the cache +// DeleteCachedImage removes an image from the cache. Admin-only (review +// 2026-10-13 M-2). func (svc *Service) DeleteCachedImage(c *gin.Context) { imageID := c.Param("id") + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("admin role required to manage the image cache")) + return + } + result, err := svc.pool.Exec(c.Request.Context(), ` UPDATE images SET cached_at = NULL @@ -117,8 +130,15 @@ func (svc *Service) DeleteCachedImage(c *gin.Context) { c.Status(http.StatusNoContent) } -// ClearCache clears all cached images +// ClearCache clears all cached images. Admin-only (review 2026-10-13 M-2): +// without the gate any member could flush the deployment-wide cache (DoS on +// boot-time caching) with one request. func (svc *Service) ClearCache(c *gin.Context) { + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("admin role required to manage the image cache")) + return + } + _, err := svc.pool.Exec(c.Request.Context(), ` UPDATE images SET cached_at = NULL diff --git a/internal/glance/images.go b/internal/glance/images.go index ff11b26f..3470db7b 100644 --- a/internal/glance/images.go +++ b/internal/glance/images.go @@ -39,6 +39,12 @@ type Service struct { cache *cache.Cache } +// maxImageUploadBytes bounds PUT /images/:id/file (review 2026-10-13 M-3): +// without it an unbounded stream lands on disk (and, in local mode, is +// qemu-img-normalized) until the host fills. 10 GiB, the same default as +// upstream Glance's image_size_cap. +const maxImageUploadBytes = 10 << 30 + // NewService creates a new Glance service. // // Returns an error if the configured storage backend fails to initialize @@ -946,11 +952,39 @@ func (svc *Service) UploadImageData(c *gin.Context) { imageID := c.Param("id") projectID := c.GetString("project_id") - // Check if image exists and is in queued status - var status string + // Ownership, not visibility (review 2026-10-13 H-1): seeing a public + // image must not grant write access to its bytes — otherwise anyone + // could poison an operator-staged public base image that every tenant + // boots from. Same rule as delete (F6): owner or admin, 403 for a + // visible-but-foreign image, 404 for an invisible one. + var uploadOwner sql.NullString + var uploadVisibility string err := svc.pool.QueryRow(c.Request.Context(), - "SELECT status FROM images WHERE id = $1 AND (visibility = 'public' OR project_id = $2)", - imageID, projectID, + "SELECT project_id, visibility FROM images WHERE id = $1", + imageID, + ).Scan(&uploadOwner, &uploadVisibility) + if errors.Is(err, pgx.ErrNoRows) { + common.SendError(c, common.NewNotFoundError("image")) + return + } + if err != nil { + log.Error().Err(err).Str("operation", "upload_image_lookup").Str("image_id", imageID).Msg("failed to look up image") + common.SendError(c, common.NewInternalServerError("failed to upload image")) + return + } + switch imageDeleteAuthz(common.HasRole(c, common.RoleAdmin), uploadOwner, projectID, uploadVisibility) { + case http.StatusForbidden: + common.SendError(c, common.NewForbiddenError("not authorized to upload data to this image")) + return + case http.StatusNotFound: + common.SendError(c, common.NewNotFoundError("image")) + return + } + + var status string + err = svc.pool.QueryRow(c.Request.Context(), + "SELECT status FROM images WHERE id = $1", + imageID, ).Scan(&status) if errors.Is(err, pgx.ErrNoRows) { @@ -963,6 +997,11 @@ func (svc *Service) UploadImageData(c *gin.Context) { return } + // Bound the upload (review 2026-10-13 M-3): without a cap an unbounded + // stream lands on disk (and, in local mode, is normalized) until the + // host fills. Same default as upstream Glance's image_size_cap. + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxImageUploadBytes) + // Update status to saving if _, err := svc.pool.Exec(c.Request.Context(), "UPDATE images SET status = $1, updated_at = $2 WHERE id = $3", @@ -993,6 +1032,22 @@ func (svc *Service) UploadImageData(c *gin.Context) { // the S3 backend stores the bytes as-is, so its create-time disk_format is left // untouched rather than mislabeled "raw" (SPEC-014). checksum := hex.EncodeToString(hasher.Sum(nil)) + if normalizedToRaw { + // The recorded checksum must describe the STORED bytes: downloads serve + // them, and EnsureLocalBase verifies a lazily fetched base against this + // value on every boot (review 2026-10-13 N3 — hashing the upload stream + // instead broke boot-from-image on any node without the local base, + // because MD5(raw replica) != MD5(original qcow2)). + if stored, csErr := svc.imageStore.StoredChecksum(c.Request.Context(), imageID); csErr == nil { + checksum = stored + } else { + // The local object is what was just written; not being able to read + // it back is exceptional. Fall back to the stream hash (the old, + // mismatch-prone behavior) rather than failing a successful upload. + log.Warn().Err(csErr).Str("image_id", imageID). + Msg("could not hash stored image bytes; recording upload-stream checksum") + } + } if _, err := svc.pool.Exec(c.Request.Context(), `UPDATE images SET status = $1, size_bytes = $2, disk_format = CASE WHEN $3 THEN 'raw' ELSE disk_format END, diff --git a/internal/glance/import.go b/internal/glance/import.go index d65b1a5f..10d921ea 100644 --- a/internal/glance/import.go +++ b/internal/glance/import.go @@ -38,7 +38,10 @@ func (svc *Service) StageImageData(c *gin.Context) { return } - // Read staged data (in real implementation, would save to staging area) + // Read staged data (in real implementation, would save to staging area). + // Bounded like the upload path (review 2026-10-13 M-3): ReadAll on a raw + // request body is an unbounded memory allocation. + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxImageUploadBytes) stagedData, err := io.ReadAll(c.Request.Body) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"message": "Failed to read image data"}) diff --git a/internal/glance/tasks.go b/internal/glance/tasks.go index cdaff4ba..94db476a 100644 --- a/internal/glance/tasks.go +++ b/internal/glance/tasks.go @@ -34,7 +34,7 @@ func (svc *Service) CreateTask(c *gin.Context) { `, taskID, req.Type, "pending", req.Input, owner, now, now) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) + c.JSON(http.StatusInternalServerError, gin.H{"message": "failed to create task"}) return } @@ -51,16 +51,19 @@ func (svc *Service) CreateTask(c *gin.Context) { }) } -// ListTasks handles GET /v2/tasks +// ListTasks handles GET /v2/tasks. Owner-scoped (review 2026-10-13 M-1): task +// input/result carry the caller's image references and endpoint URLs — a +// member must see only their own tasks; an admin sees all. func (svc *Service) ListTasks(c *gin.Context) { rows, err := svc.pool.Query(c.Request.Context(), ` SELECT id, type, status, input, result, owner, created_at, updated_at FROM image_tasks + WHERE owner = $1 OR $2 ORDER BY created_at DESC LIMIT 100 - `) + `, c.GetString("user_id"), common.HasRole(c, common.RoleAdmin)) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) + c.JSON(http.StatusInternalServerError, gin.H{"message": "failed to list tasks"}) return } defer rows.Close() @@ -108,7 +111,8 @@ func (svc *Service) ListTasks(c *gin.Context) { }) } -// GetTask handles GET /v2/tasks/:id +// GetTask handles GET /v2/tasks/:id. Owner-or-admin (review 2026-10-13 M-1): +// input/result/message are the caller's own task payload, not public data. func (svc *Service) GetTask(c *gin.Context) { taskID := c.Param("id") @@ -123,8 +127,14 @@ func (svc *Service) GetTask(c *gin.Context) { `, taskID).Scan(&taskType, &status, &inputJSON, &resultJSON, &owner, &message, &createdAt, &updatedAt) if err != nil { - // Log the actual error for debugging - c.JSON(http.StatusNotFound, gin.H{"message": "Task not found", "debug_error": err.Error()}) + // Not found — and no internal error details in the response body + // (review 2026-10-13 M-1/L-2): err.Error() leaks driver internals. + c.JSON(http.StatusNotFound, gin.H{"message": "Task not found"}) + return + } + + if owner != c.GetString("user_id") && !common.HasRole(c, common.RoleAdmin) { + c.JSON(http.StatusNotFound, gin.H{"message": "Task not found"}) return } diff --git a/internal/glance/upload_authz_test.go b/internal/glance/upload_authz_test.go new file mode 100644 index 00000000..6ba25203 --- /dev/null +++ b/internal/glance/upload_authz_test.go @@ -0,0 +1,97 @@ +package glance + +import ( + "context" + "testing" + + "github.com/google/uuid" +) + +// TestUploadImageDataOwnership_DB pins review 2026-10-13 H-1: visibility is a +// read grant, not a write grant. A public image staged by one project must not +// accept bytes from another project's member — that was image poisoning: every +// tenant booting the public base image would run the attacker's data. The +// owner and an admin keep their paths. +func TestUploadImageDataOwnership_DB(t *testing.T) { + requireDB(t) + ctx := context.Background() + + otherProject := "00000000-0000-0000-0000-0000000000bb" + id := uuid.NewString() + if _, err := testPool.Exec(ctx, + `INSERT INTO images (id, name, project_id, status, visibility) VALUES ($1,'pub-poison',$2,'queued','public')`, + id, seededProjectID); err != nil { + t.Fatalf("insert image: %v", err) + } + t.Cleanup(func() { testPool.Exec(ctx, "DELETE FROM images WHERE id=$1", id) }) + + r := newGlanceTestRouter(t) + upload := func(hdr map[string]string) int { + code, _ := doReqBody(r, "PUT", "/images/"+id+"/file", "not really an image", hdr) + return code + } + + // A foreign member that can SEE the public image: 403 (visible, not owned). + if code := upload(hdr(otherProject, "member")); code != 403 { + t.Errorf("foreign member upload to public image: code=%d; want 403", code) + } + // A foreign member that cannot even see a private image: 404. + if _, err := testPool.Exec(ctx, "UPDATE images SET visibility='private' WHERE id=$1", id); err != nil { + t.Fatalf("set private: %v", err) + } + if code := upload(hdr(otherProject, "member")); code != 404 { + t.Errorf("foreign member upload to private image: code=%d; want 404", code) + } + if _, err := testPool.Exec(ctx, "UPDATE images SET visibility='public' WHERE id=$1", id); err != nil { + t.Fatalf("set public: %v", err) + } + // The owner may still upload (2xx; the stub store accepts anything). + if code := upload(hdr(seededProjectID, "member")); code < 200 || code >= 300 { + t.Errorf("owner upload: code=%d; want 2xx", code) + } + // An admin may upload to a fresh queued public image. + id2 := uuid.NewString() + if _, err := testPool.Exec(ctx, + `INSERT INTO images (id, name, project_id, status, visibility) VALUES ($1,'pub-admin',$2,'queued','public')`, + id2, seededProjectID); err != nil { + t.Fatalf("insert image 2: %v", err) + } + t.Cleanup(func() { testPool.Exec(ctx, "DELETE FROM images WHERE id=$1", id2) }) + if code, _ := doReqBody(r, "PUT", "/images/"+id2+"/file", "admin bytes", hdr(otherProject, "admin")); code < 200 || code >= 300 { + t.Errorf("admin upload: code=%d; want 2xx", code) + } +} + +// TestImageCacheManagementAdminOnly_DB pins review 2026-10-13 M-2: cache +// mutation is an operator surface — a member flushing the deployment-wide +// cache (ClearCache) or evicting entries is a denial of service on +// boot-time caching. +func TestImageCacheManagementAdminOnly_DB(t *testing.T) { + requireDB(t) + ctx := context.Background() + + id := uuid.NewString() + if _, err := testPool.Exec(ctx, + `INSERT INTO images (id, name, project_id, status, visibility, cached_at) VALUES ($1,'cached',$2,'active','public',NOW())`, + id, seededProjectID); err != nil { + t.Fatalf("insert image: %v", err) + } + t.Cleanup(func() { testPool.Exec(ctx, "DELETE FROM images WHERE id=$1", id) }) + + r := newGlanceTestRouter(t) + + if code, _ := doReq(r, "DELETE", "/cache/images/"+id, hdr(seededProjectID, "member")); code != 403 { + t.Errorf("member cache eviction: code=%d; want 403", code) + } + if code, _ := doReq(r, "DELETE", "/cache/images", hdr(seededProjectID, "member")); code != 403 { + t.Errorf("member cache clear: code=%d; want 403", code) + } + if code, _ := doReq(r, "PUT", "/cache/images/"+id, hdr(seededProjectID, "member")); code != 403 { + t.Errorf("member cache prefetch: code=%d; want 403", code) + } + // Still cached after the denied attempts. + var cached bool + if err := testPool.QueryRow(ctx, "SELECT cached_at IS NOT NULL FROM images WHERE id=$1", id).Scan(&cached); err != nil || !cached { + t.Errorf("denied cache clear still took effect (cached=%v err=%v)", cached, err) + } +} diff --git a/pkg/storage/image_store.go b/pkg/storage/image_store.go index fc57a43e..38d815b2 100644 --- a/pkg/storage/image_store.go +++ b/pkg/storage/image_store.go @@ -605,6 +605,30 @@ func (s *ImageStore) uploadLocalToS3(ctx context.Context, imageID string) error return nil } +// StoredChecksum returns the MD5 hex digest of the image's stored local bytes. +// Normalizing backends (local, local,rbd, local,s3) rewrite the upload — a +// qcow2 becomes a raw base — so the checksum Glance advertises, downloads +// serve and EnsureLocalBase verifies against must be taken from the stored +// object, not from the client's upload stream (review 2026-10-13 N3: the +// stream hash made boot-from-image fail on any node without the local base, +// because MD5(raw replica) never equals MD5(original qcow2)). For S3-only and +// stub modes the stored bytes are the upload bytes, so the stream hash the +// caller already computed is the stored checksum and this method is not +// consulted. A future RBD-only backend should hash its own object instead. +func (s *ImageStore) StoredChecksum(ctx context.Context, imageID string) (string, error) { + imagePath := filepath.Join(s.localPath, imageFileName(imageID)) + f, err := os.Open(imagePath) + if err != nil { + return "", err + } + defer f.Close() + h := md5.New() // #nosec G401 -- Glance checksum is MD5 (compatibility), not a security control + if _, err := io.Copy(h, &ctxReader{ctx: ctx, r: f}); err != nil { + return "", err + } + return hex.EncodeToString(h.Sum(nil)), nil +} + // DownloadImage downloads an image from storage func (s *ImageStore) DownloadImage(ctx context.Context, imageID string, writer io.Writer) error { switch s.mode { diff --git a/pkg/storage/image_store_test.go b/pkg/storage/image_store_test.go index cd2dd9f4..11752720 100644 --- a/pkg/storage/image_store_test.go +++ b/pkg/storage/image_store_test.go @@ -274,3 +274,62 @@ func makeQcow2(t *testing.T, dir string) string { _ = os.Remove(raw) return qcow } + +// TestStoredChecksumMatchesStoredBytesNotUploadBytes pins review 2026-10-13 +// N3: normalizing backends rewrite the upload (qcow2 → raw base), so the +// checksum recorded for an image must describe the STORED bytes — downloads +// serve them and EnsureLocalBase verifies a lazily fetched base against the +// recorded value on every boot. Hashing the upload stream instead made +// MD5(raw replica) != MD5(original qcow2), so boot-from-image failed on any +// node without the local base. +func TestStoredChecksumMatchesStoredBytesNotUploadBytes(t *testing.T) { + if _, err := exec.LookPath("qemu-img"); err != nil { + t.Skip("qemu-img not available") + } + s, err := NewImageStore("local", "", "", S3Options{}) + if err != nil { + t.Fatalf("NewImageStore: %v", err) + } + s.localPath = t.TempDir() // same-package override: keep the test off /var/lib + + // A real qcow2: not raw bytes, so normalizeToRaw must convert it. + qcow2Path := filepath.Join(t.TempDir(), "disk.qcow2") + if out, err := exec.Command("qemu-img", "create", "-f", "qcow2", qcow2Path, "1M").CombinedOutput(); err != nil { + t.Fatalf("qemu-img create: %v: %s", err, out) + } + qcow2Bytes, err := os.ReadFile(qcow2Path) + if err != nil { + t.Fatal(err) + } + + imageID := "11111111-2222-3333-4444-555555555555" + size, normalized, err := s.UploadImage(context.Background(), imageID, bytes.NewReader(qcow2Bytes)) + if err != nil { + t.Fatalf("UploadImage: %v", err) + } + if !normalized { + t.Fatal("local mode must report normalization") + } + // The stored size is the raw virtual size, not the qcow2 file size. + if size != 1<<20 { + t.Errorf("stored size = %d; want the raw size %d", size, 1<<20) + } + + streamHash := fmt.Sprintf("%x", md5.Sum(qcow2Bytes)) + stored, err := s.StoredChecksum(context.Background(), imageID) + if err != nil { + t.Fatalf("StoredChecksum: %v", err) + } + storedBytes, err := os.ReadFile(filepath.Join(s.localPath, imageFileName(imageID))) + if err != nil { + t.Fatalf("read stored image: %v", err) + } + wantHash := fmt.Sprintf("%x", md5.Sum(storedBytes)) + + if stored != wantHash { + t.Errorf("StoredChecksum = %s; want MD5 of the stored bytes %s", stored, wantHash) + } + if stored == streamHash { + t.Error("StoredChecksum equals the upload-stream hash; the qcow2 bytes were normalized, so the hashes must differ — this is the N3 regression") + } +} From 4f4ff3a67fde07dc63fd154ed0bf6fe54df94100 Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Tue, 13 Oct 2026 10:29:04 +0000 Subject: [PATCH 03/26] fix(nova): flavor admin surface, port lifecycle, attach/detach atomicity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Comprehensive review 2026-10-13 (H-2, N-6, N-8, N-9, N-10, L-3, L-4): H-2 — the flavor catalog was member-writable: create/delete flavor, extra-specs CRUD, and tenant access grants (FlavorAction) all honored any authenticated caller. The catalog is a shared deployment-wide resource; upstream gates all of it admin_api. Now requireAdmin on all seven handlers. A member could resize the world's flavors out from under every tenant, or grant itself another project's private flavor. L-4 — flavor inputs get upper sanity bounds (1 TiB RAM, 4096 vCPUs, 1 PiB disk) instead of only >= 0: a typo (MB vs GB) used to schedule absurd values that surfaced only at boot time. N-9 — AttachVolume was check-then-act across two statements: the loser of a concurrent double-attach surfaced as a 500 from the UNIQUE constraint, and a failure between the attachment INSERT and the volume UPDATE left the row inconsistent. Now one transaction with the volume row locked (FOR UPDATE), mirroring cinder's attach CAS; the concurrent double-attach answers 409, the state refusal stays 400. N-10 — DetachVolume had the mirror problem: DELETE then UPDATE as two statements, so a failure after the DELETE stranded the volume in-use with no attachment row — unfixable through the API. Now one transaction; both statements or neither. N-6 — a multi-network boot that failed on a later network leaked the ports already allocated for the earlier ones. The boot goroutine now releases what it allocated on every failure path after the first successful allocation (new NeutronService.ReleaseInstancePorts: deletes the instance's compute:nova ports, their TAP/FDB plumbing and DHCP reservations). Pinned by TestFailedBootReleasesAllocatedPorts / TestSuccessfulBootKeepsItsPorts. N-8 — DeleteServer used a blanket DELETE of every port with the server's device_id, destroying ports the TENANT created and attached (os-interface with port_id) along with the boot-allocated ones. Upstream semantics now: nova-owned ports are deleted with the server, tenant-created ports are detached (device cleared, parked DOWN) and survive for their owner. Pinned by TestDeleteServerDetachesTenantPortsButDeletesItsOwn. L-3 — CreateKeypair raced check-then-insert into duplicate-key 500s; the UNIQUE(user_id, name) violation now maps to the same 409 as the pre-check. Imported public keys are also bounded (64 KiB) before being parsed and stored (M-3). Unit suite (incl. DB-backed): PASS. Contract suite: unchanged (only the 5 docker-exec-dependent tests fail, same as baseline). Signed-off-by: Uwe Schwaeke --- internal/neutron/ports.go | 70 ++++++++++ internal/nova/boot_failure_test.go | 120 ++++++++++++++++- internal/nova/flavors.go | 70 +++++++++- internal/nova/handlers.go | 55 +++++++- internal/nova/keypairs.go | 17 ++- internal/nova/port_lifecycle_test.go | 83 ++++++++++++ internal/nova/volume_attachment.go | 187 +++++++++++++++------------ 7 files changed, 507 insertions(+), 95 deletions(-) create mode 100644 internal/nova/port_lifecycle_test.go diff --git a/internal/neutron/ports.go b/internal/neutron/ports.go index ae46ba95..c0751392 100644 --- a/internal/neutron/ports.go +++ b/internal/neutron/ports.go @@ -1725,3 +1725,73 @@ func derefOr[T any](v *T, fallback T) T { } return *v } + +// ReleaseInstancePorts deletes the ports Neutron allocated for an instance +// (device_owner 'compute:nova'), with the same best-effort host cleanup +// DeletePort does. It is the rollback for a boot that allocated ports and then +// failed on a later network (review 2026-10-13 N-6): the loop used to +// markInstanceError and return with the earlier networks' ports still in the +// database — rows the API still listed for a server that would never exist, +// with leases still reserved, that only an operator could clean up. +// +// Scoped to 'compute:nova' on purpose: a port the tenant created and attached +// (os-interface with port_id) is theirs, not the instance's — releasing a +// failed boot must not delete it (same discriminator as server delete). +func (svc *Service) ReleaseInstancePorts(ctx context.Context, instanceID string) error { + rows, err := svc.pool.Query(ctx, ` + SELECT id::text, network_id::text FROM ports + WHERE device_id = $1 AND device_owner = 'compute:nova' + `, instanceID) + if err != nil { + return fmt.Errorf("looking up instance ports: %w", err) + } + defer rows.Close() + + type released struct{ portID, networkID string } + var toRelease []released + for rows.Next() { + var r released + if err := rows.Scan(&r.portID, &r.networkID); err != nil { + return fmt.Errorf("scanning instance port: %w", err) + } + toRelease = append(toRelease, r) + } + if err := rows.Err(); err != nil { + return fmt.Errorf("iterating instance ports: %w", err) + } + if len(toRelease) == 0 { + return nil + } + + for _, r := range toRelease { + // TAP device, same placement and derivation as DeletePort. + if svc.mode != "stub" { + tapName := "tap-" + r.portID[:8] + if err := svc.tapManager.DeleteTAPDevice(tapName); err != nil { + log.Warn().Err(err).Str("port_id", r.portID).Str("tap", tapName). + Msg("release: failed to delete TAP device") + } + } + if svc.vxlanCoordinator != nil { + if err := svc.vxlanCoordinator.RemoveFDBEntry(ctx, r.portID); err != nil { + log.Warn().Err(err).Str("port_id", r.portID).Msg("release: failed to remove FDB entry") + } + } + } + + if _, err := svc.pool.Exec(ctx, ` + DELETE FROM ports WHERE device_id = $1 AND device_owner = 'compute:nova' + `, instanceID); err != nil { + return fmt.Errorf("deleting instance ports: %w", err) + } + + // Refresh DHCP for the affected networks so the released addresses stop + // being reserved for a server that will not boot. + for _, r := range toRelease { + if _, _, err := svc.applyDHCPForNetwork(ctx, r.networkID, true); err != nil { + log.Warn().Err(err).Str("network_id", r.networkID). + Msg("release: failed to refresh DHCP after port release") + } + } + return nil +} diff --git a/internal/nova/boot_failure_test.go b/internal/nova/boot_failure_test.go index c66f64f3..129ff883 100644 --- a/internal/nova/boot_failure_test.go +++ b/internal/nova/boot_failure_test.go @@ -6,6 +6,8 @@ import ( "errors" "fmt" "net/http" + "os" + "path/filepath" "strings" "sync" "testing" @@ -39,9 +41,11 @@ func (e tenantErr) TenantMessage() string { return string(e) } // fakeNeutron is a NeutronService whose behaviour each test chooses. type fakeNeutron struct { - allocErrFor map[string]error // network id -> error to return - usableErr map[string]error // network id -> pre-flight verdict + allocErrFor map[string]error // network id -> error to return + usableErr map[string]error // network id -> pre-flight verdict + portFor map[string]*neutron.PortInfo // network id -> successful port payload allocCalls int + released []string // instance ids passed to ReleaseInstancePorts (N-6) // The admin flag each call carried. Guarded because allocation runs in the // VM-creation goroutine while the test reads. @@ -58,6 +62,9 @@ func (f *fakeNeutron) AllocatePortForInstance(_ context.Context, networkID, _, _ if err, ok := f.allocErrFor[networkID]; ok && err != nil { return nil, err } + if p, ok := f.portFor[networkID]; ok && p != nil { + return p, nil + } return nil, errors.New("fakeNeutron: no port payload configured") } @@ -71,6 +78,23 @@ func (f *fakeNeutron) NetworkUsableBy(_ context.Context, networkID, _ string, ad return nil } +// ReleaseInstancePorts records the rollback calls (review 2026-10-13 N-6) so a +// test can assert that a failed boot released the ports it had already +// allocated. +func (f *fakeNeutron) ReleaseInstancePorts(_ context.Context, instanceID string) error { + f.mu.Lock() + f.released = append(f.released, instanceID) + f.mu.Unlock() + return nil +} + +// releasedInstances returns the instance ids whose ports were released. +func (f *fakeNeutron) releasedInstances() []string { + f.mu.Lock() + defer f.mu.Unlock() + return append([]string(nil), f.released...) +} + // adminFlags returns the admin flag of every pre-flight and every allocation so far. func (f *fakeNeutron) adminFlags() (usable, alloc []bool) { f.mu.Lock() @@ -501,3 +525,95 @@ func TestFailedBootLeavesADeletableServer(t *testing.T) { t.Errorf("%d ports still reference the deleted server (FR-019)", orphanPorts) } } + +// Review 2026-10-13 N-6: a multi-network boot that fails on a later network used +// to leak the ports already allocated for the earlier ones — rows the API kept +// listing for a server that would never run, with leases still reserved. The +// failure path must release what it allocated (the rollback is scoped to the +// instance's nova-owned ports by the network service). +func TestFailedBootReleasesAllocatedPorts(t *testing.T) { + requireDB(t) + + good := seedBootNetwork(t, seededProjectID, "n6-good-net") + bad := seedBootNetwork(t, seededProjectID, "n6-bad-net") + fake := &fakeNeutron{ + portFor: map[string]*neutron.PortInfo{ + good: {ID: "11111111-1111-1111-1111-111111111111", NetworkID: good, + MAC: "52:54:00:12:34:56", IPAddress: "192.168.100.10"}, + }, + allocErrFor: map[string]error{ + bad: errors.New("subnet exhausted"), + }, + } + _, r := newBootService(t, fake) + + const name = "n6-partial-release" + cleanupServerNamed(t, name) + + body := fmt.Sprintf(`{"server":{"name":%q,"flavorRef":"m1.small","imageRef":%q,"networks":[{"uuid":%q},{"uuid":%q}]}}`, + name, seededImageID(t), good, bad) + if code, respBody := doReq(r, http.MethodPost, "/v2.1/servers", body, bootHdr()); code != http.StatusAccepted && code != http.StatusCreated { + t.Fatalf("create = %d. body: %s", code, respBody) + } + + if got := awaitInstanceStatus(t, name, "ERROR"); got != "ERROR" { + t.Fatalf("status = %q; want ERROR (the later network's failure fails the boot)", got) + } + + var instanceID string + if err := testPool.QueryRow(t.Context(), + `SELECT id::text FROM instances WHERE name = $1 AND project_id = $2`, name, seededProjectID, + ).Scan(&instanceID); err != nil { + t.Fatalf("resolving the failed instance: %v", err) + } + + released := fake.releasedInstances() + if len(released) != 1 || released[0] != instanceID { + t.Errorf("ReleaseInstancePorts calls = %v; want exactly [%s] — the ports allocated for the good network must be rolled back with the failed boot", released, instanceID) + } +} + +// The release must not fire on the success path: a booted server keeps its ports. +// One network, one working port payload; the instance reaches ACTIVE and no +// release is recorded. +func TestSuccessfulBootKeepsItsPorts(t *testing.T) { + requireDB(t) + + good := seedBootNetwork(t, seededProjectID, "n6-ok-net") + fake := &fakeNeutron{ + portFor: map[string]*neutron.PortInfo{ + good: {ID: "22222222-2222-2222-2222-222222222222", NetworkID: good, + MAC: "52:54:00:12:34:57", IPAddress: "192.168.100.11"}, + }, + } + _, r := newBootService(t, fake) + + const name = "n6-keep-ports" + cleanupServerNamed(t, name) + + imgID := seededImageID(t) + // The boot path stages a per-instance overlay over the image's local raw + // base; without the base file the boot fails for reasons unrelated to + // ports, so stage a real one. + basePath := "/var/lib/lightstack/images/image-" + imgID + ".raw" + if err := os.MkdirAll(filepath.Dir(basePath), 0o755); err != nil { + t.Fatalf("staging image base dir: %v", err) + } + if err := os.WriteFile(basePath, make([]byte, 1<<20), 0o644); err != nil { + t.Fatalf("staging image base file: %v", err) + } + t.Cleanup(func() { os.Remove(basePath) }) + + body := fmt.Sprintf(`{"server":{"name":%q,"flavorRef":"m1.small","imageRef":%q,"networks":[{"uuid":%q}]}}`, + name, imgID, good) + if code, respBody := doReq(r, http.MethodPost, "/v2.1/servers", body, bootHdr()); code != http.StatusAccepted && code != http.StatusCreated { + t.Fatalf("create = %d. body: %s", code, respBody) + } + + if got := awaitInstanceStatus(t, name, "ACTIVE"); got != "ACTIVE" { + t.Fatalf("status = %q; want ACTIVE (nothing on this path fails)", got) + } + if released := fake.releasedInstances(); len(released) != 0 { + t.Errorf("ReleaseInstancePorts calls = %v; want none — a booted server keeps its ports", released) + } +} diff --git a/internal/nova/flavors.go b/internal/nova/flavors.go index 13fdc55b..ad8121d4 100644 --- a/internal/nova/flavors.go +++ b/internal/nova/flavors.go @@ -11,6 +11,13 @@ import ( // CreateFlavor handles POST /v2.1/flavors func (svc *Service) CreateFlavor(c *gin.Context) { + // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // deployment-wide resource — members must not create, delete, or + // re-scope it (upstream: admin_api for all flavor management). + if !requireAdmin(c, "flavor-create") { + return + } + var req struct { Flavor struct { Name string `json:"name" binding:"required"` @@ -27,17 +34,24 @@ func (svc *Service) CreateFlavor(c *gin.Context) { return } - // Validate input ranges - if req.Flavor.RAM < 0 { - common.SendError(c, common.NewBadRequestError("RAM must be non-negative")) + // Validate input ranges. Upper bounds are sanity caps (review 2026-10-13 + // L-4), not policy: without them a typo (MB vs GB, or a negative sign + // flip) schedules absurd values that only surface at boot time. 1 TiB of + // RAM, 4096 vCPUs and 1 PiB of disk comfortably exceed any real flavor. + if req.Flavor.RAM < 0 || req.Flavor.RAM > 1<<20 { + common.SendError(c, common.NewBadRequestError("RAM must be between 0 and 1048576 MB")) return } - if req.Flavor.VCPUs < 0 { - common.SendError(c, common.NewBadRequestError("VCPUs must be non-negative")) + if req.Flavor.VCPUs < 0 || req.Flavor.VCPUs > 4096 { + common.SendError(c, common.NewBadRequestError("VCPUs must be between 0 and 4096")) return } - if req.Flavor.Disk < 0 { - common.SendError(c, common.NewBadRequestError("Disk must be non-negative")) + if req.Flavor.Disk < 0 || req.Flavor.Disk > 1<<20 { + common.SendError(c, common.NewBadRequestError("Disk must be between 0 and 1048576 GB")) + return + } + if req.Flavor.Ephemeral < 0 || req.Flavor.Ephemeral > 1<<20 { + common.SendError(c, common.NewBadRequestError("Ephemeral disk must be between 0 and 1048576 GB")) return } @@ -89,6 +103,13 @@ func (svc *Service) CreateFlavor(c *gin.Context) { // DeleteFlavor handles DELETE /v2.1/flavors/:id func (svc *Service) DeleteFlavor(c *gin.Context) { + // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // deployment-wide resource — members must not create, delete, or + // re-scope it (upstream: admin_api for all flavor management). + if !requireAdmin(c, "flavor-delete") { + return + } + flavorID := c.Param("id") ctx := c.Request.Context() @@ -152,6 +173,13 @@ func (svc *Service) GetFlavorExtraSpecs(c *gin.Context) { // CreateFlavorExtraSpecs handles POST /v2.1/flavors/:id/os-extra_specs func (svc *Service) CreateFlavorExtraSpecs(c *gin.Context) { + // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // deployment-wide resource — members must not create, delete, or + // re-scope it (upstream: admin_api for all flavor management). + if !requireAdmin(c, "flavor-extra-specs-create") { + return + } + flavorID := c.Param("id") var req struct { @@ -202,6 +230,13 @@ func (svc *Service) GetFlavorExtraSpecKey(c *gin.Context) { // UpdateFlavorExtraSpecKey handles PUT /v2.1/flavors/:id/os-extra_specs/:key func (svc *Service) UpdateFlavorExtraSpecKey(c *gin.Context) { + // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // deployment-wide resource — members must not create, delete, or + // re-scope it (upstream: admin_api for all flavor management). + if !requireAdmin(c, "flavor-extra-specs-update") { + return + } + flavorID := c.Param("id") key := c.Param("key") @@ -236,6 +271,13 @@ func (svc *Service) UpdateFlavorExtraSpecKey(c *gin.Context) { // DeleteFlavorExtraSpecKey handles DELETE /v2.1/flavors/:id/os-extra_specs/:key func (svc *Service) DeleteFlavorExtraSpecKey(c *gin.Context) { + // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // deployment-wide resource — members must not create, delete, or + // re-scope it (upstream: admin_api for all flavor management). + if !requireAdmin(c, "flavor-extra-specs-delete") { + return + } + flavorID := c.Param("id") key := c.Param("key") @@ -260,6 +302,13 @@ func (svc *Service) DeleteFlavorExtraSpecKey(c *gin.Context) { // FlavorAction handles POST /v2.1/flavors/:id/action func (svc *Service) FlavorAction(c *gin.Context) { + // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // deployment-wide resource — members must not create, delete, or + // re-scope it (upstream: admin_api for all flavor management). + if !requireAdmin(c, "flavor-access-modify") { + return + } + flavorID := c.Param("id") var req map[string]any @@ -326,6 +375,13 @@ func (svc *Service) FlavorAction(c *gin.Context) { // GetFlavorAccess handles GET /v2.1/flavors/:id/os-flavor-access func (svc *Service) GetFlavorAccess(c *gin.Context) { + // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // deployment-wide resource — members must not create, delete, or + // re-scope it (upstream: admin_api for all flavor management). + if !requireAdmin(c, "flavor-access-list") { + return + } + flavorID := c.Param("id") rows, err := svc.pool.Query(c.Request.Context(), diff --git a/internal/nova/handlers.go b/internal/nova/handlers.go index b94af10d..bb6779e5 100644 --- a/internal/nova/handlers.go +++ b/internal/nova/handlers.go @@ -62,6 +62,12 @@ const ( // NeutronService defines the interface for Neutron operations Nova needs. type NeutronService interface { AllocatePortForInstance(ctx context.Context, networkID, projectID, instanceID string, securityGroupIDs []string, admin bool) (any, error) + // ReleaseInstancePorts is AllocatePortForInstance's rollback: it deletes the + // ports Neutron allocated for a boot that failed after allocating them + // (review 2026-10-13 N-6) — the port rows, their host plumbing and their + // DHCP reservations. Scoped to nova-owned ports; a tenant-created port the + // server was told to use is the tenant's, not the boot's. + ReleaseInstancePorts(ctx context.Context, instanceID string) error // NetworkUsableBy reports whether a project may attach a guest to a network, // returning a non-nil error naming the reason if not. admin is whether the caller // holds the administrator role, which decides an unshared external network; both @@ -1113,6 +1119,23 @@ func (svc *Service) CreateServer(c *gin.Context) { // Allocate ports from Neutron for requested networks log.Debug().Str("instance_id", instanceID).Int("network_count", len(req.Server.Networks)).Msg("Allocating ports") var networks []hypervisor.NetworkConfig + portsAllocated := false + bootSucceeded := false + // Any failure from the first successful allocation onward (a later + // network, the boot volume, libvirt itself) leaves the earlier ports + // allocated for a server that will never run. Release them on the way + // out unless the boot completed (review 2026-10-13 N-6: they used to + // leak — still listed by the API, leases still reserved). + defer func() { + if portsAllocated && !bootSucceeded && svc.neutronSvc != nil { + rctx, rcancel := context.WithTimeout(context.Background(), 15*time.Second) + defer rcancel() + if err := svc.neutronSvc.ReleaseInstancePorts(rctx, instanceID); err != nil { + log.Warn().Err(err).Str("instance_id", instanceID). + Msg("failed to release allocated ports after failed boot") + } + } + }() if svc.neutronSvc != nil && len(req.Server.Networks) > 0 { for _, network := range req.Server.Networks { log.Debug().Str("network_id", network.UUID).Msg("Allocating port for network") @@ -1157,6 +1180,7 @@ func (svc *Service) CreateServer(c *gin.Context) { MACAddress: portInfo.MAC, BridgeName: networking.BridgeNameForNetwork(portInfo.NetworkID), }) + portsAllocated = true } } @@ -1335,6 +1359,9 @@ func (svc *Service) CreateServer(c *gin.Context) { } log.Debug().Str("libvirt_uuid", libvirtUUID).Msg("VM created successfully") + // The boot completed: the allocated ports belong to the running + // server now, and the release defer must leave them alone. + bootSucceeded = true logger.Info(). Str("instance_id", instanceID). @@ -2106,11 +2133,29 @@ func (svc *Service) DeleteServer(c *gin.Context) { return } - // Delete the instance's auto-allocated neutron ports (Nova owns them). Without - // this the port lingers after the server is gone, and deleting its network then - // fails with "network has active ports" — which breaks `terraform destroy` - // (SPEC-009 / SC-007) and leaks ports. - if _, e := tx.Exec(ctx, "DELETE FROM ports WHERE device_id = $1", instanceUUID); e != nil { + // The instance's ports, split by who owns them (review 2026-10-13 N-8): + // + // - Ports Neutron allocated for the boot (device_owner 'compute:nova') are + // Nova's to delete. Without this they linger after the server is gone and + // deleting the network then fails with "network has active ports" — which + // breaks `terraform destroy` (SPEC-009 / SC-007) and leaks ports. + // + // - A port the tenant created and attached (os-interface with port_id) is + // the tenant's resource, not the server's: upstream detaches it — clears + // the device, parks it DOWN — rather than deleting it. The blanket + // `DELETE ... WHERE device_id = $1` used to destroy it: the tenant lost a + // port they made, with its fixed IP, by deleting a server. + if _, e := tx.Exec(ctx, ` + UPDATE ports + SET device_id = NULL, device_owner = NULL, status = 'DOWN', updated_at = NOW() + WHERE device_id = $1 AND COALESCE(device_owner, '') <> 'compute:nova' + `, instanceUUID); e != nil { + logger.Error().Err(e).Str("instance_id", instanceUUID).Msg("delete: detach tenant ports failed") + common.SendError(c, common.NewInternalServerError("failed to delete server")) + return + } + if _, e := tx.Exec(ctx, + "DELETE FROM ports WHERE device_id = $1 AND device_owner = 'compute:nova'", instanceUUID); e != nil { logger.Error().Err(e).Str("instance_id", instanceUUID).Msg("delete: remove instance ports failed") common.SendError(c, common.NewInternalServerError("failed to delete server")) return diff --git a/internal/nova/keypairs.go b/internal/nova/keypairs.go index 27d2c81e..a3f9cde7 100644 --- a/internal/nova/keypairs.go +++ b/internal/nova/keypairs.go @@ -126,6 +126,14 @@ func (svc *Service) CreateKeypair(c *gin.Context) { var privateKey *string var fingerprint string + // Bound the import (review 2026-10-13 M-3): an imported public key is + // parsed and stored verbatim — an unbounded string is an unbounded row. + // Authorized-key blobs are a few hundred bytes; 64 KiB is generous. + if len(req.Keypair.PublicKey) > 64<<10 { + common.SendError(c, common.NewBadRequestError("public key too large")) + return + } + if req.Keypair.PublicKey != "" { // Import existing public key publicKey = req.Keypair.PublicKey @@ -163,7 +171,10 @@ func (svc *Service) CreateKeypair(c *gin.Context) { return } - // Insert into database + // Insert into database. The name pre-check above is only a friendly fast + // path — under concurrency the UNIQUE(user_id, name) constraint is the + // real guard, so its violation is the same 409, not a 500 (review + // 2026-10-13 L-3: check-then-insert raced into duplicate keys). now := time.Now() _, err = svc.pool.Exec(c.Request.Context(), ` INSERT INTO keypairs (user_id, name, public_key, fingerprint, created_at) @@ -171,6 +182,10 @@ func (svc *Service) CreateKeypair(c *gin.Context) { `, userID, req.Keypair.Name, publicKey, fingerprint, now) if err != nil { + if common.IsUniqueViolation(err) { + common.SendError(c, common.NewConflictError("Keypair with this name already exists")) + return + } log.Error().Err(err).Str("operation", "create_keypair").Msg("database error") common.SendError(c, common.NewInternalServerError("failed to create keypair")) return diff --git a/internal/nova/port_lifecycle_test.go b/internal/nova/port_lifecycle_test.go new file mode 100644 index 00000000..ab9c2c34 --- /dev/null +++ b/internal/nova/port_lifecycle_test.go @@ -0,0 +1,83 @@ +package nova + +import ( + "context" + "fmt" + "net/http" + "testing" +) + +// Review 2026-10-13 N-8: DeleteServer used a blanket +// `DELETE FROM ports WHERE device_id = $1`, which destroyed ports the TENANT +// created and attached (os-interface with port_id) along with the ones Nova +// allocated for the boot. Upstream deletes only the ports it created and +// detaches the rest — the tenant's port survives the server, parked DOWN with +// no device, reusable or deletable by its owner. This pins both halves. +func TestDeleteServerDetachesTenantPortsButDeletesItsOwn(t *testing.T) { + requireDB(t) + ctx := context.Background() + + netID := seedBootNetwork(t, seededProjectID, "n8-net") + + var instanceID string + if err := testPool.QueryRow(ctx, + `INSERT INTO instances (name, project_id, user_id, flavor_id, status) + VALUES ('n8-server', $1, $2, (SELECT id FROM flavors LIMIT 1), 'ACTIVE') + RETURNING id::text`, seededProjectID, seededUserID).Scan(&instanceID); err != nil { + t.Fatalf("seeding instance: %v", err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM ports WHERE network_id::text = $1`, netID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM instances WHERE id::text = $1`, instanceID) + }) + + seedPort := func(owner string) string { + var id string + if err := testPool.QueryRow(ctx, ` + INSERT INTO ports (name, network_id, project_id, device_id, device_owner, mac_address, status) + VALUES ($1, $2, $3, $4, NULLIF($5, ''), $6, 'ACTIVE') RETURNING id::text`, + "n8-"+owner, netID, seededProjectID, instanceID, owner, fmt.Sprintf("52:54:00:00:00:%02x", len(owner))).Scan(&id); err != nil { + t.Fatalf("seeding %s port: %v", owner, err) + } + return id + } + novaPort := seedPort("compute:nova") + tenantPort := seedPort("") // tenant-created, attached via os-interface + + r := newNovaTestRouter() + owner := map[string]string{ + "X-Test-Project": seededProjectID, "X-Test-User": seededUserID, "X-Test-Roles": "member", + } + if code, body := doReq(r, http.MethodDelete, "/v2.1/servers/"+instanceID, "", owner); code != http.StatusNoContent { + t.Fatalf("deleting the server: code=%d body=%s", code, body) + } + + // Nova's own port is gone with the server. + var novaPortGone bool + if err := testPool.QueryRow(ctx, + `SELECT NOT EXISTS(SELECT 1 FROM ports WHERE id::text = $1)`, novaPort).Scan(&novaPortGone); err != nil { + t.Fatalf("checking the nova-owned port: %v", err) + } + if !novaPortGone { + t.Error("the boot-allocated port (device_owner compute:nova) survived the server delete; it must be deleted with it") + } + + // The tenant's port survives, detached: no device, parked DOWN. + var deviceID, deviceOwner, status string + var exists bool + if err := testPool.QueryRow(ctx, + `SELECT EXISTS(SELECT 1 FROM ports WHERE id::text = $1)`, tenantPort).Scan(&exists); err != nil || !exists { + t.Fatalf("the tenant-created port must survive the server delete (exists=%v err=%v)", exists, err) + } + if err := testPool.QueryRow(ctx, ` + SELECT COALESCE(device_id::text, ''), COALESCE(device_owner, ''), status + FROM ports WHERE id::text = $1`, tenantPort).Scan(&deviceID, &deviceOwner, &status); err != nil { + t.Fatalf("reading the surviving port: %v", err) + } + if deviceID != "" || deviceOwner != "" { + t.Errorf("the surviving port is still attached (device_id=%q device_owner=%q); want it detached", deviceID, deviceOwner) + } + if status != "DOWN" { + t.Errorf("the surviving port's status = %q; want DOWN (detached, as upstream parks it)", status) + } +} diff --git a/internal/nova/volume_attachment.go b/internal/nova/volume_attachment.go index e9c02c27..e7720e72 100644 --- a/internal/nova/volume_attachment.go +++ b/internal/nova/volume_attachment.go @@ -9,6 +9,7 @@ import ( "time" "github.com/cobaltcore-dev/lightstack/internal/common" + "github.com/cobaltcore-dev/lightstack/internal/database" "github.com/cobaltcore-dev/lightstack/pkg/hypervisor" "github.com/cobaltcore-dev/lightstack/pkg/storage" "github.com/gin-gonic/gin" @@ -67,6 +68,20 @@ type VolumeAttachment struct { AttachedAt time.Time `json:"attachedAt"` } +// novaAttachRefusal is a client-facing attach refusal (wrong volume state, +// already attached) carried out of the attach transaction so the handler can +// answer with the right status instead of a generic 500. Mirrors cinder's +// attachRefusal. +type novaAttachRefusal struct { + status int + message string +} + +func (e *novaAttachRefusal) Error() string { return e.message } + +var errNovaVolumeNotFound = errors.New("volume not found") +var errNovaAttachmentNotFound = errors.New("volume attachment not found") + // AttachVolumeRequest represents a volume attachment request type AttachVolumeRequest struct { VolumeAttachment struct { @@ -105,70 +120,76 @@ func (svc *Service) AttachVolume(c *gin.Context) { return } - // Verify volume exists and is available - var volumeStatus string - var attachedToInstance any - err = svc.pool.QueryRow(c.Request.Context(), - "SELECT status, attached_to_instance_id FROM volumes WHERE id = $1 AND project_id = $2", - req.VolumeAttachment.VolumeID, projectID, - ).Scan(&volumeStatus, &attachedToInstance) - - if errors.Is(err, pgx.ErrNoRows) { - common.SendError(c, common.NewNotFoundError("volume")) - return - } - - if volumeStatus != "available" { - common.SendError(c, common.NewBadRequestError(fmt.Sprintf("volume status is %s, must be available", volumeStatus))) - return - } - - if attachedToInstance != nil { - common.SendError(c, common.NewBadRequestError("volume is already attached to an instance")) - return - } - - // Auto-assign device if not provided - device := req.VolumeAttachment.Device - if device == "" { - device, err = svc.getNextAvailableDevice(c.Request.Context(), instanceID) - if err != nil { - log.Error().Err(err).Str("operation", "get_next_device").Msg("device assignment error") - common.SendError(c, common.NewInternalServerError("failed to assign device")) - return + // Verify volume exists and is available, create the attachment and flip + // the volume to in-use — all in one transaction with the volume row + // locked (review 2026-10-13 N-9): the old check-then-act let the loser of + // a concurrent double-attach surface as a 500 from the UNIQUE constraint, + // and a failure between the INSERT and the UPDATE left the volume row + // inconsistent with its attachment. Mirrors cinder's attach CAS. + var device string + attachmentID := uuid.NewString() + var now time.Time + err = database.WithTx(c.Request.Context(), svc.pool, func(tx pgx.Tx) error { + var volumeStatus string + var attachedToInstance any + if err := tx.QueryRow(c.Request.Context(), + "SELECT status, attached_to_instance_id FROM volumes WHERE id = $1 AND project_id = $2 FOR UPDATE", + req.VolumeAttachment.VolumeID, projectID, + ).Scan(&volumeStatus, &attachedToInstance); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return errNovaVolumeNotFound + } + return err + } + if volumeStatus != "available" { + return &novaAttachRefusal{http.StatusBadRequest, + fmt.Sprintf("volume status is %s, must be available", volumeStatus)} + } + if attachedToInstance != nil { + return &novaAttachRefusal{http.StatusConflict, "volume is already attached to an instance"} } - } - - // Create attachment record - attachmentID := uuid.New().String() - now := time.Now() - - _, err = svc.pool.Exec(c.Request.Context(), ` - INSERT INTO volume_attachments (id, volume_id, instance_id, device, attached_at) - VALUES ($1, $2, $3, $4, $5) - `, attachmentID, req.VolumeAttachment.VolumeID, instanceID, device, now) - - if err != nil { - log.Error().Err(err).Str("operation", "create_volume_attachment").Msg("database error") - common.SendError(c, common.NewInternalServerError("failed to create volume attachment")) - return - } - // Update volume status - _, err = svc.pool.Exec(c.Request.Context(), ` - UPDATE volumes - SET attached_to_instance_id = $1, status = $2, updated_at = $3 - WHERE id = $4 - `, instanceID, "in-use", now, req.VolumeAttachment.VolumeID) + // Auto-assign device if not provided + device = req.VolumeAttachment.Device + if device == "" { + device, err = svc.getNextAvailableDevice(c.Request.Context(), instanceID) + if err != nil { + return fmt.Errorf("device assignment: %w", err) + } + } + now = time.Now() + if _, err := tx.Exec(c.Request.Context(), ` + INSERT INTO volume_attachments (id, volume_id, instance_id, device, attached_at) + VALUES ($1, $2, $3, $4, $5) + `, attachmentID, req.VolumeAttachment.VolumeID, instanceID, device, now); err != nil { + return err + } + if _, err := tx.Exec(c.Request.Context(), ` + UPDATE volumes + SET attached_to_instance_id = $1, status = $2, updated_at = $3 + WHERE id = $4 + `, instanceID, "in-use", now, req.VolumeAttachment.VolumeID); err != nil { + return err + } + return nil + }) if err != nil { - // Rollback attachment (best-effort; the update error is the - // user-facing one) - if _, rbErr := svc.pool.Exec(c.Request.Context(), "DELETE FROM volume_attachments WHERE id = $1", attachmentID); rbErr != nil { - log.Warn().Err(rbErr).Str("attachment_id", attachmentID).Msg("rollback: failed to delete volume attachment after volume update failure") + var refusal *novaAttachRefusal + if errors.As(err, &refusal) { + if refusal.status == http.StatusConflict { + common.SendError(c, common.NewConflictError(refusal.message)) + } else { + common.SendError(c, common.NewBadRequestError(refusal.message)) + } + return + } + if errors.Is(err, errNovaVolumeNotFound) { + common.SendError(c, common.NewNotFoundError("volume")) + return } - log.Error().Err(err).Str("operation", "update_volume_status").Msg("database error") - common.SendError(c, common.NewInternalServerError("failed to update volume status")) + log.Error().Err(err).Str("operation", "attach_volume_tx").Msg("database error") + common.SendError(c, common.NewInternalServerError("failed to attach volume")) return } @@ -314,29 +335,35 @@ func (svc *Service) DetachVolume(c *gin.Context) { } } - // Delete attachment record - _, err = svc.pool.Exec(c.Request.Context(), - "DELETE FROM volume_attachments WHERE id = $1", - attachmentID, - ) - - if err != nil { - log.Error().Err(err).Str("operation", "delete_volume_attachment").Msg("database error") - common.SendError(c, common.NewInternalServerError("failed to delete volume attachment")) - return - } - - // Update volume status + // Delete the attachment and release the volume in one transaction (review + // 2026-10-13 N-10): as two statements, a failure after the DELETE left the + // volume stranded in-use with no attachment row — unfixable through the + // API, since detach could no longer find the attachment to remove. now := time.Now() - _, err = svc.pool.Exec(c.Request.Context(), ` - UPDATE volumes - SET attached_to_instance_id = NULL, status = $1, updated_at = $2 - WHERE id = $3 - `, "available", now, volumeID) - + err = database.WithTx(c.Request.Context(), svc.pool, func(tx pgx.Tx) error { + tag, err := tx.Exec(c.Request.Context(), + "DELETE FROM volume_attachments WHERE id = $1", + attachmentID) + if err != nil { + return err + } + if tag.RowsAffected() == 0 { + return errNovaAttachmentNotFound + } + _, err = tx.Exec(c.Request.Context(), ` + UPDATE volumes + SET attached_to_instance_id = NULL, status = $1, updated_at = $2 + WHERE id = $3 + `, "available", now, volumeID) + return err + }) if err != nil { - log.Error().Err(err).Str("operation", "update_volume_status_detach").Msg("database error") - common.SendError(c, common.NewInternalServerError("failed to update volume status")) + if errors.Is(err, errNovaAttachmentNotFound) { + common.SendError(c, common.NewNotFoundError("volume attachment")) + return + } + log.Error().Err(err).Str("operation", "detach_volume_tx").Msg("database error") + common.SendError(c, common.NewInternalServerError("failed to detach volume")) return } From b4ad7ff841efbf4a7154a6ea122de5ccb7e16b92 Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Tue, 13 Oct 2026 10:32:54 +0000 Subject: [PATCH 04/26] fix(cinder): admin-gate volume types, QoS specs and the manage surface; constant-time transfer keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Comprehensive review 2026-10-13 (H-3, L-1): H-3 — volume types, QoS specs and manage/unmanage were authentication-only. Any member could create volume types every tenant then booted from, rewrite extra specs, add its project to a private type (VolumeTypeAction), define QoS specs, and import host backings into its own quota (os-volume-manage addresses operator storage). All admin-only now, matching upstream's volume_extension:* and volume_admin_actions:* policies; reads of the type catalog stay open since tenants need them to pick a type at create. Pinned by TestOperatorSurfacesAdminOnly; the concurrent managed-import quota test drives the route as an administrator now (quota enforcement itself unchanged). L-1 — the volume-transfer auth key was compared with a plain string equality, which short-circuits on the first differing byte; a transfer id is cheap to enumerate, so the key could be recovered byte-by-byte from response timing. Now crypto/subtle. ConstantTimeCompare. Unit suite (incl. DB-backed): PASS. Signed-off-by: Uwe Schwaeke --- internal/cinder/manage.go | 30 ++++++++++++ internal/cinder/manage_authz_test.go | 45 +++++++++++++++++ internal/cinder/qos_specs.go | 30 ++++++++++++ internal/cinder/quota_enforcement_test.go | 19 +++++-- internal/cinder/transfers.go | 8 ++- internal/cinder/volume_type_access.go | 9 ++++ internal/cinder/volume_types.go | 60 +++++++++++++++++++++++ 7 files changed, 196 insertions(+), 5 deletions(-) create mode 100644 internal/cinder/manage_authz_test.go diff --git a/internal/cinder/manage.go b/internal/cinder/manage.go index 87ad121c..91034b64 100644 --- a/internal/cinder/manage.go +++ b/internal/cinder/manage.go @@ -13,6 +13,16 @@ import ( // ManageVolume handles POST /v3/os-volume-manage func (svc *Service) ManageVolume(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + projectID := c.GetString("project_id") var req struct { @@ -72,6 +82,16 @@ func (svc *Service) ManageVolume(c *gin.Context) { // ListManageableVolumes handles GET /v3/manageable_volumes func (svc *Service) ListManageableVolumes(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + host := c.Query("host") if host == "" { @@ -88,6 +108,16 @@ func (svc *Service) ListManageableVolumes(c *gin.Context) { // UnmanageVolume handles volume action: os-unmanage func (svc *Service) UnmanageVolume(c *gin.Context, volumeID string) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + projectID := c.GetString("project_id") // Verify volume exists and belongs to project diff --git a/internal/cinder/manage_authz_test.go b/internal/cinder/manage_authz_test.go new file mode 100644 index 00000000..10fcade2 --- /dev/null +++ b/internal/cinder/manage_authz_test.go @@ -0,0 +1,45 @@ +package cinder + +import ( + "net/http" + "testing" +) + +// TestOperatorSurfacesAdminOnly pins review 2026-10-13 H-3: volume types, QoS +// specs and the manage/unmanage surface shape storage for every tenant (and +// manage addresses host-level backends). They were authentication-only — a +// plain member could create volume types every tenant then booted from, add +// its project to a private type, and import host backings into its quota. +// Reads of volume types stay open: tenants need them to pick one at create. +func TestOperatorSurfacesAdminOnly(t *testing.T) { + requireDB(t) + r := newCinderTestRouter() + + const project = "00000000-0000-0000-0000-000000000002" // seeded default project + member := map[string]string{"X-Test-Project": project, "X-Test-Roles": "member"} + admin := map[string]string{"X-Test-Project": project, "X-Test-Roles": "admin"} + + memberDenied := []struct{ name, method, path, body string }{ + {"create volume type", http.MethodPost, "/v3/types", `{"volume_type":{"name":"member-type"}}`}, + {"manage a volume", http.MethodPost, "/v3/os-volume-manage", `{"volume":{"host":"h1","ref":{"source-name":"x"}}}`}, + {"list manageable volumes", http.MethodGet, "/v3/manageable_volumes", ""}, + {"create a QoS spec", http.MethodPost, "/v3/qos-specs", `{"qos_specs":{"name":"member-qos"}}`}, + } + for _, tc := range memberDenied { + t.Run(tc.name, func(t *testing.T) { + if code, body := doReq(r, tc.method, tc.path, tc.body, member); code != http.StatusForbidden { + t.Errorf("%s %s as member: code=%d body=%s; want 403", tc.method, tc.path, code, body) + } + }) + } + + // The type catalog stays readable for members (they pick a type at create). + if code, body := doReq(r, http.MethodGet, "/v3/types", "", member); code != http.StatusOK { + t.Errorf("GET /v3/types as member: code=%d body=%s; want 200 (reads stay open)", code, body) + } + + // And the admin keeps the write paths. + if code, body := doReq(r, http.MethodPost, "/v3/types", `{"volume_type":{"name":"authz-admin-type"}}`, admin); code != http.StatusCreated && code != http.StatusOK { + t.Errorf("create volume type as admin: code=%d body=%s; want 2xx", code, body) + } +} diff --git a/internal/cinder/qos_specs.go b/internal/cinder/qos_specs.go index 224227f1..a464f79d 100644 --- a/internal/cinder/qos_specs.go +++ b/internal/cinder/qos_specs.go @@ -65,6 +65,16 @@ func (svc *Service) ListQoSSpecs(c *gin.Context) { // CreateQoSSpec creates a new QoS specification func (svc *Service) CreateQoSSpec(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + var req struct { QosSpecs struct { Name string `json:"name" binding:"required"` @@ -151,6 +161,16 @@ func (svc *Service) GetQoSSpec(c *gin.Context) { // UpdateQoSSpec updates a QoS specification func (svc *Service) UpdateQoSSpec(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + qosID := c.Param("id") projectID := c.GetString("project_id") @@ -232,6 +252,16 @@ func (svc *Service) UpdateQoSSpec(c *gin.Context) { // DeleteQoSSpec deletes a QoS specification func (svc *Service) DeleteQoSSpec(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + qosID := c.Param("id") projectID := c.GetString("project_id") diff --git a/internal/cinder/quota_enforcement_test.go b/internal/cinder/quota_enforcement_test.go index 97fc501e..65dbb178 100644 --- a/internal/cinder/quota_enforcement_test.go +++ b/internal/cinder/quota_enforcement_test.go @@ -529,15 +529,25 @@ func TestQuotaConcurrentCreates(t *testing.T) { // assertion is the one that matters, since it holds even if the HTTP codes race. // concurrentQuotaReqs fires n identical requests at once and reports how many were -// accepted. +// accepted. admin marks the caller's role: os-volume-manage is admin-only (review +// 2026-10-13 H-3) while still consuming the target project's quota, so its test +// drives the route as an administrator. func concurrentQuotaReqs(r *gin.Engine, n int, method, path, body string) []int { + return concurrentQuotaReqsAs(r, n, method, path, body, "member") +} + +func concurrentQuotaReqsAs(r *gin.Engine, n int, method, path, body, roles string) []int { codes := make([]int, n) var wg sync.WaitGroup for i := range codes { wg.Add(1) go func(i int) { defer wg.Done() - codes[i], _ = quotaReq(r, method, path, body, quotaProject) + codes[i], _ = doReq(r, method, path, body, map[string]string{ + "X-Test-Project": quotaProject, + "X-Test-User": "00000000-0000-0000-0000-000000000001", + "X-Test-Roles": roles, + }) }(i) } wg.Wait() @@ -651,6 +661,9 @@ func TestQuotaConcurrentExtends(t *testing.T) { // and with it the old checkVolumeQuota: that helper ran the ceiling read, the count check // and the gigabytes check as three separate round trips under two separate lock // acquisitions, so its own three parts were not atomic with each other either. +// The caller is an administrator because os-volume-manage is admin-only (review +// 2026-10-13 H-3) — upstream's volume_admin_actions:manage — while the import still +// consumes the target project's quota. func TestQuotaConcurrentManagedVolumes(t *testing.T) { requireDB(t) seedQuotaProject(t) @@ -659,7 +672,7 @@ func TestQuotaConcurrentManagedVolumes(t *testing.T) { r := quotaRouter(t) body := `{"volume":{"host":"h1","ref":{"source-name":"race"},"name":"race"}}` - codes := concurrentQuotaReqs(r, 4, http.MethodPost, "/v3/"+quotaProject+"/os-volume-manage", body) + codes := concurrentQuotaReqsAs(r, 4, http.MethodPost, "/v3/"+quotaProject+"/os-volume-manage", body, "admin") if n := countAccepted(codes); n != 1 { t.Errorf("concurrent managed imports against one remaining allowance: %d accepted (codes %v); want exactly 1", n, codes) diff --git a/internal/cinder/transfers.go b/internal/cinder/transfers.go index ec6fce43..ebee9a48 100644 --- a/internal/cinder/transfers.go +++ b/internal/cinder/transfers.go @@ -2,6 +2,7 @@ package cinder import ( "crypto/rand" + "crypto/subtle" "encoding/hex" "errors" "fmt" @@ -318,8 +319,11 @@ func (svc *Service) AcceptVolumeTransfer(c *gin.Context) { return } - // Verify auth key - if req.Accept.AuthKey != storedAuthKey { + // Verify auth key. Constant-time (review 2026-10-13 L-1): a plain string + // compare short-circuits on the first differing byte, and a transfer id is + // cheap to enumerate, so the key could be recovered byte-by-byte from + // response timing. + if subtle.ConstantTimeCompare([]byte(req.Accept.AuthKey), []byte(storedAuthKey)) != 1 { common.SendError(c, common.NewBadRequestError("invalid auth key")) return } diff --git a/internal/cinder/volume_type_access.go b/internal/cinder/volume_type_access.go index ca72e229..100e1ae4 100644 --- a/internal/cinder/volume_type_access.go +++ b/internal/cinder/volume_type_access.go @@ -14,6 +14,15 @@ import ( func (svc *Service) VolumeTypeAction(c *gin.Context) { typeID := c.Param("id") + // Admin-only (review 2026-10-13 H-3): project access to a private volume + // type is a cross-tenant grant — without the check any member could add + // their project to (or strip another project from) any private type. + // Checked before the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + var req map[string]any if err := c.ShouldBindJSON(&req); err != nil { common.SendError(c, common.NewBadRequestError("invalid request body")) diff --git a/internal/cinder/volume_types.go b/internal/cinder/volume_types.go index 5b41cfb0..01e7ee29 100644 --- a/internal/cinder/volume_types.go +++ b/internal/cinder/volume_types.go @@ -14,6 +14,16 @@ import ( // CreateVolumeType creates a new volume type func (svc *Service) CreateVolumeType(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + var req struct { VolumeType struct { Name string `json:"name" binding:"required"` @@ -57,6 +67,16 @@ func (svc *Service) CreateVolumeType(c *gin.Context) { // UpdateVolumeType updates a volume type func (svc *Service) UpdateVolumeType(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + typeID := c.Param("id") var req struct { @@ -120,6 +140,16 @@ func (svc *Service) UpdateVolumeType(c *gin.Context) { // DeleteVolumeType deletes a volume type func (svc *Service) DeleteVolumeType(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + typeID := c.Param("id") result, err := svc.pool.Exec(c.Request.Context(), @@ -171,6 +201,16 @@ func (svc *Service) ListVolumeTypeExtraSpecs(c *gin.Context) { // CreateVolumeTypeExtraSpecs handles POST /v3/types/:id/extra_specs func (svc *Service) CreateVolumeTypeExtraSpecs(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + typeID := c.Param("id") var req struct { @@ -234,6 +274,16 @@ func (svc *Service) GetVolumeTypeExtraSpecKey(c *gin.Context) { // UpdateVolumeTypeExtraSpecKey handles PUT /v3/types/:id/extra_specs/:key func (svc *Service) UpdateVolumeTypeExtraSpecKey(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + typeID := c.Param("id") key := c.Param("key") @@ -296,6 +346,16 @@ func (svc *Service) UpdateVolumeTypeExtraSpecKey(c *gin.Context) { // DeleteVolumeTypeExtraSpecKey handles DELETE /v3/types/:id/extra_specs/:key func (svc *Service) DeleteVolumeTypeExtraSpecKey(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // manage/unmanage surface shape storage for every tenant and address + // host-level backends (upstream: volume_extension:* and + // volume_extension:volume_admin_actions:*, all admin_api). Checked before + // the body is bound so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + typeID := c.Param("id") key := c.Param("key") From e2ff794f89f95f8be4724ece941057cf4597b49f Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Tue, 13 Oct 2026 10:32:54 +0000 Subject: [PATCH 05/26] fix(neutron,metadata): collision-free IP allocation, shared-network gate, working network_data.json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Comprehensive review 2026-10-13 (N-4, M-4, N-7): N-4 — port IP allocation was a purely random draw from the subnet, so two ports in one subnet landed on the same address at birthday- problem rates: two MACs claiming one IP, dnsmasq handing it to one of them, and a guest that could not reliably reach anything. New Service.allocateFreeIP draws random candidates until one is free of every port's fixed_ips AND the subnet's gateway, used by both CreatePort and AllocatePortForInstance; a full subnet is an honest refusal instead of a duplicate. Adjacent defect found and fixed while in here: allocateIP's candidate range [10, maxOffset) goes negative for subnets with fewer than ~12 addresses, and crypto/rand.Int PANICS on a non-positive bound — tiny subnets now fall back to the whole usable range. Pinned by TestAllocateFreeIP* (4 tests). M-4 — CreateNetwork accepted shared=true from anyone, letting a member open their network to every project in the deployment (and every project's guests to it). Now gated exactly like router:external on create: only true is refused to a member, so clients echoing the default keep working (upstream's explicit-set rule). UpdateNetwork does not accept shared at all, so no gap there. Pinned by TestNonAdminCannotCreateSharedNetwork. N-7 — the metadata service's network_data.json selected p.fixed_ip, a column that does not exist (ports store fixed_ips as a JSONB array), so the query failed and every guest fetching it got a 500 — cloud-init fell back to DHCP or configured nothing. Rewritten to resolve each port's addresses through the JSONB and their own subnets: one link per port (not per address, and not fanned out per subnet-on-network as the old join would have), subnet-resolved netmask/gateway/DNS, and a link even for an address-less port. Pinned by TestNetworkDataJSONServesPortAddresses. Race-enabled unit suite (incl. DB-backed): PASS. Signed-off-by: Uwe Schwaeke --- internal/metadata/network_data_test.go | 101 ++++++++++++++++ internal/metadata/service.go | 49 +++++--- internal/neutron/allocate_free_ip_test.go | 134 ++++++++++++++++++++++ internal/neutron/network.go | 9 ++ internal/neutron/ports.go | 98 ++++++++++++++-- internal/neutron/shared_gate_test.go | 35 ++++++ 6 files changed, 404 insertions(+), 22 deletions(-) create mode 100644 internal/metadata/network_data_test.go create mode 100644 internal/neutron/allocate_free_ip_test.go create mode 100644 internal/neutron/shared_gate_test.go diff --git a/internal/metadata/network_data_test.go b/internal/metadata/network_data_test.go new file mode 100644 index 00000000..f2d429dc --- /dev/null +++ b/internal/metadata/network_data_test.go @@ -0,0 +1,101 @@ +package metadata + +import ( + "encoding/json" + "net/http" + "testing" + + "github.com/google/uuid" +) + +// Review 2026-10-13 N-7: GetNetworkDataJSON selected `p.fixed_ip`, a column +// that does not exist — ports store `fixed_ips` as a JSONB array — so the +// query failed and every guest fetching network_data.json got a 500, leaving +// cloud-init to fall back to DHCP or configure nothing. This pins the fixed +// shape: one link per port, one network entry per address, resolved through +// the address's own subnet (cidr, netmask, gateway, DNS). +func TestNetworkDataJSONServesPortAddresses(t *testing.T) { + requireDB(t) + ctx := t.Context() + + instanceID := seedInstance(t, nil) + + var networkID string + if err := testPool.QueryRow(ctx, + `INSERT INTO networks (name, project_id, status, admin_state_up) + VALUES ('md-net', $1, 'ACTIVE', true) RETURNING id::text`, + seededProjectID).Scan(&networkID); err != nil { + t.Fatalf("seed network: %v", err) + } + t.Cleanup(func() { testPool.Exec(ctx, "DELETE FROM networks WHERE id::text = $1", networkID) }) + + var subnetID string + if err := testPool.QueryRow(ctx, + `INSERT INTO subnets (name, network_id, project_id, cidr, gateway_ip, dns_nameservers) + VALUES ('md-sub', $1, $2, '192.168.42.0/24', '192.168.42.1', ARRAY['1.1.1.1']) RETURNING id::text`, + networkID, seededProjectID).Scan(&subnetID); err != nil { + t.Fatalf("seed subnet: %v", err) + } + + portID := uuid.NewString() + fixed := `[{"subnet_id":"` + subnetID + `","ip_address":"192.168.42.77"},` + + `{"subnet_id":"` + subnetID + `","ip_address":"192.168.42.78"}]` + if _, err := testPool.Exec(ctx, + `INSERT INTO ports (id, name, network_id, project_id, device_id, device_owner, mac_address, status, fixed_ips) + VALUES ($1, 'md-port', $2, $3, $4, 'compute:nova', '52:54:00:0a:0b:0c', 'ACTIVE', $5::jsonb)`, + portID, networkID, seededProjectID, instanceID, fixed); err != nil { + t.Fatalf("seed port: %v", err) + } + t.Cleanup(func() { testPool.Exec(ctx, "DELETE FROM ports WHERE id::text = $1", portID) }) + + r := newMetadataTestRouter() + code, body := metaGet(r, "/openstack/latest/network_data.json", instanceID) + if code != http.StatusOK { + t.Fatalf("network_data.json: code=%d body=%s; want 200 (the N-7 defect answered 500)", code, body) + } + + var doc struct { + Links []struct { + ID string `json:"id"` + EthernetMACAddress string `json:"ethernet_mac_address"` + } `json:"links"` + Networks []struct { + ID string `json:"id"` + Link string `json:"link"` + IPAddress string `json:"ip_address"` + Netmask string `json:"netmask"` + Gateway string `json:"gateway"` + DNSServers []string `json:"dns_nameservers"` + } `json:"networks"` + } + if err := json.Unmarshal([]byte(body), &doc); err != nil { + t.Fatalf("network_data.json is not JSON: %v (%s)", err, body) + } + + // One port with two addresses: one interface, two network entries. + if len(doc.Links) != 1 { + t.Errorf("links = %d; want 1 (one link per port, not per address): %s", len(doc.Links), body) + } + if len(doc.Links) == 1 && doc.Links[0].EthernetMACAddress != "52:54:00:0a:0b:0c" { + t.Errorf("link mac = %q; want the port's", doc.Links[0].EthernetMACAddress) + } + if len(doc.Networks) != 2 { + t.Fatalf("networks = %d; want 2 (one per address): %s", len(doc.Networks), body) + } + ips := map[string]bool{} + for _, n := range doc.Networks { + ips[n.IPAddress] = true + if n.Netmask != "255.255.255.0" { + t.Errorf("network %s: netmask = %q; want 255.255.255.0", n.IPAddress, n.Netmask) + } + if n.Gateway != "192.168.42.1" { + t.Errorf("network %s: gateway = %q; want the subnet's", n.IPAddress, n.Gateway) + } + if len(n.DNSServers) != 1 || n.DNSServers[0] != "1.1.1.1" { + t.Errorf("network %s: dns = %v; want [1.1.1.1]", n.IPAddress, n.DNSServers) + } + } + if !ips["192.168.42.77"] || !ips["192.168.42.78"] { + t.Errorf("networks missing the port's addresses: %s", body) + } +} diff --git a/internal/metadata/service.go b/internal/metadata/service.go index 4ada7f37..6cb0f7ed 100644 --- a/internal/metadata/service.go +++ b/internal/metadata/service.go @@ -284,12 +284,25 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { return } - // Query all ports for this instance + // Query the instance's ports and, through each port's fixed_ips JSONB, + // its addresses and subnets (review 2026-10-13 N-7: this used to select + // `p.fixed_ip`, a column that does not exist — ports store `fixed_ips` as + // a JSONB array — so every request failed and network_data.json answered + // 500, and cloud-init fell back to DHCP or configured nothing). + // + // One row per (port, address): a link per port (one MAC, one interface), + // a network per address. The lateral is LEFT so a port with no address + // still yields its link; subnets resolve by the address's own subnet_id, + // not by the network, so a network with several subnets no longer fans a + // single port into duplicate rows. rows, err := svc.pool.Query(c.Request.Context(), ` - SELECT p.id, p.mac_address, p.fixed_ip, p.network_id, n.name, s.cidr, s.gateway_ip, s.dns_nameservers + SELECT p.id, p.mac_address, COALESCE(fx->>'ip_address', ''), p.network_id, n.name, + COALESCE(s.cidr, ''), COALESCE(s.gateway_ip, ''), + COALESCE(s.dns_nameservers, ARRAY[]::text[]) FROM ports p JOIN networks n ON p.network_id = n.id - LEFT JOIN subnets s ON p.network_id = s.network_id + LEFT JOIN LATERAL jsonb_array_elements(COALESCE(p.fixed_ips, '[]'::jsonb)) fx ON true + LEFT JOIN subnets s ON fx->>'subnet_id' IS NOT NULL AND s.id::text = fx->>'subnet_id' WHERE p.device_id = $1 ORDER BY p.created_at `, instanceID) @@ -305,6 +318,7 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { var networks []gin.H linkID := 0 + currentPort := "" for rows.Next() { var portID, mac, ip, networkID, networkName, cidr, gateway string var dnsServers []string @@ -315,18 +329,28 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { return } - // Create link (interface) - links = append(links, gin.H{ - "id": fmt.Sprintf("tap%d", linkID), - "type": "bridge", - "ethernet_mac_address": mac, - "mtu": 1500, - }) + // One link per port, not per row: a port with two addresses is still + // one interface with one MAC. + if portID != currentPort { + currentPort = portID + links = append(links, gin.H{ + "id": fmt.Sprintf("tap%d", linkID), + "type": "bridge", + "ethernet_mac_address": mac, + "mtu": 1500, + }) + linkID++ + } + + // An address-less port contributes its link and nothing else. + if ip == "" { + continue + } // Create network config netConfig := gin.H{ - "id": fmt.Sprintf("network%d", linkID), - "link": fmt.Sprintf("tap%d", linkID), + "id": fmt.Sprintf("network%d", linkID-1), + "link": fmt.Sprintf("tap%d", linkID-1), "type": "ipv4", "ip_address": ip, "netmask": cidrToNetmask(cidr), // Convert /24 → "255.255.255.0" @@ -341,7 +365,6 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { } networks = append(networks, netConfig) - linkID++ } // Fail rather than serve a partial network_data.json: a missing interface would // leave the guest misconfigured, and cloud-init retries on error. diff --git a/internal/neutron/allocate_free_ip_test.go b/internal/neutron/allocate_free_ip_test.go new file mode 100644 index 00000000..9137b0e5 --- /dev/null +++ b/internal/neutron/allocate_free_ip_test.go @@ -0,0 +1,134 @@ +package neutron + +import ( + "context" + "encoding/json" + "testing" + + "github.com/google/uuid" +) + +// seedN4Subnet inserts a network + subnet and returns their ids. +func seedN4Subnet(t *testing.T, cidr, gateway string) (networkID, subnetID string) { + t.Helper() + ctx := context.Background() + if err := testPool.QueryRow(ctx, + `INSERT INTO networks (name, project_id, status, admin_state_up) + VALUES ($1, $2, 'ACTIVE', true) RETURNING id::text`, + "n4-net-"+uuid.NewString()[:8], seededProjectID).Scan(&networkID); err != nil { + t.Fatalf("seeding network: %v", err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM networks WHERE id::text = $1`, networkID) + }) + if err := testPool.QueryRow(ctx, + `INSERT INTO subnets (name, network_id, project_id, cidr, gateway_ip) + VALUES ('n4-sub', $1, $2, $3, NULLIF($4, '')) RETURNING id::text`, + networkID, seededProjectID, cidr, gateway).Scan(&subnetID); err != nil { + t.Fatalf("seeding subnet: %v", err) + } + return networkID, subnetID +} + +// seedN4Port inserts a port holding one fixed_ip in the subnet. +func seedN4Port(t *testing.T, networkID, subnetID, ip string) { + t.Helper() + fixed, _ := json.Marshal([]map[string]any{ + {"subnet_id": subnetID, "ip_address": ip}, + }) + if _, err := testPool.Exec(context.Background(), + `INSERT INTO ports (name, network_id, project_id, mac_address, status, fixed_ips) + VALUES ('n4-port', $1, $2, $3, 'ACTIVE', $4)`, + networkID, seededProjectID, "52:54:00:aa:bb:cc", fixed); err != nil { + t.Fatalf("seeding port: %v", err) + } +} + +func newN4Service(t *testing.T) *Service { + t.Helper() + svc := &Service{pool: testPool} + return svc +} + +// TestAllocateFreeIPAvoidsUsedAddresses pins review 2026-10-13 N-4: allocation +// used to be a purely random draw, so two ports in one subnet landed on the +// same address at birthday-problem rates — two MACs claiming one IP. On a /28 +// with 12 of 14 candidates already held, the old code duplicated an address +// ~86% of the time; the fixed allocator must never return a held one. +func TestAllocateFreeIPAvoidsUsedAddresses(t *testing.T) { + requireDB(t) + _, subnetID := seedN4Subnet(t, "192.168.250.0/28", "") + + held := map[string]bool{"192.168.250.1": true, "192.168.250.14": true} + // Hold every candidate except .13 (offset 10..13 in a /28: 10,11,12,13). + for _, ip := range []string{"192.168.250.10", "192.168.250.11", "192.168.250.12"} { + held[ip] = true + seedN4Port(t, mustN4NetworkID(t, subnetID), subnetID, ip) + } + + svc := newN4Service(t) + for range 20 { + ip, err := svc.allocateFreeIP(context.Background(), subnetID, "192.168.250.0/28") + if err != nil { + t.Fatalf("allocateFreeIP: %v", err) + } + if held[ip] { + t.Fatalf("allocated %s, which another port already holds — the N-4 collision", ip) + } + } +} + +// TestAllocateFreeIPSkipsGateway: the gateway belongs to the subnet; a port +// claiming it breaks routing for everyone in the subnet. +func TestAllocateFreeIPSkipsGateway(t *testing.T) { + requireDB(t) + _, subnetID := seedN4Subnet(t, "192.168.251.0/29", "192.168.251.1") + + svc := newN4Service(t) + for range 20 { + ip, err := svc.allocateFreeIP(context.Background(), subnetID, "192.168.251.0/29") + if err != nil { + t.Fatalf("allocateFreeIP: %v", err) + } + if ip == "192.168.251.1" { + t.Fatalf("allocated the subnet's own gateway address") + } + } +} + +// TestAllocateFreeIPExhaustedSubnet: a full subnet is an error, not a silent +// duplicate or an infinite loop. A /30 has two usable addresses; holding both +// must exhaust it. +func TestAllocateFreeIPExhaustedSubnet(t *testing.T) { + requireDB(t) + networkID, subnetID := seedN4Subnet(t, "192.168.252.0/30", "") + seedN4Port(t, networkID, subnetID, "192.168.252.1") + seedN4Port(t, networkID, subnetID, "192.168.252.2") + + svc := newN4Service(t) + if _, err := svc.allocateFreeIP(context.Background(), subnetID, "192.168.252.0/30"); err == nil { + t.Fatal("allocating from a subnet whose every usable address is held must fail") + } +} + +// TestAllocateIPTinySubnetDoesNotPanic pins the adjacent defect found while +// fixing N-4: the old candidate range [10, maxOffset) went negative for +// subnets with fewer than ~12 addresses, and crypto/rand.Int PANICS on a +// non-positive bound. +func TestAllocateIPTinySubnetDoesNotPanic(t *testing.T) { + for _, cidr := range []string{"192.168.253.0/30", "192.168.253.0/29", "192.168.253.0/28"} { + if ip := allocateIP(cidr); ip == "" { + t.Errorf("allocateIP(%s) = \"\"; want a usable address", cidr) + } + } +} + +func mustN4NetworkID(t *testing.T, subnetID string) string { + t.Helper() + var id string + if err := testPool.QueryRow(context.Background(), + `SELECT network_id::text FROM subnets WHERE id::text = $1`, subnetID).Scan(&id); err != nil { + t.Fatalf("resolving the subnet's network: %v", err) + } + return id +} diff --git a/internal/neutron/network.go b/internal/neutron/network.go index 96176d91..02303af7 100644 --- a/internal/neutron/network.go +++ b/internal/neutron/network.go @@ -570,8 +570,17 @@ func (svc *Service) CreateNetwork(c *gin.Context) { adminStateUp = *req.Network.AdminStateUp } + // Making a network shared exposes it to every tenant in the deployment — + // an operator decision, like router:external below (review 2026-10-13 + // M-4: a member-created shared network was visible to, and attachable by, + // every project). Same shape as the external gate: only `true` is gated, + // matching upstream's explicit-set rule. shared := false if req.Network.Shared != nil { + if *req.Network.Shared && !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("only an administrator may create a shared network")) + return + } shared = *req.Network.Shared } diff --git a/internal/neutron/ports.go b/internal/neutron/ports.go index c0751392..fcb16e1b 100644 --- a/internal/neutron/ports.go +++ b/internal/neutron/ports.go @@ -11,6 +11,7 @@ import ( "net" "net/http" "strconv" + "strings" "time" "github.com/cobaltcore-dev/lightstack/internal/common" @@ -85,8 +86,13 @@ func (svc *Service) CreatePort(c *gin.Context) { var fixedIPs []map[string]any if err == nil { - // Allocate IP from subnet - allocatedIP := allocateIP(cidr) + // Allocate an address no other port in the subnet holds (N-4). + allocatedIP, allocErr := svc.allocateFreeIP(c.Request.Context(), subnetID, cidr) + if allocErr != nil { + log.Warn().Err(allocErr).Str("subnet_id", subnetID).Msg("port create: address allocation failed") + common.SendError(c, common.NewConflictError("no IP address available in the network's subnet")) + return + } fixedIPs = []map[string]any{ { "subnet_id": subnetID, @@ -663,16 +669,86 @@ func allocateIP(cidr string) string { } maxOffset := int64(1<>'ip_address' + FROM ports, jsonb_array_elements(fixed_ips) fx + WHERE fx->>'subnet_id' = $1 + `, subnetID) + if err != nil { + return "", fmt.Errorf("reading the subnet's used addresses: %w", err) + } + defer rows.Close() + for rows.Next() { + var ip string + if err := rows.Scan(&ip); err != nil { + return "", fmt.Errorf("scanning a used address: %w", err) + } + if ip != "" { + used[strings.ToLower(ip)] = true + } + } + if err := rows.Err(); err != nil { + return "", fmt.Errorf("iterating the subnet's used addresses: %w", err) + } + + // The gateway belongs to the subnet, not to any port. + var gateway sql.NullString + if err := svc.pool.QueryRow(ctx, + "SELECT gateway_ip FROM subnets WHERE id = $1", subnetID).Scan(&gateway); err == nil && + gateway.Valid && gateway.String != "" { + used[strings.ToLower(gateway.String)] = true + } + + // Random candidates until one is free. A hundred attempts covers any + // realistically dense subnet well before it is full; a subnet that is full + // is an error, not an infinite loop. + for range 100 { + candidate := allocateIP(cidr) + if candidate == "" { + return "", fmt.Errorf("subnet %s has no usable addresses", subnetID) + } + if !used[strings.ToLower(candidate)] { + return candidate, nil + } + } + return "", fmt.Errorf("no free address in subnet %s", subnetID) +} + // Security Groups implementation // CreateSecurityGroupRequest represents a security group creation request @@ -1505,10 +1581,14 @@ func (svc *Service) AllocatePortForInstance(ctx context.Context, networkID, proj return nil, fmt.Errorf("looking up a subnet for network %s: %w", networkID, err) } - // Allocate IP from subnet - allocatedIP := allocateIP(cidr) - if allocatedIP == "" { - return nil, &tenantVisibleError{msg: fmt.Sprintf("failed to allocate IP from subnet %s", subnetID)} + // Allocate an IP no other port in the subnet holds (review 2026-10-13 + // N-4): the old random pick collided at birthday-problem rates, giving two + // ports — two MACs — the same address. Tenant-visible wording so the + // server's fault says what the tenant can do about it (a full or broken + // subnet), matching the no-subnet case above. + allocatedIP, allocErr := svc.allocateFreeIP(ctx, subnetID, cidr) + if allocErr != nil { + return nil, &tenantVisibleError{msg: fmt.Sprintf("failed to allocate a free IP from subnet %s", subnetID)} } fixedIPs := []map[string]any{ diff --git a/internal/neutron/shared_gate_test.go b/internal/neutron/shared_gate_test.go new file mode 100644 index 00000000..8691dfcc --- /dev/null +++ b/internal/neutron/shared_gate_test.go @@ -0,0 +1,35 @@ +package neutron + +import ( + "net/http" + "testing" +) + +// Review 2026-10-13 M-4: `shared` exposes a network to every tenant in the +// deployment, but CreateNetwork accepted it from anyone — a member could open +// their network to every project (and every project's guests to it). It is an +// operator decision, gated exactly like router:external on create: only +// `true` is refused to a member, so a client that echoes the default +// (shared=false) keeps working for tenants, as upstream's explicit-set rule +// does for external. +func TestNonAdminCannotCreateSharedNetwork(t *testing.T) { + requireDB(t) + r := newNeutronTestRouter() + tenant := map[string]string{"X-Test-Project": seededProjectID, "X-Test-Roles": "member"} + admin := map[string]string{"X-Test-Project": seededProjectID, "X-Test-Roles": "admin"} + + if code, network := createNetwork(t, r, tenant, `{"network":{"name":"m4-tenant-shared","shared":true}}`); code != http.StatusForbidden { + t.Fatalf("member creating a shared network: code=%d body=%v; want 403", code, network) + } + + // shared=false is the default — a tenant echoing it is not asking for + // anything, and must not be refused (upstream's explicit-set rule). + if code, _ := createNetwork(t, r, tenant, `{"network":{"name":"m4-tenant-internal","shared":false}}`); code != http.StatusCreated { + t.Fatalf("member creating a normal network: want 201") + } + + // An administrator keeps the shared path. + if code, network := createNetwork(t, r, admin, `{"network":{"name":"m4-admin-shared","shared":true}}`); code != http.StatusCreated || network["shared"] != true { + t.Fatalf("admin creating a shared network: code=%d body=%v; want 201 with shared=true", code, network) + } +} From 28e87f368c3d1a6e3c3ccd935bb63b741558b1d9 Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Tue, 13 Oct 2026 10:33:40 +0000 Subject: [PATCH 06/26] docs(changelog): record the 2026-10-13 security and correctness fixes Signed-off-by: Uwe Schwaeke --- CHANGELOG.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b26f2fbf..9d94bdec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 --- +## [Unreleased] + +### 🔒 Security — identity & service authorization +- **Keystone management surface authorized** (review 2026-10-13): the identity API was authentication-only, so any member could reset any user's password (including the admin's) via `PATCH /users/:id`, self-grant the admin role via role assignment, dump every tenant's recoverable EC2/S3 secret via the credentials API, mint application credentials for other users, and enumerate users/projects/roles/groups/domains/catalog. User/project/role/group/domain/service/endpoint management is now admin-only; user-scoped subresources are self-or-admin; PATCH of one's own password/enabled stays admin-only so ChangePassword keeps its original-password contract. Role assignment also 404s on nonexistent project/user/role ids instead of silently recording garbage. +- **Glance image upload ownership** (H-1): uploading bytes to a public image was allowed for any project that could *see* it — image poisoning of an operator-staged base every tenant boots. Upload now follows the same owner-or-admin rule as delete (403 visible-but-foreign, 404 invisible). +- **Glance tasks & cache** (M-1/M-2): tasks are owner-scoped and no longer echo internal errors; cache management (prefetch/evict/clear) is admin-only. +- **Nova flavor catalog** (H-2): flavor create/delete, extra-specs and tenant-access grants are admin-only; flavor inputs get sanity bounds. +- **Cinder operator surfaces** (H-3): volume types (incl. project-access grants), QoS specs and manage/unmanage are admin-only; volume-type reads stay open for tenants. +- **Neutron shared networks** (M-4): `shared=true` on create is admin-only, like `router:external`. +- **Volume-transfer auth keys** (L-1) are compared in constant time. + +### 🐛 Correctness +- **Glance checksums describe the stored bytes** (N-3): normalizing backends rewrite a qcow2 upload to a raw base, but the recorded checksum hashed the upload stream, so `EnsureLocalBase` failed on every node without the local base and boot-from-image errored. The checksum is now taken from the stored object. +- **Neutron IP allocation is collision-free** (N-4): allocation drew purely at random, so ports in one subnet could land on the same address; allocation now avoids every held address and the subnet gateway, and tiny subnets (≤ /29) no longer panic `crypto/rand.Int`. +- **Metadata `network_data.json` works** (N-7): the query selected a nonexistent `fixed_ip` column and answered 500 to every guest; it now resolves each port's JSONB addresses with their own subnets (one link per port). +- **Nova volume attach/detach are atomic** (N-9/N-10): single transaction with the volume row locked — no more 500s on concurrent double-attach, and no stranded in-use volume after a failed detach. +- **Nova boot port lifecycle** (N-6/N-8): a failed multi-network boot releases the ports it already allocated; server delete deletes only the ports Nova created and *detaches* tenant-created ports instead of destroying them. +- **Keypair name races** (L-3) map the unique-constraint violation to the same 409 as the pre-check. +- **Request bodies bounded** (M-3): glance upload/stage wrap the body in `MaxBytesReader` (10 GiB, upstream's `image_size_cap`); imported SSH public keys are capped at 64 KiB. + +--- + ## [0.6.1] - 2026-04-22 ### 📋 v0.7.0 Planning & Specifications From 64c9c978594f588c8bf452bf571102f4f2cf2e21 Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Tue, 13 Oct 2026 10:47:22 +0000 Subject: [PATCH 07/26] fix: round-1 review findings on the authz-and-correctness PR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Findings from the review of this PR's own diff, all fixed: HIGH — the N-8 tenant-port fix was unreachable through the real flow: os-interface stamped device_owner='compute:nova' on EVERY port it attached, including a tenant's pre-existing port_id, so every attached port looked nova-owned and server delete destroyed it anyway. The stamp now keys on which path ran: a port Nova creates (net_id) is nova's and deleted with the server; a tenant's port keeps its own owner and is detached. Mirrors upstream deallocate_for_instance, which skips ports it did not create. Pinned end-to-end by TestDeleteServerDetachesTenantPortsButDeletesItsOwn (attach via os-interface, then delete) and the new net_id companion TestDeleteServerDeletesInterfaceAttachCreatedPort. MEDIUM — os-snapshot-manage missed by the H-3 sweep: admin-gated like os-volume-manage (upstream volume_admin_actions:snapshot_manage). MEDIUM — volume types with a NULL description vanished from GET /v3/types (scan into a plain string failed and the row was silently dropped). COALESCE'd; pinned by TestListVolumeTypesServesNullDescription. The authz test is also idempotent now (unique names + cleanup; it would fail a second run against the shared test database). MEDIUM — os-interface's net_id path wrote the NETWORK id into its fixed_ips subnet_id, so every consumer that joins on the subnet — network_data.json (N-7), DHCP, the N-4 collision check — silently missed those ports; and it ran its own next-free IP allocator (allocateNextIP), a second allocator over the same subnet that could hand out an address another path held. It now resolves the real subnet id and allocates through neutron.AllocateFreeIP (exported for exactly this); allocateNextIP is deleted. The port_id path also reported a hardcoded placeholder IP in its response — it now parses the port's actual first fixed IP. MEDIUM — glance cache LIST was ungated while the rest of the cache surface was admin-only. Gated (upstream cache_list). LOW — nova double-attach now answers 400 like upstream, not 409. LOW — over-cap image uploads answer 413 (upstream image_size_cap semantics), not 503/400; new common.NewRequestEntityTooLargeError. LOW — network_data.json port ordering gets a deterministic tiebreaker (created_at, id). LOW — the boot test no longer writes under /var/lib: the node-local state root is redirected via paths.SetBase(t.TempDir()). Unit suite (incl. DB-backed): PASS. Contract suite: unchanged (only the 5 docker-exec-dependent tests fail, same as baseline). Signed-off-by: Uwe Schwaeke --- internal/cinder/manage.go | 9 ++ internal/cinder/manage_authz_test.go | 45 ++++++++- internal/cinder/volumes.go | 2 +- internal/common/errors.go | 10 ++ internal/glance/cache.go | 8 ++ internal/glance/images.go | 8 ++ internal/glance/import.go | 7 ++ internal/glance/upload_authz_test.go | 9 ++ internal/metadata/service.go | 2 +- internal/neutron/allocate_free_ip_test.go | 15 +-- internal/neutron/ports.go | 17 ++-- internal/nova/boot_failure_test.go | 14 ++- internal/nova/interface_attach.go | 114 +++++++++------------ internal/nova/port_lifecycle_test.go | 117 ++++++++++++++++++++-- internal/nova/volume_attachment.go | 4 +- 15 files changed, 280 insertions(+), 101 deletions(-) diff --git a/internal/cinder/manage.go b/internal/cinder/manage.go index 91034b64..c950d450 100644 --- a/internal/cinder/manage.go +++ b/internal/cinder/manage.go @@ -158,6 +158,15 @@ func (svc *Service) UnmanageVolume(c *gin.Context, volumeID string) { // ManageSnapshot handles POST /v3/os-snapshot-manage func (svc *Service) ManageSnapshot(c *gin.Context) { + // Admin-only (review 2026-10-13 H-3): like os-volume-manage, this imports + // an operator's existing backing into the API — upstream gates it + // volume_admin_actions:snapshot_manage. Checked before the body is bound + // so a non-admin gets 403, not a 400. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + projectID := c.GetString("project_id") var req struct { diff --git a/internal/cinder/manage_authz_test.go b/internal/cinder/manage_authz_test.go index 10fcade2..879c84e8 100644 --- a/internal/cinder/manage_authz_test.go +++ b/internal/cinder/manage_authz_test.go @@ -1,8 +1,12 @@ package cinder import ( + "context" "net/http" + "strings" "testing" + + "github.com/google/uuid" ) // TestOperatorSurfacesAdminOnly pins review 2026-10-13 H-3: volume types, QoS @@ -22,6 +26,7 @@ func TestOperatorSurfacesAdminOnly(t *testing.T) { memberDenied := []struct{ name, method, path, body string }{ {"create volume type", http.MethodPost, "/v3/types", `{"volume_type":{"name":"member-type"}}`}, {"manage a volume", http.MethodPost, "/v3/os-volume-manage", `{"volume":{"host":"h1","ref":{"source-name":"x"}}}`}, + {"manage a snapshot", http.MethodPost, "/v3/os-snapshot-manage", `{"snapshot":{"volume_id":"00000000-0000-0000-0000-000000000000","ref":{"source-name":"x"}}}`}, {"list manageable volumes", http.MethodGet, "/v3/manageable_volumes", ""}, {"create a QoS spec", http.MethodPost, "/v3/qos-specs", `{"qos_specs":{"name":"member-qos"}}`}, } @@ -38,8 +43,42 @@ func TestOperatorSurfacesAdminOnly(t *testing.T) { t.Errorf("GET /v3/types as member: code=%d body=%s; want 200 (reads stay open)", code, body) } - // And the admin keeps the write paths. - if code, body := doReq(r, http.MethodPost, "/v3/types", `{"volume_type":{"name":"authz-admin-type"}}`, admin); code != http.StatusCreated && code != http.StatusOK { - t.Errorf("create volume type as admin: code=%d body=%s; want 2xx", code, body) + // And the admin keeps the write paths. Unique name + cleanup: the volume + // types table is shared across runs of the shared test database. + typeName := "authz-admin-type-" + uuid.NewString()[:8] + if code, body := doReq(r, http.MethodPost, "/v3/types", `{"volume_type":{"name":"`+typeName+`"}}`, admin); code != http.StatusCreated && code != http.StatusOK { + t.Fatalf("create volume type as admin: code=%d body=%s; want 2xx", code, body) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM volume_types WHERE name = $1`, typeName) + }) +} + +// Round 2 of this review: a volume type created without a description has a +// NULL one, and ListVolumeTypes scanned it into a plain string — the row +// failed to scan and the type silently vanished from the catalog (and +// GetVolumeType 500'd). The list must serve both descriptions and their +// absence. +func TestListVolumeTypesServesNullDescription(t *testing.T) { + requireDB(t) + r := newCinderTestRouter() + + const project = "00000000-0000-0000-0000-000000000002" + admin := map[string]string{"X-Test-Project": project, "X-Test-Roles": "admin"} + + typeName := "authz-undescribed-" + uuid.NewString()[:8] + code, body := doReq(r, http.MethodPost, "/v3/types", `{"volume_type":{"name":"`+typeName+`"}}`, admin) + if code != http.StatusCreated && code != http.StatusOK { + t.Fatalf("create undescribed volume type: code=%d body=%s", code, body) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM volume_types WHERE name = $1`, typeName) + }) + + if code, body := doReq(r, http.MethodGet, "/v3/types", "", admin); code != http.StatusOK { + t.Fatalf("list volume types: code=%d body=%s", code, body) + } + if !strings.Contains(body, typeName) { + t.Errorf("a volume type created without a description is missing from GET /v3/types (the NULL-description scan drop): %s", body) } } diff --git a/internal/cinder/volumes.go b/internal/cinder/volumes.go index a8ce827f..f25f93d3 100644 --- a/internal/cinder/volumes.go +++ b/internal/cinder/volumes.go @@ -1847,7 +1847,7 @@ func (svc *Service) DeleteSnapshot(c *gin.Context) { // ListVolumeTypes lists all volume types func (svc *Service) ListVolumeTypes(c *gin.Context) { rows, err := svc.pool.Query(c.Request.Context(), ` - SELECT id, name, description, is_public, created_at + SELECT id, name, COALESCE(description, ''), is_public, created_at FROM volume_types ORDER BY name `) diff --git a/internal/common/errors.go b/internal/common/errors.go index dfefb875..9e7c318d 100644 --- a/internal/common/errors.go +++ b/internal/common/errors.go @@ -249,6 +249,16 @@ func NewServiceUnavailableError(message string) *OpenStackError { } } +// NewRequestEntityTooLargeError creates a 413 error, upstream's answer to a +// request body over the configured size cap (e.g. Glance's image_size_cap). +func NewRequestEntityTooLargeError(message string) *OpenStackError { + return &OpenStackError{ + StatusCode: http.StatusRequestEntityTooLarge, + Code: "requestEntityTooLarge", + Message: message, + } +} + func NewInternalServerError(message string) *OpenStackError { return &OpenStackError{ StatusCode: http.StatusInternalServerError, diff --git a/internal/glance/cache.go b/internal/glance/cache.go index ca1fa23d..3b89ed64 100644 --- a/internal/glance/cache.go +++ b/internal/glance/cache.go @@ -11,6 +11,14 @@ import ( // ListCachedImages lists images in the cache func (svc *Service) ListCachedImages(c *gin.Context) { + // Admin-only (review 2026-10-13 M-2): the cache is a per-node operator + // surface — upstream's cache_list policy. The rest of the cache handlers + // were gated in this review's first pass; the list was missed. + if !common.HasRole(c, common.RoleAdmin) { + common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + return + } + // Query images that have been marked as cached rows, err := svc.pool.Query(c.Request.Context(), ` SELECT id, name, size_bytes, cached_at diff --git a/internal/glance/images.go b/internal/glance/images.go index 3470db7b..75bed21b 100644 --- a/internal/glance/images.go +++ b/internal/glance/images.go @@ -1022,6 +1022,14 @@ func (svc *Service) UploadImageData(c *gin.Context) { "queued", imageID); revertErr != nil { log.Warn().Err(revertErr).Str("image_id", imageID).Msg("rollback: failed to revert image status to queued after upload failure") } + // Over the cap is 413 — upstream's answer to image_size_cap — not a + // storage failure (round 2 of this review: the MaxBytesReader error + // surfaced from the storage read as a 503). + var tooLarge *http.MaxBytesError + if errors.As(err, &tooLarge) { + common.SendError(c, common.NewRequestEntityTooLargeError("Image data exceeds the maximum upload size")) + return + } log.Error().Err(err).Str("operation", "upload_image").Str("image_id", imageID).Msg("failed to upload image to storage") common.SendError(c, common.NewServiceUnavailableError("failed to upload image")) return diff --git a/internal/glance/import.go b/internal/glance/import.go index 10d921ea..5c57ff01 100644 --- a/internal/glance/import.go +++ b/internal/glance/import.go @@ -44,6 +44,13 @@ func (svc *Service) StageImageData(c *gin.Context) { c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxImageUploadBytes) stagedData, err := io.ReadAll(c.Request.Body) if err != nil { + // Over the cap is 413, upstream's answer to image_size_cap (round 2 + // of this review — it used to read as a 400 read failure). + var tooLarge *http.MaxBytesError + if errors.As(err, &tooLarge) { + c.JSON(http.StatusRequestEntityTooLarge, gin.H{"message": "Image data exceeds the maximum upload size"}) + return + } c.JSON(http.StatusBadRequest, gin.H{"message": "Failed to read image data"}) return } diff --git a/internal/glance/upload_authz_test.go b/internal/glance/upload_authz_test.go index 6ba25203..27cd74c4 100644 --- a/internal/glance/upload_authz_test.go +++ b/internal/glance/upload_authz_test.go @@ -2,6 +2,7 @@ package glance import ( "context" + "strings" "testing" "github.com/google/uuid" @@ -89,6 +90,14 @@ func TestImageCacheManagementAdminOnly_DB(t *testing.T) { if code, _ := doReq(r, "PUT", "/cache/images/"+id, hdr(seededProjectID, "member")); code != 403 { t.Errorf("member cache prefetch: code=%d; want 403", code) } + // The list is part of the same operator surface (round 2 of this review: + // the write paths were gated, the list was missed). + if code, _ := doReq(r, "GET", "/cache/images", hdr(seededProjectID, "member")); code != 403 { + t.Errorf("member cache list: code=%d; want 403", code) + } + if code, body := doReq(r, "GET", "/cache/images", hdr(seededProjectID, "admin")); code != 200 || !strings.Contains(body, id) { + t.Errorf("admin cache list: code=%d body=%s; want 200 listing the cached image", code, body) + } // Still cached after the denied attempts. var cached bool if err := testPool.QueryRow(ctx, "SELECT cached_at IS NOT NULL FROM images WHERE id=$1", id).Scan(&cached); err != nil || !cached { diff --git a/internal/metadata/service.go b/internal/metadata/service.go index 6cb0f7ed..67182964 100644 --- a/internal/metadata/service.go +++ b/internal/metadata/service.go @@ -304,7 +304,7 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { LEFT JOIN LATERAL jsonb_array_elements(COALESCE(p.fixed_ips, '[]'::jsonb)) fx ON true LEFT JOIN subnets s ON fx->>'subnet_id' IS NOT NULL AND s.id::text = fx->>'subnet_id' WHERE p.device_id = $1 - ORDER BY p.created_at + ORDER BY p.created_at, p.id `, instanceID) if err != nil { diff --git a/internal/neutron/allocate_free_ip_test.go b/internal/neutron/allocate_free_ip_test.go index 9137b0e5..154386ff 100644 --- a/internal/neutron/allocate_free_ip_test.go +++ b/internal/neutron/allocate_free_ip_test.go @@ -44,12 +44,6 @@ func seedN4Port(t *testing.T, networkID, subnetID, ip string) { } } -func newN4Service(t *testing.T) *Service { - t.Helper() - svc := &Service{pool: testPool} - return svc -} - // TestAllocateFreeIPAvoidsUsedAddresses pins review 2026-10-13 N-4: allocation // used to be a purely random draw, so two ports in one subnet landed on the // same address at birthday-problem rates — two MACs claiming one IP. On a /28 @@ -66,9 +60,8 @@ func TestAllocateFreeIPAvoidsUsedAddresses(t *testing.T) { seedN4Port(t, mustN4NetworkID(t, subnetID), subnetID, ip) } - svc := newN4Service(t) for range 20 { - ip, err := svc.allocateFreeIP(context.Background(), subnetID, "192.168.250.0/28") + ip, err := AllocateFreeIP(context.Background(), testPool, subnetID, "192.168.250.0/28") if err != nil { t.Fatalf("allocateFreeIP: %v", err) } @@ -84,9 +77,8 @@ func TestAllocateFreeIPSkipsGateway(t *testing.T) { requireDB(t) _, subnetID := seedN4Subnet(t, "192.168.251.0/29", "192.168.251.1") - svc := newN4Service(t) for range 20 { - ip, err := svc.allocateFreeIP(context.Background(), subnetID, "192.168.251.0/29") + ip, err := AllocateFreeIP(context.Background(), testPool, subnetID, "192.168.251.0/29") if err != nil { t.Fatalf("allocateFreeIP: %v", err) } @@ -105,8 +97,7 @@ func TestAllocateFreeIPExhaustedSubnet(t *testing.T) { seedN4Port(t, networkID, subnetID, "192.168.252.1") seedN4Port(t, networkID, subnetID, "192.168.252.2") - svc := newN4Service(t) - if _, err := svc.allocateFreeIP(context.Background(), subnetID, "192.168.252.0/30"); err == nil { + if _, err := AllocateFreeIP(context.Background(), testPool, subnetID, "192.168.252.0/30"); err == nil { t.Fatal("allocating from a subnet whose every usable address is held must fail") } } diff --git a/internal/neutron/ports.go b/internal/neutron/ports.go index fcb16e1b..14508bf7 100644 --- a/internal/neutron/ports.go +++ b/internal/neutron/ports.go @@ -87,7 +87,7 @@ func (svc *Service) CreatePort(c *gin.Context) { var fixedIPs []map[string]any if err == nil { // Allocate an address no other port in the subnet holds (N-4). - allocatedIP, allocErr := svc.allocateFreeIP(c.Request.Context(), subnetID, cidr) + allocatedIP, allocErr := AllocateFreeIP(c.Request.Context(), svc.pool, subnetID, cidr) if allocErr != nil { log.Warn().Err(allocErr).Str("subnet_id", subnetID).Msg("port create: address allocation failed") common.SendError(c, common.NewConflictError("no IP address available in the network's subnet")) @@ -689,7 +689,7 @@ func allocateIP(cidr string) string { return ip.String() } -// allocateFreeIP picks a random usable address in the subnet that no port in +// AllocateFreeIP picks a random usable address in the subnet that no port in // it already holds (review 2026-10-13 N-4): allocateIP drew purely at random, // so two ports in one subnet landed on the same address at birthday-problem // rates — two MACs claiming one IP, dnsmasq handing it to one of them, and a @@ -697,14 +697,19 @@ func allocateIP(cidr string) string { // set) is excluded too: a port claiming it breaks routing for the whole // subnet. // +// Exported and pool-taking because Nova's os-interface path allocates against +// the same subnets and used to run its own next-free scan — two allocators +// over one subnet is how they hand out the same address (round 2 of this +// review). +// // The check-then-use is not a lock: two simultaneous allocations can still // race for the last free address. It collapses the collision space from // "every existing port" to "the same instant", which for port creation — a // user- or boot-paced operation — is the practical fix; a DB-level exclusion // on the JSONB elements would be the complete one. -func (svc *Service) allocateFreeIP(ctx context.Context, subnetID, cidr string) (string, error) { +func AllocateFreeIP(ctx context.Context, pool *pgxpool.Pool, subnetID, cidr string) (string, error) { used := map[string]bool{} - rows, err := svc.pool.Query(ctx, ` + rows, err := pool.Query(ctx, ` SELECT fx->>'ip_address' FROM ports, jsonb_array_elements(fixed_ips) fx WHERE fx->>'subnet_id' = $1 @@ -728,7 +733,7 @@ func (svc *Service) allocateFreeIP(ctx context.Context, subnetID, cidr string) ( // The gateway belongs to the subnet, not to any port. var gateway sql.NullString - if err := svc.pool.QueryRow(ctx, + if err := pool.QueryRow(ctx, "SELECT gateway_ip FROM subnets WHERE id = $1", subnetID).Scan(&gateway); err == nil && gateway.Valid && gateway.String != "" { used[strings.ToLower(gateway.String)] = true @@ -1586,7 +1591,7 @@ func (svc *Service) AllocatePortForInstance(ctx context.Context, networkID, proj // ports — two MACs — the same address. Tenant-visible wording so the // server's fault says what the tenant can do about it (a full or broken // subnet), matching the no-subnet case above. - allocatedIP, allocErr := svc.allocateFreeIP(ctx, subnetID, cidr) + allocatedIP, allocErr := AllocateFreeIP(ctx, svc.pool, subnetID, cidr) if allocErr != nil { return nil, &tenantVisibleError{msg: fmt.Sprintf("failed to allocate a free IP from subnet %s", subnetID)} } diff --git a/internal/nova/boot_failure_test.go b/internal/nova/boot_failure_test.go index 129ff883..e1c0c04d 100644 --- a/internal/nova/boot_failure_test.go +++ b/internal/nova/boot_failure_test.go @@ -15,6 +15,8 @@ import ( "github.com/cobaltcore-dev/lightstack/internal/neutron" "github.com/cobaltcore-dev/lightstack/pkg/hypervisor" + "github.com/cobaltcore-dev/lightstack/pkg/paths" + "github.com/cobaltcore-dev/lightstack/pkg/storage" "github.com/gin-gonic/gin" ) @@ -591,18 +593,22 @@ func TestSuccessfulBootKeepsItsPorts(t *testing.T) { const name = "n6-keep-ports" cleanupServerNamed(t, name) - imgID := seededImageID(t) // The boot path stages a per-instance overlay over the image's local raw // base; without the base file the boot fails for reasons unrelated to - // ports, so stage a real one. - basePath := "/var/lib/lightstack/images/image-" + imgID + ".raw" + // ports. Redirect the node-local state root into a test directory (the + // image store resolves it per call, so /var/lib is never touched) and + // stage a real base there. + origBase := paths.Base() + paths.SetBase(t.TempDir()) + t.Cleanup(func() { paths.SetBase(origBase) }) + imgID := seededImageID(t) + basePath := storage.LocalImagePath(imgID) if err := os.MkdirAll(filepath.Dir(basePath), 0o755); err != nil { t.Fatalf("staging image base dir: %v", err) } if err := os.WriteFile(basePath, make([]byte, 1<<20), 0o644); err != nil { t.Fatalf("staging image base file: %v", err) } - t.Cleanup(func() { os.Remove(basePath) }) body := fmt.Sprintf(`{"server":{"name":%q,"flavorRef":"m1.small","imageRef":%q,"networks":[{"uuid":%q}]}}`, name, imgID, good) diff --git a/internal/nova/interface_attach.go b/internal/nova/interface_attach.go index f5b77971..5150e00d 100644 --- a/internal/nova/interface_attach.go +++ b/internal/nova/interface_attach.go @@ -1,7 +1,6 @@ package nova import ( - "context" "encoding/json" "errors" "fmt" @@ -13,7 +12,6 @@ import ( "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgxpool" "github.com/rs/zerolog/log" ) @@ -60,6 +58,10 @@ func (svc *Service) AttachInterface(c *gin.Context) { var networkID string var fixedIP string var macAddress string + // novaCreatedPort is true when this request had Nova create the port + // (net_id path); false when it attached a port the tenant already owned + // (port_id path). It decides the device_owner stamp below (N-8). + novaCreatedPort := false // If port_id provided, use existing port if req.InterfaceAttachment.PortID != "" { @@ -83,12 +85,19 @@ func (svc *Service) AttachInterface(c *gin.Context) { return } - // Parse fixed_ips from JSONB - // For simplicity, just extract first IP - fixedIP = "192.168.1.10" // Placeholder, would parse from fixedIPsJSON + // Parse the port's first fixed IP for the response. Its addressing lives + // in the fixed_ips JSONB; a hardcoded placeholder here used to describe + // an address the port does not hold (round 2 of this review). + var entries []struct { + IPAddress string `json:"ip_address"` + } + if jsonErr := json.Unmarshal(fixedIPsJSON, &entries); jsonErr == nil && len(entries) > 0 { + fixedIP = entries[0].IPAddress + } } else if req.InterfaceAttachment.NetID != "" { // Create new port on the specified network networkID = req.InterfaceAttachment.NetID + novaCreatedPort = true // The rule booting applies (neutron.GuestMayAttach): the server's own networks and shared // ones, and an unshared external network for administrators only — upstream applies @@ -116,32 +125,44 @@ func (svc *Service) AttachInterface(c *gin.Context) { macAddress = generateMACAddress() // Get subnet info for IP allocation - var cidr string + var subnetID, cidr string err = svc.pool.QueryRow(c.Request.Context(), - "SELECT cidr FROM subnets WHERE network_id = $1 LIMIT 1", + "SELECT id, cidr FROM subnets WHERE network_id = $1 LIMIT 1", networkID, - ).Scan(&cidr) + ).Scan(&subnetID, &cidr) if errors.Is(err, pgx.ErrNoRows) { common.SendError(c, common.NewBadRequestError("network has no subnet")) return } + if err != nil { + log.Error().Err(err).Str("operation", "allocate_ip").Msg("subnet lookup error") + common.SendError(c, common.NewInternalServerError("failed to allocate IP")) + return + } // Use provided fixed_ip or allocate one if req.InterfaceAttachment.FixedIP != "" { fixedIP = req.InterfaceAttachment.FixedIP } else { - // Allocate next available IP from subnet - fixedIP, err = allocateNextIP(c.Request.Context(), svc.pool, networkID, cidr) + // The same collision-free allocator every other port-creation path + // uses (review 2026-10-13 N-4): this path used to run its own + // next-free scan, and two allocators over one subnet is how they + // hand out the same address. + fixedIP, err = neutron.AllocateFreeIP(c.Request.Context(), svc.pool, subnetID, cidr) if err != nil { log.Error().Err(err).Str("operation", "allocate_ip").Msg("IP allocation error") - common.SendError(c, common.NewInternalServerError("failed to allocate IP")) + common.SendError(c, common.NewConflictError("no IP address available in the network's subnet")) return } } - // Create port - fixedIPsJSON := fmt.Sprintf(`[{"ip_address": "%s", "subnet_id": "%s"}]`, fixedIP, networkID) + // Create port. The fixed_ips entry carries the SUBNET's id, not the + // network's (round 2 of this review): everything that resolves a port's + // addressing — the metadata service's network_data.json, DHCP, the + // collision check above — joins on the subnet id and silently missed + // these ports when it held a network id. + fixedIPsJSON := fmt.Sprintf(`[{"ip_address": "%s", "subnet_id": "%s"}]`, fixedIP, subnetID) _, err = svc.pool.Exec(c.Request.Context(), ` INSERT INTO ports (id, network_id, project_id, name, mac_address, fixed_ips, device_id, device_owner, status, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8, $9, $10, $11) @@ -157,12 +178,24 @@ func (svc *Service) AttachInterface(c *gin.Context) { return } - // Update port to attach to instance + // Attach the port to the instance. device_owner is stamped + // 'compute:nova' only on the port Nova itself just created — that stamp + // is the ownership discriminator server delete and the boot-failure + // rollback key on (review 2026-10-13 N-8): a tenant's port attached by id + // keeps its own device_owner, so deleting the server DETACHES it instead + // of destroying it — upstream's deallocate_for_instance skips ports it + // did not create for exactly this reason. The old blanket stamp is also + // what made the tenant-port half of N-8 unreachable through the real + // attach flow (round 2 of this review). + var deviceOwner any + if novaCreatedPort { + deviceOwner = "compute:nova" + } _, err = svc.pool.Exec(c.Request.Context(), ` UPDATE ports - SET device_id = $1, device_owner = $2, status = $3, updated_at = $4 + SET device_id = $1, device_owner = COALESCE($2, device_owner), status = $3, updated_at = $4 WHERE id = $5 - `, instanceID, "compute:nova", "ACTIVE", time.Now(), portID) + `, instanceID, deviceOwner, "ACTIVE", time.Now(), portID) if err != nil { log.Error().Err(err).Str("operation", "update_port_attach").Msg("database error") @@ -330,52 +363,3 @@ func generateMACAddress() string { return fmt.Sprintf("fa:16:3e:%02x:%02x:%02x", uuid.New()[0], uuid.New()[1], uuid.New()[2]) } - -// allocateNextIP allocates the next available IP from a subnet -func allocateNextIP(ctx context.Context, pool *pgxpool.Pool, networkID, cidr string) (string, error) { - // Simple allocation: get last used IP and increment - // In production, would use proper IPAM - var fixedIPsJSON []byte - err := pool.QueryRow(ctx, - "SELECT fixed_ips FROM ports WHERE network_id = $1 ORDER BY created_at DESC LIMIT 1", - networkID, - ).Scan(&fixedIPsJSON) - - if errors.Is(err, pgx.ErrNoRows) { - // First allocation, use .10 - baseIP := cidr[:len(cidr)-3] // Remove /24 - return baseIP + "10", nil - } - - if err != nil { - return "", err - } - - // For simplicity, parse JSON and increment - // In production, would use proper IPAM library - var ips []map[string]any - if err := json.Unmarshal(fixedIPsJSON, &ips); err != nil { - // Fallback to simple allocation - baseIP := cidr[:len(cidr)-3] - return baseIP + "11", nil - } - - if len(ips) == 0 { - baseIP := cidr[:len(cidr)-3] - return baseIP + "10", nil - } - - lastIP := ips[0]["ip_address"].(string) - - // Increment last octet - var a, b, c, d int - if _, err := fmt.Sscanf(lastIP, "%d.%d.%d.%d", &a, &b, &c, &d); err != nil { - return "", fmt.Errorf("malformed IP %q in subnet allocation table: %w", lastIP, err) - } - d++ - if d > 254 { - return "", fmt.Errorf("subnet exhausted") - } - - return fmt.Sprintf("%d.%d.%d.%d", a, b, c, d), nil -} diff --git a/internal/nova/port_lifecycle_test.go b/internal/nova/port_lifecycle_test.go index ab9c2c34..957a4f4c 100644 --- a/internal/nova/port_lifecycle_test.go +++ b/internal/nova/port_lifecycle_test.go @@ -5,6 +5,8 @@ import ( "fmt" "net/http" "testing" + + "github.com/cobaltcore-dev/lightstack/pkg/paths" ) // Review 2026-10-13 N-8: DeleteServer used a blanket @@ -12,7 +14,12 @@ import ( // created and attached (os-interface with port_id) along with the ones Nova // allocated for the boot. Upstream deletes only the ports it created and // detaches the rest — the tenant's port survives the server, parked DOWN with -// no device, reusable or deletable by its owner. This pins both halves. +// no device, reusable or deletable by its owner. +// +// The tenant port is attached through the real os-interface flow (round 2 of +// this review): the fix keys on device_owner='compute:nova' marking Nova's own +// ports, and AttachInterface used to stamp that owner on a tenant's port too — +// which would have made the discriminator, and this behaviour, unreachable. func TestDeleteServerDetachesTenantPortsButDeletesItsOwn(t *testing.T) { requireDB(t) ctx := context.Background() @@ -34,8 +41,8 @@ func TestDeleteServerDetachesTenantPortsButDeletesItsOwn(t *testing.T) { seedPort := func(owner string) string { var id string if err := testPool.QueryRow(ctx, ` - INSERT INTO ports (name, network_id, project_id, device_id, device_owner, mac_address, status) - VALUES ($1, $2, $3, $4, NULLIF($5, ''), $6, 'ACTIVE') RETURNING id::text`, + INSERT INTO ports (name, network_id, project_id, device_id, device_owner, mac_address, status, fixed_ips) + VALUES ($1, $2, $3, $4, NULLIF($5, ''), $6, 'ACTIVE', '[]'::jsonb) RETURNING id::text`, "n8-"+owner, netID, seededProjectID, instanceID, owner, fmt.Sprintf("52:54:00:00:00:%02x", len(owner))).Scan(&id); err != nil { t.Fatalf("seeding %s port: %v", owner, err) } @@ -48,6 +55,30 @@ func TestDeleteServerDetachesTenantPortsButDeletesItsOwn(t *testing.T) { owner := map[string]string{ "X-Test-Project": seededProjectID, "X-Test-User": seededUserID, "X-Test-Roles": "member", } + + // Detach the tenant port from the instance first, then re-attach it + // through the real os-interface flow, so the test exercises the handler + // that used to stamp it compute:nova. + if _, err := testPool.Exec(ctx, + `UPDATE ports SET device_id = NULL WHERE id::text = $1`, tenantPort); err != nil { + t.Fatalf("freeing the tenant port for attach: %v", err) + } + attachBody := fmt.Sprintf(`{"interfaceAttachment":{"port_id":%q}}`, tenantPort) + if code, body := doReq(r, http.MethodPost, "/v2.1/servers/"+instanceID+"/os-interface", attachBody, owner); code != http.StatusOK { + t.Fatalf("attaching the tenant port via os-interface: code=%d body=%s", code, body) + } + + // The attach must NOT have claimed the port as Nova's: device_owner is the + // ownership discriminator server delete keys on. + var deviceOwner string + if err := testPool.QueryRow(ctx, + `SELECT COALESCE(device_owner, '') FROM ports WHERE id::text = $1`, tenantPort).Scan(&deviceOwner); err != nil { + t.Fatalf("reading the attached port's owner: %v", err) + } + if deviceOwner == "compute:nova" { + t.Fatal("os-interface stamped a tenant-created port as compute:nova; it would be deleted with the server instead of detached") + } + if code, body := doReq(r, http.MethodDelete, "/v2.1/servers/"+instanceID, "", owner); code != http.StatusNoContent { t.Fatalf("deleting the server: code=%d body=%s", code, body) } @@ -63,21 +94,91 @@ func TestDeleteServerDetachesTenantPortsButDeletesItsOwn(t *testing.T) { } // The tenant's port survives, detached: no device, parked DOWN. - var deviceID, deviceOwner, status string + var deviceID, status string var exists bool if err := testPool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM ports WHERE id::text = $1)`, tenantPort).Scan(&exists); err != nil || !exists { t.Fatalf("the tenant-created port must survive the server delete (exists=%v err=%v)", exists, err) } if err := testPool.QueryRow(ctx, ` - SELECT COALESCE(device_id::text, ''), COALESCE(device_owner, ''), status - FROM ports WHERE id::text = $1`, tenantPort).Scan(&deviceID, &deviceOwner, &status); err != nil { + SELECT COALESCE(device_id::text, ''), status + FROM ports WHERE id::text = $1`, tenantPort).Scan(&deviceID, &status); err != nil { t.Fatalf("reading the surviving port: %v", err) } - if deviceID != "" || deviceOwner != "" { - t.Errorf("the surviving port is still attached (device_id=%q device_owner=%q); want it detached", deviceID, deviceOwner) + if deviceID != "" { + t.Errorf("the surviving port is still attached (device_id=%q); want it detached", deviceID) } if status != "DOWN" { t.Errorf("the surviving port's status = %q; want DOWN (detached, as upstream parks it)", status) } } + +// The net_id half of the same discriminator: a port Nova creates for an +// os-interface attach IS Nova's, and must be deleted with the server. +func TestDeleteServerDeletesInterfaceAttachCreatedPort(t *testing.T) { + requireDB(t) + ctx := context.Background() + + netID := seedBootNetwork(t, seededProjectID, "n8b-net") + // The net_id path needs a subnet to allocate from. + if _, err := testPool.Exec(ctx, + `INSERT INTO subnets (name, network_id, project_id, cidr) + VALUES ('n8b-sub', $1, $2, '192.168.77.0/24')`, netID, seededProjectID); err != nil { + t.Fatalf("seeding subnet: %v", err) + } + + // Redirect the node-local state root (image bases, overlays) into a test + // directory so nothing touches /var/lib. + origBase := paths.Base() + paths.SetBase(t.TempDir()) + t.Cleanup(func() { paths.SetBase(origBase) }) + + var instanceID string + if err := testPool.QueryRow(ctx, + `INSERT INTO instances (name, project_id, user_id, flavor_id, status) + VALUES ('n8b-server', $1, $2, (SELECT id FROM flavors LIMIT 1), 'ACTIVE') + RETURNING id::text`, seededProjectID, seededUserID).Scan(&instanceID); err != nil { + t.Fatalf("seeding instance: %v", err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM ports WHERE network_id::text = $1`, netID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM instances WHERE id::text = $1`, instanceID) + }) + + r := newNovaTestRouter() + owner := map[string]string{ + "X-Test-Project": seededProjectID, "X-Test-User": seededUserID, "X-Test-Roles": "member", + } + + attachBody := fmt.Sprintf(`{"interfaceAttachment":{"net_id":%q}}`, netID) + code, body := doReq(r, http.MethodPost, "/v2.1/servers/"+instanceID+"/os-interface", attachBody, owner) + if code != http.StatusOK { + t.Fatalf("attaching via net_id: code=%d body=%s", code, body) + } + var createdPort string + if err := testPool.QueryRow(ctx, + `SELECT id::text FROM ports WHERE network_id::text = $1 AND device_id::text = $2`, + netID, instanceID).Scan(&createdPort); err != nil { + t.Fatalf("resolving the attached port: %v", err) + } + var deviceOwner string + if err := testPool.QueryRow(ctx, + `SELECT COALESCE(device_owner, '') FROM ports WHERE id::text = $1`, createdPort).Scan(&deviceOwner); err != nil { + t.Fatalf("reading the created port's owner: %v", err) + } + if deviceOwner != "compute:nova" { + t.Fatalf("a port Nova created for os-interface must be stamped compute:nova (got %q); it is Nova's to clean up", deviceOwner) + } + + if code, body := doReq(r, http.MethodDelete, "/v2.1/servers/"+instanceID, "", owner); code != http.StatusNoContent { + t.Fatalf("deleting the server: code=%d body=%s", code, body) + } + var exists bool + if err := testPool.QueryRow(ctx, + `SELECT EXISTS(SELECT 1 FROM ports WHERE id::text = $1)`, createdPort).Scan(&exists); err != nil { + t.Fatalf("checking the created port: %v", err) + } + if exists { + t.Error("the port Nova created for os-interface survived the server delete; it must be deleted with it") + } +} diff --git a/internal/nova/volume_attachment.go b/internal/nova/volume_attachment.go index e7720e72..9d8670db 100644 --- a/internal/nova/volume_attachment.go +++ b/internal/nova/volume_attachment.go @@ -146,7 +146,9 @@ func (svc *Service) AttachVolume(c *gin.Context) { fmt.Sprintf("volume status is %s, must be available", volumeStatus)} } if attachedToInstance != nil { - return &novaAttachRefusal{http.StatusConflict, "volume is already attached to an instance"} + // Upstream answers a re-attach with 400 (Invalid input / + // volume already attached), not 409 — round 2 of this review. + return &novaAttachRefusal{http.StatusBadRequest, "volume is already attached to an instance"} } // Auto-assign device if not provided From 0e14680266807135c7f818a2c7c11a0f8869e2e9 Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Tue, 13 Oct 2026 10:59:34 +0000 Subject: [PATCH 08/26] =?UTF-8?q?fix:=20round-2=20review=20findings=20?= =?UTF-8?q?=E2=80=94=20os-interface=20port=5Fid=20path,=20upload-cap=20413?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Round 2 of the review loop (0 High, 1 Medium, 4 Low — all fixed): MEDIUM (F1) — the port_id path of os-interface scanned the ports row's nullable device_id into a plain string. Every free port has NULL there, so the scan FAILED on every successful attach; the error was dropped and the attach worked only because pgx fills earlier columns before the failed one — and any real database error on that lookup surfaced as a bogus 200 with an empty net_id and MAC. The lookup now reads COALESCE(device_id::text, '') and handles its errors like the instance lookup above it. Pinned by TestAttachInterfacePortIDReportsRealState (attach a device_id-NULL port, assert the response carries the port's real net_id, MAC and fixed_ips). LOW (F2) — the attach response fabricated one {"ip_address": ""} for a port with no addresses and omitted subnet ids. It now reports the port's actual fixed_ips entries (or an empty list). Pinned by TestAttachInterfacePortIDReportsRealState and TestAttachInterfacePortIDWithoutAddressesReportsNone. LOW (F3) — an explicit fixed_ip on the net_id path was stored unvalidated: not checked against the subnet, not against the addresses other ports hold (the explicit side of the N-4 collision rule), and arbitrary text reached the JSONB build (a quote 500'd on the cast). Now: parse + in-subnet (400), availability via the new exported neutron.IPAvailable (409, upstream's 'IP address already allocated'). AllocateFreeIP and IPAvailable share one usedSubnetAddresses helper. Pinned by TestAttachInterfaceExplicitFixedIPIsValidated. LOW (F4) — uploadImageLocal applied the 5-second metadata-op timeout to the body copy, so an over-cap upload hit the deadline (503) long before the 10 GiB MaxBytesReader cap could answer 413. The copy now runs under the caller's context only (cancelled on client disconnect), matching the S3 path's connect-only timeout policy (FR-014). maxImageUploadBytes is a var now, and TestOverCapUploadAnswers413 pins the 413 end-to-end (image left queued, not stuck saving). LOW (F5) — the boot test's paths.SetBase restore could run while the boot goroutine was still reading the paths global (LIFO cleanup ran the restore before the service Shutdown). The redirect is now registered before newBootService so Shutdown stops the goroutine first. Unit suite (incl. DB-backed): PASS. Contract suite: unchanged (only the 5 docker-exec-dependent tests fail, same as baseline). Signed-off-by: Uwe Schwaeke --- internal/glance/images.go | 7 +- internal/glance/import.go | 2 +- internal/glance/upload_authz_test.go | 36 ++++ internal/neutron/ports.go | 67 ++++-- internal/nova/boot_failure_test.go | 17 +- internal/nova/interface_attach.go | 84 ++++++-- internal/nova/interface_attach_portid_test.go | 204 ++++++++++++++++++ pkg/storage/image_store.go | 14 +- 8 files changed, 382 insertions(+), 49 deletions(-) create mode 100644 internal/nova/interface_attach_portid_test.go diff --git a/internal/glance/images.go b/internal/glance/images.go index 75bed21b..7beb175c 100644 --- a/internal/glance/images.go +++ b/internal/glance/images.go @@ -42,8 +42,9 @@ type Service struct { // maxImageUploadBytes bounds PUT /images/:id/file (review 2026-10-13 M-3): // without it an unbounded stream lands on disk (and, in local mode, is // qemu-img-normalized) until the host fills. 10 GiB, the same default as -// upstream Glance's image_size_cap. -const maxImageUploadBytes = 10 << 30 +// upstream Glance's image_size_cap. A var, not a const, so tests can pin the +// over-cap response without streaming 10 GiB. +var maxImageUploadBytes = 10 << 30 // NewService creates a new Glance service. // @@ -1000,7 +1001,7 @@ func (svc *Service) UploadImageData(c *gin.Context) { // Bound the upload (review 2026-10-13 M-3): without a cap an unbounded // stream lands on disk (and, in local mode, is normalized) until the // host fills. Same default as upstream Glance's image_size_cap. - c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxImageUploadBytes) + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(maxImageUploadBytes)) // Update status to saving if _, err := svc.pool.Exec(c.Request.Context(), diff --git a/internal/glance/import.go b/internal/glance/import.go index 5c57ff01..a22e73d0 100644 --- a/internal/glance/import.go +++ b/internal/glance/import.go @@ -41,7 +41,7 @@ func (svc *Service) StageImageData(c *gin.Context) { // Read staged data (in real implementation, would save to staging area). // Bounded like the upload path (review 2026-10-13 M-3): ReadAll on a raw // request body is an unbounded memory allocation. - c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxImageUploadBytes) + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(maxImageUploadBytes)) stagedData, err := io.ReadAll(c.Request.Body) if err != nil { // Over the cap is 413, upstream's answer to image_size_cap (round 2 diff --git a/internal/glance/upload_authz_test.go b/internal/glance/upload_authz_test.go index 27cd74c4..aae05a0d 100644 --- a/internal/glance/upload_authz_test.go +++ b/internal/glance/upload_authz_test.go @@ -2,6 +2,7 @@ package glance import ( "context" + "net/http" "strings" "testing" @@ -104,3 +105,38 @@ func TestImageCacheManagementAdminOnly_DB(t *testing.T) { t.Errorf("denied cache clear still took effect (cached=%v err=%v)", cached, err) } } + +// TestOverCapUploadAnswers413 pins the round-3 fix for the M-3 cap: a body +// over maxImageUploadBytes must answer 413 (upstream's image_size_cap +// semantics). The cap is a var so the test can lower it instead of streaming +// 10 GiB; in stub mode the store's read surfaces http.MaxBytesError raw, and +// in local mode it is wrapped with %w — errors.As covers both. +func TestOverCapUploadAnswers413(t *testing.T) { + requireDB(t) + ctx := context.Background() + + id := uuid.NewString() + if _, err := testPool.Exec(ctx, + `INSERT INTO images (id, name, project_id, status, visibility) VALUES ($1,'cap-me',$2,'queued','private')`, + id, seededProjectID); err != nil { + t.Fatalf("insert image: %v", err) + } + t.Cleanup(func() { testPool.Exec(ctx, "DELETE FROM images WHERE id=$1", id) }) + + origCap := maxImageUploadBytes + maxImageUploadBytes = 8 + t.Cleanup(func() { maxImageUploadBytes = origCap }) + + r := newGlanceTestRouter(t) + code, body := doReqBody(r, "PUT", "/images/"+id+"/file", + "this body is longer than eight bytes", hdr(seededProjectID, "member")) + if code != http.StatusRequestEntityTooLarge { + t.Errorf("over-cap upload: code=%d body=%s; want 413", code, body) + } + + // And the image is left queued, not stuck in saving. + var status string + if err := testPool.QueryRow(ctx, "SELECT status FROM images WHERE id=$1", id).Scan(&status); err != nil || status != "queued" { + t.Errorf("image status after refused upload = %q (err=%v); want queued", status, err) + } +} diff --git a/internal/neutron/ports.go b/internal/neutron/ports.go index 14508bf7..4a26e1ad 100644 --- a/internal/neutron/ports.go +++ b/internal/neutron/ports.go @@ -689,25 +689,9 @@ func allocateIP(cidr string) string { return ip.String() } -// AllocateFreeIP picks a random usable address in the subnet that no port in -// it already holds (review 2026-10-13 N-4): allocateIP drew purely at random, -// so two ports in one subnet landed on the same address at birthday-problem -// rates — two MACs claiming one IP, dnsmasq handing it to one of them, and a -// guest that could not reliably reach anything. The subnet's gateway (when -// set) is excluded too: a port claiming it breaks routing for the whole -// subnet. -// -// Exported and pool-taking because Nova's os-interface path allocates against -// the same subnets and used to run its own next-free scan — two allocators -// over one subnet is how they hand out the same address (round 2 of this -// review). -// -// The check-then-use is not a lock: two simultaneous allocations can still -// race for the last free address. It collapses the collision space from -// "every existing port" to "the same instant", which for port creation — a -// user- or boot-paced operation — is the practical fix; a DB-level exclusion -// on the JSONB elements would be the complete one. -func AllocateFreeIP(ctx context.Context, pool *pgxpool.Pool, subnetID, cidr string) (string, error) { +// usedSubnetAddresses returns every address held by a port in the subnet +// (lower-cased), plus the subnet's own gateway when set. +func usedSubnetAddresses(ctx context.Context, pool *pgxpool.Pool, subnetID string) (map[string]bool, error) { used := map[string]bool{} rows, err := pool.Query(ctx, ` SELECT fx->>'ip_address' @@ -715,20 +699,20 @@ func AllocateFreeIP(ctx context.Context, pool *pgxpool.Pool, subnetID, cidr stri WHERE fx->>'subnet_id' = $1 `, subnetID) if err != nil { - return "", fmt.Errorf("reading the subnet's used addresses: %w", err) + return nil, fmt.Errorf("reading the subnet's used addresses: %w", err) } defer rows.Close() for rows.Next() { var ip string if err := rows.Scan(&ip); err != nil { - return "", fmt.Errorf("scanning a used address: %w", err) + return nil, fmt.Errorf("scanning a used address: %w", err) } if ip != "" { used[strings.ToLower(ip)] = true } } if err := rows.Err(); err != nil { - return "", fmt.Errorf("iterating the subnet's used addresses: %w", err) + return nil, fmt.Errorf("iterating the subnet's used addresses: %w", err) } // The gateway belongs to the subnet, not to any port. @@ -738,6 +722,45 @@ func AllocateFreeIP(ctx context.Context, pool *pgxpool.Pool, subnetID, cidr stri gateway.Valid && gateway.String != "" { used[strings.ToLower(gateway.String)] = true } + return used, nil +} + +// IPAvailable reports whether no port in the subnet holds the address (and it +// is not the subnet's gateway). Exported for Nova's os-interface path, which +// accepts a caller-specified fixed_ip and must refuse one another port holds — +// the explicit side of the N-4 collision rule the allocator covers for +// generated addresses. +func IPAvailable(ctx context.Context, pool *pgxpool.Pool, subnetID, ip string) (bool, error) { + used, err := usedSubnetAddresses(ctx, pool, subnetID) + if err != nil { + return false, err + } + return !used[strings.ToLower(ip)], nil +} + +// AllocateFreeIP picks a random usable address in the subnet that no port in +// it already holds (review 2026-10-13 N-4): allocateIP drew purely at random, +// so two ports in one subnet landed on the same address at birthday-problem +// rates — two MACs claiming one IP, dnsmasq handing it to one of them, and a +// guest that could not reliably reach anything. The subnet's gateway (when +// set) is excluded too: a port claiming it breaks routing for the whole +// subnet. +// +// Exported and pool-taking because Nova's os-interface path allocates against +// the same subnets and used to run its own next-free scan — two allocators +// over one subnet is how they hand out the same address (round 2 of this +// review). +// +// The check-then-use is not a lock: two simultaneous allocations can still +// race for the last free address. It collapses the collision space from +// "every existing port" to "the same instant", which for port creation — a +// user- or boot-paced operation — is the practical fix; a DB-level exclusion +// on the JSONB elements would be the complete one. +func AllocateFreeIP(ctx context.Context, pool *pgxpool.Pool, subnetID, cidr string) (string, error) { + used, err := usedSubnetAddresses(ctx, pool, subnetID) + if err != nil { + return "", err + } // Random candidates until one is free. A hundred attempts covers any // realistically dense subnet well before it is full; a subnet that is full diff --git a/internal/nova/boot_failure_test.go b/internal/nova/boot_failure_test.go index e1c0c04d..8da2e688 100644 --- a/internal/nova/boot_failure_test.go +++ b/internal/nova/boot_failure_test.go @@ -581,6 +581,16 @@ func TestFailedBootReleasesAllocatedPorts(t *testing.T) { func TestSuccessfulBootKeepsItsPorts(t *testing.T) { requireDB(t) + // Redirect the node-local state root into a test directory BEFORE the + // service is built: the boot runs in a goroutine the service's Shutdown + // (registered by newBootService, and so running LAST in cleanup's LIFO + // order) must stop before this restore runs, or a still-booting goroutine + // could read the paths global mid-rewrite (round 3 of this review). The + // image store resolves the root per call, so /var/lib is never touched. + origBase := paths.Base() + paths.SetBase(t.TempDir()) + t.Cleanup(func() { paths.SetBase(origBase) }) + good := seedBootNetwork(t, seededProjectID, "n6-ok-net") fake := &fakeNeutron{ portFor: map[string]*neutron.PortInfo{ @@ -595,12 +605,7 @@ func TestSuccessfulBootKeepsItsPorts(t *testing.T) { // The boot path stages a per-instance overlay over the image's local raw // base; without the base file the boot fails for reasons unrelated to - // ports. Redirect the node-local state root into a test directory (the - // image store resolves it per call, so /var/lib is never touched) and - // stage a real base there. - origBase := paths.Base() - paths.SetBase(t.TempDir()) - t.Cleanup(func() { paths.SetBase(origBase) }) + // ports. Stage a real base in the redirected root. imgID := seededImageID(t) basePath := storage.LocalImagePath(imgID) if err := os.MkdirAll(filepath.Dir(basePath), 0o755); err != nil { diff --git a/internal/nova/interface_attach.go b/internal/nova/interface_attach.go index 5150e00d..e3fadcc9 100644 --- a/internal/nova/interface_attach.go +++ b/internal/nova/interface_attach.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "net" "net/http" "time" @@ -58,6 +59,10 @@ func (svc *Service) AttachInterface(c *gin.Context) { var networkID string var fixedIP string var macAddress string + // fixedIPEntries is what the response reports as the attachment's + // fixed_ips: the port's actual entries (port_id path) or the one just + // allocated (net_id path). Empty means none — not a fabricated one. + var fixedIPEntries []gin.H // novaCreatedPort is true when this request had Nova create the port // (net_id path); false when it attached a port the tenant already owned // (port_id path). It decides the device_owner stamp below (N-8). @@ -67,11 +72,17 @@ func (svc *Service) AttachInterface(c *gin.Context) { if req.InterfaceAttachment.PortID != "" { portID = req.InterfaceAttachment.PortID - // Verify port exists and is available + // Verify port exists and is available. device_id is nullable — every + // free port has NULL there — so it is read COALESCE'd rather than + // scanned into a plain string (round 3 of this review): that scan + // fails on NULL, and the error used to be dropped, leaving the attach + // "correct" only because pgx fills earlier columns before the failed + // one. Any real database error on this lookup surfaced the same way: + // a bogus 200 with an empty net_id and MAC. var deviceID string var fixedIPsJSON []byte err := svc.pool.QueryRow(c.Request.Context(), - "SELECT network_id, fixed_ips, mac_address, device_id FROM ports WHERE id = $1 AND project_id = $2", + "SELECT network_id, fixed_ips, mac_address, COALESCE(device_id::text, '') FROM ports WHERE id = $1 AND project_id = $2", portID, projectID, ).Scan(&networkID, &fixedIPsJSON, &macAddress, &deviceID) @@ -79,21 +90,23 @@ func (svc *Service) AttachInterface(c *gin.Context) { common.SendError(c, common.NewNotFoundError("port")) return } + if err != nil { + log.Error().Err(err).Str("operation", "attach_interface").Str("port_id", portID).Msg("port lookup failed") + common.SendError(c, common.NewInternalServerError("database error")) + return + } if deviceID != "" { common.SendError(c, common.NewConflictError("port already attached to another device")) return } - // Parse the port's first fixed IP for the response. Its addressing lives - // in the fixed_ips JSONB; a hardcoded placeholder here used to describe - // an address the port does not hold (round 2 of this review). - var entries []struct { - IPAddress string `json:"ip_address"` - } - if jsonErr := json.Unmarshal(fixedIPsJSON, &entries); jsonErr == nil && len(entries) > 0 { - fixedIP = entries[0].IPAddress - } + // The port's actual fixed_ips, for the response and for nothing else: + // its addressing lives in the JSONB, and a hardcoded placeholder used + // to be reported instead (round 2 of this review). A port with no + // addresses reports an empty list, as upstream does — not a fabricated + // {"ip_address": ""}. + fixedIPEntries = parseFixedIPEntries(fixedIPsJSON) } else if req.InterfaceAttachment.NetID != "" { // Create new port on the specified network networkID = req.InterfaceAttachment.NetID @@ -144,6 +157,28 @@ func (svc *Service) AttachInterface(c *gin.Context) { // Use provided fixed_ip or allocate one if req.InterfaceAttachment.FixedIP != "" { fixedIP = req.InterfaceAttachment.FixedIP + // A caller-specified address is validated, not trusted (round 3 + // of this review): it must parse and lie in the subnet (400), and + // no other port may hold it (409, upstream's "IP address already + // allocated") — the explicit side of the N-4 collision rule the + // allocator covers for generated addresses. Parsing also keeps + // arbitrary strings out of the JSONB build below. + ip := net.ParseIP(fixedIP) + _, ipNet, cidrErr := net.ParseCIDR(cidr) + if ip == nil || cidrErr != nil || !ipNet.Contains(ip) { + common.SendError(c, common.NewBadRequestError("fixed_ip is not a valid address in the network's subnet")) + return + } + available, availErr := neutron.IPAvailable(c.Request.Context(), svc.pool, subnetID, fixedIP) + if availErr != nil { + log.Error().Err(availErr).Str("operation", "attach_interface").Msg("IP availability check failed") + common.SendError(c, common.NewInternalServerError("failed to allocate IP")) + return + } + if !available { + common.SendError(c, common.NewConflictError("IP address already allocated in the subnet")) + return + } } else { // The same collision-free allocator every other port-creation path // uses (review 2026-10-13 N-4): this path used to run its own @@ -163,6 +198,7 @@ func (svc *Service) AttachInterface(c *gin.Context) { // collision check above — joins on the subnet id and silently missed // these ports when it held a network id. fixedIPsJSON := fmt.Sprintf(`[{"ip_address": "%s", "subnet_id": "%s"}]`, fixedIP, subnetID) + fixedIPEntries = []gin.H{{"ip_address": fixedIP, "subnet_id": subnetID}} _, err = svc.pool.Exec(c.Request.Context(), ` INSERT INTO ports (id, network_id, project_id, name, mac_address, fixed_ips, device_id, device_owner, status, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8, $9, $10, $11) @@ -223,18 +259,42 @@ func (svc *Service) AttachInterface(c *gin.Context) { // an administrator's change in their own history (SPEC-023 FR-007a). recordInstanceAction(c, svc.pool, instanceID, projectID, "attach_interface") + // The port's actual fixed_ips — entries with their subnet ids, or an + // empty list for a port with none (round 3 of this review: this used to + // fabricate exactly one {"ip_address": ""}). + if fixedIPEntries == nil { + fixedIPEntries = []gin.H{} + } c.JSON(http.StatusOK, gin.H{ "interfaceAttachment": gin.H{ "id": attachmentID, "port_id": portID, "net_id": networkID, - "fixed_ips": []gin.H{{"ip_address": fixedIP}}, + "fixed_ips": fixedIPEntries, "mac_addr": macAddress, "port_state": "ACTIVE", }, }) } +// parseFixedIPEntries reads a port's fixed_ips JSONB into response entries. +// NULL or malformed JSON yields no entries — a port with no addresses reports +// none, never a fabricated one. +func parseFixedIPEntries(fixedIPsJSON []byte) []gin.H { + var entries []struct { + IPAddress string `json:"ip_address"` + SubnetID string `json:"subnet_id"` + } + if err := json.Unmarshal(fixedIPsJSON, &entries); err != nil { + return nil + } + out := make([]gin.H, 0, len(entries)) + for _, e := range entries { + out = append(out, gin.H{"ip_address": e.IPAddress, "subnet_id": e.SubnetID}) + } + return out +} + // DetachInterface detaches a network interface from an instance func (svc *Service) DetachInterface(c *gin.Context) { instanceID, ownerProject, authorized := resolveServerOr404(c, svc.pool, c.Param("id")) diff --git a/internal/nova/interface_attach_portid_test.go b/internal/nova/interface_attach_portid_test.go new file mode 100644 index 00000000..8cd0b7aa --- /dev/null +++ b/internal/nova/interface_attach_portid_test.go @@ -0,0 +1,204 @@ +package nova + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "strings" + "testing" + + "github.com/google/uuid" +) + +// Round 3 of the 2026-10-13 review, F1/F2/F3: the port_id path of +// os-interface. +// +// F1 — every free port has device_id NULL, and the lookup scanned it into a +// plain string. The scan failed, the error was dropped, and the attach +// "worked" only because pgx fills earlier columns before the failed one; any +// real database error on that lookup also surfaced as a bogus 200 with an +// empty net_id and MAC. The lookup must read device_id nullably and handle +// its errors. +// +// F2 — the response reported the port's first fixed IP (a hardcoded +// placeholder before, then only partially fixed) and fabricated +// {"ip_address": ""} for a port with no addresses. It must report the port's +// actual fixed_ips entries with their subnet ids, or an empty list. +// +// F3 — an explicit fixed_ip was stored unvalidated: not checked against the +// subnet, not checked against the addresses other ports hold (the explicit +// side of the N-4 collision rule), and arbitrary text reached the JSONB +// build. It must 400 out-of-subnet and 409 taken addresses. +func TestAttachInterfacePortIDReportsRealState(t *testing.T) { + requireDB(t) + ctx := context.Background() + r := newNovaTestRouter() + + server, _ := seedAuthzFixtures(t) + member := map[string]string{"X-Test-Project": authzProjectA, "X-Test-User": seededUserID, "X-Test-Roles": "member"} + + netID := uuid.NewString() + subnetID := uuid.NewString() + if _, err := testPool.Exec(ctx, + `INSERT INTO networks (id, name, project_id, status, admin_state_up) + VALUES ($1, 'r3-iface-net', $2, 'ACTIVE', true)`, netID, authzProjectA); err != nil { + t.Fatalf("seeding network: %v", err) + } + if _, err := testPool.Exec(ctx, + `INSERT INTO subnets (id, name, network_id, project_id, cidr) + VALUES ($1, 'r3-iface-sub', $2, $3, '10.249.0.0/24')`, subnetID, netID, authzProjectA); err != nil { + t.Fatalf("seeding subnet: %v", err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM interface_attachments WHERE port_id IN (SELECT id FROM ports WHERE network_id = $1)`, netID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM ports WHERE network_id = $1`, netID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM subnets WHERE network_id = $1`, netID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM networks WHERE id = $1`, netID) + }) + + // A free tenant port (device_id NULL — the state every detached port is + // in) holding one address. + freePort := uuid.NewString() + fixed, _ := json.Marshal([]map[string]any{ + {"subnet_id": subnetID, "ip_address": "10.249.0.55"}, + }) + if _, err := testPool.Exec(ctx, + `INSERT INTO ports (id, name, network_id, project_id, mac_address, status, fixed_ips) + VALUES ($1, 'r3-free', $2, $3, '52:54:00:aa:bb:01', 'DOWN', $4::jsonb)`, + freePort, netID, authzProjectA, fixed); err != nil { + t.Fatalf("seeding free port: %v", err) + } + + code, body := doReq(r, http.MethodPost, "/v2.1/servers/"+server+"/os-interface", + fmt.Sprintf(`{"interfaceAttachment":{"port_id":%q}}`, freePort), member) + if code != http.StatusOK { + t.Fatalf("attaching a free (device_id NULL) port: code=%d body=%s; want 200 — the F1 defect made this ride on a swallowed scan error", code, body) + } + var resp struct { + InterfaceAttachment struct { + NetID string `json:"net_id"` + MacAddr string `json:"mac_addr"` + FixedIPs []struct { + IPAddress string `json:"ip_address"` + SubnetID string `json:"subnet_id"` + } `json:"fixed_ips"` + } `json:"interfaceAttachment"` + } + if err := json.Unmarshal([]byte(body), &resp); err != nil { + t.Fatalf("response is not JSON: %v (%s)", err, body) + } + if resp.InterfaceAttachment.NetID != netID || resp.InterfaceAttachment.MacAddr != "52:54:00:aa:bb:01" { + t.Errorf("attachment reports net_id=%q mac=%q; want the port's real values (the F1 defect reported empties on any lookup error)", resp.InterfaceAttachment.NetID, resp.InterfaceAttachment.MacAddr) + } + if len(resp.InterfaceAttachment.FixedIPs) != 1 || + resp.InterfaceAttachment.FixedIPs[0].IPAddress != "10.249.0.55" || + resp.InterfaceAttachment.FixedIPs[0].SubnetID != subnetID { + t.Errorf("attachment fixed_ips = %+v; want the port's actual entry with its subnet id (F2)", resp.InterfaceAttachment.FixedIPs) + } +} + +// The empty half of F2: a port with no addresses reports an empty list, not a +// fabricated {"ip_address": ""}. +func TestAttachInterfacePortIDWithoutAddressesReportsNone(t *testing.T) { + requireDB(t) + ctx := context.Background() + r := newNovaTestRouter() + + server, _ := seedAuthzFixtures(t) + member := map[string]string{"X-Test-Project": authzProjectA, "X-Test-User": seededUserID, "X-Test-Roles": "member"} + + netID := uuid.NewString() + if _, err := testPool.Exec(ctx, + `INSERT INTO networks (id, name, project_id, status, admin_state_up) + VALUES ($1, 'r3-iface-empty', $2, 'ACTIVE', true)`, netID, authzProjectA); err != nil { + t.Fatalf("seeding network: %v", err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM interface_attachments WHERE port_id IN (SELECT id FROM ports WHERE network_id = $1)`, netID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM ports WHERE network_id = $1`, netID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM networks WHERE id = $1`, netID) + }) + + addrlessPort := uuid.NewString() + if _, err := testPool.Exec(ctx, + `INSERT INTO ports (id, name, network_id, project_id, mac_address, status, fixed_ips) + VALUES ($1, 'r3-addrless', $2, $3, '52:54:00:aa:bb:02', 'DOWN', '[]'::jsonb)`, + addrlessPort, netID, authzProjectA); err != nil { + t.Fatalf("seeding address-less port: %v", err) + } + + code, body := doReq(r, http.MethodPost, "/v2.1/servers/"+server+"/os-interface", + fmt.Sprintf(`{"interfaceAttachment":{"port_id":%q}}`, addrlessPort), member) + if code != http.StatusOK { + t.Fatalf("attach: code=%d body=%s", code, body) + } + if want := `"fixed_ips":[]`; !strings.Contains(body, want) { + t.Errorf("a port with no addresses must report fixed_ips: [], not a fabricated entry: %s", body) + } +} + +// F3: a caller-specified fixed_ip is validated — 400 outside the subnet, 409 +// when another port holds it — instead of being stored blind. +func TestAttachInterfaceExplicitFixedIPIsValidated(t *testing.T) { + requireDB(t) + ctx := context.Background() + r := newNovaTestRouter() + + server, _ := seedAuthzFixtures(t) + member := map[string]string{"X-Test-Project": authzProjectA, "X-Test-User": seededUserID, "X-Test-Roles": "member"} + + netID := uuid.NewString() + subnetID := uuid.NewString() + if _, err := testPool.Exec(ctx, + `INSERT INTO networks (id, name, project_id, status, admin_state_up) + VALUES ($1, 'r3-iface-explicit', $2, 'ACTIVE', true)`, netID, authzProjectA); err != nil { + t.Fatalf("seeding network: %v", err) + } + if _, err := testPool.Exec(ctx, + `INSERT INTO subnets (id, name, network_id, project_id, cidr, gateway_ip) + VALUES ($1, 'r3-iface-explicit-sub', $2, $3, '10.250.0.0/24', '10.250.0.1')`, + subnetID, netID, authzProjectA); err != nil { + t.Fatalf("seeding subnet: %v", err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM ports WHERE network_id = $1`, netID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM subnets WHERE network_id = $1`, netID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM networks WHERE id = $1`, netID) + }) + + // A port already holding the address the caller will ask for. + holder := uuid.NewString() + fixed, _ := json.Marshal([]map[string]any{ + {"subnet_id": subnetID, "ip_address": "10.250.0.77"}, + }) + if _, err := testPool.Exec(ctx, + `INSERT INTO ports (id, name, network_id, project_id, mac_address, status, fixed_ips) + VALUES ($1, 'r3-holder', $2, $3, '52:54:00:aa:bb:03', 'DOWN', $4::jsonb)`, + holder, netID, authzProjectA, fixed); err != nil { + t.Fatalf("seeding holder port: %v", err) + } + + attach := func(body string) (int, string) { + return doReq(r, http.MethodPost, "/v2.1/servers/"+server+"/os-interface", body, member) + } + + if code, body := attach(fmt.Sprintf(`{"interfaceAttachment":{"net_id":%q,"fixed_ip":"10.250.0.77"}}`, netID)); code != http.StatusConflict { + t.Errorf("explicit fixed_ip held by another port: code=%d body=%s; want 409 (the explicit side of N-4)", code, body) + } + if code, body := attach(fmt.Sprintf(`{"interfaceAttachment":{"net_id":%q,"fixed_ip":"192.0.2.1"}}`, netID)); code != http.StatusBadRequest { + t.Errorf("explicit fixed_ip outside the subnet: code=%d body=%s; want 400", code, body) + } + if code, body := attach(fmt.Sprintf(`{"interfaceAttachment":{"net_id":%q,"fixed_ip":"not-an-ip"}}`, netID)); code != http.StatusBadRequest { + t.Errorf("non-IP fixed_ip: code=%d body=%s; want 400 (not a 500 from the JSONB cast)", code, body) + } + + // A free, in-subnet address still works. + code, body := attach(fmt.Sprintf(`{"interfaceAttachment":{"net_id":%q,"fixed_ip":"10.250.0.99"}}`, netID)) + if code != http.StatusOK { + t.Fatalf("valid explicit fixed_ip: code=%d body=%s; want 200", code, body) + } + if !strings.Contains(body, `"ip_address": "10.250.0.99"`) && !strings.Contains(body, `"ip_address":"10.250.0.99"`) { + t.Errorf("response must report the requested address: %s", body) + } +} diff --git a/pkg/storage/image_store.go b/pkg/storage/image_store.go index 38d815b2..bee2dcc8 100644 --- a/pkg/storage/image_store.go +++ b/pkg/storage/image_store.go @@ -344,14 +344,18 @@ func (s *ImageStore) uploadImageStub(imageID string, reader io.Reader) (int64, e // this raw file. The upload streams to a temp file, then qemu-img converts it // to raw (or renames it if already raw / qemu-img is unavailable). func (s *ImageStore) uploadImageLocal(ctx context.Context, imageID string, reader io.Reader) (int64, error) { - ctx, cancel := context.WithTimeout(ctx, s.timeout) - defer cancel() - + // No deadline here, deliberately: s.timeout is for metadata ops + // (head/delete, see its field comment); the body transfer streams to + // completion under the CALLER's context — the request's, cancelled on + // client disconnect — exactly as the S3 path's connect-only timeout does + // not bound transfers (FR-014). The 5 s cap here used to fire long before + // glance's 10 GiB upload cap could, turning oversized uploads into 503s + // instead of 413s (round 3 of this review). imagePath := filepath.Join(s.localPath, imageFileName(imageID)) tmpPath := imagePath + ".upload" - // Stream the upload to a temp file. Wrap the source so the timeout fires — - // os.File doesn't natively respect context. + // Stream the upload to a temp file. Wrap the source so cancellation fires + // — os.File doesn't natively respect context. tmp, err := os.Create(tmpPath) if err != nil { return 0, fmt.Errorf("failed to create image file: %w", err) From 8018c617c83da294b6c05b909027e47076ffe789 Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Tue, 13 Oct 2026 11:04:14 +0000 Subject: [PATCH 09/26] =?UTF-8?q?fix(nova):=20round-3=20review=20follow-up?= =?UTF-8?q?s=20=E2=80=94=20os-interface=20list=20and=20detach?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two LOW pre-existing findings surfaced by round 3 of the review loop, in the same file and class as the round-2 fixes: L-1 — ListInterfaceAttachments skipped any attachment whose port has no addresses, so an attachment created a moment earlier was invisible in the list — disagreeing with the attach response, which (since F2) reports it with an empty fixed_ips. The list now emits every attachment, defaulting fixed_ips to [] (JSON null in the column included). Pinned by TestListInterfacesIncludesAddresslessAttachments. L-2 — DetachInterface had the dropped-error NULL scan F1 fixed in the attach path: a detached port (device_id NULL) failed the scan, the error was dropped, and the 'not attached' 409 was right only by accident — a real database error also 409'd instead of 500-ing. Mirrors the F1 fix: COALESCE(device_id::text, ''), errors handled. Round 3's verdict on the PR's own commits was CLEAN; these two were pre-existing on main. Unit suite (incl. DB-backed): PASS. Contract suite: unchanged (only the 5 docker-exec-dependent tests fail, same as baseline). Signed-off-by: Uwe Schwaeke --- internal/nova/interface_attach.go | 42 +++++++++++------ internal/nova/interface_attach_portid_test.go | 46 +++++++++++++++++++ 2 files changed, 75 insertions(+), 13 deletions(-) diff --git a/internal/nova/interface_attach.go b/internal/nova/interface_attach.go index e3fadcc9..7c593bbc 100644 --- a/internal/nova/interface_attach.go +++ b/internal/nova/interface_attach.go @@ -303,10 +303,15 @@ func (svc *Service) DetachInterface(c *gin.Context) { } portID := c.Param("port_id") - // Verify port is attached to this instance + // Verify port is attached to this instance. device_id is nullable — a + // detached port has NULL there — so it is read COALESCE'd rather than + // scanned into a plain string (same class as the attach-path fix in this + // review: that scan fails on NULL, the error was dropped, and the "not + // attached" answer below was right only by accident, with real database + // errors 409-ing as "not attached" instead of 500-ing). var attachedInstanceID string err := svc.pool.QueryRow(c.Request.Context(), - "SELECT device_id FROM ports WHERE id = $1", + "SELECT COALESCE(device_id::text, '') FROM ports WHERE id = $1", portID, ).Scan(&attachedInstanceID) @@ -314,6 +319,11 @@ func (svc *Service) DetachInterface(c *gin.Context) { common.SendError(c, common.NewNotFoundError("port")) return } + if err != nil { + log.Error().Err(err).Str("operation", "detach_interface").Str("port_id", portID).Msg("port lookup failed") + common.SendError(c, common.NewInternalServerError("database error")) + return + } if attachedInstanceID != instanceID { common.SendError(c, common.NewConflictError("port not attached to this instance")) @@ -391,18 +401,24 @@ func (svc *Service) ListInterfaceAttachments(c *gin.Context) { state = *portState } - // Parse fixed_ips from JSONB - var fixedIPs []map[string]any - if err := json.Unmarshal(fixedIPsJSON, &fixedIPs); err == nil && len(fixedIPs) > 0 { - attachments = append(attachments, gin.H{ - "id": id, - "port_id": portID, - "net_id": networkID, - "fixed_ips": fixedIPs, - "mac_addr": macAddress, - "port_state": state, - }) + // Parse fixed_ips from JSONB. An attachment whose port has no + // addresses is still an attachment: it is listed with an empty + // fixed_ips, exactly as the attach response reports it (round 3 of + // this review — the guard here used to make an attachment invisible + // in the list the moment it was created). + fixedIPs := []map[string]any{} + _ = json.Unmarshal(fixedIPsJSON, &fixedIPs) + if fixedIPs == nil { // JSON null in the column + fixedIPs = []map[string]any{} } + attachments = append(attachments, gin.H{ + "id": id, + "port_id": portID, + "net_id": networkID, + "fixed_ips": fixedIPs, + "mac_addr": macAddress, + "port_state": state, + }) } if err := rows.Err(); err != nil { log.Error().Err(err).Msg("rows iteration error") diff --git a/internal/nova/interface_attach_portid_test.go b/internal/nova/interface_attach_portid_test.go index 8cd0b7aa..e272961c 100644 --- a/internal/nova/interface_attach_portid_test.go +++ b/internal/nova/interface_attach_portid_test.go @@ -202,3 +202,49 @@ func TestAttachInterfaceExplicitFixedIPIsValidated(t *testing.T) { t.Errorf("response must report the requested address: %s", body) } } + +// Round 3, L-1: an attachment whose port has no addresses must appear in the +// os-interface list with an empty fixed_ips — the list used to skip it, so an +// attachment created a moment earlier was invisible, disagreeing with the +// attach response that (since F2) reports it. +func TestListInterfacesIncludesAddresslessAttachments(t *testing.T) { + requireDB(t) + ctx := context.Background() + r := newNovaTestRouter() + + server, _ := seedAuthzFixtures(t) + member := map[string]string{"X-Test-Project": authzProjectA, "X-Test-User": seededUserID, "X-Test-Roles": "member"} + + netID := uuid.NewString() + if _, err := testPool.Exec(ctx, + `INSERT INTO networks (id, name, project_id, status, admin_state_up) + VALUES ($1, 'r3-list-empty', $2, 'ACTIVE', true)`, netID, authzProjectA); err != nil { + t.Fatalf("seeding network: %v", err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM interface_attachments WHERE port_id IN (SELECT id FROM ports WHERE network_id = $1)`, netID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM ports WHERE network_id = $1`, netID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM networks WHERE id = $1`, netID) + }) + + addrlessPort := uuid.NewString() + if _, err := testPool.Exec(ctx, + `INSERT INTO ports (id, name, network_id, project_id, mac_address, status, fixed_ips) + VALUES ($1, 'r3-list-addrless', $2, $3, '52:54:00:aa:bb:04', 'ACTIVE', '[]'::jsonb)`, + addrlessPort, netID, authzProjectA); err != nil { + t.Fatalf("seeding address-less port: %v", err) + } + + if code, body := doReq(r, http.MethodPost, "/v2.1/servers/"+server+"/os-interface", + fmt.Sprintf(`{"interfaceAttachment":{"port_id":%q}}`, addrlessPort), member); code != http.StatusOK { + t.Fatalf("attach: code=%d body=%s", code, body) + } + + code, body := doReq(r, http.MethodGet, "/v2.1/servers/"+server+"/os-interface", "", member) + if code != http.StatusOK { + t.Fatalf("list interfaces: code=%d body=%s", code, body) + } + if !strings.Contains(body, addrlessPort) { + t.Errorf("an attachment whose port has no addresses is missing from the os-interface list: %s", body) + } +} From c914bd5e6b582bedeaa0598aa8cbb8f29def61df Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Tue, 13 Oct 2026 11:12:00 +0000 Subject: [PATCH 10/26] test(nova): assert the empty fixed_ips serialization in the os-interface list Round 4's only observation: the list test pinned the attachment's presence but not the [] serialization it must agree with the attach response on. Signed-off-by: Uwe Schwaeke --- internal/nova/interface_attach_portid_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/nova/interface_attach_portid_test.go b/internal/nova/interface_attach_portid_test.go index e272961c..71fc3b94 100644 --- a/internal/nova/interface_attach_portid_test.go +++ b/internal/nova/interface_attach_portid_test.go @@ -247,4 +247,7 @@ func TestListInterfacesIncludesAddresslessAttachments(t *testing.T) { if !strings.Contains(body, addrlessPort) { t.Errorf("an attachment whose port has no addresses is missing from the os-interface list: %s", body) } + if !strings.Contains(body, `"fixed_ips":[]`) { + t.Errorf("the address-less attachment must list fixed_ips as [], matching the attach response: %s", body) + } } From fa43d457e60101632c6f1828bb68a463ce232f6f Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Tue, 13 Oct 2026 12:43:09 +0000 Subject: [PATCH 11/26] test(nova): skip the success-path boot test when qemu-img is absent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CI unit-test runner does not install qemu-img; the test's premise (a boot that reaches ACTIVE) requires overlay staging, which needs it. Same guard as pkg/storage's requireQemuImg — the failure-path N-6 tests, which expect ERROR anyway, still run everywhere. Signed-off-by: Uwe Schwaeke --- internal/nova/boot_failure_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/nova/boot_failure_test.go b/internal/nova/boot_failure_test.go index 8da2e688..883ab775 100644 --- a/internal/nova/boot_failure_test.go +++ b/internal/nova/boot_failure_test.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http" "os" + "os/exec" "path/filepath" "strings" "sync" @@ -581,6 +582,15 @@ func TestFailedBootReleasesAllocatedPorts(t *testing.T) { func TestSuccessfulBootKeepsItsPorts(t *testing.T) { requireDB(t) + // A fully successful boot stages a per-instance qcow2 overlay, which + // needs qemu-img. The CI unit-test runner does not install it (the same + // guard pkg/storage's tests use); without the guard this test fails there + // with the boot in ERROR on overlay staging instead of testing port + // release. + if _, err := exec.LookPath("qemu-img"); err != nil { + t.Skip("qemu-img not installed — the success-path boot needs overlay staging") + } + // Redirect the node-local state root into a test directory BEFORE the // service is built: the boot runs in a goroutine the service's Shutdown // (registered by newBootService, and so running LAST in cleanup's LIFO From 6f52ba527592168ec1510dd8dfeed7fcf287b56a Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Mon, 2 Nov 2026 10:26:42 +0000 Subject: [PATCH 12/26] fix(neutron): close the JSON-null allocation DoS; align allocation with upstream Review findings (comprehensive review of the open senolcolak PRs): - HIGH: usedSubnetAddresses scanned ports.fixed_ips with a bare jsonb_array_elements. A port on a subnet-less network persists JSON-scalar null, and one such row made every allocation fail deployment-wide (SQLSTATE 22023) with a misleading 409. Guard the SRF with jsonb_typeof and write [] instead of null; legacy rows are tolerated by the readers. - MEDIUM: metadata network_data.json used COALESCE, which does not fire for JSON null; same guard. A guest on such a port got a 500. - LOW: the same unguarded scan in the nova ?ip= server filter. - LOW: gate /os-hypervisors, /os-hypervisors/detail, /os-hypervisors/stats, /os-services and /os-console-auth-tokens/{token} with requireAdmin; statistics leaked a real deployment-wide running-VM count to members. - MEDIUM: allocateIP now mirrors upstream generate_pools() for /30, /31, /32 and IPv6 pools instead of overflowing int64 on IPv6; small pools allocate, only exhaustion fails. Tests pin the DoS and the metadata 500 (each fails on the unfixed query), the admin gates, and tiny/IPv6 allocation. Signed-off-by: Uwe Schwaeke --- internal/metadata/network_data_test.go | 48 ++++++++++ internal/metadata/service.go | 4 +- internal/neutron/allocate_free_ip_test.go | 110 +++++++++++++++++++++- internal/neutron/ports.go | 57 +++++++---- internal/nova/console.go | 3 + internal/nova/handlers.go | 12 +++ internal/nova/operator_surfaces_test.go | 57 +++++++++++ internal/nova/server_filter.go | 5 +- 8 files changed, 271 insertions(+), 25 deletions(-) diff --git a/internal/metadata/network_data_test.go b/internal/metadata/network_data_test.go index f2d429dc..bb315a7d 100644 --- a/internal/metadata/network_data_test.go +++ b/internal/metadata/network_data_test.go @@ -99,3 +99,51 @@ func TestNetworkDataJSONServesPortAddresses(t *testing.T) { t.Errorf("networks missing the port's addresses: %s", body) } } + +// TestNetworkDataJSONToleratesNullFixedIPs pins the metadata half of the +// JSON-scalar-null defect. A port on a subnet-less network persisted JSON +// `null` in fixed_ips; COALESCE does not rescue that (JSON null is not SQL +// NULL), so jsonb_array_elements raised and network_data.json answered 500. +func TestNetworkDataJSONToleratesNullFixedIPs(t *testing.T) { + requireDB(t) + ctx := t.Context() + + instanceID := seedInstance(t, nil) + + var networkID string + if err := testPool.QueryRow(ctx, + `INSERT INTO networks (name, project_id, status, admin_state_up) + VALUES ('md-null-net', $1, 'ACTIVE', true) RETURNING id::text`, + seededProjectID).Scan(&networkID); err != nil { + t.Fatalf("seed network: %v", err) + } + t.Cleanup(func() { testPool.Exec(ctx, "DELETE FROM networks WHERE id::text = $1", networkID) }) + + portID := uuid.NewString() + if _, err := testPool.Exec(ctx, + `INSERT INTO ports (id, name, network_id, project_id, device_id, device_owner, mac_address, status, fixed_ips) + VALUES ($1, 'md-null-port', $2, $3, $4, 'compute:nova', '52:54:00:0a:0b:0d', 'ACTIVE', 'null'::jsonb)`, + portID, networkID, seededProjectID, instanceID); err != nil { + t.Fatalf("seed port: %v", err) + } + t.Cleanup(func() { testPool.Exec(ctx, "DELETE FROM ports WHERE id::text = $1", portID) }) + + code, body := metaGet(newMetadataTestRouter(), "/openstack/latest/network_data.json", instanceID) + if code != http.StatusOK { + t.Fatalf("network_data.json with a JSON-null fixed_ips: code=%d body=%s; want 200", code, body) + } + + var doc struct { + Links []json.RawMessage `json:"links"` + Networks []json.RawMessage `json:"networks"` + } + if err := json.Unmarshal([]byte(body), &doc); err != nil { + t.Fatalf("network_data.json is not JSON: %v (%s)", err, body) + } + if len(doc.Links) != 1 { + t.Errorf("links = %d; want the address-less port's one link: %s", len(doc.Links), body) + } + if len(doc.Networks) != 0 { + t.Errorf("networks = %d; an address-less port contributes none: %s", len(doc.Networks), body) + } +} diff --git a/internal/metadata/service.go b/internal/metadata/service.go index 67182964..82bf679e 100644 --- a/internal/metadata/service.go +++ b/internal/metadata/service.go @@ -301,7 +301,9 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { COALESCE(s.dns_nameservers, ARRAY[]::text[]) FROM ports p JOIN networks n ON p.network_id = n.id - LEFT JOIN LATERAL jsonb_array_elements(COALESCE(p.fixed_ips, '[]'::jsonb)) fx ON true + LEFT JOIN LATERAL jsonb_array_elements( + CASE WHEN jsonb_typeof(p.fixed_ips) = 'array' THEN p.fixed_ips ELSE '[]'::jsonb END + ) fx ON true LEFT JOIN subnets s ON fx->>'subnet_id' IS NOT NULL AND s.id::text = fx->>'subnet_id' WHERE p.device_id = $1 ORDER BY p.created_at, p.id diff --git a/internal/neutron/allocate_free_ip_test.go b/internal/neutron/allocate_free_ip_test.go index 154386ff..863fcb13 100644 --- a/internal/neutron/allocate_free_ip_test.go +++ b/internal/neutron/allocate_free_ip_test.go @@ -3,6 +3,7 @@ package neutron import ( "context" "encoding/json" + "net" "testing" "github.com/google/uuid" @@ -54,8 +55,9 @@ func TestAllocateFreeIPAvoidsUsedAddresses(t *testing.T) { _, subnetID := seedN4Subnet(t, "192.168.250.0/28", "") held := map[string]bool{"192.168.250.1": true, "192.168.250.14": true} - // Hold every candidate except .13 (offset 10..13 in a /28: 10,11,12,13). - for _, ip := range []string{"192.168.250.10", "192.168.250.11", "192.168.250.12"} { + // Hold addresses across the range the allocator may now draw from — it is + // upstream-conformant and no longer biased to offsets 10..13. + for _, ip := range []string{"192.168.250.1", "192.168.250.10", "192.168.250.11", "192.168.250.12", "192.168.250.14"} { held[ip] = true seedN4Port(t, mustN4NetworkID(t, subnetID), subnetID, ip) } @@ -107,13 +109,115 @@ func TestAllocateFreeIPExhaustedSubnet(t *testing.T) { // subnets with fewer than ~12 addresses, and crypto/rand.Int PANICS on a // non-positive bound. func TestAllocateIPTinySubnetDoesNotPanic(t *testing.T) { - for _, cidr := range []string{"192.168.253.0/30", "192.168.253.0/29", "192.168.253.0/28"} { + for _, cidr := range []string{ + "192.168.253.0/30", + "192.168.253.0/29", + "192.168.253.0/28", + "192.168.253.0/31", + "192.168.253.1/32", + "fd00::/64", + "fd00::/126", + } { if ip := allocateIP(cidr); ip == "" { t.Errorf("allocateIP(%s) = \"\"; want a usable address", cidr) } } } +// TestAllocateFreeIPv6Subnet: an IPv6 subnet is allocated from normally. The +// old allocator shifted by hostBits, so a /64 overflowed int64 and panicked — +// IPv6 could not be used at all. +func TestAllocateFreeIPv6Subnet(t *testing.T) { + requireDB(t) + _, subnetID := seedN4Subnet(t, "fd00:1::/64", "") + for range 10 { + ip, err := AllocateFreeIP(context.Background(), testPool, subnetID, "fd00:1::/64") + if err != nil { + t.Fatalf("allocateFreeIP on IPv6: %v", err) + } + if parsed := net.ParseIP(ip); parsed == nil { + t.Fatalf("allocated %q, which is not an address", ip) + } + } +} + +// TestAllocateFreeIPToleratesLegacyNullFixedIPs is the regression for the +// tenant-triggerable deployment-wide allocation DoS. A port on a subnet-less +// network used to persist JSON scalar `null` in ports.fixed_ips. Because +// jsonb_array_elements was an unguarded FROM-clause SRF, one such row made +// every allocation in every subnet fail with SQLSTATE 22023. +func TestAllocateFreeIPToleratesLegacyNullFixedIPs(t *testing.T) { + requireDB(t) + ctx := context.Background() + + // A network with no subnet and a LEGACY row whose fixed_ips is JSON null, + // exactly as the old writer left it. + var bareNetworkID string + if err := testPool.QueryRow(ctx, + `INSERT INTO networks (name, project_id, status, admin_state_up) + VALUES ($1, $2, 'ACTIVE', true) RETURNING id::text`, + "n4-null-net-"+uuid.NewString()[:8], seededProjectID).Scan(&bareNetworkID); err != nil { + t.Fatalf("seeding subnet-less network: %v", err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM networks WHERE id::text = $1`, bareNetworkID) + }) + if _, err := testPool.Exec(ctx, + `INSERT INTO ports (name, network_id, project_id, mac_address, status, fixed_ips) + VALUES ('n4-null-port', $1, $2, '52:54:00:aa:bb:cc', 'ACTIVE', 'null'::jsonb)`, + bareNetworkID, seededProjectID); err != nil { + t.Fatalf("seeding legacy null-fixed_ips port: %v", err) + } + + // A different, healthy subnet must still allocate. Before the guard this + // failed for every subnet in the deployment. + _, subnetID := seedN4Subnet(t, "192.168.254.0/29", "") + ip, err := AllocateFreeIP(ctx, testPool, subnetID, "192.168.254.0/29") + if err != nil { + t.Fatalf("allocation on a healthy subnet failed because another port has JSON-null fixed_ips: %v", err) + } + if ip == "" { + t.Fatal("allocated an empty address") + } +} + +// TestCreatePortOnSubnetlessNetworkWritesEmptyArray: the writer side. A port on +// a network with no subnet must persist `[]`, never JSON null, so no NEW poison +// rows are created even though readers still tolerate old ones. +func TestCreatePortOnSubnetlessNetworkWritesEmptyArray(t *testing.T) { + requireDB(t) + ctx := context.Background() + r := newNeutronTestRouter() + + var networkID string + if err := testPool.QueryRow(ctx, + `INSERT INTO networks (name, project_id, status, admin_state_up) + VALUES ($1, $2, 'ACTIVE', true) RETURNING id::text`, + "n4-empty-net-"+uuid.NewString()[:8], seededProjectID).Scan(&networkID); err != nil { + t.Fatalf("seeding subnet-less network: %v", err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM ports WHERE network_id = $1`, networkID) + _, _ = testPool.Exec(context.Background(), `DELETE FROM networks WHERE id = $1`, networkID) + }) + + body := `{"port":{"network_id":"` + networkID + `","name":"n4-empty-port"}}` + code, resp := doReqBody(r, "POST", "/v2.0/ports", body, + map[string]string{"X-Test-Project": seededProjectID}) + if code != 201 { + t.Fatalf("create port on subnet-less network: code=%d body=%s; want 201", code, resp) + } + + var typeof string + if err := testPool.QueryRow(ctx, + `SELECT jsonb_typeof(fixed_ips) FROM ports WHERE network_id = $1`, networkID).Scan(&typeof); err != nil { + t.Fatalf("reading the created port's fixed_ips: %v", err) + } + if typeof != "array" { + t.Fatalf("fixed_ips is JSON %s; want an array so every reader can unpack it", typeof) + } +} + func mustN4NetworkID(t *testing.T, subnetID string) string { t.Helper() var id string diff --git a/internal/neutron/ports.go b/internal/neutron/ports.go index 4a26e1ad..5ae46eb3 100644 --- a/internal/neutron/ports.go +++ b/internal/neutron/ports.go @@ -84,7 +84,11 @@ func (svc *Service) CreatePort(c *gin.Context) { req.Port.NetworkID, ).Scan(&subnetID, &cidr) - var fixedIPs []map[string]any + // Non-nil even when the network has no subnet: marshalling a nil slice + // produces JSON `null`, and ports.fixed_ips must always be an array so + // every jsonb_array_elements reader can unpack it. A legacy row may still + // hold `null`; readers guard on jsonb_typeof for those. + fixedIPs := []map[string]any{} if err == nil { // Allocate an address no other port in the subnet holds (N-4). allocatedIP, allocErr := AllocateFreeIP(c.Request.Context(), svc.pool, subnetID, cidr) @@ -661,32 +665,45 @@ func allocateIP(cidr string) string { return "" } - // Count usable host addresses (excluding network and broadcast) ones, bits := ipNet.Mask.Size() hostBits := bits - ones - if hostBits < 2 { + if hostBits < 0 { return "" } - maxOffset := int64(1<= 31: // /31: both addresses usable + lo, span = 0, 2 + case isV4: // exclude network and broadcast + lo, span = 1, (int64(1)<= 32: // IPv6: bounded random window inside the subnet + // The offset must fit incrementIP's uint and int64 arithmetic; a /64 + // has far more host bits than this, so sample a window rather than + // shifting by 64 (which overflows int64 and used to panic). + lo, span = 1, 1<<32 + default: // small IPv6 subnet: first+1 .. last + lo, span = 1, (int64(1)<>'ip_address' - FROM ports, jsonb_array_elements(fixed_ips) fx + FROM ports, jsonb_array_elements( + CASE WHEN jsonb_typeof(fixed_ips) = 'array' THEN fixed_ips ELSE '[]'::jsonb END + ) fx WHERE fx->>'subnet_id' = $1 `, subnetID) if err != nil { diff --git a/internal/nova/console.go b/internal/nova/console.go index ca174287..e8672307 100644 --- a/internal/nova/console.go +++ b/internal/nova/console.go @@ -380,6 +380,9 @@ func unsupportedConsoleTypeMsg(consoleType string) string { // projection directory without asking us, so nothing about the console depends // on this endpoint working — but `openstack` and third-party tooling expect it. func (svc *Service) ShowConsoleAuthToken(c *gin.Context) { + if !requireAdmin(c, "os-console-auth-tokens:show") { + return + } if svc.consoleStore == nil { common.SendError(c, common.NewNotFoundError("console token")) return diff --git a/internal/nova/handlers.go b/internal/nova/handlers.go index bb6779e5..32100370 100644 --- a/internal/nova/handlers.go +++ b/internal/nova/handlers.go @@ -2935,6 +2935,9 @@ func (svc *Service) ListImagesDetail(c *gin.Context) { // ListHypervisors lists hypervisors (mock for Horizon) func (svc *Service) ListHypervisors(c *gin.Context) { + if !requireAdmin(c, "os-hypervisors:index") { + return + } c.JSON(200, gin.H{"hypervisors": []gin.H{ { "id": 1, @@ -2958,6 +2961,9 @@ func (svc *Service) ListHypervisors(c *gin.Context) { // ListHypervisorsDetail lists hypervisors with details (mock for Horizon) func (svc *Service) ListHypervisorsDetail(c *gin.Context) { + if !requireAdmin(c, "os-hypervisors:detail") { + return + } cpuInfoJSON := `{"arch":"x86_64","model":"Skylake-Server-IBRS","vendor":"Intel","features":[],"topology":{"cores":8,"threads":2,"sockets":1}}` c.JSON(200, gin.H{"hypervisors": []gin.H{ @@ -2984,6 +2990,9 @@ func (svc *Service) ListHypervisorsDetail(c *gin.Context) { // GetHypervisorStatistics returns aggregated hypervisor statistics (for Horizon) func (svc *Service) GetHypervisorStatistics(c *gin.Context) { + if !requireAdmin(c, "os-hypervisors:statistics") { + return + } // Count running instances. Scan failures degrade to zero (the stats // response still ships) and are logged for diagnosis. var runningVMs int @@ -3229,6 +3238,9 @@ func (svc *Service) GetLimits(c *gin.Context) { // ListServices returns list of compute services func (svc *Service) ListServices(c *gin.Context) { + if !requireAdmin(c, "os-services:index") { + return + } // Format: "2006-01-02T15:04:05.000000" (without Z, microseconds) now := time.Now().Format("2006-01-02T15:04:05.000000") diff --git a/internal/nova/operator_surfaces_test.go b/internal/nova/operator_surfaces_test.go index 1b59fa3d..99a6f390 100644 --- a/internal/nova/operator_surfaces_test.go +++ b/internal/nova/operator_surfaces_test.go @@ -129,6 +129,63 @@ func TestAggregateAdminUnchanged(t *testing.T) { _, _ = testPool.Exec(context.Background(), `DELETE FROM host_aggregates WHERE name = 'spec023-agg-2'`) } +// TestDeploymentReadSurfacesRequireAdmin: the hypervisor and service views +// describe the whole deployment, not a project, so they are administrator-only +// like the migration list and host aggregates. /os-hypervisors/statistics is +// the sharp one: it reports a real deployment-wide running-VM count. +func TestDeploymentReadSurfacesRequireAdmin(t *testing.T) { + requireDB(t) + seedAuthzFixtures(t) + r := newNovaTestRouter() + + paths := []string{ + "/v2.1/os-hypervisors", + "/v2.1/os-hypervisors/detail", + "/v2.1/os-hypervisors/statistics", + "/v2.1/os-services", + } + for _, path := range paths { + t.Run("member "+path, func(t *testing.T) { + code, body := doReq(r, http.MethodGet, path, "", operatorHdr("member")) + if code != http.StatusForbidden { + t.Errorf("member GET %s: code=%d, want 403. body: %s", path, code, body) + } + }) + t.Run("admin "+path, func(t *testing.T) { + code, body := doReq(r, http.MethodGet, path, "", operatorHdr("admin")) + if code != http.StatusOK { + t.Errorf("admin GET %s: code=%d, want 200. body: %s", path, code, body) + } + }) + } +} + +// TestConsoleAuthTokenRequiresAdmin: the compatibility token view resolves an +// arbitrary console token, which names another user's instance and its +// internal access path. It is an operator surface, gated before the store is +// even consulted (a member gets 403, not a store-dependent 404). +func TestConsoleAuthTokenRequiresAdmin(t *testing.T) { + requireDB(t) + r, store := newConsoleTestRouter(t, "stub") + id := seedConsoleInstance(t, seededProjectID) + tok, err := store.Issue(t.Context(), id, seededProjectID, testConsoleSocket, testDomainID) + if err != nil { + t.Fatalf("issuing a console token: %v", err) + } + + member := operatorHdr("member") + code, body := doReq(r, http.MethodGet, "/v2.1/os-console-auth-tokens/"+tok, "", member) + if code != http.StatusForbidden { + t.Errorf("member reading a console token: code=%d, want 403. body: %s", code, body) + } + + admin := testAuthHdr(seededProjectID, seededUserID) + code, body = doReq(r, http.MethodGet, "/v2.1/os-console-auth-tokens/"+tok, "", admin) + if code != http.StatusOK { + t.Errorf("admin reading a console token: code=%d, want 200. body: %s", code, body) + } +} + // TestMigrationListRequiresAdmin: O1. The deployment-wide list reports every // project's instance movements — server uuid, source and destination node, // flavour changes — and was readable by any authenticated tenant. The handler's diff --git a/internal/nova/server_filter.go b/internal/nova/server_filter.go index 5c7debfe..d08a57d2 100644 --- a/internal/nova/server_filter.go +++ b/internal/nova/server_filter.go @@ -241,8 +241,9 @@ func (f *serverFilter) applyRegexFilters(c *gin.Context, alias string) *common.O if alias == "" { outerID, outerProject = "instances.id", "instances.project_id" } - f.Cond("EXISTS (SELECT 1 FROM ports p, jsonb_array_elements(p.fixed_ips) e" + - " WHERE p.device_id = " + outerID + " AND p.project_id = " + outerProject + + f.Cond("EXISTS (SELECT 1 FROM ports p, jsonb_array_elements(" + + "CASE WHEN jsonb_typeof(p.fixed_ips) = 'array' THEN p.fixed_ips ELSE '[]'::jsonb END" + + ") e WHERE p.device_id = " + outerID + " AND p.project_id = " + outerProject + " AND e->>'ip_address' ~ " + f.Arg(v) + ")") } return nil From e026de878cfe71f6fa3e9d7b2f6ea4cb29960f80 Mon Sep 17 00:00:00 2001 From: Uwe Schwaeke Date: Mon, 2 Nov 2026 10:43:11 +0000 Subject: [PATCH 13/26] ci: move to Go 1.27.3 (GO-2026-6809) and pin Helm 3.22.1 Shared CI unblocker for the open PRs (independent of this PR's content): - govulncheck now reports GO-2026-6809 in crypto/x509 (reachable through the HTTP server), fixed in Go 1.27.3. Main pins 1.27.2 everywhere, so every PR's Runtime Vulnerabilities and SBOM job fails. Pin 1.27.3 in go.mod, tools/go.mod, CI workflows, the builder image, the test container and the single-node deploy script (version + sha256). - The unpinned Helm install now pulls Helm 4, whose 'helm plugin install' rejects the helm-unittest source without --verify=false, failing the Helm Charts job with 'plugin source does not support verification'. Install Helm 3.22.1 and helm-unittest 1.2.1 in an isolated plugin dir, matching PR #107, so the gate is version-stable. Signed-off-by: Uwe Schwaeke --- .github/workflows/ci.yml | 27 +++++++++++++-------------- .github/workflows/security.yml | 2 +- build/package/Dockerfile | 2 +- deployments/docker-compose.test.yml | 2 +- docs/MULTIARCH.md | 2 +- docs/security/SBOM_TRIAGE.md | 3 ++- go.mod | 2 +- scripts/deploy-single-node.sh | 4 ++-- tools/go.mod | 2 +- 9 files changed, 23 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8dd960fe..9e0b6806 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v6 with: - go-version: '1.27.2' + go-version: '1.27.3' - name: Cache Go modules uses: actions/cache@v5 @@ -83,7 +83,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v6 with: - go-version: '1.27.2' + go-version: '1.27.3' - name: Cache Go modules uses: actions/cache@v5 @@ -118,7 +118,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v6 with: - go-version: '1.27.2' + go-version: '1.27.3' - name: Cache Go modules uses: actions/cache@v5 @@ -322,6 +322,9 @@ jobs: name: Helm Charts runs-on: ubuntu-latest timeout-minutes: 10 + env: + HELM_VERSION: v3.22.1 + HELM_UNITTEST_VERSION: v1.2.1 steps: - name: Checkout code @@ -329,18 +332,14 @@ jobs: # Same mechanism as `make install-tools`: the official get-helm-3 script plus the # helm-unittest plugin, which `make chart-test` hard-requires (Makefile:103-104). - - name: Install Helm and the helm-unittest plugin + - name: Install pinned Helm and helm-unittest run: | - if ! command -v helm >/dev/null 2>&1; then - # Downloaded, not piped: the default run shell is `bash -e` without pipefail, - # so `curl ... | bash` would swallow a failed download. - curl -fsSL -o "$RUNNER_TEMP/get-helm-3" \ - https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 - bash "$RUNNER_TEMP/get-helm-3" - fi - if ! helm plugin list 2>/dev/null | grep -q '^unittest'; then - helm plugin install https://github.com/helm-unittest/helm-unittest - fi + export HELM_PLUGINS="$RUNNER_TEMP/helm-plugins" + echo "HELM_PLUGINS=$HELM_PLUGINS" >> "$GITHUB_ENV" + curl -fsSL -o "$RUNNER_TEMP/get-helm-3" \ + https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 + bash "$RUNNER_TEMP/get-helm-3" --version "$HELM_VERSION" + helm plugin install https://github.com/helm-unittest/helm-unittest --version "$HELM_UNITTEST_VERSION" helm version --short # Lints + renders + unit-tests both product charts: charts/lightstack, then diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index 2aaaaf58..5bafc671 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -24,7 +24,7 @@ jobs: - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: '1.27.2' + go-version: '1.27.3' cache: true - name: Scan Go code and pinned development tool diff --git a/build/package/Dockerfile b/build/package/Dockerfile index 969e9c9a..baabed86 100644 --- a/build/package/Dockerfile +++ b/build/package/Dockerfile @@ -1,5 +1,5 @@ # Multi-stage build for Lightstack - Multi-architecture support -FROM --platform=$BUILDPLATFORM golang:1.27.2-alpine3.24 AS builder +FROM --platform=$BUILDPLATFORM golang:1.27.3-alpine3.24 AS builder # Build arguments for cross-compilation ARG TARGETOS diff --git a/deployments/docker-compose.test.yml b/deployments/docker-compose.test.yml index 78f78bae..85aa9795 100644 --- a/deployments/docker-compose.test.yml +++ b/deployments/docker-compose.test.yml @@ -2,7 +2,7 @@ version: '3.8' services: test-runner: - image: golang:1.27.2 + image: golang:1.27.3 container_name: lightstack-test-runner working_dir: /workspace volumes: diff --git a/docs/MULTIARCH.md b/docs/MULTIARCH.md index e648a281..6155ed73 100644 --- a/docs/MULTIARCH.md +++ b/docs/MULTIARCH.md @@ -76,7 +76,7 @@ The Dockerfile uses Docker's built-in multi-architecture support: ```dockerfile # Builder stage - runs on native architecture -FROM --platform=$BUILDPLATFORM golang:1.27.2-alpine3.24 AS builder +FROM --platform=$BUILDPLATFORM golang:1.27.3-alpine3.24 AS builder # Build arguments automatically set by Docker ARG TARGETOS # Target OS (linux) diff --git a/docs/security/SBOM_TRIAGE.md b/docs/security/SBOM_TRIAGE.md index 9cd4d3be..797b46d9 100644 --- a/docs/security/SBOM_TRIAGE.md +++ b/docs/security/SBOM_TRIAGE.md @@ -46,7 +46,8 @@ The generated runtime SPDX artifact replaces this export as the release inventor | Go 1.26.2 standard-library advisories | Toolchain affects every binary | Pin Go 1.26.8 in modules, CI, builder image, and test container | | GPL and other linter-only dependencies in the root module | Development-only, not shipped | Move the pinned linter graph to `tools/go.mod` | | Vulnerable transitive modules in the pinned linter graph | Development-only but executed in CI | Upgrade the affected modules and scan the linter command with `govulncheck` | -| GO-2026-6603, -6605, -6607, -6608, -6610, -6611, -6612, -6613, -6617 in the Go 1.26.8 standard library, published 2026-10-08 | Reachable; toolchain affects every binary | Pin Go 1.27.2, the current release, in modules, CI, builder image, test container, and the single-node deploy script | +| GO-2026-6603, -6605, -6607, -6608, -6610, -6611, -6612, -6613, -6617 in the Go 1.26.8 standard library, published 2026-10-08 | Reachable; toolchain affects every binary | Upgrade the toolchain in modules, CI, builder image, test container, and the single-node deploy script; current pin is Go 1.27.3 | +| GO-2026-6809 in `crypto/x509`, detected by CI on 2026-10-30 | Reachable through the HTTP server; Go 1.27.2 trusts compromised intermediate certificates | Pin Go 1.27.3 throughout the build and test toolchain; keep vulnerability scanning enabled | | GO-2026-6603, -6610, -6611, -6612, -6617 in `x/net`, published 2026-10-08 | Reachable through gin's HTTP/2 support | Upgrade transitively from 0.57.0 to 0.60.0; `x/crypto`, `x/sys`, `x/sync`, and `x/text` move to its minimums | ## Triage procedure diff --git a/go.mod b/go.mod index 62a3d15b..11b695b1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/cobaltcore-dev/lightstack -go 1.27.2 +go 1.27.3 require ( github.com/aws/aws-sdk-go-v2 v1.42.0 diff --git a/scripts/deploy-single-node.sh b/scripts/deploy-single-node.sh index c61fa981..bde0f8d6 100755 --- a/scripts/deploy-single-node.sh +++ b/scripts/deploy-single-node.sh @@ -470,8 +470,8 @@ build_lightstack() { log_info "Building Lightstack binary..." # Use the same patched toolchain as CI and the release image. - local go_version="1.27.2" - local go_sha256="ecbadb99091a3f46e31f5f934b068b1864eafa7995211b39eaddf76996045fe5" + local go_version="1.27.3" + local go_sha256="65efbc80fdc1fa2cd2631cad5b481777a68e8725f34e89f50fc6ac714e379b40" local installed_go="" if command -v go &> /dev/null; then installed_go=$(go env GOVERSION 2>/dev/null || true) diff --git a/tools/go.mod b/tools/go.mod index fe6322a3..095810b4 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -1,6 +1,6 @@ module github.com/cobaltcore-dev/lightstack/tools -go 1.27.2 +go 1.27.3 tool github.com/golangci/golangci-lint/v2/cmd/golangci-lint From d051348f73670094f791ce541a842d87cab90978 Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Fri, 6 Nov 2026 14:48:09 +0100 Subject: [PATCH 14/26] fix(keystone): bind S3 credentials to authorized project scope Signed-off-by: Senol Colak --- internal/keystone/authz.go | 11 +++++ internal/keystone/credentials.go | 4 +- internal/keystone/ec2_credentials.go | 6 +-- internal/keystone/handlers.go | 4 +- .../keystone/identity_authz_gates_test.go | 19 ++++++++ internal/keystone/s3tokens.go | 39 +++++++++------- internal/keystone/s3tokens_disabled_test.go | 44 +++++++++++++++++++ 7 files changed, 105 insertions(+), 22 deletions(-) diff --git a/internal/keystone/authz.go b/internal/keystone/authz.go index 9d915cd0..5dc22689 100644 --- a/internal/keystone/authz.go +++ b/internal/keystone/authz.go @@ -40,3 +40,14 @@ func denyIfMalformedID(c *gin.Context, kind, id string) bool { } return false } + +// sameUser compares UUID values, including the compact IDs used by RGW tokens. +// Invalid or missing caller IDs must never authorize self-service. +func sameUser(caller, target string) bool { + callerID, err := uuid.Parse(caller) + if err != nil { + return false + } + targetID, err := uuid.Parse(target) + return err == nil && callerID == targetID +} diff --git a/internal/keystone/credentials.go b/internal/keystone/credentials.go index 46ab3208..b381c5f9 100644 --- a/internal/keystone/credentials.go +++ b/internal/keystone/credentials.go @@ -82,7 +82,7 @@ func (svc *Service) CreateCredential(c *gin.Context) { // Self-or-admin (review 2026-10-13 C-3): a credential's blob is the // recoverable secret consumed by S3/EC2 signature auth — minting one for // another user (with a known secret) would defeat that auth. - if c.GetString("user_id") != req.Credential.UserID && !common.HasRole(c, common.RoleAdmin) { + if !sameUser(c.GetString("user_id"), req.Credential.UserID) && !common.HasRole(c, common.RoleAdmin) { common.SendError(c, common.NewForbiddenError("cannot create credentials for another user")) return } @@ -137,7 +137,7 @@ func (svc *Service) credentialOwnerOrViewer(c *gin.Context, credID string) bool common.SendError(c, common.NewInternalServerError("failed to query credential")) return false } - if c.GetString("user_id") != owner && !common.HasRole(c, common.RoleAdmin) { + if !sameUser(c.GetString("user_id"), owner) && !common.HasRole(c, common.RoleAdmin) { common.SendError(c, common.NewForbiddenError("cannot manage another user's credentials")) return false } diff --git a/internal/keystone/ec2_credentials.go b/internal/keystone/ec2_credentials.go index abdf13dc..4d8e9271 100644 --- a/internal/keystone/ec2_credentials.go +++ b/internal/keystone/ec2_credentials.go @@ -52,7 +52,7 @@ func ec2CredentialView(userID, tenantID, access, secret string) gin.H { // could read or mint another user's S3 credential and reach that tenant's RGW // namespace (SPEC-014 FR-007; mirrors the self-or-admin check in ChangePassword). func denyIfNotSelfOrAdmin(c *gin.Context, userID string) bool { - if c.GetString("user_id") != userID && !common.HasRole(c, common.RoleAdmin) { + if !sameUser(c.GetString("user_id"), userID) && !common.HasRole(c, common.RoleAdmin) { common.SendError(c, common.NewForbiddenError("cannot manage another user's EC2 credentials")) return true } @@ -178,7 +178,7 @@ func (svc *Service) ListEC2Credentials(c *gin.Context) { continue } tid := b.TenantID - if tid == "" && projectID != nil { + if projectID != nil { tid = *projectID } creds = append(creds, ec2CredentialView(userID, tid, b.Access, b.Secret)) @@ -219,7 +219,7 @@ func (svc *Service) GetEC2Credential(c *gin.Context) { var b ec2Blob _ = json.Unmarshal([]byte(blob), &b) tid := b.TenantID - if tid == "" && projectID != nil { + if projectID != nil { tid = *projectID } c.JSON(200, gin.H{"credential": ec2CredentialView(userID, tid, b.Access, b.Secret)}) diff --git a/internal/keystone/handlers.go b/internal/keystone/handlers.go index 108fc2cb..4545e0d2 100644 --- a/internal/keystone/handlers.go +++ b/internal/keystone/handlers.go @@ -1353,7 +1353,7 @@ func (svc *Service) UpdateUser(c *gin.Context) { // admin's — or disable any account, because this handler (unlike // ChangePassword) never asked for the original password. isAdmin := common.HasRole(c, common.RoleAdmin) - if c.GetString("user_id") != userID && !isAdmin { + if !sameUser(c.GetString("user_id"), userID) && !isAdmin { common.SendError(c, common.NewForbiddenError("cannot update another user")) return } @@ -1518,7 +1518,7 @@ func (svc *Service) ChangePassword(c *gin.Context) { // Users may only change their own password; admins may change any user's password callerID := c.GetString("user_id") - if callerID != userID && !common.HasRole(c, common.RoleAdmin) { + if !sameUser(callerID, userID) && !common.HasRole(c, common.RoleAdmin) { common.SendError(c, common.NewForbiddenError("cannot change another user's password")) return } diff --git a/internal/keystone/identity_authz_gates_test.go b/internal/keystone/identity_authz_gates_test.go index 4b77538d..d23cce45 100644 --- a/internal/keystone/identity_authz_gates_test.go +++ b/internal/keystone/identity_authz_gates_test.go @@ -105,6 +105,25 @@ func TestIdentitySelfServiceStillWorks(t *testing.T) { }) member := map[string]string{"X-Test-Roles": "member", "X-Test-User": created.User.ID} + for _, alias := range []string{strings.ToUpper(created.User.ID), strings.ReplaceAll(created.User.ID, "-", "")} { + for _, tc := range []struct{ method, suffix, body string }{ + {"GET", "", ""}, + {"GET", "/application_credentials", ""}, + {"PATCH", "", `{"user":{"email":"self@example.com"}}`}, + } { + req := httptest.NewRequest(tc.method, "/v3/users/"+alias+tc.suffix, strings.NewReader(tc.body)) + req.Header.Set("Content-Type", "application/json") + for k, v := range member { + req.Header.Set(k, v) + } + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + if w.Code != http.StatusOK { + t.Errorf("self %s %s: %d %s", tc.method, alias+tc.suffix, w.Code, w.Body.String()) + } + } + } + doPatch := func(body string) int { req := httptest.NewRequest("PATCH", "/v3/users/"+created.User.ID, strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") diff --git a/internal/keystone/s3tokens.go b/internal/keystone/s3tokens.go index 6abd32e2..fd746dcd 100644 --- a/internal/keystone/s3tokens.go +++ b/internal/keystone/s3tokens.go @@ -122,7 +122,10 @@ func (svc *Service) validateSignedRequest(c *gin.Context) { } pid := b.TenantID - if pid == "" && projectID != nil { + // The generic credential API allows owners to edit the blob. Its embedded + // tenant must not override the credential's immutable, recorded project. + // Retain the blob fallback only for legacy rows without project_id. + if projectID != nil { pid = *projectID } svc.renderS3Token(c, userID, pid) @@ -221,21 +224,27 @@ func (svc *Service) renderS3Token(c *gin.Context, userID, projectID string) { SELECT DISTINCT r.name FROM roles r JOIN role_assignments ra ON ra.role_id = r.id WHERE ra.user_id = $1 AND ra.project_id = $2`, userID, projectID) - if err == nil { - defer rows.Close() - for rows.Next() { - var name string - if err := rows.Scan(&name); err != nil { - log.Warn().Err(err).Str("operation", "renderS3Token").Msg("failed to scan role row") - } else { - roles = append(roles, gin.H{"name": name}) - } - } - // Log rather than fail the token: RGW uses the role list for tenant mapping, and - // refusing the token outright would deny S3 access entirely. - if err := rows.Err(); err != nil { - log.Error().Err(err).Str("user_id", userID).Msg("s3token role rows iteration error; role list may be truncated") + if err != nil { + log.Warn().Err(err).Msg("s3tokens: role lookup failed") + common.SendError(c, common.NewUnauthorizedError(authFailed)) + return + } + defer rows.Close() + for rows.Next() { + var name string + if err := rows.Scan(&name); err != nil { + log.Warn().Err(err).Msg("s3tokens: role scan failed") + common.SendError(c, common.NewUnauthorizedError(authFailed)) + return } + roles = append(roles, gin.H{"name": name}) + } + // Credentials survive role removal. A valid signature alone no longer + // authorizes a project after that membership has been revoked. + if err := rows.Err(); err != nil || len(roles) == 0 { + log.Warn().Err(err).Str("user_id", userID).Msg("s3tokens: no verified project role") + common.SendError(c, common.NewUnauthorizedError(authFailed)) + return } // RGW parses the response as a full Keystone token and requires expires_at diff --git a/internal/keystone/s3tokens_disabled_test.go b/internal/keystone/s3tokens_disabled_test.go index d4f05b15..76db581e 100644 --- a/internal/keystone/s3tokens_disabled_test.go +++ b/internal/keystone/s3tokens_disabled_test.go @@ -169,6 +169,50 @@ func TestS3TokensDisabledProjectDenied(t *testing.T) { assertMaskedS3Denial(t, postS3Tokens(t, signedS3TokensBody(access, secret))) } +func TestS3TokensRevokedProjectRoleDenied(t *testing.T) { + access, secret := seedS3DisabledIdentity(t) + if _, err := testPool.Exec(t.Context(), "DELETE FROM role_assignments WHERE user_id=$1 AND project_id=$2", s3DisabledUserID, s3DisabledProjectID); err != nil { + t.Fatal(err) + } + assertMaskedS3Denial(t, postS3Tokens(t, signedS3TokensBody(access, secret))) +} + +func TestCredentialBlobCannotChangeS3Project(t *testing.T) { + access, secret := seedS3DisabledIdentity(t) + var credentialID string + if err := testPool.QueryRow(t.Context(), "SELECT id FROM credentials WHERE access_key=$1", access).Scan(&credentialID); err != nil { + t.Fatal(err) + } + blob, _ := json.Marshal(ec2Blob{Access: access, Secret: secret, UserID: s3DisabledUserID, TenantID: rgwCompatProjectID}) + body, _ := json.Marshal(map[string]any{"credential": map[string]any{"blob": string(blob)}}) + req := httptest.NewRequest(http.MethodPatch, "/v3/credentials/"+credentialID, strings.NewReader(string(body))) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Test-User", s3DisabledUserID) + req.Header.Set("X-Test-Roles", "member") + w := httptest.NewRecorder() + newIdentityTestRouter().ServeHTTP(w, req) + if w.Code != http.StatusOK { + t.Fatalf("update own blob: %d %s", w.Code, w.Body.String()) + } + w = postS3Tokens(t, signedS3TokensBody(access, secret)) + if w.Code != http.StatusOK { + t.Fatalf("authenticate original credential: %d %s", w.Code, w.Body.String()) + } + var result struct { + Token struct { + Project struct { + ID string `json:"id"` + } `json:"project"` + } `json:"token"` + } + if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil { + t.Fatal(err) + } + if result.Token.Project.ID != canonicalID(s3DisabledProjectID) { + t.Errorf("mutable blob changed scoped project to %q", result.Token.Project.ID) + } +} + // TestRenderS3TokenMissingIdentityDenied: a stale EC2 blob tenant (project // row gone while the credential survives) or a user row that vanished under // the credential must deny — not render an envelope with empty names. From 879ecfcd3a42457904c99b021ef2a57fa6ff16de Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Fri, 6 Nov 2026 14:48:14 +0100 Subject: [PATCH 15/26] fix: serialize IP and volume device allocation Signed-off-by: Senol Colak --- internal/neutron/allocate_free_ip_test.go | 27 +++ internal/neutron/ports.go | 162 +++++++++++------- internal/nova/interface_attach.go | 70 ++++---- .../nova/ip_allocation_concurrency_test.go | 103 +++++++++++ internal/nova/volume_attachment.go | 17 +- .../volume_attachment_concurrency_test.go | 96 +++++++++++ 6 files changed, 374 insertions(+), 101 deletions(-) create mode 100644 internal/nova/ip_allocation_concurrency_test.go create mode 100644 internal/nova/volume_attachment_concurrency_test.go diff --git a/internal/neutron/allocate_free_ip_test.go b/internal/neutron/allocate_free_ip_test.go index 863fcb13..13e99bd4 100644 --- a/internal/neutron/allocate_free_ip_test.go +++ b/internal/neutron/allocate_free_ip_test.go @@ -3,12 +3,39 @@ package neutron import ( "context" "encoding/json" + "fmt" "net" "testing" "github.com/google/uuid" ) +func TestAllocateFreeIPFindsLastAddress(t *testing.T) { + requireDB(t) + networkID, subnetID := seedN4Subnet(t, "192.168.249.0/24", "192.168.249.1") + for i := 2; i < 254; i++ { + seedN4Port(t, networkID, subnetID, fmt.Sprintf("192.168.249.%d", i)) + } + for range 10 { + ip, err := AllocateFreeIP(t.Context(), testPool, subnetID, "192.168.249.0/24") + if err != nil || ip != "192.168.249.254" { + t.Fatalf("last address: ip=%q err=%v; want 192.168.249.254", ip, err) + } + } +} + +func TestIPAvailableCanonicalIPv6(t *testing.T) { + requireDB(t) + networkID, subnetID := seedN4Subnet(t, "2001:db8::/64", "2001:0db8:0:0:0:0:0:1") + seedN4Port(t, networkID, subnetID, "2001:0db8:0:0:0:0:0:2") + for _, ip := range []string{"2001:db8::1", "2001:db8::2", "2001:DB8::2"} { + available, err := IPAvailable(t.Context(), testPool, subnetID, ip) + if err != nil || available { + t.Errorf("IPAvailable(%s)=%v, %v; want occupied", ip, available, err) + } + } +} + // seedN4Subnet inserts a network + subnet and returns their ids. func seedN4Subnet(t *testing.T, cidr, gateway string) (networkID, subnetID string) { t.Helper() diff --git a/internal/neutron/ports.go b/internal/neutron/ports.go index 5ae46eb3..1662f4eb 100644 --- a/internal/neutron/ports.go +++ b/internal/neutron/ports.go @@ -10,8 +10,8 @@ import ( "math/big" "net" "net/http" + "net/netip" "strconv" - "strings" "time" "github.com/cobaltcore-dev/lightstack/internal/common" @@ -84,28 +84,12 @@ func (svc *Service) CreatePort(c *gin.Context) { req.Port.NetworkID, ).Scan(&subnetID, &cidr) - // Non-nil even when the network has no subnet: marshalling a nil slice - // produces JSON `null`, and ports.fixed_ips must always be an array so - // every jsonb_array_elements reader can unpack it. A legacy row may still - // hold `null`; readers guard on jsonb_typeof for those. - fixedIPs := []map[string]any{} - if err == nil { - // Allocate an address no other port in the subnet holds (N-4). - allocatedIP, allocErr := AllocateFreeIP(c.Request.Context(), svc.pool, subnetID, cidr) - if allocErr != nil { - log.Warn().Err(allocErr).Str("subnet_id", subnetID).Msg("port create: address allocation failed") - common.SendError(c, common.NewConflictError("no IP address available in the network's subnet")) - return - } - fixedIPs = []map[string]any{ - { - "subnet_id": subnetID, - "ip_address": allocatedIP, - }, - } + if err != nil && !errors.Is(err, pgx.ErrNoRows) { + log.Error().Err(err).Msg("port create: subnet lookup failed") + common.SendError(c, common.NewInternalServerError("failed to look up subnet")) + return } - - fixedIPsJSON, _ := json.Marshal(fixedIPs) + fixedIPs := []map[string]any{} // Create the TAP where the bridge is — the namespace Lightstack runs in. CreateTAPDevice no longer takes // a namespace at all, so this placement is now a property of the API rather than of the call. @@ -145,15 +129,33 @@ func (svc *Service) CreatePort(c *gin.Context) { // Insert into database now := time.Now() - _, err = svc.pool.Exec(c.Request.Context(), ` + err = database.WithTx(c.Request.Context(), svc.pool, func(tx pgx.Tx) error { + if subnetID != "" { + cidr, err = LockSubnet(c.Request.Context(), tx, subnetID) + if err != nil { + return err + } + ip, err := AllocateFreeIP(c.Request.Context(), tx, subnetID, cidr) + if err != nil { + return err + } + fixedIPs = append(fixedIPs, map[string]any{"subnet_id": subnetID, "ip_address": ip}) + } + fixedIPsJSON, _ := json.Marshal(fixedIPs) + _, err = tx.Exec(c.Request.Context(), ` INSERT INTO ports (id, name, network_id, project_id, device_id, device_owner, mac_address, admin_state_up, status, fixed_ips, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) `, portID, req.Port.Name, req.Port.NetworkID, projectID, sql.NullString{String: req.Port.DeviceID, Valid: req.Port.DeviceID != ""}, - sql.NullString{String: req.Port.DeviceOwner, Valid: req.Port.DeviceOwner != ""}, macAddress, adminStateUp, "ACTIVE", fixedIPsJSON, now, now) - + sql.NullString{String: req.Port.DeviceOwner, Valid: req.Port.DeviceOwner != ""}, macAddress, adminStateUp, "ACTIVE", fixedIPsJSON, now, now) + return err + }) if err != nil { - log.Error().Err(err).Str("operation", "create_port").Msg("database error") _ = svc.tapManager.DeleteTAPDevice(tapName) + if errors.Is(err, ErrNoAvailableIP) { + common.SendError(c, common.NewConflictError("no IP address available in the network's subnet")) + return + } + log.Error().Err(err).Str("operation", "create_port").Msg("database error") common.SendError(c, common.NewInternalServerError("failed to create port")) return } @@ -706,9 +708,25 @@ func allocateIP(cidr string) string { return incrementIP(ipNet.IP, uint(offset)).String() } -// usedSubnetAddresses returns every address held by a port in the subnet -// (lower-cased), plus the subnet's own gateway when set. -func usedSubnetAddresses(ctx context.Context, pool *pgxpool.Pool, subnetID string) (map[string]bool, error) { +type subnetQuerier interface { + Query(context.Context, string, ...any) (pgx.Rows, error) + QueryRow(context.Context, string, ...any) pgx.Row +} + +// ErrNoAvailableIP distinguishes exhaustion from a database failure. +var ErrNoAvailableIP = errors.New("no IP address available in the subnet") + +// LockSubnet serializes allocation and insertion across Neutron and Nova. Hold +// this transaction until the port's fixed_ips have been inserted. +func LockSubnet(ctx context.Context, tx pgx.Tx, subnetID string) (string, error) { + var cidr string + err := tx.QueryRow(ctx, "SELECT cidr FROM subnets WHERE id = $1 FOR NO KEY UPDATE", subnetID).Scan(&cidr) + return cidr, err +} + +// usedSubnetAddresses includes port addresses and the gateway, canonicalized so +// compressed/expanded IPv6 spellings refer to the same address. +func usedSubnetAddresses(ctx context.Context, pool subnetQuerier, subnetID string) (map[string]bool, error) { used := map[string]bool{} rows, err := pool.Query(ctx, ` SELECT fx->>'ip_address' @@ -726,8 +744,8 @@ func usedSubnetAddresses(ctx context.Context, pool *pgxpool.Pool, subnetID strin if err := rows.Scan(&ip); err != nil { return nil, fmt.Errorf("scanning a used address: %w", err) } - if ip != "" { - used[strings.ToLower(ip)] = true + if addr := net.ParseIP(ip); addr != nil { + used[addr.String()] = true } } if err := rows.Err(); err != nil { @@ -737,9 +755,11 @@ func usedSubnetAddresses(ctx context.Context, pool *pgxpool.Pool, subnetID strin // The gateway belongs to the subnet, not to any port. var gateway sql.NullString if err := pool.QueryRow(ctx, - "SELECT gateway_ip FROM subnets WHERE id = $1", subnetID).Scan(&gateway); err == nil && - gateway.Valid && gateway.String != "" { - used[strings.ToLower(gateway.String)] = true + "SELECT gateway_ip FROM subnets WHERE id = $1", subnetID).Scan(&gateway); err != nil { + return nil, fmt.Errorf("reading the subnet's gateway: %w", err) + } + if addr := net.ParseIP(gateway.String); addr != nil { + used[addr.String()] = true } return used, nil } @@ -749,12 +769,16 @@ func usedSubnetAddresses(ctx context.Context, pool *pgxpool.Pool, subnetID strin // accepts a caller-specified fixed_ip and must refuse one another port holds — // the explicit side of the N-4 collision rule the allocator covers for // generated addresses. -func IPAvailable(ctx context.Context, pool *pgxpool.Pool, subnetID, ip string) (bool, error) { +func IPAvailable(ctx context.Context, pool subnetQuerier, subnetID, ip string) (bool, error) { + addr := net.ParseIP(ip) + if addr == nil { + return false, fmt.Errorf("invalid IP address %q", ip) + } used, err := usedSubnetAddresses(ctx, pool, subnetID) if err != nil { return false, err } - return !used[strings.ToLower(ip)], nil + return !used[addr.String()], nil } // AllocateFreeIP picks a random usable address in the subnet that no port in @@ -770,30 +794,43 @@ func IPAvailable(ctx context.Context, pool *pgxpool.Pool, subnetID, ip string) ( // over one subnet is how they hand out the same address (round 2 of this // review). // -// The check-then-use is not a lock: two simultaneous allocations can still -// race for the last free address. It collapses the collision space from -// "every existing port" to "the same instant", which for port creation — a -// user- or boot-paced operation — is the practical fix; a DB-level exclusion -// on the JSONB elements would be the complete one. -func AllocateFreeIP(ctx context.Context, pool *pgxpool.Pool, subnetID, cidr string) (string, error) { +// Writers must LockSubnet and insert the port in the same transaction. +func AllocateFreeIP(ctx context.Context, pool subnetQuerier, subnetID, cidr string) (string, error) { + prefix, err := netip.ParsePrefix(cidr) + if err != nil { + return "", fmt.Errorf("invalid subnet CIDR: %w", err) + } used, err := usedSubnetAddresses(ctx, pool, subnetID) if err != nil { return "", err } - // Random candidates until one is free. A hundred attempts covers any - // realistically dense subnet well before it is full; a subnet that is full - // is an error, not an infinite loop. + // Random candidates make sparse subnets cheap. Dense subnets get a complete + // search below, so the last free address is never missed by chance. for range 100 { candidate := allocateIP(cidr) if candidate == "" { return "", fmt.Errorf("subnet %s has no usable addresses", subnetID) } - if !used[strings.ToLower(candidate)] { + if !used[candidate] { return candidate, nil } } - return "", fmt.Errorf("no free address in subnet %s", subnetID) + // At most len(used)+1 candidates are visited, even on a huge IPv6 range. + candidate := prefix.Masked().Addr() + if (candidate.Is4() && prefix.Bits() < 31) || (candidate.Is6() && prefix.Bits() < 128) { + candidate = candidate.Next() + } + for candidate.IsValid() && prefix.Contains(candidate) { + if candidate.Is4() && prefix.Bits() < 31 && !prefix.Contains(candidate.Next()) { + break // IPv4 broadcast address + } + if !used[candidate.String()] { + return candidate.String(), nil + } + candidate = candidate.Next() + } + return "", fmt.Errorf("%w: %s", ErrNoAvailableIP, subnetID) } // Security Groups implementation @@ -1628,23 +1665,7 @@ func (svc *Service) AllocatePortForInstance(ctx context.Context, networkID, proj return nil, fmt.Errorf("looking up a subnet for network %s: %w", networkID, err) } - // Allocate an IP no other port in the subnet holds (review 2026-10-13 - // N-4): the old random pick collided at birthday-problem rates, giving two - // ports — two MACs — the same address. Tenant-visible wording so the - // server's fault says what the tenant can do about it (a full or broken - // subnet), matching the no-subnet case above. - allocatedIP, allocErr := AllocateFreeIP(ctx, svc.pool, subnetID, cidr) - if allocErr != nil { - return nil, &tenantVisibleError{msg: fmt.Sprintf("failed to allocate a free IP from subnet %s", subnetID)} - } - - fixedIPs := []map[string]any{ - { - "subnet_id": subnetID, - "ip_address": allocatedIP, - }, - } - fixedIPsJSON, _ := json.Marshal(fixedIPs) + var allocatedIP string // The security groups the instance was created with, so rules apply to the // VM's north-south traffic (SPEC-009 WS-3 / T034). port_security_groups is what @@ -1708,6 +1729,19 @@ func (svc *Service) AllocatePortForInstance(ctx context.Context, networkID, proj // which keeps the API and the dataplane in agreement. now := time.Now() if err := database.WithTx(ctx, svc.pool, func(tx pgx.Tx) error { + cidr, err = LockSubnet(ctx, tx, subnetID) + if err != nil { + return err + } + allocatedIP, err = AllocateFreeIP(ctx, tx, subnetID, cidr) + if errors.Is(err, ErrNoAvailableIP) { + return &tenantVisibleError{msg: fmt.Sprintf("failed to allocate a free IP from subnet %s", subnetID)} + } + if err != nil { + return err + } + fixedIPsJSON, _ := json.Marshal([]map[string]any{{"subnet_id": subnetID, "ip_address": allocatedIP}}) + if _, err := tx.Exec(ctx, ` INSERT INTO ports (id, name, network_id, project_id, device_id, device_owner, mac_address, admin_state_up, status, fixed_ips, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) diff --git a/internal/nova/interface_attach.go b/internal/nova/interface_attach.go index 7c593bbc..62c26d39 100644 --- a/internal/nova/interface_attach.go +++ b/internal/nova/interface_attach.go @@ -9,6 +9,7 @@ import ( "time" "github.com/cobaltcore-dev/lightstack/internal/common" + "github.com/cobaltcore-dev/lightstack/internal/database" "github.com/cobaltcore-dev/lightstack/internal/neutron" "github.com/gin-gonic/gin" "github.com/google/uuid" @@ -169,42 +170,47 @@ func (svc *Service) AttachInterface(c *gin.Context) { common.SendError(c, common.NewBadRequestError("fixed_ip is not a valid address in the network's subnet")) return } - available, availErr := neutron.IPAvailable(c.Request.Context(), svc.pool, subnetID, fixedIP) - if availErr != nil { - log.Error().Err(availErr).Str("operation", "attach_interface").Msg("IP availability check failed") - common.SendError(c, common.NewInternalServerError("failed to allocate IP")) - return + fixedIP = ip.String() + } + + // Allocate/check and insert while holding the subnet lock shared with + // Neutron's port-create and server-boot paths. + err = database.WithTx(c.Request.Context(), svc.pool, func(tx pgx.Tx) error { + cidr, err = neutron.LockSubnet(c.Request.Context(), tx, subnetID) + if err != nil { + return err } - if !available { - common.SendError(c, common.NewConflictError("IP address already allocated in the subnet")) - return + if fixedIP != "" { + available, err := neutron.IPAvailable(c.Request.Context(), tx, subnetID, fixedIP) + if err != nil { + return err + } + if !available { + return neutron.ErrNoAvailableIP + } + } else { + fixedIP, err = neutron.AllocateFreeIP(c.Request.Context(), tx, subnetID, cidr) + if err != nil { + return err + } } - } else { - // The same collision-free allocator every other port-creation path - // uses (review 2026-10-13 N-4): this path used to run its own - // next-free scan, and two allocators over one subnet is how they - // hand out the same address. - fixedIP, err = neutron.AllocateFreeIP(c.Request.Context(), svc.pool, subnetID, cidr) - if err != nil { - log.Error().Err(err).Str("operation", "allocate_ip").Msg("IP allocation error") - common.SendError(c, common.NewConflictError("no IP address available in the network's subnet")) + fixedIPEntries = []gin.H{{"ip_address": fixedIP, "subnet_id": subnetID}} + fixedIPsJSON, _ := json.Marshal(fixedIPEntries) + _, err = tx.Exec(c.Request.Context(), ` + INSERT INTO ports (id, network_id, project_id, name, mac_address, fixed_ips, device_id, device_owner, status, created_at, updated_at) + VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8, $9, $10, $11) + `, portID, networkID, projectID, fmt.Sprintf("port-%s", portID[:8]), macAddress, fixedIPsJSON, instanceID, "compute:nova", "ACTIVE", time.Now(), time.Now()) + return err + }) + if err != nil { + if errors.Is(err, neutron.ErrNoAvailableIP) { + message := "no IP address available in the network's subnet" + if req.InterfaceAttachment.FixedIP != "" { + message = "IP address already allocated in the subnet" + } + common.SendError(c, common.NewConflictError(message)) return } - } - - // Create port. The fixed_ips entry carries the SUBNET's id, not the - // network's (round 2 of this review): everything that resolves a port's - // addressing — the metadata service's network_data.json, DHCP, the - // collision check above — joins on the subnet id and silently missed - // these ports when it held a network id. - fixedIPsJSON := fmt.Sprintf(`[{"ip_address": "%s", "subnet_id": "%s"}]`, fixedIP, subnetID) - fixedIPEntries = []gin.H{{"ip_address": fixedIP, "subnet_id": subnetID}} - _, err = svc.pool.Exec(c.Request.Context(), ` - INSERT INTO ports (id, network_id, project_id, name, mac_address, fixed_ips, device_id, device_owner, status, created_at, updated_at) - VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8, $9, $10, $11) - `, portID, networkID, projectID, fmt.Sprintf("port-%s", portID[:8]), macAddress, fixedIPsJSON, instanceID, "compute:nova", "ACTIVE", time.Now(), time.Now()) - - if err != nil { log.Error().Err(err).Str("operation", "create_port").Msg("database error") common.SendError(c, common.NewInternalServerError("failed to create port")) return diff --git a/internal/nova/ip_allocation_concurrency_test.go b/internal/nova/ip_allocation_concurrency_test.go new file mode 100644 index 00000000..faaa671a --- /dev/null +++ b/internal/nova/ip_allocation_concurrency_test.go @@ -0,0 +1,103 @@ +package nova + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/cobaltcore-dev/lightstack/internal/neutron" + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +// All three port writers share one last address. An exhausted request must +// fail without creating a second port holding it. +func TestConcurrentPortWritersReserveOneAddress(t *testing.T) { + requireDB(t) + serverID := seedConsoleInstance(t, seededProjectID) + networkID, subnetID := uuid.NewString(), uuid.NewString() + if _, err := testPool.Exec(t.Context(), `INSERT INTO networks (id, name, project_id, status) + VALUES ($1, 'concurrent-ip', $2, 'ACTIVE')`, networkID, seededProjectID); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), `DELETE FROM interface_attachments WHERE port_id IN (SELECT id FROM ports WHERE network_id=$1)`, networkID) + _, _ = testPool.Exec(context.Background(), "DELETE FROM networks WHERE id=$1", networkID) + }) + if _, err := testPool.Exec(t.Context(), `INSERT INTO subnets (id, name, network_id, project_id, cidr) + VALUES ($1, 'one-address', $2, $3, '192.0.2.7/32')`, subnetID, networkID, seededProjectID); err != nil { + t.Fatal(err) + } + if _, err := neutron.EnsureDefaultSecurityGroup(t.Context(), testPool, seededProjectID); err != nil { + t.Fatal(err) + } + novaRouter := newNovaTestRouter() + networkSvc := neutron.NewService(testPool, "stub", nil) + portRouter := gin.New() + portRouter.Use(func(c *gin.Context) { + c.Set("project_id", seededProjectID) + c.Set("roles", []string{"member"}) + }) + networkSvc.RegisterRoutes(&portRouter.RouterGroup) + ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second) + defer cancel() + const count = 12 + start := make(chan struct{}) + type result struct { + code int + body string + } + results := make(chan result, count) + for i := range count { + go func() { + <-start + if i%3 == 0 { + _, err := networkSvc.AllocatePortForInstance(ctx, networkID, seededProjectID, serverID, nil, false) + if err == nil { + results <- result{http.StatusCreated, ""} + return + } + code := http.StatusInternalServerError + if strings.Contains(err.Error(), "failed to allocate a free IP") { + code = http.StatusConflict + } + results <- result{code, err.Error()} + return + } + router, path, body := portRouter, "/v2.0/ports", fmt.Sprintf(`{"port":{"network_id":%q}}`, networkID) + if i%3 == 1 { + router, path = novaRouter, "/v2.1/servers/"+serverID+"/os-interface" + body = fmt.Sprintf(`{"interfaceAttachment":{"net_id":%q,"fixed_ip":"192.0.2.7"}}`, networkID) + } + req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body)).WithContext(ctx) + req.Header.Set("Content-Type", "application/json") + for k, v := range testAuthHdr(seededProjectID, seededUserID) { + req.Header.Set(k, v) + } + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + results <- result{w.Code, w.Body.String()} + }() + } + close(start) + successes := 0 + for range count { + r := <-results + if r.code == http.StatusOK || r.code == http.StatusCreated { + successes++ + } else if r.code != http.StatusConflict { + t.Errorf("unexpected response %d %s", r.code, r.body) + } + } + var ports int + if err := testPool.QueryRow(t.Context(), "SELECT count(*) FROM ports WHERE network_id=$1", networkID).Scan(&ports); err != nil { + t.Fatal(err) + } + if successes != 1 || ports != 1 { + t.Errorf("successes=%d ports=%d; want one address claimed exactly once", successes, ports) + } +} diff --git a/internal/nova/volume_attachment.go b/internal/nova/volume_attachment.go index 9d8670db..07946615 100644 --- a/internal/nova/volume_attachment.go +++ b/internal/nova/volume_attachment.go @@ -130,6 +130,14 @@ func (svc *Service) AttachVolume(c *gin.Context) { attachmentID := uuid.NewString() var now time.Time err = database.WithTx(c.Request.Context(), svc.pool, func(tx pgx.Tx) error { + // Different volumes attached to one server must reserve distinct devices. + // Lock the instance before the volume, and use this transaction for every + // query: acquiring another pool connection here can deadlock a full pool. + if err := tx.QueryRow(c.Request.Context(), + "SELECT COALESCE(libvirt_domain_id, '') FROM instances WHERE id = $1 FOR NO KEY UPDATE", + instanceID).Scan(&libvirtDomainID); err != nil { + return err + } var volumeStatus string var attachedToInstance any if err := tx.QueryRow(c.Request.Context(), @@ -154,7 +162,7 @@ func (svc *Service) AttachVolume(c *gin.Context) { // Auto-assign device if not provided device = req.VolumeAttachment.Device if device == "" { - device, err = svc.getNextAvailableDevice(c.Request.Context(), instanceID) + device, err = getNextAvailableDevice(c.Request.Context(), tx, instanceID) if err != nil { return fmt.Errorf("device assignment: %w", err) } @@ -471,9 +479,9 @@ func attachedVolumes(vols []gin.H) []gin.H { } // getNextAvailableDevice finds the next available device name (vdb, vdc, etc.) -func (svc *Service) getNextAvailableDevice(ctx context.Context, instanceID string) (string, error) { +func getNextAvailableDevice(ctx context.Context, tx pgx.Tx, instanceID string) (string, error) { // Query existing devices - rows, err := svc.pool.Query(ctx, + rows, err := tx.Query(ctx, "SELECT device FROM volume_attachments WHERE instance_id = $1", instanceID, ) @@ -486,8 +494,7 @@ func (svc *Service) getNextAvailableDevice(ctx context.Context, instanceID strin for rows.Next() { var device string if err := rows.Scan(&device); err != nil { - log.Warn().Err(err).Str("operation", "getNextAvailableDevice").Msg("failed to scan row") - continue + return "", fmt.Errorf("scan volume device: %w", err) } usedDevices[device] = true } diff --git a/internal/nova/volume_attachment_concurrency_test.go b/internal/nova/volume_attachment_concurrency_test.go new file mode 100644 index 00000000..0f4a2b4e --- /dev/null +++ b/internal/nova/volume_attachment_concurrency_test.go @@ -0,0 +1,96 @@ +package nova + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/gin-gonic/gin" + "github.com/jackc/pgx/v5/pgxpool" +) + +func seedAttachVolume(t *testing.T) string { + t.Helper() + var id string + if err := testPool.QueryRow(t.Context(), `INSERT INTO volumes (name, project_id, user_id, size_gb, status) + VALUES ('attach-review', $1, $2, 1, 'available') RETURNING id`, seededProjectID, seededUserID).Scan(&id); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), "DELETE FROM volume_attachments WHERE volume_id = $1", id) + _, _ = testPool.Exec(context.Background(), "DELETE FROM volumes WHERE id = $1", id) + }) + return id +} + +func attachWithPool(ctx context.Context, pool *pgxpool.Pool, instanceID, volumeID string) *httptest.ResponseRecorder { + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + c.Request = httptest.NewRequest(http.MethodPost, "/", strings.NewReader( + fmt.Sprintf(`{"volumeAttachment":{"volumeId":%q}}`, volumeID))).WithContext(ctx) + c.Request.Header.Set("Content-Type", "application/json") + c.Params = gin.Params{{Key: "id", Value: instanceID}} + c.Set("project_id", seededProjectID) + c.Set("user_id", seededUserID) + c.Set("roles", []string{"member"}) + svc := &Service{pool: pool} + svc.AttachVolume(c) + return w +} + +func TestAttachVolumeWithOneConnection(t *testing.T) { + requireDB(t) + instanceID, volumeID := seedConsoleInstance(t, seededProjectID), seedAttachVolume(t) + config := testPool.Config() + config.MaxConns, config.MinConns = 1, 0 + pool, err := pgxpool.NewWithConfig(t.Context(), config) + if err != nil { + t.Fatal(err) + } + defer pool.Close() + ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second) + defer cancel() + w := attachWithPool(ctx, pool, instanceID, volumeID) + if w.Code != http.StatusOK { + t.Fatalf("attach with one connection: %d %s", w.Code, w.Body.String()) + } +} + +func TestConcurrentVolumeAttachmentsUseDistinctDevices(t *testing.T) { + requireDB(t) + instanceID := seedConsoleInstance(t, seededProjectID) + const count = 8 + volumes := make([]string, count) + for i := range volumes { + volumes[i] = seedAttachVolume(t) + } + start := make(chan struct{}) + results := make(chan *httptest.ResponseRecorder, count) + ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second) + defer cancel() + for _, id := range volumes { + go func() { + <-start + results <- attachWithPool(ctx, testPool, instanceID, id) + }() + } + close(start) + for range count { + w := <-results + if w.Code != http.StatusOK { + t.Errorf("concurrent attach: %d %s", w.Code, w.Body.String()) + } + } + var total, distinct int + if err := testPool.QueryRow(t.Context(), `SELECT count(*), count(DISTINCT device) + FROM volume_attachments WHERE instance_id = $1`, instanceID).Scan(&total, &distinct); err != nil { + t.Fatal(err) + } + if total != count || distinct != count { + t.Errorf("attachments=%d distinct devices=%d; want %d each", total, distinct, count) + } +} From 6378f11592402caf2879001d4d4017183540153a Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Fri, 6 Nov 2026 14:48:19 +0100 Subject: [PATCH 16/26] fix(glance): bound staging memory and fail incomplete uploads Signed-off-by: Senol Colak --- internal/glance/images.go | 90 ++++++++++++++------------ internal/glance/import.go | 28 ++++---- internal/glance/stage_limits_test.go | 73 +++++++++++++++++++++ internal/glance/upload_failure_test.go | 73 +++++++++++++++++++++ 4 files changed, 210 insertions(+), 54 deletions(-) create mode 100644 internal/glance/stage_limits_test.go create mode 100644 internal/glance/upload_failure_test.go diff --git a/internal/glance/images.go b/internal/glance/images.go index 7beb175c..382742e1 100644 --- a/internal/glance/images.go +++ b/internal/glance/images.go @@ -98,6 +98,7 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup) { // Image import r.POST("/images/:id/stage", svc.StageImageData) + r.PUT("/images/:id/stage", svc.StageImageData) r.POST("/images/:id/import", svc.ImportImage) r.GET("/images/:id/import", svc.GetImageImportInfo) @@ -982,33 +983,36 @@ func (svc *Service) UploadImageData(c *gin.Context) { return } - var status string - err = svc.pool.QueryRow(c.Request.Context(), - "SELECT status FROM images WHERE id = $1", - imageID, - ).Scan(&status) - - if errors.Is(err, pgx.ErrNoRows) { - common.SendError(c, common.NewNotFoundError("image")) - return - } - - if status != "queued" { - common.SendError(c, common.NewConflictError("image data already exists")) - return - } - // Bound the upload (review 2026-10-13 M-3): without a cap an unbounded // stream lands on disk (and, in local mode, is normalized) until the // host fills. Same default as upstream Glance's image_size_cap. c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(maxImageUploadBytes)) - // Update status to saving - if _, err := svc.pool.Exec(c.Request.Context(), - "UPDATE images SET status = $1, updated_at = $2 WHERE id = $3", - "saving", time.Now(), imageID); err != nil { - log.Warn().Err(err).Str("image_id", imageID).Msg("failed to mark image as saving") + // Claim the queued image atomically: only one request may write its bytes. + claimed, err := svc.pool.Exec(c.Request.Context(), + "UPDATE images SET status = 'saving', updated_at = $1 WHERE id = $2 AND status = 'queued'", + time.Now(), imageID) + if err != nil { + log.Error().Err(err).Str("image_id", imageID).Msg("failed to mark image as saving") + common.SendError(c, common.NewInternalServerError("failed to start image upload")) + return } + if claimed.RowsAffected() == 0 { + common.SendError(c, common.NewConflictError("image data already exists")) + return + } + active := false + defer func() { + if active { + return + } + // A disconnected client must not leave the image permanently in saving. + ctx, cancel := context.WithTimeout(context.WithoutCancel(c.Request.Context()), time.Second) + defer cancel() + if _, err := svc.pool.Exec(ctx, "UPDATE images SET status = 'queued' WHERE id = $1 AND status = 'saving'", imageID); err != nil { + log.Warn().Err(err).Str("image_id", imageID).Msg("rollback: failed to reset image upload") + } + }() // Upload to storage, hashing the uploaded bytes so the image's checksum // (MD5, Glance convention) is recorded — it lets a lazily fetched base be @@ -1016,13 +1020,6 @@ func (svc *Service) UploadImageData(c *gin.Context) { hasher := md5.New() // #nosec G401 -- Glance checksum is MD5 (compatibility), not a security control size, normalizedToRaw, err := svc.imageStore.UploadImage(c.Request.Context(), imageID, io.TeeReader(c.Request.Body, hasher)) if err != nil { - // Revert status to queued (best-effort; the upload error is the - // user-facing one) - if _, revertErr := svc.pool.Exec(c.Request.Context(), - "UPDATE images SET status = $1 WHERE id = $2", - "queued", imageID); revertErr != nil { - log.Warn().Err(revertErr).Str("image_id", imageID).Msg("rollback: failed to revert image status to queued after upload failure") - } // Over the cap is 413 — upstream's answer to image_size_cap — not a // storage failure (round 2 of this review: the MaxBytesReader error // surfaced from the storage read as a 503). @@ -1036,36 +1033,47 @@ func (svc *Service) UploadImageData(c *gin.Context) { return } + if err := svc.activateUploadedImage(c.Request.Context(), imageID, size, normalizedToRaw, hex.EncodeToString(hasher.Sum(nil))); err != nil { + log.Error().Err(err).Str("image_id", imageID).Msg("failed to finalize image upload") + common.SendError(c, common.NewServiceUnavailableError("failed to finalize image upload")) + return + } + active = true + c.Status(http.StatusNoContent) +} + +// activateUploadedImage publishes only a stored object whose checksum and state +// were recorded successfully. An unreadable normalized object must never fall +// back to the original stream's checksum: those bytes can be different. +func (svc *Service) activateUploadedImage(ctx context.Context, imageID string, size int64, normalizedToRaw bool, checksum string) error { // Mark active and set size + checksum. Normalizing backends (local/RBD real // mode) store a raw base, so disk_format becomes "raw" (SPEC-009 WS-0/FR-003); // the S3 backend stores the bytes as-is, so its create-time disk_format is left // untouched rather than mislabeled "raw" (SPEC-014). - checksum := hex.EncodeToString(hasher.Sum(nil)) if normalizedToRaw { // The recorded checksum must describe the STORED bytes: downloads serve // them, and EnsureLocalBase verifies a lazily fetched base against this // value on every boot (review 2026-10-13 N3 — hashing the upload stream // instead broke boot-from-image on any node without the local base, // because MD5(raw replica) != MD5(original qcow2)). - if stored, csErr := svc.imageStore.StoredChecksum(c.Request.Context(), imageID); csErr == nil { + if stored, csErr := svc.imageStore.StoredChecksum(ctx, imageID); csErr == nil { checksum = stored } else { - // The local object is what was just written; not being able to read - // it back is exceptional. Fall back to the stream hash (the old, - // mismatch-prone behavior) rather than failing a successful upload. - log.Warn().Err(csErr).Str("image_id", imageID). - Msg("could not hash stored image bytes; recording upload-stream checksum") + return fmt.Errorf("hash stored image: %w", csErr) } } - if _, err := svc.pool.Exec(c.Request.Context(), + result, err := svc.pool.Exec(ctx, `UPDATE images SET status = $1, size_bytes = $2, disk_format = CASE WHEN $3 THEN 'raw' ELSE disk_format END, - checksum = $4, updated_at = $5 WHERE id = $6`, - "active", size, normalizedToRaw, checksum, time.Now(), imageID); err != nil { - log.Error().Err(err).Str("image_id", imageID).Msg("failed to mark image as active after successful upload") + checksum = $4, updated_at = $5 WHERE id = $6 AND status = 'saving'`, + "active", size, normalizedToRaw, checksum, time.Now(), imageID) + if err != nil { + return fmt.Errorf("record active image: %w", err) } - - c.Status(http.StatusNoContent) + if result.RowsAffected() != 1 { + return errors.New("image is no longer in saving state") + } + return nil } // DownloadImageData downloads image data diff --git a/internal/glance/import.go b/internal/glance/import.go index a22e73d0..54d70650 100644 --- a/internal/glance/import.go +++ b/internal/glance/import.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jackc/pgx/v5" + "github.com/rs/zerolog/log" ) // StageImageData stages image data before import @@ -28,7 +29,8 @@ func (svc *Service) StageImageData(c *gin.Context) { } if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) + log.Error().Err(err).Str("image_id", imageID).Msg("image staging database error") + c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to stage image data"}) return } @@ -38,11 +40,10 @@ func (svc *Service) StageImageData(c *gin.Context) { return } - // Read staged data (in real implementation, would save to staging area). - // Bounded like the upload path (review 2026-10-13 M-3): ReadAll on a raw - // request body is an unbounded memory allocation. + // Consume staging data with bounded memory. The staging backend is currently + // a stub, so only its byte count is retained; the upload cap can be 10 GiB. c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(maxImageUploadBytes)) - stagedData, err := io.ReadAll(c.Request.Body) + stagedSize, err := io.Copy(io.Discard, c.Request.Body) if err != nil { // Over the cap is 413, upstream's answer to image_size_cap (round 2 // of this review — it used to read as a 400 read failure). @@ -56,20 +57,21 @@ func (svc *Service) StageImageData(c *gin.Context) { } // Update image status to uploading - _, err = svc.pool.Exec(c.Request.Context(), ` + result, err := svc.pool.Exec(c.Request.Context(), ` UPDATE images SET status = $1, size_bytes = $2, updated_at = $3 - WHERE id = $4 - `, "uploading", len(stagedData), time.Now(), imageID) + WHERE id = $4 AND status = 'queued' + `, "uploading", stagedSize, time.Now(), imageID) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) + log.Error().Err(err).Str("image_id", imageID).Msg("image staging database error") + c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to stage image data"}) + return + } + if result.RowsAffected() == 0 { + c.JSON(http.StatusConflict, gin.H{"message": "Image is not in queued state"}) return } - - // In stub mode, data is discarded - // In real mode, would save to staging location - _ = stagedData c.Status(http.StatusNoContent) } diff --git a/internal/glance/stage_limits_test.go b/internal/glance/stage_limits_test.go new file mode 100644 index 00000000..dbba4d5f --- /dev/null +++ b/internal/glance/stage_limits_test.go @@ -0,0 +1,73 @@ +package glance + +import ( + "context" + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/google/uuid" +) + +// Records the largest read buffer without allocating a large request body. +type stageReader struct { + remaining int64 + maxRead int +} + +func (r *stageReader) Read(p []byte) (int, error) { + r.maxRead = max(r.maxRead, len(p)) + if r.remaining == 0 { + return 0, io.EOF + } + n := int(min(int64(len(p)), r.remaining)) + clear(p[:n]) + r.remaining -= int64(n) + return n, nil +} + +func TestStageImageStreamsWithinLimit(t *testing.T) { + requireDB(t) + const size = 8 << 20 + origCap := maxImageUploadBytes + maxImageUploadBytes = size + t.Cleanup(func() { maxImageUploadBytes = origCap }) + router := newGlanceTestRouter(t) + for _, extra := range []int64{0, 1} { + id := uuid.NewString() + if _, err := testPool.Exec(t.Context(), `INSERT INTO images (id, name, project_id, status, visibility) + VALUES ($1, 'stage-limit', $2, 'queued', 'private')`, id, seededProjectID); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { testPool.Exec(context.Background(), "DELETE FROM images WHERE id=$1", id) }) + reader := &stageReader{remaining: size + extra} + req := httptest.NewRequest(http.MethodPut, "/images/"+id+"/stage", reader) + for k, v := range hdr(seededProjectID, "member") { + req.Header.Set(k, v) + } + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + wantCode, wantStatus := http.StatusNoContent, "uploading" + if extra != 0 { + wantCode, wantStatus = http.StatusRequestEntityTooLarge, "queued" + } + if w.Code != wantCode { + t.Errorf("stage: %d %s; want %d", w.Code, w.Body.String(), wantCode) + } + if reader.maxRead > 64<<10 { + t.Errorf("stage allocated a %d-byte read buffer; want bounded streaming", reader.maxRead) + } + var status string + var storedSize int64 + if err := testPool.QueryRow(t.Context(), "SELECT status, COALESCE(size_bytes, 0) FROM images WHERE id=$1", id).Scan(&status, &storedSize); err != nil { + t.Fatal(err) + } + if status != wantStatus { + t.Errorf("status=%s; want %s", status, wantStatus) + } + if extra == 0 && storedSize != size { + t.Errorf("size=%d; want %d", storedSize, size) + } + } +} diff --git a/internal/glance/upload_failure_test.go b/internal/glance/upload_failure_test.go new file mode 100644 index 00000000..d18e4c48 --- /dev/null +++ b/internal/glance/upload_failure_test.go @@ -0,0 +1,73 @@ +package glance + +import ( + "context" + "fmt" + "net/http" + "strings" + "testing" + + "github.com/cobaltcore-dev/lightstack/pkg/storage" +) + +func TestUploadFailsWhenStateCannotBeRecorded(t *testing.T) { + requireDB(t) + for _, status := range []string{"saving", "active"} { + t.Run(status, func(t *testing.T) { + id := seedPatchImage(t) + if _, err := testPool.Exec(t.Context(), "UPDATE images SET status='queued' WHERE id=$1", id); err != nil { + t.Fatal(err) + } + // A trigger injects a real write failure for only this image and state. + name := "reject_upload_" + strings.ReplaceAll(id, "-", "") + _, err := testPool.Exec(t.Context(), fmt.Sprintf(` + CREATE FUNCTION %s() RETURNS trigger LANGUAGE plpgsql AS $$ + BEGIN + IF NEW.id = '%s'::uuid AND NEW.status = '%s' THEN + RAISE EXCEPTION 'injected image write failure'; + END IF; + RETURN NEW; + END $$; + CREATE TRIGGER %s BEFORE UPDATE ON images FOR EACH ROW EXECUTE FUNCTION %s();`, name, id, status, name, name)) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + testPool.Exec(context.Background(), fmt.Sprintf("DROP TRIGGER %s ON images; DROP FUNCTION %s();", name, name)) + }) + code, body := doReqBody(newGlanceTestRouter(t), http.MethodPut, "/images/"+id+"/file", "test image", hdr(seededProjectID, "member")) + if code < 500 { + t.Errorf("failed state write returned %d %s; want 5xx", code, body) + } + if strings.Contains(body, "injected") { + t.Errorf("database details leaked: %s", body) + } + if got := imageColumn(t, id, "status"); got != "queued" { + t.Errorf("status=%s; want queued for retry", got) + } + }) + } +} + +func TestUnreadableNormalizedImageCannotBecomeActive(t *testing.T) { + requireDB(t) + id := seedPatchImage(t) + if _, err := testPool.Exec(t.Context(), "UPDATE images SET status='saving' WHERE id=$1", id); err != nil { + t.Fatal(err) + } + svc, err := NewService(testPool, "stub", "", "", storage.S3Options{}, nil) + if err != nil { + t.Fatal(err) + } + // No local object exists for this fresh UUID. StoredChecksum performs a real + // filesystem read and fails; the upload-stream hash must not be substituted. + if err := svc.activateUploadedImage(t.Context(), id, 1024, true, "original-stream-checksum"); err == nil { + t.Fatal("activated an unreadable normalized image") + } + if got := imageColumn(t, id, "status"); got != "saving" { + t.Errorf("status=%s; checksum failure must not activate the image", got) + } + if got := imageColumn(t, id, "checksum"); got != "" { + t.Errorf("recorded an unverified checksum %q", got) + } +} From 83f8b077b6dd3c48cc74dce6c15fa4ab315c8750 Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Fri, 6 Nov 2026 14:48:26 +0100 Subject: [PATCH 17/26] fix: align guest network metadata and flavor bounds with Flamingo Signed-off-by: Senol Colak --- internal/metadata/network_data_test.go | 70 ++++++++++++++++++++++++-- internal/metadata/service.go | 40 ++++++++++++--- internal/nova/flavor_bounds_test.go | 41 +++++++++++++++ internal/nova/flavors.go | 29 ++++++----- 4 files changed, 153 insertions(+), 27 deletions(-) create mode 100644 internal/nova/flavor_bounds_test.go diff --git a/internal/metadata/network_data_test.go b/internal/metadata/network_data_test.go index bb315a7d..801549b6 100644 --- a/internal/metadata/network_data_test.go +++ b/internal/metadata/network_data_test.go @@ -38,8 +38,16 @@ func TestNetworkDataJSONServesPortAddresses(t *testing.T) { } portID := uuid.NewString() + var subnetV6 string + if err := testPool.QueryRow(ctx, + `INSERT INTO subnets (name, network_id, project_id, cidr, gateway_ip, dns_nameservers) + VALUES ('md-sub-v6', $1, $2, '2001:db8::/64', '2001:db8::1', ARRAY['2001:4860:4860::8888']) RETURNING id::text`, + networkID, seededProjectID).Scan(&subnetV6); err != nil { + t.Fatal(err) + } fixed := `[{"subnet_id":"` + subnetID + `","ip_address":"192.168.42.77"},` + - `{"subnet_id":"` + subnetID + `","ip_address":"192.168.42.78"}]` + `{"subnet_id":"` + subnetID + `","ip_address":"192.168.42.78"},` + + `{"subnet_id":"` + subnetV6 + `","ip_address":"2001:db8::77"}]` if _, err := testPool.Exec(ctx, `INSERT INTO ports (id, name, network_id, project_id, device_id, device_owner, mac_address, status, fixed_ips) VALUES ($1, 'md-port', $2, $3, $4, 'compute:nova', '52:54:00:0a:0b:0c', 'ACTIVE', $5::jsonb)`, @@ -61,30 +69,78 @@ func TestNetworkDataJSONServesPortAddresses(t *testing.T) { } `json:"links"` Networks []struct { ID string `json:"id"` + Type string `json:"type"` + NetworkID string `json:"network_id"` Link string `json:"link"` IPAddress string `json:"ip_address"` Netmask string `json:"netmask"` Gateway string `json:"gateway"` DNSServers []string `json:"dns_nameservers"` + Routes []struct { + Network string `json:"network"` + Netmask string `json:"netmask"` + Gateway string `json:"gateway"` + } `json:"routes"` + Services []struct { + Type string `json:"type"` + Address string `json:"address"` + } `json:"services"` } `json:"networks"` + Services []struct { + Type string `json:"type"` + Address string `json:"address"` + } `json:"services"` } if err := json.Unmarshal([]byte(body), &doc); err != nil { t.Fatalf("network_data.json is not JSON: %v (%s)", err, body) } - // One port with two addresses: one interface, two network entries. + // One dual-stack port with three addresses: one interface, three networks. if len(doc.Links) != 1 { t.Errorf("links = %d; want 1 (one link per port, not per address): %s", len(doc.Links), body) } if len(doc.Links) == 1 && doc.Links[0].EthernetMACAddress != "52:54:00:0a:0b:0c" { t.Errorf("link mac = %q; want the port's", doc.Links[0].EthernetMACAddress) } - if len(doc.Networks) != 2 { - t.Fatalf("networks = %d; want 2 (one per address): %s", len(doc.Networks), body) + if len(doc.Networks) != 3 { + t.Fatalf("networks = %d; want 3 (one per address): %s", len(doc.Networks), body) } ips := map[string]bool{} + networkIDs := map[string]bool{} for _, n := range doc.Networks { + if networkIDs[n.ID] { + t.Errorf("duplicate network id %q", n.ID) + } + networkIDs[n.ID] = true + if len(doc.Links) == 1 && n.Link != doc.Links[0].ID { + t.Errorf("network link %q does not reference its port", n.Link) + } ips[n.IPAddress] = true + zero := "0.0.0.0" + if n.Type == "ipv6" { + zero = "::" + } + if len(n.Routes) != 1 || n.Routes[0].Network != zero || n.Routes[0].Netmask != zero || n.Routes[0].Gateway != n.Gateway { + t.Errorf("invalid default route for %s: %+v", n.IPAddress, n.Routes) + } + if len(n.Services) != 1 || n.Services[0].Type != "dns" || len(n.DNSServers) != 1 || n.Services[0].Address != n.DNSServers[0] { + t.Errorf("invalid DNS services for %s: %+v", n.IPAddress, n.Services) + } + if n.NetworkID != networkID { + t.Errorf("network_id=%q; want %q", n.NetworkID, networkID) + } + if n.IPAddress == "2001:db8::77" { + if n.Type != "ipv6" || n.Netmask != "ffff:ffff:ffff:ffff::" || n.Gateway != "2001:db8::1" { + t.Errorf("invalid IPv6 metadata: %+v", n) + } + if len(n.DNSServers) != 1 || n.DNSServers[0] != "2001:4860:4860::8888" { + t.Errorf("invalid IPv6 DNS: %v", n.DNSServers) + } + continue + } + if n.Type != "ipv4" { + t.Errorf("IPv4 type=%q", n.Type) + } if n.Netmask != "255.255.255.0" { t.Errorf("network %s: netmask = %q; want 255.255.255.0", n.IPAddress, n.Netmask) } @@ -98,6 +154,9 @@ func TestNetworkDataJSONServesPortAddresses(t *testing.T) { if !ips["192.168.42.77"] || !ips["192.168.42.78"] { t.Errorf("networks missing the port's addresses: %s", body) } + if len(doc.Services) != 2 { + t.Errorf("services must contain both DNS servers once: %+v", doc.Services) + } } // TestNetworkDataJSONToleratesNullFixedIPs pins the metadata half of the @@ -146,4 +205,7 @@ func TestNetworkDataJSONToleratesNullFixedIPs(t *testing.T) { if len(doc.Networks) != 0 { t.Errorf("networks = %d; an address-less port contributes none: %s", len(doc.Networks), body) } + if doc.Networks == nil { + t.Errorf("networks must be an empty array, not null: %s", body) + } } diff --git a/internal/metadata/service.go b/internal/metadata/service.go index 82bf679e..6ce1ce85 100644 --- a/internal/metadata/service.go +++ b/internal/metadata/service.go @@ -316,8 +316,10 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { } defer rows.Close() - var links []gin.H - var networks []gin.H + links := []gin.H{} + networks := []gin.H{} + services := []gin.H{} + seenDNS := map[string]bool{} linkID := 0 currentPort := "" @@ -349,21 +351,44 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { continue } - // Create network config + // Match Nova metadata for each address family, with a distinct network ID + // even when several addresses share one link. + addressType := "ipv4" + if addr := net.ParseIP(ip); addr != nil && addr.To4() == nil { + addressType = "ipv6" + } netConfig := gin.H{ - "id": fmt.Sprintf("network%d", linkID-1), + "id": fmt.Sprintf("network%d", len(networks)), "link": fmt.Sprintf("tap%d", linkID-1), - "type": "ipv4", + "type": addressType, + "network_id": networkID, "ip_address": ip, "netmask": cidrToNetmask(cidr), // Convert /24 → "255.255.255.0" + "routes": []gin.H{}, + "services": []gin.H{}, } if gateway != "" { netConfig["gateway"] = gateway + zero := "0.0.0.0" + if addressType == "ipv6" { + zero = "::" + } + netConfig["routes"] = []gin.H{{"network": zero, "netmask": zero, "gateway": gateway}} } if len(dnsServers) > 0 { netConfig["dns_nameservers"] = dnsServers + dnsServices := make([]gin.H, 0, len(dnsServers)) + for _, dns := range dnsServers { + entry := gin.H{"type": "dns", "address": dns} + dnsServices = append(dnsServices, entry) + if !seenDNS[dns] { + services = append(services, entry) + seenDNS[dns] = true + } + } + netConfig["services"] = dnsServices } networks = append(networks, netConfig) @@ -380,7 +405,7 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "links": links, "networks": networks, - "services": []gin.H{}, + "services": services, }) } @@ -716,6 +741,5 @@ func cidrToNetmask(cidr string) string { } return cidr } - mask := ipNet.Mask - return fmt.Sprintf("%d.%d.%d.%d", mask[0], mask[1], mask[2], mask[3]) + return net.IP(ipNet.Mask).String() } diff --git a/internal/nova/flavor_bounds_test.go b/internal/nova/flavor_bounds_test.go new file mode 100644 index 00000000..02e5022b --- /dev/null +++ b/internal/nova/flavor_bounds_test.go @@ -0,0 +1,41 @@ +package nova + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/uuid" +) + +func TestFlavorBoundsMatchFlamingo(t *testing.T) { + requireDB(t) + r := newNovaTestRouter() + // Flamingo parameter_types.flavor_param_{positive,non_negative} use + // signed 32-bit limits. A zero root disk is valid for boot-from-volume. + for _, tc := range []struct { + name, fields string + want int + }{ + {"zero-disk", `"ram":512,"vcpus":1,"disk":0`, http.StatusOK}, + {"upper-bound", `"ram":2147483647,"vcpus":2147483647,"disk":2147483647`, http.StatusOK}, + {"missing-disk", `"ram":512,"vcpus":1`, http.StatusBadRequest}, + {"zero-ram", `"ram":0,"vcpus":1,"disk":1`, http.StatusBadRequest}, + {"zero-cpu", `"ram":512,"vcpus":0,"disk":1`, http.StatusBadRequest}, + {"negative-disk", `"ram":512,"vcpus":1,"disk":-1`, http.StatusBadRequest}, + {"overflow-ram", `"ram":2147483648,"vcpus":1,"disk":1`, http.StatusBadRequest}, + {"overflow-cpu", `"ram":512,"vcpus":2147483648,"disk":1`, http.StatusBadRequest}, + {"overflow-disk", `"ram":512,"vcpus":1,"disk":2147483648`, http.StatusBadRequest}, + {"overflow-ephemeral", `"ram":512,"vcpus":1,"disk":1,"OS-FLV-EXT-DATA:ephemeral":2147483648`, http.StatusBadRequest}, + } { + t.Run(tc.name, func(t *testing.T) { + name := "bounds-" + uuid.NewString() + t.Cleanup(func() { testPool.Exec(context.Background(), "DELETE FROM flavors WHERE name=$1", name) }) + code, body := doReq(r, http.MethodPost, "/v2.1/flavors", fmt.Sprintf(`{"flavor":{"name":%q,%s}}`, name, tc.fields), operatorHdr("admin")) + if code != tc.want { + t.Errorf("create flavor: %d %s; want %d", code, body, tc.want) + } + }) + } +} diff --git a/internal/nova/flavors.go b/internal/nova/flavors.go index ad8121d4..7b76e7f8 100644 --- a/internal/nova/flavors.go +++ b/internal/nova/flavors.go @@ -1,6 +1,7 @@ package nova import ( + "math" "net/http" "github.com/cobaltcore-dev/lightstack/internal/common" @@ -23,7 +24,7 @@ func (svc *Service) CreateFlavor(c *gin.Context) { Name string `json:"name" binding:"required"` RAM int `json:"ram" binding:"required"` VCPUs int `json:"vcpus" binding:"required"` - Disk int `json:"disk" binding:"required"` + Disk *int `json:"disk" binding:"required"` Ephemeral int `json:"OS-FLV-EXT-DATA:ephemeral"` IsPublic *bool `json:"os-flavor-access:is_public"` } `json:"flavor" binding:"required"` @@ -34,24 +35,22 @@ func (svc *Service) CreateFlavor(c *gin.Context) { return } - // Validate input ranges. Upper bounds are sanity caps (review 2026-10-13 - // L-4), not policy: without them a typo (MB vs GB, or a negative sign - // flip) schedules absurd values that only surface at boot time. 1 TiB of - // RAM, 4096 vCPUs and 1 PiB of disk comfortably exceed any real flavor. - if req.Flavor.RAM < 0 || req.Flavor.RAM > 1<<20 { - common.SendError(c, common.NewBadRequestError("RAM must be between 0 and 1048576 MB")) + // Flamingo's flavor schema uses signed 32-bit values. RAM/vCPUs must be + // positive; zero root disk is valid for a server booted from a volume. + if req.Flavor.RAM < 1 || req.Flavor.RAM > math.MaxInt32 { + common.SendError(c, common.NewBadRequestError("RAM must be between 1 and 2147483647 MB")) return } - if req.Flavor.VCPUs < 0 || req.Flavor.VCPUs > 4096 { - common.SendError(c, common.NewBadRequestError("VCPUs must be between 0 and 4096")) + if req.Flavor.VCPUs < 1 || req.Flavor.VCPUs > math.MaxInt32 { + common.SendError(c, common.NewBadRequestError("VCPUs must be between 1 and 2147483647")) return } - if req.Flavor.Disk < 0 || req.Flavor.Disk > 1<<20 { - common.SendError(c, common.NewBadRequestError("Disk must be between 0 and 1048576 GB")) + if *req.Flavor.Disk < 0 || *req.Flavor.Disk > math.MaxInt32 { + common.SendError(c, common.NewBadRequestError("Disk must be between 0 and 2147483647 GB")) return } - if req.Flavor.Ephemeral < 0 || req.Flavor.Ephemeral > 1<<20 { - common.SendError(c, common.NewBadRequestError("Ephemeral disk must be between 0 and 1048576 GB")) + if req.Flavor.Ephemeral < 0 || req.Flavor.Ephemeral > math.MaxInt32 { + common.SendError(c, common.NewBadRequestError("Ephemeral disk must be between 0 and 2147483647 GB")) return } @@ -67,7 +66,7 @@ func (svc *Service) CreateFlavor(c *gin.Context) { _, err := svc.pool.Exec(ctx, `INSERT INTO flavors (id, name, vcpus, ram_mb, disk_gb, is_public) VALUES ($1, $2, $3, $4, $5, $6)`, - flavorID, req.Flavor.Name, req.Flavor.VCPUs, req.Flavor.RAM, req.Flavor.Disk, isPublic, + flavorID, req.Flavor.Name, req.Flavor.VCPUs, req.Flavor.RAM, *req.Flavor.Disk, isPublic, ) if err != nil { // Check for unique constraint violation (duplicate name) @@ -91,7 +90,7 @@ func (svc *Service) CreateFlavor(c *gin.Context) { "name": req.Flavor.Name, "vcpus": req.Flavor.VCPUs, "ram": req.Flavor.RAM, - "disk": req.Flavor.Disk, + "disk": *req.Flavor.Disk, "OS-FLV-EXT-DATA:ephemeral": req.Flavor.Ephemeral, "OS-FLV-DISABLED:disabled": false, "os-flavor-access:is_public": isPublic, From c03a598912dc7b269efb5ea4ca47433f8a957f3b Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Fri, 6 Nov 2026 14:48:26 +0100 Subject: [PATCH 18/26] docs: record PR 104 follow-up review and validation Signed-off-by: Senol Colak --- CHANGELOG.md | 14 +++++----- docs/review/pr-104-follow-up-2026-11-06.md | 30 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 docs/review/pr-104-follow-up-2026-11-06.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d94bdec..5fa49fd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,19 +13,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **Keystone management surface authorized** (review 2026-10-13): the identity API was authentication-only, so any member could reset any user's password (including the admin's) via `PATCH /users/:id`, self-grant the admin role via role assignment, dump every tenant's recoverable EC2/S3 secret via the credentials API, mint application credentials for other users, and enumerate users/projects/roles/groups/domains/catalog. User/project/role/group/domain/service/endpoint management is now admin-only; user-scoped subresources are self-or-admin; PATCH of one's own password/enabled stays admin-only so ChangePassword keeps its original-password contract. Role assignment also 404s on nonexistent project/user/role ids instead of silently recording garbage. - **Glance image upload ownership** (H-1): uploading bytes to a public image was allowed for any project that could *see* it — image poisoning of an operator-staged base every tenant boots. Upload now follows the same owner-or-admin rule as delete (403 visible-but-foreign, 404 invisible). - **Glance tasks & cache** (M-1/M-2): tasks are owner-scoped and no longer echo internal errors; cache management (prefetch/evict/clear) is admin-only. -- **Nova flavor catalog** (H-2): flavor create/delete, extra-specs and tenant-access grants are admin-only; flavor inputs get sanity bounds. +- **Nova flavor catalog** (H-2): flavor create/delete, extra-specs and tenant-access grants are admin-only; flavor limits match Flamingo's signed 32-bit range, including zero root disk for boot-from-volume. - **Cinder operator surfaces** (H-3): volume types (incl. project-access grants), QoS specs and manage/unmanage are admin-only; volume-type reads stay open for tenants. - **Neutron shared networks** (M-4): `shared=true` on create is admin-only, like `router:external`. +- **S3 credential scope and revocation**: editable credential blobs cannot override the recorded project, and S3/EC2 authentication refuses missing or revoked project roles and failed role lookups. +- **Identity self-service UUIDs**: uppercase and compact UUIDs identify the same user for authorization, including credentials and profile updates. - **Volume-transfer auth keys** (L-1) are compared in constant time. ### 🐛 Correctness -- **Glance checksums describe the stored bytes** (N-3): normalizing backends rewrite a qcow2 upload to a raw base, but the recorded checksum hashed the upload stream, so `EnsureLocalBase` failed on every node without the local base and boot-from-image errored. The checksum is now taken from the stored object. -- **Neutron IP allocation is collision-free** (N-4): allocation drew purely at random, so ports in one subnet could land on the same address; allocation now avoids every held address and the subnet gateway, and tiny subnets (≤ /29) no longer panic `crypto/rand.Int`. -- **Metadata `network_data.json` works** (N-7): the query selected a nonexistent `fixed_ip` column and answered 500 to every guest; it now resolves each port's JSONB addresses with their own subnets (one link per port). -- **Nova volume attach/detach are atomic** (N-9/N-10): single transaction with the volume row locked — no more 500s on concurrent double-attach, and no stranded in-use volume after a failed detach. +- **Glance checksums describe the stored bytes** (N-3): normalizing backends rewrite a qcow2 upload to a raw base, but the recorded checksum hashed the upload stream, so `EnsureLocalBase` failed on every node without the local base and boot-from-image errored. The checksum is now taken from the stored object; unreadable stored data or failed state writes fail the upload instead of returning success. Concurrent uploads atomically claim the queued image, and failed uploads reset it for retry. +- **Neutron IP allocation is collision-free** (N-4): allocation drew purely at random, so ports in one subnet could land on the same address; allocation and insertion share a subnet lock across port creation, server boot and interface attach. Canonical IPv6 comparisons prevent duplicate spellings, a complete fallback finds the last free address, and tiny subnets (≤ /29) no longer panic `crypto/rand.Int`. +- **Metadata `network_data.json` works** (N-7): the query selected a nonexistent `fixed_ip` column and answered 500 to every guest; it now resolves each port's JSONB addresses with their own subnets: one link per port, unique network IDs, correct IPv4/IPv6 masks, routes and DNS services, and empty arrays instead of null. +- **Nova volume attach/detach are atomic** (N-9/N-10): single transactions keep attachments and volume state consistent. Attach serializes device assignment per server and uses the existing transaction connection, avoiding pool deadlock and device collisions. - **Nova boot port lifecycle** (N-6/N-8): a failed multi-network boot releases the ports it already allocated; server delete deletes only the ports Nova created and *detaches* tenant-created ports instead of destroying them. - **Keypair name races** (L-3) map the unique-constraint violation to the same 409 as the pre-check. -- **Request bodies bounded** (M-3): glance upload/stage wrap the body in `MaxBytesReader` (10 GiB, upstream's `image_size_cap`); imported SSH public keys are capped at 64 KiB. +- **Request bodies bounded** (M-3): glance upload/stage enforce the 10 GiB cap with bounded memory; staging accepts OpenStack's PUT method; imported SSH public keys are capped at 64 KiB. --- diff --git a/docs/review/pr-104-follow-up-2026-11-06.md b/docs/review/pr-104-follow-up-2026-11-06.md new file mode 100644 index 00000000..88d70d6c --- /dev/null +++ b/docs/review/pr-104-follow-up-2026-11-06.md @@ -0,0 +1,30 @@ +# PR #104 follow-up review — 2026-11-06 + +Reviewed the authorization and correctness changes at `e026de8`, including their failure and concurrency paths. The following additional findings are fixed on the same PR. + +| Area | Reproduced failure | Fix | +|---|---|---| +| S3 credentials | Editing one's own generic credential blob changed the project returned by S3 authentication. | Authenticate against the recorded project; use the blob's tenant only for legacy rows without a project column. EC2 credential views use the same precedence. | +| S3 role revocation | A correctly signed request still returned 200 after the user's project role was removed. | Require a current project role and fail closed on role-query errors. | +| Identity self-service | Uppercase and compact UUID spellings returned 403 for the user's own profile and application credentials. | Compare parsed UUID values while preserving authorization-before-validation ordering. | +| Volume attachment | One database connection caused a timeout; concurrent attachments to different volumes collided on the device uniqueness constraint. | Lock the instance before choosing a device and use the transaction's connection for the device query. | +| Concurrent port allocation | Twelve concurrent requests through Neutron create, Nova boot allocation and Nova interface attach created eight ports holding one address. | Hold a shared subnet row lock across address selection and insertion in all three paths. | +| Dense subnets and IPv6 | A subnet with one free address falsely reported exhaustion; expanded and compressed IPv6 addresses were treated as different claims. | Complete the random search with a bounded sequential scan and canonicalize addresses. Gateway lookup errors now fail allocation. | +| Image staging | OpenStack's PUT route returned 404; an 8 MiB body caused a growing read buffer of over 2 MiB despite the upload cap. | Register PUT, stream with bounded memory, preserve 413, and prevent staging from overwriting a concurrently changed image state. | +| Image upload finalization | Injected database write failures still returned 204, sometimes leaving the image in saving. The normalized-checksum error branch substituted the original stream hash. | Atomically claim queued images, fail unsuccessful activation, require the stored checksum and reset unsuccessful uploads using a bounded cleanup context. | +| Guest network metadata | Multiple addresses on one port shared a network ID; addressless ports returned null networks; IPv6 was labeled IPv4. Routes and DNS lacked the fields consumed by cloud-init. | Unique IDs, proper IPv4/IPv6 masks, routes and DNS services, and empty arrays. | +| Flavor validation | Valid zero-disk and large flavors were rejected by arbitrary limits. | Match Flamingo's signed 32-bit bounds, positive RAM/vCPU requirements and nonnegative disk sizes. | + +The metadata shape and flavor bounds were checked against OpenStack Nova's `stable/2025.2` implementations in [netutils.py](https://github.com/openstack/nova/blob/stable/2025.2/nova/virt/netutils.py) and [parameter_types.py](https://github.com/openstack/nova/blob/stable/2025.2/nova/api/validation/parameter_types.py). + +## Validation + +- Linux, Go 1.27.3 and isolated PostgreSQL 18.3. +- Full unit suite with the race detector and database tests enabled passed. Packages changed afterward were rerun with the race detector. +- Pinned `make lint`: zero issues. Linux binary build passed. +- Horizon create-UI API probe: 85 passed, zero failed. This is an API probe, not a browser test. +- Live contract suite: 308 passed, 12 skipped, five failed. Running the original PR head in a separate database produced exactly the same counts and failing tests. These fixtures invoke a hardcoded `docker exec lightstack-postgres`; the isolated test container does not provide that command or access to the user's Docker containers. + +The five unchanged fixture failures are `TestCinderUnmanageVolume_Contract`, `TestNeutronGetAgent_Contract`, `TestNeutronAddL3AgentToRouter_Contract`, `TestTrunkBasicCRUD`, and `TestTrunkSubports`. The Cinder fixture panics when parsing the missing command's output. This local contract run is not a complete pass; the repository's CI environment remains the full contract gate. + +No real libvirt VM, host networking or Ceph RGW deployment was exercised locally. PostgreSQL-backed handler tests cover the authorization, transaction and error paths above; existing storage tests cover the real qemu-img conversion. From 100b6ded1d5efc5624b30c22353de67cb2245a4e Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Tue, 10 Nov 2026 12:29:18 +0100 Subject: [PATCH 19/26] fix: correct merge adaptations for SQLite and Helm CI Signed-off-by: Senol Colak --- .github/workflows/ci.yml | 2 +- internal/keystone/application_credentials.go | 12 ++++++++---- internal/keystone/handlers.go | 9 ++++++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a4dcd97d..a57ecbb9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -441,7 +441,7 @@ jobs: curl -fsSL -o "$RUNNER_TEMP/get-helm-3" \ https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 bash "$RUNNER_TEMP/get-helm-3" --version "$HELM_VERSION" - helm plugin install https://github.com/helm-unittest/helm-unittest --version "$HELM_UNITTEST_VERSION" --verify=false + helm plugin install https://github.com/helm-unittest/helm-unittest --version "$HELM_UNITTEST_VERSION" helm version --short diff --git a/internal/keystone/application_credentials.go b/internal/keystone/application_credentials.go index fb8d90fe..7efdbc85 100644 --- a/internal/keystone/application_credentials.go +++ b/internal/keystone/application_credentials.go @@ -21,7 +21,8 @@ func (svc *Service) ListApplicationCredentials(c *gin.Context) { // Self-or-admin (review 2026-10-13 H-4): credential metadata and role // lists are footprint information. - if denyIfNotSelfOrAdmin(c, userID) { + userID, denied := denyIfNotSelfOrAdmin(c, userID) + if denied { return } @@ -111,7 +112,8 @@ func (svc *Service) CreateApplicationCredential(c *gin.Context) { // Self-or-admin (review 2026-10-13 H-4): the 201 response hands back the // plaintext secret, and requested roles are stored verbatim — minting one // for another user would be both secret disclosure and impersonation. - if denyIfNotSelfOrAdmin(c, userID) { + userID, denied := denyIfNotSelfOrAdmin(c, userID) + if denied { return } @@ -215,7 +217,8 @@ func (svc *Service) GetApplicationCredential(c *gin.Context) { credID := c.Param("cred_id") // Self-or-admin (review 2026-10-13 H-4). - if denyIfNotSelfOrAdmin(c, userID) { + userID, denied := denyIfNotSelfOrAdmin(c, userID) + if denied { return } @@ -292,7 +295,8 @@ func (svc *Service) DeleteApplicationCredential(c *gin.Context) { // Self-or-admin (review 2026-10-13 H-4): deleting another user's // credential is an availability attack on their automation. - if denyIfNotSelfOrAdmin(c, userID) { + userID, denied := denyIfNotSelfOrAdmin(c, userID) + if denied { return } diff --git a/internal/keystone/handlers.go b/internal/keystone/handlers.go index a80928b9..1d312f46 100644 --- a/internal/keystone/handlers.go +++ b/internal/keystone/handlers.go @@ -448,7 +448,8 @@ func (svc *Service) GetUser(c *gin.Context) { // Self-or-admin (review 2026-10-13 M-5): user records (name, email, // enabled) are only for the user itself or an admin. - if denyIfNotSelfOrAdmin(c, userID) { + userID, denied := denyIfNotSelfOrAdmin(c, userID) + if denied { return } @@ -1589,7 +1590,8 @@ func (svc *Service) GetUserProjects(c *gin.Context) { // Self-or-admin (review 2026-10-13 M-5): a user's project membership is // footprint information. - if denyIfNotSelfOrAdmin(c, userID) { + userID, denied := denyIfNotSelfOrAdmin(c, userID) + if denied { return } @@ -1654,7 +1656,8 @@ func (svc *Service) GetUserGroups(c *gin.Context) { userID := c.Param("id") // Self-or-admin (review 2026-10-13 M-5). - if denyIfNotSelfOrAdmin(c, userID) { + userID, denied := denyIfNotSelfOrAdmin(c, userID) + if denied { return } From 11084a03475b16cee7ad11ceeaac6677d17c0e2f Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Tue, 10 Nov 2026 12:30:57 +0100 Subject: [PATCH 20/26] fix(nova): use database unique violation helper Signed-off-by: Senol Colak --- internal/nova/keypairs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/nova/keypairs.go b/internal/nova/keypairs.go index 165983de..0947917d 100644 --- a/internal/nova/keypairs.go +++ b/internal/nova/keypairs.go @@ -183,7 +183,7 @@ func (svc *Service) CreateKeypair(c *gin.Context) { `, userID, req.Keypair.Name, publicKey, fingerprint, now) if err != nil { - if common.IsUniqueViolation(err) { + if database.IsUniqueViolation(err) { common.SendError(c, common.NewConflictError("Keypair with this name already exists")) return } From 695021a87a5a2df6b8de33c9a604abbda515898c Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Tue, 10 Nov 2026 12:33:32 +0100 Subject: [PATCH 21/26] refactor(nova): remove obsolete IP allocator Signed-off-by: Senol Colak --- internal/nova/interface_attach.go | 50 ------------------------------- 1 file changed, 50 deletions(-) diff --git a/internal/nova/interface_attach.go b/internal/nova/interface_attach.go index 751069f3..1796272c 100644 --- a/internal/nova/interface_attach.go +++ b/internal/nova/interface_attach.go @@ -1,7 +1,6 @@ package nova import ( - "context" "encoding/json" "errors" "fmt" @@ -444,52 +443,3 @@ func generateMACAddress() string { return fmt.Sprintf("fa:16:3e:%02x:%02x:%02x", uuid.New()[0], uuid.New()[1], uuid.New()[2]) } - -// allocateNextIP allocates the next available IP from a subnet -func allocateNextIP(ctx context.Context, pool database.DB, networkID, cidr string) (string, error) { - // Simple allocation: get last used IP and increment - // In production, would use proper IPAM - var fixedIPsJSON []byte - err := pool.QueryRow(ctx, - "SELECT fixed_ips FROM ports WHERE network_id = $1 ORDER BY created_at DESC LIMIT 1", - networkID, - ).Scan(&fixedIPsJSON) - - if errors.Is(err, database.ErrNoRows) { - // First allocation, use .10 - baseIP := cidr[:len(cidr)-3] // Remove /24 - return baseIP + "10", nil - } - - if err != nil { - return "", err - } - - // For simplicity, parse JSON and increment - // In production, would use proper IPAM library - var ips []map[string]any - if err := json.Unmarshal(fixedIPsJSON, &ips); err != nil { - // Fallback to simple allocation - baseIP := cidr[:len(cidr)-3] - return baseIP + "11", nil - } - - if len(ips) == 0 { - baseIP := cidr[:len(cidr)-3] - return baseIP + "10", nil - } - - lastIP := ips[0]["ip_address"].(string) - - // Increment last octet - var a, b, c, d int - if _, err := fmt.Sscanf(lastIP, "%d.%d.%d.%d", &a, &b, &c, &d); err != nil { - return "", fmt.Errorf("malformed IP %q in subnet allocation table: %w", lastIP, err) - } - d++ - if d > 254 { - return "", fmt.Errorf("subnet exhausted") - } - - return fmt.Sprintf("%d.%d.%d.%d", a, b, c, d), nil -} From b4e3602153634aab9e3d05c68e0fc31b45d044ea Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Tue, 10 Nov 2026 13:37:21 +0100 Subject: [PATCH 22/26] fix: repair PR 104 unit test failures Signed-off-by: Senol Colak --- internal/glance/upload_failure_test.go | 6 +++++- internal/metadata/service.go | 13 +++++++++++++ internal/neutron/allocate_free_ip_test.go | 12 ++++++++---- internal/nova/volume_attachment_concurrency_test.go | 4 +++- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/internal/glance/upload_failure_test.go b/internal/glance/upload_failure_test.go index d18e4c48..f93af14f 100644 --- a/internal/glance/upload_failure_test.go +++ b/internal/glance/upload_failure_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "github.com/cobaltcore-dev/lightstack/internal/database/dbtest" "github.com/cobaltcore-dev/lightstack/pkg/storage" ) @@ -14,13 +15,16 @@ func TestUploadFailsWhenStateCannotBeRecorded(t *testing.T) { requireDB(t) for _, status := range []string{"saving", "active"} { t.Run(status, func(t *testing.T) { + dbtest.RequirePostgres(t) id := seedPatchImage(t) if _, err := testPool.Exec(t.Context(), "UPDATE images SET status='queued' WHERE id=$1", id); err != nil { t.Fatal(err) } // A trigger injects a real write failure for only this image and state. name := "reject_upload_" + strings.ReplaceAll(id, "-", "") - _, err := testPool.Exec(t.Context(), fmt.Sprintf(` + _, err := testPool.Exec(t.Context(), fmt.Sprintf( + //sqlcheck:postgres-only + ` CREATE FUNCTION %s() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.id = '%s'::uuid AND NEW.status = '%s' THEN diff --git a/internal/metadata/service.go b/internal/metadata/service.go index 9c2a01f5..f654f5fd 100644 --- a/internal/metadata/service.go +++ b/internal/metadata/service.go @@ -528,6 +528,19 @@ func decodeNetworkDataDNS(value any) ([]string, error) { return []string{}, nil case []string: return v, nil + case []any: + values := make([]string, 0, len(v)) + for i, item := range v { + switch s := item.(type) { + case string: + values = append(values, s) + case []byte: + values = append(values, string(s)) + default: + return nil, fmt.Errorf("unsupported DNS nameserver value at index %d: %T", i, item) + } + } + return values, nil case []byte: var values []string if err := json.Unmarshal(v, &values); err != nil { diff --git a/internal/neutron/allocate_free_ip_test.go b/internal/neutron/allocate_free_ip_test.go index 13e99bd4..e0ff8cea 100644 --- a/internal/neutron/allocate_free_ip_test.go +++ b/internal/neutron/allocate_free_ip_test.go @@ -235,13 +235,17 @@ func TestCreatePortOnSubnetlessNetworkWritesEmptyArray(t *testing.T) { t.Fatalf("create port on subnet-less network: code=%d body=%s; want 201", code, resp) } - var typeof string + var fixedIPsJSON []byte if err := testPool.QueryRow(ctx, - `SELECT jsonb_typeof(fixed_ips) FROM ports WHERE network_id = $1`, networkID).Scan(&typeof); err != nil { + `SELECT fixed_ips FROM ports WHERE network_id = $1`, networkID).Scan(&fixedIPsJSON); err != nil { t.Fatalf("reading the created port's fixed_ips: %v", err) } - if typeof != "array" { - t.Fatalf("fixed_ips is JSON %s; want an array so every reader can unpack it", typeof) + var fixedIPs []json.RawMessage + if err := json.Unmarshal(fixedIPsJSON, &fixedIPs); err != nil { + t.Fatalf("decoding the created port's fixed_ips %s: %v", fixedIPsJSON, err) + } + if fixedIPs == nil || len(fixedIPs) != 0 { + t.Fatalf("fixed_ips is %s; want an empty JSON array so every reader can unpack it", fixedIPsJSON) } } diff --git a/internal/nova/volume_attachment_concurrency_test.go b/internal/nova/volume_attachment_concurrency_test.go index 07284ad1..cf8fcb2d 100644 --- a/internal/nova/volume_attachment_concurrency_test.go +++ b/internal/nova/volume_attachment_concurrency_test.go @@ -46,8 +46,10 @@ func attachWithPool(ctx context.Context, pool database.DB, instanceID, volumeID func TestAttachVolumeWithOneConnection(t *testing.T) { requireDB(t) instanceID, volumeID := seedConsoleInstance(t, seededProjectID), seedAttachVolume(t) + poolConfig := *database.DefaultPoolConfig() + poolConfig.MaxConns = 1 pool, err := database.Open(t.Context(), dbtest.URL(), database.Options{ - Pool: database.PoolConfig{MaxConns: 1}, + Pool: poolConfig, }) if err != nil { t.Fatal(err) From 760acb1daa861c5cb99efc7ef4cfc5954fc8a056 Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Tue, 10 Nov 2026 17:00:03 +0100 Subject: [PATCH 23/26] fix: address PR 104 review feedback Signed-off-by: Senol Colak --- .github/workflows/ci.yml | 35 ++--- .github/workflows/security.yml | 2 +- CHANGELOG.md | 35 ++--- build/package/Dockerfile | 2 +- deployments/docker-compose.test.yml | 2 +- docs/MULTIARCH.md | 2 +- docs/security/SBOM_TRIAGE.md | 3 +- internal/cinder/manage.go | 20 ++- internal/cinder/manage_authz_test.go | 2 +- .../cinder/operator_authz_regression_test.go | 95 ++++++++++++++ internal/cinder/qos_specs.go | 15 +-- internal/cinder/quota_enforcement_test.go | 8 +- internal/cinder/quotas.go | 6 +- internal/cinder/transfers.go | 2 +- internal/cinder/volume_type_access.go | 5 +- internal/cinder/volume_types.go | 30 ++--- internal/cinder/volumes.go | 6 +- internal/common/authz.go | 22 ++++ internal/glance/cache.go | 23 ++-- internal/glance/images.go | 23 ++-- internal/glance/images_db_test.go | 25 +++- internal/glance/import.go | 2 +- internal/glance/stage_limits_test.go | 7 +- internal/glance/tasks.go | 6 +- internal/glance/tasks_authz_test.go | 80 +++++++++++ internal/glance/upload_authz_test.go | 20 ++- internal/keystone/application_credentials.go | 18 ++- internal/keystone/authz.go | 10 -- internal/keystone/credentials.go | 6 +- internal/keystone/domains.go | 24 ++-- internal/keystone/groups.go | 32 ++--- internal/keystone/handlers.go | 124 +++++++++--------- .../keystone/identity_authz_gates_test.go | 3 +- internal/keystone/identity_notfound_test.go | 2 +- .../keystone/review_authz_regressions_test.go | 124 ++++++++++++++++++ internal/keystone/routes_wiring_test.go | 4 +- internal/keystone/services.go | 32 ++--- internal/metadata/network_data_test.go | 2 +- internal/neutron/allocate_free_ip_test.go | 43 +++++- internal/neutron/network.go | 4 +- internal/neutron/ports.go | 112 +++++++--------- internal/neutron/shared_gate_test.go | 2 +- internal/nova/aggregates.go | 12 +- internal/nova/authz.go | 29 ---- internal/nova/boot_failure_test.go | 4 +- internal/nova/console.go | 2 +- internal/nova/flavors.go | 28 ++-- internal/nova/handlers.go | 14 +- internal/nova/interface_attach.go | 58 ++++++-- internal/nova/interface_attach_portid_test.go | 56 +++++++- internal/nova/keypairs.go | 6 +- internal/nova/migrations.go | 2 +- internal/nova/port_lifecycle_test.go | 6 +- internal/nova/review_regressions_test.go | 90 +++++++++++++ internal/nova/tenant_usage.go | 2 +- internal/nova/volume_attachment.go | 18 +-- .../nova/volume_attachment_refusal_test.go | 71 ++++++++++ pkg/storage/image_store.go | 2 +- pkg/storage/image_store_test.go | 4 +- scripts/deploy-single-node.sh | 2 +- 60 files changed, 986 insertions(+), 440 deletions(-) create mode 100644 internal/cinder/operator_authz_regression_test.go create mode 100644 internal/common/authz.go create mode 100644 internal/glance/tasks_authz_test.go create mode 100644 internal/keystone/review_authz_regressions_test.go create mode 100644 internal/nova/review_regressions_test.go create mode 100644 internal/nova/volume_attachment_refusal_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a57ecbb9..78e9e3dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v6 with: - go-version: '1.27.3' + go-version: 'stable' - name: Cache Go modules uses: actions/cache@v5 @@ -83,7 +83,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v6 with: - go-version: '1.27.3' + go-version: 'stable' - name: Cache Go modules uses: actions/cache@v5 @@ -93,6 +93,9 @@ jobs: restore-keys: | ${{ runner.os }}-go- + - name: Install qemu-img for image normalization tests + run: sudo apt-get update && sudo apt-get install -y --no-install-recommends qemu-utils + - name: Run unit tests with race detector run: go test -race -coverprofile=coverage.txt -covermode=atomic $(go list ./... | grep -v '/test/contract') env: @@ -125,7 +128,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v6 with: - go-version: '1.27.3' + go-version: 'stable' - name: Cache Go modules uses: actions/cache@v5 @@ -246,7 +249,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v6 with: - go-version: '1.27.3' + go-version: 'stable' - name: Cache Go modules uses: actions/cache@v5 @@ -424,9 +427,6 @@ jobs: name: Helm Charts runs-on: ubuntu-latest timeout-minutes: 10 - env: - HELM_VERSION: v3.22.1 - HELM_UNITTEST_VERSION: v1.2.1 steps: - name: Checkout code @@ -434,15 +434,20 @@ jobs: # Same mechanism as `make install-tools`: the official get-helm-3 script plus the # helm-unittest plugin, which `make chart-test` hard-requires (Makefile:103-104). - - name: Install pinned Helm and helm-unittest + - name: Install Helm and the helm-unittest plugin run: | - export HELM_PLUGINS="$RUNNER_TEMP/helm-plugins" - echo "HELM_PLUGINS=$HELM_PLUGINS" >> "$GITHUB_ENV" - curl -fsSL -o "$RUNNER_TEMP/get-helm-3" \ - https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 - bash "$RUNNER_TEMP/get-helm-3" --version "$HELM_VERSION" - helm plugin install https://github.com/helm-unittest/helm-unittest --version "$HELM_UNITTEST_VERSION" - + if ! command -v helm >/dev/null 2>&1; then + # Downloaded, not piped: the default run shell is `bash -e` without pipefail, + # so `curl ... | bash` would swallow a failed download. + curl -fsSL -o "$RUNNER_TEMP/get-helm-3" \ + https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 + bash "$RUNNER_TEMP/get-helm-3" + fi + if ! helm plugin list 2>/dev/null | grep -q '^unittest'; then + # helm-unittest ships no provenance file; recent Helm refuses an + # unverified plugin unless verification is explicitly skipped. + helm plugin install https://github.com/helm-unittest/helm-unittest --verify=false + fi helm version --short # Lints + renders + unit-tests both product charts: charts/lightstack, then diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index 5bafc671..c0f48d3c 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -24,7 +24,7 @@ jobs: - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: '1.27.3' + go-version: 'stable' cache: true - name: Scan Go code and pinned development tool diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fa49fd5..f61084ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,24 +10,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### 🔒 Security — identity & service authorization -- **Keystone management surface authorized** (review 2026-10-13): the identity API was authentication-only, so any member could reset any user's password (including the admin's) via `PATCH /users/:id`, self-grant the admin role via role assignment, dump every tenant's recoverable EC2/S3 secret via the credentials API, mint application credentials for other users, and enumerate users/projects/roles/groups/domains/catalog. User/project/role/group/domain/service/endpoint management is now admin-only; user-scoped subresources are self-or-admin; PATCH of one's own password/enabled stays admin-only so ChangePassword keeps its original-password contract. Role assignment also 404s on nonexistent project/user/role ids instead of silently recording garbage. -- **Glance image upload ownership** (H-1): uploading bytes to a public image was allowed for any project that could *see* it — image poisoning of an operator-staged base every tenant boots. Upload now follows the same owner-or-admin rule as delete (403 visible-but-foreign, 404 invisible). -- **Glance tasks & cache** (M-1/M-2): tasks are owner-scoped and no longer echo internal errors; cache management (prefetch/evict/clear) is admin-only. -- **Nova flavor catalog** (H-2): flavor create/delete, extra-specs and tenant-access grants are admin-only; flavor limits match Flamingo's signed 32-bit range, including zero root disk for boot-from-volume. -- **Cinder operator surfaces** (H-3): volume types (incl. project-access grants), QoS specs and manage/unmanage are admin-only; volume-type reads stay open for tenants. -- **Neutron shared networks** (M-4): `shared=true` on create is admin-only, like `router:external`. +- **Keystone management surface authorized**: the identity API was authentication-only, so any member could reset any user's password (including the admin's) via `PATCH /users/:id`, self-grant the admin role via role assignment, dump every tenant's recoverable EC2/S3 secret via the credentials API, mint application credentials for other users, and enumerate users/projects/roles/groups/domains/catalog. User/project/role/group/domain/service/endpoint management is now admin-only; user-scoped subresources are self-or-admin; PATCH of one's own password/enabled stays admin-only so ChangePassword keeps its original-password contract. Role assignment also 404s on nonexistent project/user/role ids instead of silently recording garbage. +- **Glance image upload ownership**: uploading bytes to a public image was allowed for any project that could *see* it — image poisoning of an operator-staged base every tenant boots. Upload now follows the same owner-or-admin rule as delete (403 visible-but-foreign, 404 invisible). +- **Glance tasks & cache**: tasks are owner-scoped and no longer echo internal errors; cache management (prefetch/evict/clear) is admin-only. +- **Nova flavor catalog**: flavor create/delete, extra-specs and tenant-access grants are admin-only; flavor limits match Flamingo's signed 32-bit range, including zero root disk for boot-from-volume. +- **Nova deployment views**: `os-hypervisors` (index/detail/statistics), `os-services` and `os-console-auth-tokens` require the admin role because they expose deployment-wide state. +- **Cinder operator surfaces**: volume types (incl. project-access grants), QoS specs and manage/unmanage are admin-only; volume-type reads stay open for tenants. +- **Neutron shared networks**: `shared=true` on create is admin-only, like `router:external`. - **S3 credential scope and revocation**: editable credential blobs cannot override the recorded project, and S3/EC2 authentication refuses missing or revoked project roles and failed role lookups. - **Identity self-service UUIDs**: uppercase and compact UUIDs identify the same user for authorization, including credentials and profile updates. -- **Volume-transfer auth keys** (L-1) are compared in constant time. +- **Volume-transfer auth keys** are compared in constant time. ### 🐛 Correctness -- **Glance checksums describe the stored bytes** (N-3): normalizing backends rewrite a qcow2 upload to a raw base, but the recorded checksum hashed the upload stream, so `EnsureLocalBase` failed on every node without the local base and boot-from-image errored. The checksum is now taken from the stored object; unreadable stored data or failed state writes fail the upload instead of returning success. Concurrent uploads atomically claim the queued image, and failed uploads reset it for retry. -- **Neutron IP allocation is collision-free** (N-4): allocation drew purely at random, so ports in one subnet could land on the same address; allocation and insertion share a subnet lock across port creation, server boot and interface attach. Canonical IPv6 comparisons prevent duplicate spellings, a complete fallback finds the last free address, and tiny subnets (≤ /29) no longer panic `crypto/rand.Int`. -- **Metadata `network_data.json` works** (N-7): the query selected a nonexistent `fixed_ip` column and answered 500 to every guest; it now resolves each port's JSONB addresses with their own subnets: one link per port, unique network IDs, correct IPv4/IPv6 masks, routes and DNS services, and empty arrays instead of null. -- **Nova volume attach/detach are atomic** (N-9/N-10): single transactions keep attachments and volume state consistent. Attach serializes device assignment per server and uses the existing transaction connection, avoiding pool deadlock and device collisions. -- **Nova boot port lifecycle** (N-6/N-8): a failed multi-network boot releases the ports it already allocated; server delete deletes only the ports Nova created and *detaches* tenant-created ports instead of destroying them. -- **Keypair name races** (L-3) map the unique-constraint violation to the same 409 as the pre-check. -- **Request bodies bounded** (M-3): glance upload/stage enforce the 10 GiB cap with bounded memory; staging accepts OpenStack's PUT method; imported SSH public keys are capped at 64 KiB. +- **Glance checksums describe the stored bytes**: normalizing backends rewrite a qcow2 upload to a raw base, but the recorded checksum hashed the upload stream, so `EnsureLocalBase` failed on every node without the local base and boot-from-image errored. The checksum is now taken from the stored object; unreadable stored data or failed state writes fail the upload instead of returning success. Concurrent uploads atomically claim the queued image, and failed uploads reset it for retry. +- **Neutron IP allocation is collision-free**: allocation drew purely at random, so ports in one subnet could land on the same address; allocation and insertion share a subnet lock across port creation, server boot and interface attach. Canonical IPv6 comparisons prevent duplicate spellings, a complete fallback finds the last free address, and tiny subnets (≤ /29) no longer panic `crypto/rand.Int`. +- **Metadata `network_data.json` works**: the query selected a nonexistent `fixed_ip` column and answered 500 to every guest; it now resolves each port's JSONB addresses with their own subnets: one link per port, unique network IDs, correct IPv4/IPv6 masks, routes and DNS services, and empty arrays instead of null. +- **Nova volume attach/detach are atomic**: single transactions keep attachments and volume state consistent. Attach serializes device assignment per server and uses the existing transaction connection, avoiding pool deadlock and device collisions. +- **Nova boot port lifecycle**: a failed multi-network boot releases the ports it already allocated; server delete deletes only the ports Nova created and *detaches* tenant-created ports instead of destroying them. +- **Keypair name races** map the unique-constraint violation to the same 409 as the pre-check. +- **Request bodies bounded**: glance upload/stage enforce the 10 GiB cap with bounded memory; staging accepts OpenStack's PUT method; imported SSH public keys are capped at 64 KiB. --- @@ -83,11 +84,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### 📊 Observability - **Zerolog migration**: Replace remaining `log.Printf` calls with structured `zerolog` logging (M-1) -- **Scan error logging**: Add warnings for previously silent `rows.Scan` failures across all list handlers (M-4) +- **Scan error logging**: Add warnings for previously silent `rows.Scan` failures across all list handlers - **Swallowed error propagation**: Log or propagate 15 instances of silently discarded errors (M-6) ### 🛠️ Utilities & Fixes -- **Shared pagination helper**: `common.ParsePagination()` extracts duplicated limit/offset/marker parsing (M-3) +- **Shared pagination helper**: `common.ParsePagination()` extracts duplicated limit/offset/marker parsing - **Version consistency**: Fix Placement (1.39→1.40) and Nova (2.79→2.90) version mismatches with constants (H-10, L-4) - **Quota queries**: `GetLimits` reads project quotas from database instead of hardcoded values (H-12) - **Flavor pagination**: Replace UUID-based cursor with `created_at` for deterministic ordering (M-10) @@ -109,7 +110,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **Updated INDEX.md**: Add Testing & Quality section with links to all moved files ### Deferred to v0.7.0 -- Database dependency injection (H-3) — 665 call sites, needs dedicated design +- Database dependency injection — 665 call sites, needs dedicated design - Rate limiting (H-6) — needs design spec for per-project limits - Re-enable CI linting (H-4) — needs lint error cleanup first diff --git a/build/package/Dockerfile b/build/package/Dockerfile index baabed86..24d02835 100644 --- a/build/package/Dockerfile +++ b/build/package/Dockerfile @@ -1,5 +1,5 @@ # Multi-stage build for Lightstack - Multi-architecture support -FROM --platform=$BUILDPLATFORM golang:1.27.3-alpine3.24 AS builder +FROM --platform=$BUILDPLATFORM golang:1-alpine3.24 AS builder # Build arguments for cross-compilation ARG TARGETOS diff --git a/deployments/docker-compose.test.yml b/deployments/docker-compose.test.yml index 85aa9795..a8bc3f55 100644 --- a/deployments/docker-compose.test.yml +++ b/deployments/docker-compose.test.yml @@ -2,7 +2,7 @@ version: '3.8' services: test-runner: - image: golang:1.27.3 + image: golang:1 container_name: lightstack-test-runner working_dir: /workspace volumes: diff --git a/docs/MULTIARCH.md b/docs/MULTIARCH.md index c3c53605..a39079a7 100644 --- a/docs/MULTIARCH.md +++ b/docs/MULTIARCH.md @@ -76,7 +76,7 @@ The Dockerfile uses Docker's built-in multi-architecture support: ```dockerfile # Builder stage - runs on native architecture -FROM --platform=$BUILDPLATFORM golang:1.27.3-alpine3.24 AS builder +FROM --platform=$BUILDPLATFORM golang:1-alpine3.24 AS builder # Build arguments automatically set by Docker ARG TARGETOS # Target OS (linux) diff --git a/docs/security/SBOM_TRIAGE.md b/docs/security/SBOM_TRIAGE.md index 1641063c..2079e9b1 100644 --- a/docs/security/SBOM_TRIAGE.md +++ b/docs/security/SBOM_TRIAGE.md @@ -46,8 +46,7 @@ The generated runtime SPDX artifact replaces this export as the release inventor | Go 1.26.2 standard-library advisories | Toolchain affects every binary | Pin Go 1.26.8 in modules, CI, builder image, and test container | | GPL and other linter-only dependencies in the root module | Development-only, not shipped | Move the pinned linter graph to `tools/go.mod` | | Vulnerable transitive modules in the pinned linter graph | Development-only but executed in CI | Upgrade the affected modules and scan the linter command with `govulncheck` | -| GO-2026-6603, -6605, -6607, -6608, -6610, -6611, -6612, -6613, -6617 in the Go 1.26.8 standard library, published 2026-10-08 | Reachable; toolchain affects every binary | Upgrade the toolchain in modules, CI, builder image, test container, and the single-node deploy script; current pin is Go 1.27.3 | -| GO-2026-6809 in `crypto/x509`, detected by CI on 2026-10-30 | Reachable through the HTTP server; Go 1.27.2 trusts compromised intermediate certificates | Pin Go 1.27.3 throughout the build and test toolchain; keep vulnerability scanning enabled | +| GO-2026-6603, -6605, -6607, -6608, -6610, -6611, -6612, -6613, -6617 in the Go 1.26.8 standard library, published 2026-10-08 | Reachable; toolchain affects every binary | Pin Go 1.27.2, the current release, in modules, CI, builder image, test container, and the single-node deploy script | | GO-2026-6603, -6610, -6611, -6612, -6617 in `x/net`, published 2026-10-08 | Reachable through gin's HTTP/2 support | Upgrade transitively from 0.57.0 to 0.60.0; `x/crypto`, `x/sys`, `x/sync`, and `x/text` move to its minimums | | GO-2026-6809 (`crypto/x509` intermediate-certificate distrust) in the Go 1.27.2 standard library, published after the 1.27.2 pin; reachable via `http.Server.Serve` → `x509.Certificate.Verify` | Reachable; toolchain affects every binary | Raise the go.mod floor to 1.27.3; CI (setup-go `go-version: stable`) and the builder and test images (`golang:1`) track the latest stable release, so later standard-library patches apply without a code change. The checksum-pinned single-node deploy script is the deliberate exception. | diff --git a/internal/cinder/manage.go b/internal/cinder/manage.go index 97aa8e0f..506119ab 100644 --- a/internal/cinder/manage.go +++ b/internal/cinder/manage.go @@ -15,13 +15,12 @@ import ( // ManageVolume handles POST /v3/os-volume-manage func (svc *Service) ManageVolume(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:ManageVolume") { return } @@ -84,13 +83,12 @@ func (svc *Service) ManageVolume(c *gin.Context) { // ListManageableVolumes handles GET /v3/manageable_volumes func (svc *Service) ListManageableVolumes(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:ListManageableVolumes") { return } @@ -110,13 +108,12 @@ func (svc *Service) ListManageableVolumes(c *gin.Context) { // UnmanageVolume handles volume action: os-unmanage func (svc *Service) UnmanageVolume(c *gin.Context, volumeID string) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:UnmanageVolume") { return } @@ -166,12 +163,11 @@ func (svc *Service) UnmanageVolume(c *gin.Context, volumeID string) { // ManageSnapshot handles POST /v3/os-snapshot-manage func (svc *Service) ManageSnapshot(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): like os-volume-manage, this imports + // Admin-only: like os-volume-manage, this imports // an operator's existing backing into the API — upstream gates it // volume_admin_actions:snapshot_manage. Checked before the body is bound // so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:ManageSnapshot") { return } diff --git a/internal/cinder/manage_authz_test.go b/internal/cinder/manage_authz_test.go index 879c84e8..4a7c91d1 100644 --- a/internal/cinder/manage_authz_test.go +++ b/internal/cinder/manage_authz_test.go @@ -9,7 +9,7 @@ import ( "github.com/google/uuid" ) -// TestOperatorSurfacesAdminOnly pins review 2026-10-13 H-3: volume types, QoS +// TestOperatorSurfacesAdminOnly pins the admin gates on volume types, QoS // specs and the manage/unmanage surface shape storage for every tenant (and // manage addresses host-level backends). They were authentication-only — a // plain member could create volume types every tenant then booted from, add diff --git a/internal/cinder/operator_authz_regression_test.go b/internal/cinder/operator_authz_regression_test.go new file mode 100644 index 00000000..7fa8031b --- /dev/null +++ b/internal/cinder/operator_authz_regression_test.go @@ -0,0 +1,95 @@ +package cinder + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/uuid" +) + +func TestOperatorUpdateDeleteAndUnmanageRoutesRequireAdmin(t *testing.T) { + requireDB(t) + r := newCinderTestRouter() + member := map[string]string{"X-Test-Project": seededProjectID, "X-Test-User": seededUserID, "X-Test-Roles": "member"} + admin := map[string]string{"X-Test-Project": seededProjectID, "X-Test-User": seededUserID, "X-Test-Roles": "admin"} + + typeID, qosID := uuid.NewString(), uuid.NewString() + volumeID, unmanageVolumeID, resetVolumeID, snapshotID := uuid.NewString(), uuid.NewString(), uuid.NewString(), uuid.NewString() + if _, err := testPool.Exec(t.Context(), "INSERT INTO volume_types (id, name, description, is_public) VALUES ($1, $2, '', false)", typeID, "review-type-"+typeID[:8]); err != nil { + t.Fatalf("seed volume type: %v", err) + } + if _, err := testPool.Exec(t.Context(), "INSERT INTO qos_specs (id, name, consumer, specs, project_id) VALUES ($1, $2, 'back-end', '{}', $3)", qosID, "review-qos-"+qosID[:8], seededProjectID); err != nil { + t.Fatalf("seed QoS spec: %v", err) + } + for _, volume := range []struct{ id, name string }{ + {volumeID, "review-managed-snapshot-" + volumeID[:8]}, + {unmanageVolumeID, "review-unmanage-" + unmanageVolumeID[:8]}, + {resetVolumeID, "review-reset-" + resetVolumeID[:8]}, + } { + if _, err := testPool.Exec(t.Context(), "INSERT INTO volumes (id, name, project_id, user_id, size_gb, status) VALUES ($1, $2, $3, $4, 1, 'available')", volume.id, volume.name, seededProjectID, seededUserID); err != nil { + t.Fatalf("seed volume: %v", err) + } + } + if _, err := testPool.Exec(t.Context(), "INSERT INTO snapshots (id, name, volume_id, project_id, size_gb, status) VALUES ($1, 'review-snapshot-reset', $2, $3, 1, 'creating')", snapshotID, resetVolumeID, seededProjectID); err != nil { + t.Fatalf("seed snapshot: %v", err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), "DELETE FROM snapshots WHERE id = $1 OR volume_id = $2", snapshotID, volumeID) + _, _ = testPool.Exec(context.Background(), "DELETE FROM volumes WHERE id IN ($1, $2, $3)", volumeID, unmanageVolumeID, resetVolumeID) + _, _ = testPool.Exec(context.Background(), "DELETE FROM qos_specs WHERE id = $1", qosID) + _, _ = testPool.Exec(context.Background(), "DELETE FROM volume_types WHERE id = $1", typeID) + }) + + manageSnapshotBody := fmt.Sprintf("{\"snapshot\":{\"volume_id\":%q,\"ref\":{\"source-name\":\"source\"},\"name\":\"review-import-%s\"}}", volumeID, volumeID[:8]) + memberCases := []struct{ name, method, path, body string }{ + {"type update", http.MethodPut, "/v3/types/" + typeID, "{}"}, + {"type delete", http.MethodDelete, "/v3/types/" + typeID, ""}, + {"extra specs create", http.MethodPost, "/v3/types/" + typeID + "/extra_specs", "{}"}, + {"extra spec update", http.MethodPut, "/v3/types/" + typeID + "/extra_specs/review", "{}"}, + {"extra spec delete", http.MethodDelete, "/v3/types/" + typeID + "/extra_specs/review", ""}, + {"type access grant", http.MethodPost, "/v3/types/" + typeID + "/action", "{}"}, + {"QoS create", http.MethodPost, "/v3/qos-specs", "{}"}, + {"QoS update", http.MethodPut, "/v3/qos-specs/" + qosID, "{}"}, + {"QoS delete", http.MethodDelete, "/v3/qos-specs/" + qosID, ""}, + {"manage volume", http.MethodPost, "/v3/os-volume-manage", "{}"}, + {"list manageable volumes", http.MethodGet, "/v3/manageable_volumes?host=h1", ""}, + {"manage snapshot", http.MethodPost, "/v3/os-snapshot-manage", manageSnapshotBody}, + {"unmanage volume", http.MethodPost, "/v3/volumes/" + unmanageVolumeID + "/action", `{"os-unmanage":{}}`}, + {"reset volume status", http.MethodPost, "/v3/volumes/" + resetVolumeID + "/action", `{"os-reset_status":{"status":"available"}}`}, + {"reset snapshot status", http.MethodPost, "/v3/snapshots/" + snapshotID + "/action", `{"os-reset_status":{"status":"available"}}`}, + } + for _, tc := range memberCases { + t.Run(tc.name, func(t *testing.T) { + if code, body := doReq(r, tc.method, tc.path, tc.body, member); code != http.StatusForbidden { + t.Errorf("member %s %s: code=%d body=%s; want 403", tc.method, tc.path, code, body) + } + }) + } + + checkAdmin := func(method, path, body string, want int) { + t.Helper() + if code, response := doReq(r, method, path, body, admin); code != want { + t.Errorf("admin %s %s: code=%d body=%s; want %d", method, path, code, response, want) + } + } + checkAdmin(http.MethodPut, "/v3/types/"+typeID, "{\"volume_type\":{\"description\":\"admin\"}}", http.StatusOK) + checkAdmin(http.MethodPost, "/v3/types/"+typeID+"/extra_specs", "{\"extra_specs\":{\"review\":\"admin\"}}", http.StatusOK) + checkAdmin(http.MethodPut, "/v3/types/"+typeID+"/extra_specs/review", "{\"review\":\"updated\"}", http.StatusOK) + checkAdmin(http.MethodDelete, "/v3/types/"+typeID+"/extra_specs/review", "", http.StatusNoContent) + checkAdmin(http.MethodPost, "/v3/types/"+typeID+"/action", fmt.Sprintf("{\"addProjectAccess\":{\"project\":%q}}", seededProjectID), http.StatusAccepted) + checkAdmin(http.MethodPost, "/v3/types/"+typeID+"/action", fmt.Sprintf("{\"removeProjectAccess\":{\"project\":%q}}", seededProjectID), http.StatusAccepted) + checkAdmin(http.MethodPut, "/v3/qos-specs/"+qosID, "{\"qos_specs\":{\"iops\":\"10\"}}", http.StatusOK) + checkAdmin(http.MethodDelete, "/v3/qos-specs/"+qosID, "", http.StatusAccepted) + checkAdmin(http.MethodGet, "/v3/manageable_volumes?host=h1", "", http.StatusOK) + checkAdmin(http.MethodPost, "/v3/os-snapshot-manage", manageSnapshotBody, http.StatusAccepted) + checkAdmin(http.MethodPost, "/v3/volumes/"+unmanageVolumeID+"/action", "{\"os-unmanage\":{}}", http.StatusAccepted) + checkAdmin(http.MethodPost, "/v3/volumes/"+resetVolumeID+"/action", "{\"os-reset_status\":{\"status\":\"available\"}}", http.StatusAccepted) + checkAdmin(http.MethodPost, "/v3/snapshots/"+snapshotID+"/action", "{\"os-reset_status\":{\"status\":\"available\"}}", http.StatusAccepted) + checkAdmin(http.MethodDelete, "/v3/types/"+typeID, "", http.StatusAccepted) + checkAdmin(http.MethodPost, "/v3/os-volume-manage", "{\"volume\":{\"host\":\"h1\",\"ref\":{\"source-name\":\"x\"},\"name\":\"review-managed\"}}", http.StatusAccepted) + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), "DELETE FROM volumes WHERE name = 'review-managed'") + }) +} diff --git a/internal/cinder/qos_specs.go b/internal/cinder/qos_specs.go index f3d9bb71..76ede9e9 100644 --- a/internal/cinder/qos_specs.go +++ b/internal/cinder/qos_specs.go @@ -66,13 +66,12 @@ func (svc *Service) ListQoSSpecs(c *gin.Context) { // CreateQoSSpec creates a new QoS specification func (svc *Service) CreateQoSSpec(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:CreateQoSSpec") { return } @@ -162,13 +161,12 @@ func (svc *Service) GetQoSSpec(c *gin.Context) { // UpdateQoSSpec updates a QoS specification func (svc *Service) UpdateQoSSpec(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:UpdateQoSSpec") { return } @@ -253,13 +251,12 @@ func (svc *Service) UpdateQoSSpec(c *gin.Context) { // DeleteQoSSpec deletes a QoS specification func (svc *Service) DeleteQoSSpec(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:DeleteQoSSpec") { return } diff --git a/internal/cinder/quota_enforcement_test.go b/internal/cinder/quota_enforcement_test.go index 65dbb178..38cdbc4d 100644 --- a/internal/cinder/quota_enforcement_test.go +++ b/internal/cinder/quota_enforcement_test.go @@ -529,8 +529,8 @@ func TestQuotaConcurrentCreates(t *testing.T) { // assertion is the one that matters, since it holds even if the HTTP codes race. // concurrentQuotaReqs fires n identical requests at once and reports how many were -// accepted. admin marks the caller's role: os-volume-manage is admin-only (review -// 2026-10-13 H-3) while still consuming the target project's quota, so its test +// accepted. admin marks the caller's role: os-volume-manage requires an admin +// while still consuming the target project's quota, so its test // drives the route as an administrator. func concurrentQuotaReqs(r *gin.Engine, n int, method, path, body string) []int { return concurrentQuotaReqsAs(r, n, method, path, body, "member") @@ -661,8 +661,8 @@ func TestQuotaConcurrentExtends(t *testing.T) { // and with it the old checkVolumeQuota: that helper ran the ceiling read, the count check // and the gigabytes check as three separate round trips under two separate lock // acquisitions, so its own three parts were not atomic with each other either. -// The caller is an administrator because os-volume-manage is admin-only (review -// 2026-10-13 H-3) — upstream's volume_admin_actions:manage — while the import still +// The caller is an administrator because os-volume-manage requires the +// upstream volume_admin_actions:manage policy while the import still // consumes the target project's quota. func TestQuotaConcurrentManagedVolumes(t *testing.T) { requireDB(t) diff --git a/internal/cinder/quotas.go b/internal/cinder/quotas.go index 3dc5b7a2..c4e99a4a 100644 --- a/internal/cinder/quotas.go +++ b/internal/cinder/quotas.go @@ -178,8 +178,7 @@ func (svc *Service) GetQuotaSet(c *gin.Context) { func (svc *Service) UpdateQuotaSet(c *gin.Context) { // Admin-only: quota limits are an operator concern. Checked before the body is // bound so a non-admin gets 403, not a request-validation 400 (SPEC-020 FR-020). - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow quota updates to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:UpdateQuotaSet") { return } @@ -255,8 +254,7 @@ func (svc *Service) UpdateQuotaSet(c *gin.Context) { // DeleteQuotaSet resets quotas to defaults for a project func (svc *Service) DeleteQuotaSet(c *gin.Context) { // Admin-only, matching UpdateQuotaSet and upstream Cinder (SPEC-020 FR-020). - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow quota updates to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:DeleteQuotaSet") { return } diff --git a/internal/cinder/transfers.go b/internal/cinder/transfers.go index e396834f..ad246749 100644 --- a/internal/cinder/transfers.go +++ b/internal/cinder/transfers.go @@ -318,7 +318,7 @@ func (svc *Service) AcceptVolumeTransfer(c *gin.Context) { return } - // Verify auth key. Constant-time (review 2026-10-13 L-1): a plain string + // Verify auth key. Constant-time: a plain string // compare short-circuits on the first differing byte, and a transfer id is // cheap to enumerate, so the key could be recovered byte-by-byte from // response timing. diff --git a/internal/cinder/volume_type_access.go b/internal/cinder/volume_type_access.go index 100e1ae4..eeb952fb 100644 --- a/internal/cinder/volume_type_access.go +++ b/internal/cinder/volume_type_access.go @@ -14,12 +14,11 @@ import ( func (svc *Service) VolumeTypeAction(c *gin.Context) { typeID := c.Param("id") - // Admin-only (review 2026-10-13 H-3): project access to a private volume + // Admin-only: project access to a private volume // type is a cross-tenant grant — without the check any member could add // their project to (or strip another project from) any private type. // Checked before the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:VolumeTypeAction") { return } diff --git a/internal/cinder/volume_types.go b/internal/cinder/volume_types.go index fb8223cc..c2d5b431 100644 --- a/internal/cinder/volume_types.go +++ b/internal/cinder/volume_types.go @@ -15,13 +15,12 @@ import ( // CreateVolumeType creates a new volume type func (svc *Service) CreateVolumeType(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:CreateVolumeType") { return } @@ -68,13 +67,12 @@ func (svc *Service) CreateVolumeType(c *gin.Context) { // UpdateVolumeType updates a volume type func (svc *Service) UpdateVolumeType(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:UpdateVolumeType") { return } @@ -141,13 +139,12 @@ func (svc *Service) UpdateVolumeType(c *gin.Context) { // DeleteVolumeType deletes a volume type func (svc *Service) DeleteVolumeType(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:DeleteVolumeType") { return } @@ -202,13 +199,12 @@ func (svc *Service) ListVolumeTypeExtraSpecs(c *gin.Context) { // CreateVolumeTypeExtraSpecs handles POST /v3/types/:id/extra_specs func (svc *Service) CreateVolumeTypeExtraSpecs(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:CreateVolumeTypeExtraSpecs") { return } @@ -275,13 +271,12 @@ func (svc *Service) GetVolumeTypeExtraSpecKey(c *gin.Context) { // UpdateVolumeTypeExtraSpecKey handles PUT /v3/types/:id/extra_specs/:key func (svc *Service) UpdateVolumeTypeExtraSpecKey(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:UpdateVolumeTypeExtraSpecKey") { return } @@ -347,13 +342,12 @@ func (svc *Service) UpdateVolumeTypeExtraSpecKey(c *gin.Context) { // DeleteVolumeTypeExtraSpecKey handles DELETE /v3/types/:id/extra_specs/:key func (svc *Service) DeleteVolumeTypeExtraSpecKey(c *gin.Context) { - // Admin-only (review 2026-10-13 H-3): volume types, QoS specs and the + // Admin-only: volume types, QoS specs and the // manage/unmanage surface shape storage for every tenant and address // host-level backends (upstream: volume_extension:* and // volume_extension:volume_admin_actions:*, all admin_api). Checked before // the body is bound so a non-admin gets 403, not a 400. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "cinder:DeleteVolumeTypeExtraSpecKey") { return } diff --git a/internal/cinder/volumes.go b/internal/cinder/volumes.go index a93c5d9c..2bfef1ba 100644 --- a/internal/cinder/volumes.go +++ b/internal/cinder/volumes.go @@ -1588,8 +1588,7 @@ func (svc *Service) VolumeAction(c *gin.Context) { // Handle reset status action (admin operation) if resetStatusData, ok := req["os-reset_status"]; ok { // Verify admin role - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow os-reset_status to be performed.")) + if !common.RequireAdmin(c, "cinder:VolumeAction") { return } @@ -1993,8 +1992,7 @@ func (svc *Service) SnapshotAction(c *gin.Context) { common.SendError(c, common.NewBadRequestError("unknown action")) return } - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow os-reset_status to be performed.")) + if !common.RequireAdmin(c, "cinder:SnapshotAction") { return } resetStatusMap, ok := resetStatusData.(map[string]any) diff --git a/internal/common/authz.go b/internal/common/authz.go new file mode 100644 index 00000000..faca15b9 --- /dev/null +++ b/internal/common/authz.go @@ -0,0 +1,22 @@ +package common + +import ( + "github.com/gin-gonic/gin" + "github.com/rs/zerolog/log" +) + +// RequireAdmin denies requests that do not carry the deployment-wide admin +// role. operation identifies the API operation in the error and refusal log. +func RequireAdmin(c *gin.Context, operation string) bool { + if HasRole(c, RoleAdmin) { + return true + } + log.Warn(). + Str("caller_project", c.GetString("project_id")). + Str("caller_user", c.GetString("user_id")). + Str("operation", operation). + Msg("refused operator-only API operation") + SendError(c, NewForbiddenError( + "Policy doesn't allow "+operation+" to be performed. Admin role required.")) + return false +} diff --git a/internal/glance/cache.go b/internal/glance/cache.go index 3b89ed64..45132f03 100644 --- a/internal/glance/cache.go +++ b/internal/glance/cache.go @@ -11,11 +11,10 @@ import ( // ListCachedImages lists images in the cache func (svc *Service) ListCachedImages(c *gin.Context) { - // Admin-only (review 2026-10-13 M-2): the cache is a per-node operator + // Admin-only: the cache is a per-node operator // surface — upstream's cache_list policy. The rest of the cache handlers // were gated in this review's first pass; the list was missed. - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("Policy doesn't allow this operation to be performed. Admin role required.")) + if !common.RequireAdmin(c, "glance:ListCachedImages") { return } @@ -69,14 +68,13 @@ func (svc *Service) ListCachedImages(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"cached_images": cachedImages}) } -// PrefetchImage prefetches an image into the cache. Admin-only (review -// 2026-10-13 M-2): cache management is an operator surface (upstream +// PrefetchImage prefetches an image into the cache. Admin-only: +// cache management is an operator surface (upstream // `admin_api`) — it moves shared disk state for every tenant. func (svc *Service) PrefetchImage(c *gin.Context) { imageID := c.Param("id") - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("admin role required to manage the image cache")) + if !common.RequireAdmin(c, "glance:PrefetchImage") { return } @@ -108,13 +106,11 @@ func (svc *Service) PrefetchImage(c *gin.Context) { c.Status(http.StatusAccepted) } -// DeleteCachedImage removes an image from the cache. Admin-only (review -// 2026-10-13 M-2). +// DeleteCachedImage removes an image from the cache. Admin-only. func (svc *Service) DeleteCachedImage(c *gin.Context) { imageID := c.Param("id") - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("admin role required to manage the image cache")) + if !common.RequireAdmin(c, "glance:DeleteCachedImage") { return } @@ -138,12 +134,11 @@ func (svc *Service) DeleteCachedImage(c *gin.Context) { c.Status(http.StatusNoContent) } -// ClearCache clears all cached images. Admin-only (review 2026-10-13 M-2): +// ClearCache clears all cached images. Admin-only: // without the gate any member could flush the deployment-wide cache (DoS on // boot-time caching) with one request. func (svc *Service) ClearCache(c *gin.Context) { - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("admin role required to manage the image cache")) + if !common.RequireAdmin(c, "glance:ClearCache") { return } diff --git a/internal/glance/images.go b/internal/glance/images.go index 9394ee28..29d6d028 100644 --- a/internal/glance/images.go +++ b/internal/glance/images.go @@ -37,14 +37,10 @@ type Service struct { s3Endpoint string imageStore *storage.ImageStore cache *cache.Cache + uploadCap int64 } -// maxImageUploadBytes bounds PUT /images/:id/file (review 2026-10-13 M-3): -// without it an unbounded stream lands on disk (and, in local mode, is -// qemu-img-normalized) until the host fills. 10 GiB, the same default as -// upstream Glance's image_size_cap. A var, not a const, so tests can pin the -// over-cap response without streaming 10 GiB. -var maxImageUploadBytes = 10 << 30 +const defaultMaxImageUploadBytes int64 = 10 << 30 // NewService creates a new Glance service. // @@ -68,6 +64,7 @@ func NewService(pool database.DB, mode, cephPool, cephConf string, s3opts storag s3Endpoint: s3opts.Endpoint, imageStore: imageStore, cache: cacheInstance, + uploadCap: defaultMaxImageUploadBytes, }, nil } @@ -965,7 +962,7 @@ func (svc *Service) UploadImageData(c *gin.Context) { imageID := c.Param("id") projectID := c.GetString("project_id") - // Ownership, not visibility (review 2026-10-13 H-1): seeing a public + // Ownership, not visibility: seeing a public // image must not grant write access to its bytes — otherwise anyone // could poison an operator-staged public base image that every tenant // boots from. Same rule as delete (F6): owner or admin, 403 for a @@ -994,10 +991,10 @@ func (svc *Service) UploadImageData(c *gin.Context) { return } - // Bound the upload (review 2026-10-13 M-3): without a cap an unbounded + // Bound the upload: without a cap an unbounded // stream lands on disk (and, in local mode, is normalized) until the // host fills. Same default as upstream Glance's image_size_cap. - c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(maxImageUploadBytes)) + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, svc.uploadCap) // Claim the queued image atomically: only one request may write its bytes. claimed, err := svc.pool.Exec(c.Request.Context(), @@ -1064,14 +1061,14 @@ func (svc *Service) activateUploadedImage(ctx context.Context, imageID string, s if normalizedToRaw { // The recorded checksum must describe the STORED bytes: downloads serve // them, and EnsureLocalBase verifies a lazily fetched base against this - // value on every boot (review 2026-10-13 N3 — hashing the upload stream + // value on every boot — hashing the upload stream // instead broke boot-from-image on any node without the local base, // because MD5(raw replica) != MD5(original qcow2)). - if stored, csErr := svc.imageStore.StoredChecksum(ctx, imageID); csErr == nil { - checksum = stored - } else { + stored, csErr := svc.imageStore.StoredChecksum(ctx, imageID) + if csErr != nil { return fmt.Errorf("hash stored image: %w", csErr) } + checksum = stored } result, err := svc.pool.Exec(ctx, `UPDATE images SET status = $1, size_bytes = $2, diff --git a/internal/glance/images_db_test.go b/internal/glance/images_db_test.go index 669f1dab..b41cff4c 100644 --- a/internal/glance/images_db_test.go +++ b/internal/glance/images_db_test.go @@ -46,6 +46,20 @@ const ( ) func newGlanceTestRouter(t *testing.T) *gin.Engine { + t.Helper() + return newGlanceTestRouterForService(t, newGlanceTestService(t)) +} + +func newGlanceTestService(t *testing.T) *Service { + t.Helper() + svc, err := NewService(testPool, "stub", "", "", storage.S3Options{}, nil) + if err != nil { + t.Fatalf("NewService: %v", err) + } + return svc +} + +func newGlanceTestRouterForService(t *testing.T, svc *Service) *gin.Engine { t.Helper() gin.SetMode(gin.TestMode) r := gin.New() @@ -53,14 +67,13 @@ func newGlanceTestRouter(t *testing.T) *gin.Engine { if p := c.GetHeader("X-Test-Project"); p != "" { c.Set("project_id", p) } + if userID := c.GetHeader("X-Test-User"); userID != "" { + c.Set("user_id", userID) + } if roles := c.GetHeader("X-Test-Roles"); roles != "" { c.Set("roles", strings.Split(roles, ",")) } }) - svc, err := NewService(testPool, "stub", "", "", storage.S3Options{}, nil) - if err != nil { - t.Fatalf("NewService: %v", err) - } svc.RegisterRoutes(&r.RouterGroup) return r } @@ -95,6 +108,10 @@ func hdr(project, roles string) map[string]string { return map[string]string{"X-Test-Project": project, "X-Test-Roles": roles} } +func userHdr(userID, roles string) map[string]string { + return map[string]string{"X-Test-User": userID, "X-Test-Roles": roles} +} + // TestDeleteImageAuthz_DB exercises the F6 wiring against Postgres for a public image // whose owner is retained: a non-owner non-admin gets 403 (image stays), a missing id // gets 404, and the owner (no admin role) deletes it (204, row gone via rows-affected). diff --git a/internal/glance/import.go b/internal/glance/import.go index 46f5fbd0..a0b6a994 100644 --- a/internal/glance/import.go +++ b/internal/glance/import.go @@ -43,7 +43,7 @@ func (svc *Service) StageImageData(c *gin.Context) { // Consume staging data with bounded memory. The staging backend is currently // a stub, so only its byte count is retained; the upload cap can be 10 GiB. - c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(maxImageUploadBytes)) + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, svc.uploadCap) stagedSize, err := io.Copy(io.Discard, c.Request.Body) if err != nil { // Over the cap is 413, upstream's answer to image_size_cap (round 2 diff --git a/internal/glance/stage_limits_test.go b/internal/glance/stage_limits_test.go index dbba4d5f..5e0b5715 100644 --- a/internal/glance/stage_limits_test.go +++ b/internal/glance/stage_limits_test.go @@ -30,10 +30,9 @@ func (r *stageReader) Read(p []byte) (int, error) { func TestStageImageStreamsWithinLimit(t *testing.T) { requireDB(t) const size = 8 << 20 - origCap := maxImageUploadBytes - maxImageUploadBytes = size - t.Cleanup(func() { maxImageUploadBytes = origCap }) - router := newGlanceTestRouter(t) + svc := newGlanceTestService(t) + svc.uploadCap = size + router := newGlanceTestRouterForService(t, svc) for _, extra := range []int64{0, 1} { id := uuid.NewString() if _, err := testPool.Exec(t.Context(), `INSERT INTO images (id, name, project_id, status, visibility) diff --git a/internal/glance/tasks.go b/internal/glance/tasks.go index 94db476a..ea1dd624 100644 --- a/internal/glance/tasks.go +++ b/internal/glance/tasks.go @@ -51,7 +51,7 @@ func (svc *Service) CreateTask(c *gin.Context) { }) } -// ListTasks handles GET /v2/tasks. Owner-scoped (review 2026-10-13 M-1): task +// ListTasks handles GET /v2/tasks. Owner-scoped: task // input/result carry the caller's image references and endpoint URLs — a // member must see only their own tasks; an admin sees all. func (svc *Service) ListTasks(c *gin.Context) { @@ -111,7 +111,7 @@ func (svc *Service) ListTasks(c *gin.Context) { }) } -// GetTask handles GET /v2/tasks/:id. Owner-or-admin (review 2026-10-13 M-1): +// GetTask handles GET /v2/tasks/:id. Owner-or-admin: // input/result/message are the caller's own task payload, not public data. func (svc *Service) GetTask(c *gin.Context) { taskID := c.Param("id") @@ -128,7 +128,7 @@ func (svc *Service) GetTask(c *gin.Context) { if err != nil { // Not found — and no internal error details in the response body - // (review 2026-10-13 M-1/L-2): err.Error() leaks driver internals. + // because err.Error() could leak driver internals. c.JSON(http.StatusNotFound, gin.H{"message": "Task not found"}) return } diff --git a/internal/glance/tasks_authz_test.go b/internal/glance/tasks_authz_test.go new file mode 100644 index 00000000..31677187 --- /dev/null +++ b/internal/glance/tasks_authz_test.go @@ -0,0 +1,80 @@ +package glance + +import ( + "context" + "encoding/json" + "net/http" + "strings" + "testing" + "time" + + "github.com/google/uuid" +) + +func TestTaskListAndGetAreOwnerScoped(t *testing.T) { + requireDB(t) + r := newGlanceTestRouter(t) + ownerID, foreignID := seededUserID, uuid.NewString() + ownerTaskID, foreignTaskID := uuid.NewString(), uuid.NewString() + now := time.Now().UTC() + + for _, task := range []struct{ id, owner, secret string }{ + {ownerTaskID, ownerID, "owner-private-input"}, + {foreignTaskID, foreignID, "foreign-private-input"}, + } { + input, err := json.Marshal(map[string]string{"private": task.secret}) + if err != nil { + t.Fatal(err) + } + result, err := json.Marshal(map[string]string{"private_result": task.secret}) + if err != nil { + t.Fatal(err) + } + if _, err := testPool.Exec(t.Context(), ` + INSERT INTO image_tasks (id, type, status, input, result, message, owner, created_at, updated_at) + VALUES ($1, 'import', 'success', $2, $3, $4, $5, $6, $6) + `, task.id, input, result, task.secret+" internal message", task.owner, now); err != nil { + t.Fatalf("seed task %s: %v", task.id, err) + } + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), "DELETE FROM image_tasks WHERE id IN ($1, $2)", ownerTaskID, foreignTaskID) + }) + + member := userHdr(ownerID, "member") + code, body := doReq(r, http.MethodGet, "/tasks", member) + if code != http.StatusOK { + t.Fatalf("member list tasks: code=%d body=%s", code, body) + } + var listed struct { + Tasks []map[string]any `json:"tasks"` + } + if err := json.Unmarshal([]byte(body), &listed); err != nil { + t.Fatalf("decode task list: %v", err) + } + if len(listed.Tasks) != 1 || listed.Tasks[0]["id"] != ownerTaskID { + t.Errorf("member task list = %s; want only task %s", body, ownerTaskID) + } + if strings.Contains(body, "foreign-private-input") { + t.Errorf("member task list leaked foreign task data: %s", body) + } + + code, body = doReq(r, http.MethodGet, "/tasks/"+ownerTaskID, member) + if code != http.StatusOK || !strings.Contains(body, "owner-private-input") { + t.Errorf("owner get task: code=%d body=%s; want 200 with own task data", code, body) + } + code, body = doReq(r, http.MethodGet, "/tasks/"+foreignTaskID, member) + if code != http.StatusNotFound || strings.Contains(body, "foreign-private-input") || strings.Contains(body, "internal message") { + t.Errorf("foreign get task: code=%d body=%s; want opaque 404", code, body) + } + + admin := userHdr(uuid.NewString(), "admin") + code, body = doReq(r, http.MethodGet, "/tasks", admin) + if code != http.StatusOK || !strings.Contains(body, ownerTaskID) || !strings.Contains(body, foreignTaskID) { + t.Errorf("admin list tasks: code=%d body=%s; want both tasks", code, body) + } + code, body = doReq(r, http.MethodGet, "/tasks/"+foreignTaskID, admin) + if code != http.StatusOK || !strings.Contains(body, "foreign-private-input") { + t.Errorf("admin get foreign task: code=%d body=%s; want 200", code, body) + } +} diff --git a/internal/glance/upload_authz_test.go b/internal/glance/upload_authz_test.go index aae05a0d..38decb29 100644 --- a/internal/glance/upload_authz_test.go +++ b/internal/glance/upload_authz_test.go @@ -9,7 +9,7 @@ import ( "github.com/google/uuid" ) -// TestUploadImageDataOwnership_DB pins review 2026-10-13 H-1: visibility is a +// TestUploadImageDataOwnership_DB pins the rule that visibility is a // read grant, not a write grant. A public image staged by one project must not // accept bytes from another project's member — that was image poisoning: every // tenant booting the public base image would run the attacker's data. The @@ -64,7 +64,7 @@ func TestUploadImageDataOwnership_DB(t *testing.T) { } } -// TestImageCacheManagementAdminOnly_DB pins review 2026-10-13 M-2: cache +// TestImageCacheManagementAdminOnly_DB pins the rule that cache // mutation is an operator surface — a member flushing the deployment-wide // cache (ClearCache) or evicting entries is a denial of service on // boot-time caching. @@ -106,10 +106,10 @@ func TestImageCacheManagementAdminOnly_DB(t *testing.T) { } } -// TestOverCapUploadAnswers413 pins the round-3 fix for the M-3 cap: a body -// over maxImageUploadBytes must answer 413 (upstream's image_size_cap -// semantics). The cap is a var so the test can lower it instead of streaming -// 10 GiB; in stub mode the store's read surfaces http.MaxBytesError raw, and +// TestOverCapUploadAnswers413 pins the upload cap: a body over the configured +// limit must answer 413 (upstream's image_size_cap semantics). The per-service +// cap lets the test avoid streaming 10 GiB; in stub mode the store's read +// surfaces http.MaxBytesError raw, and // in local mode it is wrapped with %w — errors.As covers both. func TestOverCapUploadAnswers413(t *testing.T) { requireDB(t) @@ -123,11 +123,9 @@ func TestOverCapUploadAnswers413(t *testing.T) { } t.Cleanup(func() { testPool.Exec(ctx, "DELETE FROM images WHERE id=$1", id) }) - origCap := maxImageUploadBytes - maxImageUploadBytes = 8 - t.Cleanup(func() { maxImageUploadBytes = origCap }) - - r := newGlanceTestRouter(t) + svc := newGlanceTestService(t) + svc.uploadCap = 8 + r := newGlanceTestRouterForService(t, svc) code, body := doReqBody(r, "PUT", "/images/"+id+"/file", "this body is longer than eight bytes", hdr(seededProjectID, "member")) if code != http.StatusRequestEntityTooLarge { diff --git a/internal/keystone/application_credentials.go b/internal/keystone/application_credentials.go index 7efdbc85..4b536b37 100644 --- a/internal/keystone/application_credentials.go +++ b/internal/keystone/application_credentials.go @@ -19,13 +19,12 @@ import ( func (svc *Service) ListApplicationCredentials(c *gin.Context) { userID := c.Param("id") - // Self-or-admin (review 2026-10-13 H-4): credential metadata and role + // Self-or-admin: credential metadata and role // lists are footprint information. userID, denied := denyIfNotSelfOrAdmin(c, userID) if denied { return } - rows, err := svc.pool.Query(c.Request.Context(), ` SELECT id, user_id, project_id, name, description, expires_at, unrestricted, created_at FROM application_credentials @@ -109,14 +108,13 @@ func (svc *Service) ListApplicationCredentials(c *gin.Context) { func (svc *Service) CreateApplicationCredential(c *gin.Context) { userID := c.Param("id") - // Self-or-admin (review 2026-10-13 H-4): the 201 response hands back the + // Self-or-admin: the 201 response hands back the // plaintext secret, and requested roles are stored verbatim — minting one // for another user would be both secret disclosure and impersonation. userID, denied := denyIfNotSelfOrAdmin(c, userID) if denied { return } - var req struct { ApplicationCredential struct { Name string `json:"name" binding:"required"` @@ -216,11 +214,14 @@ func (svc *Service) GetApplicationCredential(c *gin.Context) { userID := c.Param("id") credID := c.Param("cred_id") - // Self-or-admin (review 2026-10-13 H-4). + // Self-or-admin. userID, denied := denyIfNotSelfOrAdmin(c, userID) if denied { return } + if denyIfMalformedID(c, "Application Credential", credID) { + return + } var id, userIDVal, name string var projectID, description *string @@ -293,12 +294,15 @@ func (svc *Service) DeleteApplicationCredential(c *gin.Context) { userID := c.Param("id") credID := c.Param("cred_id") - // Self-or-admin (review 2026-10-13 H-4): deleting another user's + // Self-or-admin: deleting another user's // credential is an availability attack on their automation. userID, denied := denyIfNotSelfOrAdmin(c, userID) if denied { return } + if denyIfMalformedID(c, "Application Credential", credID) { + return + } result, err := svc.pool.Exec(c.Request.Context(), "DELETE FROM application_credentials WHERE id = $1 AND user_id = $2", @@ -343,7 +347,7 @@ func (svc *Service) GetApplicationCredentialByID(c *gin.Context) { return } - // Owner-or-admin (review 2026-10-13 H-4): a foreign credential answers + // Owner-or-admin: a foreign credential answers // 404 rather than 403 so the id-only lookup does not leak existence. if userID != c.GetString("user_id") && !common.HasRole(c, common.RoleAdmin) { common.SendError(c, common.NewIdentityNotFoundError("Application Credential", credID)) diff --git a/internal/keystone/authz.go b/internal/keystone/authz.go index 5dc22689..47629975 100644 --- a/internal/keystone/authz.go +++ b/internal/keystone/authz.go @@ -21,16 +21,6 @@ import ( // Such routes therefore sit on `mgmt` (no uuidParamMiddleware) and validate // their path ids with denyIfMalformedID below, after the authz decision. -// denyIfNotAdmin writes a 403 and returns true unless the caller holds the -// admin role. -func denyIfNotAdmin(c *gin.Context) bool { - if !common.HasRole(c, common.RoleAdmin) { - common.SendError(c, common.NewForbiddenError("admin role required")) - return true - } - return false -} - // denyIfMalformedID writes the Keystone-verbatim 404 and returns true when id // is not a well-formed UUID. Call it after the authz check (see above). func denyIfMalformedID(c *gin.Context, kind, id string) bool { diff --git a/internal/keystone/credentials.go b/internal/keystone/credentials.go index d437b63a..9c0d0c3e 100644 --- a/internal/keystone/credentials.go +++ b/internal/keystone/credentials.go @@ -14,7 +14,7 @@ import ( // ListCredentials returns all credentials func (svc *Service) ListCredentials(c *gin.Context) { - // Review 2026-10-13 C-3: the generic credentials API returns the + // The generic credentials API returns the // recoverable EC2/S3 secret blob, so it is scoped like the EC2 API — // a non-admin sees only their own credentials; an admin sees all. callerID := c.GetString("user_id") @@ -80,7 +80,7 @@ func (svc *Service) CreateCredential(c *gin.Context) { return } - // Self-or-admin (review 2026-10-13 C-3): a credential's blob is the + // Self-or-admin: a credential's blob is the // recoverable secret consumed by S3/EC2 signature auth — minting one for // another user (with a known secret) would defeat that auth. if !sameUser(c.GetString("user_id"), req.Credential.UserID) && !common.HasRole(c, common.RoleAdmin) { @@ -122,7 +122,7 @@ func (svc *Service) CreateCredential(c *gin.Context) { } // credentialOwnerOrViewer enforces the owner-or-admin rule on a single -// credential (review 2026-10-13 C-3): 404 when it does not exist, 403 when it +// credential: 404 when it does not exist, 403 when it // belongs to another user and the caller is not admin. func (svc *Service) credentialOwnerOrViewer(c *gin.Context, credID string) bool { var owner string diff --git a/internal/keystone/domains.go b/internal/keystone/domains.go index 7ead3272..7d4c9e80 100644 --- a/internal/keystone/domains.go +++ b/internal/keystone/domains.go @@ -17,10 +17,10 @@ import ( // domain instead of the one they asked for (SPEC-022 FR-001a). Names are matched // exactly — upstream treats domain names as case-sensitive. func (svc *Service) ListDomains(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -76,10 +76,10 @@ func (svc *Service) ListDomains(c *gin.Context) { // CreateDomain handles POST /v3/domains func (svc *Service) CreateDomain(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -134,10 +134,10 @@ func (svc *Service) CreateDomain(c *gin.Context) { func (svc *Service) GetDomain(c *gin.Context) { domainID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): domain records enumerate the + // Admin-only: domain records enumerate the // deployment's trust structure. Checked before the alias resolution // below, which doubles as this route's in-handler id validation (#515). - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -189,10 +189,10 @@ func (svc *Service) GetDomain(c *gin.Context) { func (svc *Service) UpdateDomain(c *gin.Context) { domainID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -289,10 +289,10 @@ func (svc *Service) UpdateDomain(c *gin.Context) { func (svc *Service) DeleteDomain(c *gin.Context) { domainID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -339,10 +339,10 @@ func (svc *Service) DeleteDomain(c *gin.Context) { func (svc *Service) GetDomainConfig(c *gin.Context) { domainID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). diff --git a/internal/keystone/groups.go b/internal/keystone/groups.go index ec03b6af..1684a955 100644 --- a/internal/keystone/groups.go +++ b/internal/keystone/groups.go @@ -14,10 +14,10 @@ import ( // ListGroups handles GET /v3/groups func (svc *Service) ListGroups(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -68,10 +68,10 @@ func (svc *Service) ListGroups(c *gin.Context) { // CreateGroup handles POST /v3/groups func (svc *Service) CreateGroup(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -139,10 +139,10 @@ func (svc *Service) CreateGroup(c *gin.Context) { func (svc *Service) GetGroup(c *gin.Context) { groupID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -183,10 +183,10 @@ func (svc *Service) GetGroup(c *gin.Context) { func (svc *Service) UpdateGroup(c *gin.Context) { groupID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -257,10 +257,10 @@ func (svc *Service) UpdateGroup(c *gin.Context) { func (svc *Service) DeleteGroup(c *gin.Context) { groupID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -291,10 +291,10 @@ func (svc *Service) DeleteGroup(c *gin.Context) { func (svc *Service) ListGroupUsers(c *gin.Context) { groupID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -351,10 +351,10 @@ func (svc *Service) AddUserToGroup(c *gin.Context) { groupID := c.Param("id") userID := c.Param("user_id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -382,10 +382,10 @@ func (svc *Service) RemoveUserFromGroup(c *gin.Context) { groupID := c.Param("id") userID := c.Param("user_id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). diff --git a/internal/keystone/handlers.go b/internal/keystone/handlers.go index 1d312f46..0ba89319 100644 --- a/internal/keystone/handlers.go +++ b/internal/keystone/handlers.go @@ -63,7 +63,7 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup, authMiddleware gin.Handle val := mgmt.Group("") val.Use(uuidParamMiddleware) - // Users. Authz (review 2026-10-13 C-1/M-5): reads and writes of the user + // Users. Authz: reads and writes of the user // collection are admin-only; user-scoped subresources are self-or-admin. // Routes with an authz check sit on `mgmt` and validate ids in-handler so // 403 precedes the 404 (#515, asserted by TestIdentityAuthzPrecedence). @@ -87,7 +87,7 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup, authMiddleware gin.Handle mgmt.DELETE("/users/:id/credentials/OS-EC2/:access", svc.DeleteEC2Credential) // Projects (role assignments must come before /projects/:id to avoid conflicts). - // Authz (review 2026-10-13 C-2/M-5): project management and role assignment + // Authz: project management and role assignment // are admin-only — the assignment pair is the privilege-escalation primitive. val.GET("/projects", svc.ListProjects) // admin-only (in-handler) val.POST("/projects", svc.CreateProject) // admin-only (in-handler) @@ -99,7 +99,7 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup, authMiddleware gin.Handle mgmt.PATCH("/projects/:id", svc.UpdateProject) // admin-only mgmt.DELETE("/projects/:id", svc.DeleteProject) // admin-only - // Groups — admin-only (review 2026-10-13 C-2). + // Groups — admin-only. val.GET("/groups", svc.ListGroups) // admin-only (in-handler) val.POST("/groups", svc.CreateGroup) // admin-only (in-handler) mgmt.GET("/groups/:id", svc.GetGroup) @@ -109,14 +109,14 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup, authMiddleware gin.Handle mgmt.PUT("/groups/:id/users/:user_id", svc.AddUserToGroup) mgmt.DELETE("/groups/:id/users/:user_id", svc.RemoveUserFromGroup) - // Roles — admin-only (review 2026-10-13 C-2). + // Roles — admin-only. val.GET("/roles", svc.ListRoles) // admin-only (in-handler) val.POST("/roles", svc.CreateRole) // admin-only (in-handler) mgmt.GET("/roles/:id", svc.GetRole) mgmt.PATCH("/roles/:id", svc.UpdateRole) mgmt.DELETE("/roles/:id", svc.DeleteRole) - // Role Assignments — admin-only (review 2026-10-13 M-5). + // Role Assignments — admin-only. val.GET("/role_assignments", svc.ListRoleAssignments) // admin-only (in-handler) // Domains. @@ -131,7 +131,7 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup, authMiddleware gin.Handle // value on it still gets #515's verbatim 404, produced in-handler; and PATCH, // DELETE and /config stay on `val`, where the middleware still answers. // - // Domain management is admin-only (review 2026-10-13 C-2). + // Domain management is admin-only. val.GET("/domains", svc.ListDomains) // admin-only (in-handler) val.POST("/domains", svc.CreateDomain) // admin-only (in-handler) mgmt.GET("/domains/:id", svc.GetDomain) // alias-aware (see above), admin-only @@ -139,14 +139,14 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup, authMiddleware gin.Handle mgmt.DELETE("/domains/:id", svc.DeleteDomain) // admin-only mgmt.GET("/domains/:id/config", svc.GetDomainConfig) // admin-only - // Services (catalog management) — admin-only (review 2026-10-13 C-2). + // Services (catalog management) — admin-only. val.GET("/services", svc.ListServices) // admin-only (in-handler) val.POST("/services", svc.CreateService) // admin-only (in-handler) mgmt.GET("/services/:id", svc.GetService) mgmt.PATCH("/services/:id", svc.UpdateService) mgmt.DELETE("/services/:id", svc.DeleteService) - // Endpoints — admin-only (review 2026-10-13 C-2). + // Endpoints — admin-only. val.GET("/endpoints", svc.ListEndpoints) // admin-only (in-handler) val.POST("/endpoints", svc.CreateEndpoint) // admin-only (in-handler) mgmt.DELETE("/endpoints/:id", svc.DeleteEndpoint) @@ -397,9 +397,9 @@ func (svc *Service) ListAuthProjects(c *gin.Context) { // ListUsers lists all users func (svc *Service) ListUsers(c *gin.Context) { - // Admin-only (review 2026-10-13 M-5): the user collection exposes every + // Admin-only: the user collection exposes every // account's name, enabled state and domain — identity enumeration. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } var f listFilter @@ -446,7 +446,7 @@ func (svc *Service) ListUsers(c *gin.Context) { func (svc *Service) GetUser(c *gin.Context) { userID := c.Param("id") - // Self-or-admin (review 2026-10-13 M-5): user records (name, email, + // Self-or-admin: user records (name, email, // enabled) are only for the user itself or an admin. userID, denied := denyIfNotSelfOrAdmin(c, userID) if denied { @@ -476,10 +476,10 @@ func (svc *Service) GetUser(c *gin.Context) { // ListProjects lists all projects func (svc *Service) ListProjects(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -557,10 +557,10 @@ func identityLink(c *gin.Context, collection, id string) gin.H { func (svc *Service) GetProject(c *gin.Context) { projectID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -593,10 +593,10 @@ func (svc *Service) GetProject(c *gin.Context) { // CreateProject handles POST /v3/projects func (svc *Service) CreateProject(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -674,10 +674,10 @@ func (svc *Service) CreateProject(c *gin.Context) { func (svc *Service) UpdateProject(c *gin.Context) { projectID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -752,10 +752,10 @@ func (svc *Service) UpdateProject(c *gin.Context) { func (svc *Service) DeleteProject(c *gin.Context) { projectID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -784,10 +784,10 @@ func (svc *Service) DeleteProject(c *gin.Context) { // ListRoles lists all roles func (svc *Service) ListRoles(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -830,10 +830,10 @@ func (svc *Service) ListRoles(c *gin.Context) { func (svc *Service) GetRole(c *gin.Context) { roleID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -861,10 +861,10 @@ func (svc *Service) GetRole(c *gin.Context) { // CreateRole handles POST /v3/roles func (svc *Service) CreateRole(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -907,10 +907,10 @@ func (svc *Service) CreateRole(c *gin.Context) { func (svc *Service) UpdateRole(c *gin.Context) { roleID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -963,10 +963,10 @@ func (svc *Service) UpdateRole(c *gin.Context) { func (svc *Service) DeleteRole(c *gin.Context) { roleID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -999,10 +999,10 @@ func (svc *Service) AssignRole(c *gin.Context) { userID := c.Param("user_id") roleID := c.Param("role_id") - // Admin-only (review 2026-10-13 C-2): role assignment is the privilege + // Admin-only: role assignment is the privilege // escalation primitive — without this check any authenticated user could // grant itself (or anyone) the admin role on any project. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -1011,22 +1011,29 @@ func (svc *Service) AssignRole(c *gin.Context) { return } - // The ids must name real rows: ON CONFLICT DO NOTHING would otherwise - // silently insert an assignment referencing nothing and answer 204. - for _, tc := range []struct{ kind, id string }{ - {"project", projectID}, {"user", userID}, {"role", roleID}, + // The foreign keys prevent dangling assignments; these lookups preserve + // Keystone's resource-specific 404 instead of leaking a database error. + var projectExists, userExists, roleExists bool + err := svc.pool.QueryRow(c.Request.Context(), ` + SELECT EXISTS(SELECT 1 FROM projects WHERE id = $1), + EXISTS(SELECT 1 FROM users WHERE id = $2), + EXISTS(SELECT 1 FROM roles WHERE id = $3) + `, projectID, userID, roleID).Scan(&projectExists, &userExists, &roleExists) + for _, tc := range []struct { + kind, id string + exists bool + }{ + {"project", projectID, projectExists}, + {"user", userID, userExists}, + {"role", roleID, roleExists}, } { - var exists bool - err := svc.pool.QueryRow(c.Request.Context(), - "SELECT EXISTS(SELECT 1 FROM "+tc.kind+"s WHERE id = $1)", tc.id, - ).Scan(&exists) - if existsOr404(c, err, exists, tc.kind, tc.id) { + if existsOr404(c, err, tc.exists, tc.kind, tc.id) { return } } // Insert role assignment (idempotent) - _, err := svc.pool.Exec(c.Request.Context(), + _, err = svc.pool.Exec(c.Request.Context(), `INSERT INTO role_assignments (id, user_id, project_id, role_id, created_at) VALUES (gen_random_uuid(), $1, $2, $3, NOW()) ON CONFLICT (user_id, project_id, role_id) DO NOTHING`, @@ -1047,10 +1054,10 @@ func (svc *Service) UnassignRole(c *gin.Context) { userID := c.Param("user_id") roleID := c.Param("role_id") - // Admin-only (review 2026-10-13 C-2): revoking roles is the inverse + // Admin-only: revoking roles is the inverse // escalation — any user could strip an admin's role and lock the // deployment out. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } if denyIfMalformedID(c, "project", projectID) || denyIfMalformedID(c, "user", userID) || @@ -1082,10 +1089,10 @@ func (svc *Service) ListUserProjectRoles(c *gin.Context) { projectID := c.Param("id") userID := c.Param("user_id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -1139,10 +1146,10 @@ func (svc *Service) CheckRoleAssignment(c *gin.Context) { userID := c.Param("user_id") roleID := c.Param("role_id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -1175,9 +1182,9 @@ func (svc *Service) CheckRoleAssignment(c *gin.Context) { // ListRoleAssignments handles GET /v3/role_assignments func (svc *Service) ListRoleAssignments(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): role assignments grant authority and expose + // Admin-only: role assignments grant authority and expose // the identity management surface. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -1336,7 +1343,7 @@ func (svc *Service) CreateUser(c *gin.Context) { func (svc *Service) UpdateUser(c *gin.Context) { userID := c.Param("id") - // Self-or-admin (review 2026-10-13 C-1): without this check any + // Self-or-admin: without this check any // authenticated caller could reset any user's password — including the // admin's — or disable any account, because this handler (unlike // ChangePassword) never asked for the original password. @@ -1346,8 +1353,7 @@ func (svc *Service) UpdateUser(c *gin.Context) { return } // Authorization passed; validate the id after it so 403 precedes the 404 (#515). - if uuid.Validate(userID) != nil { - common.SendError(c, common.NewIdentityNotFoundError("user", userID)) + if denyIfMalformedID(c, "user", userID) { return } @@ -1479,8 +1485,7 @@ func (svc *Service) DeleteUser(c *gin.Context) { } // Authorization passed; validate the id after it so 403 precedes this 404 (#515). - if uuid.Validate(userID) != nil { - common.SendError(c, common.NewIdentityNotFoundError("user", userID)) + if denyIfMalformedID(c, "user", userID) { return } @@ -1526,8 +1531,7 @@ func (svc *Service) ChangePassword(c *gin.Context) { } // Authorization passed; validate the id after it so 403 precedes this 404 (#515). - if uuid.Validate(userID) != nil { - common.SendError(c, common.NewIdentityNotFoundError("user", userID)) + if denyIfMalformedID(c, "user", userID) { return } @@ -1588,7 +1592,7 @@ func (svc *Service) ChangePassword(c *gin.Context) { func (svc *Service) GetUserProjects(c *gin.Context) { userID := c.Param("id") - // Self-or-admin (review 2026-10-13 M-5): a user's project membership is + // Self-or-admin: a user's project membership is // footprint information. userID, denied := denyIfNotSelfOrAdmin(c, userID) if denied { @@ -1655,7 +1659,7 @@ func (svc *Service) GetUserProjects(c *gin.Context) { func (svc *Service) GetUserGroups(c *gin.Context) { userID := c.Param("id") - // Self-or-admin (review 2026-10-13 M-5). + // Self-or-admin. userID, denied := denyIfNotSelfOrAdmin(c, userID) if denied { return diff --git a/internal/keystone/identity_authz_gates_test.go b/internal/keystone/identity_authz_gates_test.go index d23cce45..e69f3fe5 100644 --- a/internal/keystone/identity_authz_gates_test.go +++ b/internal/keystone/identity_authz_gates_test.go @@ -8,8 +8,7 @@ import ( "testing" ) -// TestIdentityManagementAuthz_Gates pins the review-2026-10-13 fixes (C-1, C-2, -// M-5): the identity management surface — user updates, role assignment, +// TestIdentityManagementAuthz_Gates pins the identity management gates: user updates, role assignment, // project/role/group/domain/catalog management — must not honor a plain // member. Before the fix every one of these answered 2xx to any authenticated // caller, making PATCH /users/:id a passwordless account takeover and diff --git a/internal/keystone/identity_notfound_test.go b/internal/keystone/identity_notfound_test.go index 7ac9c5be..a7292158 100644 --- a/internal/keystone/identity_notfound_test.go +++ b/internal/keystone/identity_notfound_test.go @@ -14,7 +14,7 @@ import ( // malformed OR a valid-but-unknown id returns the identical Keystone-verbatim // 404 ("Could not find : ."), while an existing id still 200s. // DB-backed (requireDB); routes are wired via the real RegisterRoutes. These -// reads are admin/self-gated (review 2026-10-13), so the caller is admin — +// reads are admin/self-gated, so the caller is admin — // the 404 shape is what this test pins, not who may see it. func TestIdentityNotFound_ReadPaths(t *testing.T) { requireDB(t) diff --git a/internal/keystone/review_authz_regressions_test.go b/internal/keystone/review_authz_regressions_test.go new file mode 100644 index 00000000..7b845699 --- /dev/null +++ b/internal/keystone/review_authz_regressions_test.go @@ -0,0 +1,124 @@ +package keystone + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +func identityRequest(r *gin.Engine, method, path, body string, headers map[string]string) (int, string) { + req := httptest.NewRequest(method, path, strings.NewReader(body)) + if body != "" { + req.Header.Set("Content-Type", "application/json") + } + for key, value := range headers { + req.Header.Set(key, value) + } + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + return w.Code, w.Body.String() +} + +func identityUserHeaders(userID, roles string) map[string]string { + return map[string]string{"X-Test-User": userID, "X-Test-Roles": roles} +} + +func TestGenericCredentialOwnerAndAdminAuthorization(t *testing.T) { + requireDB(t) + r := newIdentityTestRouter() + ownerID, outsiderID := "00000000-0000-0000-0000-000000000001", uuid.NewString() + ownerCredentialID, adminCredentialID := uuid.NewString(), uuid.NewString() + for _, id := range []string{ownerCredentialID, adminCredentialID} { + if _, err := testPool.Exec(t.Context(), "INSERT INTO credentials (id, user_id, type, blob, created_at, updated_at) VALUES ($1, $2, 'ec2', 'review-secret', now(), now())", id, ownerID); err != nil { + t.Fatalf("seed credential %s: %v", id, err) + } + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), "DELETE FROM credentials WHERE id IN ($1, $2)", ownerCredentialID, adminCredentialID) + }) + + owner := identityUserHeaders(ownerID, "member") + outsider := identityUserHeaders(outsiderID, "member") + admin := identityUserHeaders(outsiderID, "admin") + for _, tc := range []struct{ method, path, body string }{ + {http.MethodGet, "/v3/credentials/" + ownerCredentialID, ""}, + {http.MethodPatch, "/v3/credentials/" + ownerCredentialID, "{\"credential\":{\"blob\":\"outsider-secret\"}}"}, + {http.MethodDelete, "/v3/credentials/" + ownerCredentialID, ""}, + } { + if code, body := identityRequest(r, tc.method, tc.path, tc.body, outsider); code != http.StatusForbidden { + t.Errorf("outsider %s %s: code=%d body=%s; want 403", tc.method, tc.path, code, body) + } + } + for _, tc := range []struct { + headers map[string]string + wantID string + }{{owner, ownerCredentialID}, {outsider, ""}, {admin, adminCredentialID}} { + code, body := identityRequest(r, http.MethodGet, "/v3/credentials", "", tc.headers) + if code != http.StatusOK || (tc.wantID != "" && !strings.Contains(body, tc.wantID)) || (tc.wantID == "" && strings.Contains(body, ownerCredentialID)) { + t.Errorf("credential list: code=%d body=%s", code, body) + } + } + if code, body := identityRequest(r, http.MethodGet, "/v3/credentials/"+ownerCredentialID, "", owner); code != http.StatusOK || !strings.Contains(body, "review-secret") { + t.Errorf("owner get credential: code=%d body=%s", code, body) + } + if code, body := identityRequest(r, http.MethodPatch, "/v3/credentials/"+ownerCredentialID, "{\"credential\":{\"blob\":\"owner-updated\"}}", owner); code != http.StatusOK || !strings.Contains(body, "owner-updated") { + t.Errorf("owner update credential: code=%d body=%s", code, body) + } + if code, body := identityRequest(r, http.MethodDelete, "/v3/credentials/"+ownerCredentialID, "", owner); code != http.StatusNoContent { + t.Errorf("owner delete credential: code=%d body=%s", code, body) + } + if code, body := identityRequest(r, http.MethodGet, "/v3/credentials/"+adminCredentialID, "", admin); code != http.StatusOK { + t.Errorf("admin get credential: code=%d body=%s", code, body) + } + if code, body := identityRequest(r, http.MethodPatch, "/v3/credentials/"+adminCredentialID, "{\"credential\":{\"blob\":\"admin-updated\"}}", admin); code != http.StatusOK || !strings.Contains(body, "admin-updated") { + t.Errorf("admin update credential: code=%d body=%s", code, body) + } + if code, body := identityRequest(r, http.MethodDelete, "/v3/credentials/"+adminCredentialID, "", admin); code != http.StatusNoContent { + t.Errorf("admin delete credential: code=%d body=%s", code, body) + } +} + +func TestApplicationCredentialMalformedIDsAreValidatedAfterAuthorization(t *testing.T) { + requireDB(t) + r := newIdentityTestRouter() + memberID := uuid.NewString() + member := identityUserHeaders(memberID, "member") + foreignUserID := "00000000-0000-0000-0000-000000000001" + for _, method := range []string{http.MethodGet, http.MethodDelete} { + path := "/v3/users/" + memberID + "/application_credentials/not-a-uuid" + if code, body := identityRequest(r, method, path, "", member); code != http.StatusNotFound || !strings.Contains(body, "Could not find Application Credential: not-a-uuid.") { + t.Errorf("self %s malformed credential id: code=%d body=%s; want Keystone 404", method, code, body) + } + foreignPath := "/v3/users/" + foreignUserID + "/application_credentials/not-a-uuid" + if code, body := identityRequest(r, method, foreignPath, "", member); code != http.StatusForbidden { + t.Errorf("foreign %s malformed credential id: code=%d body=%s; want authz-first 403", method, code, body) + } + } +} + +func TestAssignRoleUnknownIDsReturnResourceNotFound(t *testing.T) { + requireDB(t) + const projectID = "00000000-0000-0000-0000-000000000002" + const userID = "00000000-0000-0000-0000-000000000001" + const roleID = "00000000-0000-0000-0000-000000000003" + unknownID := uuid.NewString() + admin := identityUserHeaders(uuid.NewString(), "admin") + r := newIdentityTestRouter() + for _, tc := range []struct{ kind, path string }{ + {"project", "/v3/projects/" + unknownID + "/users/" + userID + "/roles/" + roleID}, + {"user", "/v3/projects/" + projectID + "/users/" + unknownID + "/roles/" + roleID}, + {"role", "/v3/projects/" + projectID + "/users/" + userID + "/roles/" + unknownID}, + } { + t.Run(tc.kind, func(t *testing.T) { + code, body := identityRequest(r, http.MethodPut, tc.path, "", admin) + if code != http.StatusNotFound || !strings.Contains(body, "Could not find "+tc.kind+": "+unknownID+".") { + t.Errorf("unknown %s: code=%d body=%s; want resource-specific 404", tc.kind, code, body) + } + }) + } +} diff --git a/internal/keystone/routes_wiring_test.go b/internal/keystone/routes_wiring_test.go index b13c7e12..e6a3a8dc 100644 --- a/internal/keystone/routes_wiring_test.go +++ b/internal/keystone/routes_wiring_test.go @@ -54,7 +54,7 @@ func TestRegisterRoutesMiddlewareWiring(t *testing.T) { // GET /domains/:id lives on `mgmt` so it can resolve the `default` alias // (SPEC-022 FR-001a), and it is admin-only like the rest of the domain - // management surface (review 2026-10-13). A non-admin gets 403 before the + // management surface. A non-admin gets 403 before the // resolver runs; an admin still gets the #515 verbatim 404 for any other // non-identifier value, now produced in-handler. w = httptest.NewRecorder() @@ -82,7 +82,7 @@ func TestRegisterRoutesMiddlewareWiring(t *testing.T) { } // The domain write/config routes moved to `mgmt` with the admin gate - // (review 2026-10-13 C-2), so their malformed-id 404 is likewise produced + //, so their malformed-id 404 is likewise produced // in-handler, after the authz decision — same shape, different producer. for _, tc := range []struct{ method, path string }{ {http.MethodPatch, "/v3/domains/not-a-uuid"}, diff --git a/internal/keystone/services.go b/internal/keystone/services.go index 1863b183..d9b7bb22 100644 --- a/internal/keystone/services.go +++ b/internal/keystone/services.go @@ -15,10 +15,10 @@ import ( // ListServices returns all services in the catalog func (svc *Service) ListServices(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -71,10 +71,10 @@ func (svc *Service) ListServices(c *gin.Context) { // CreateService creates a new service in the catalog func (svc *Service) CreateService(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -129,10 +129,10 @@ func (svc *Service) CreateService(c *gin.Context) { func (svc *Service) GetService(c *gin.Context) { serviceID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -178,10 +178,10 @@ func (svc *Service) GetService(c *gin.Context) { func (svc *Service) DeleteService(c *gin.Context) { serviceID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -211,10 +211,10 @@ func (svc *Service) DeleteService(c *gin.Context) { func (svc *Service) UpdateService(c *gin.Context) { serviceID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). @@ -304,10 +304,10 @@ func (svc *Service) UpdateService(c *gin.Context) { // ListEndpoints returns all endpoints in the catalog func (svc *Service) ListEndpoints(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -363,10 +363,10 @@ func (svc *Service) ListEndpoints(c *gin.Context) { // CreateEndpoint creates a new endpoint in the catalog func (svc *Service) CreateEndpoint(c *gin.Context) { - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } @@ -422,10 +422,10 @@ func (svc *Service) CreateEndpoint(c *gin.Context) { func (svc *Service) DeleteEndpoint(c *gin.Context) { endpointID := c.Param("id") - // Admin-only (review 2026-10-13 C-2): the identity management surface + // Admin-only: the identity management surface // must not honor a plain member — this is the role/assignment/catalog // authority. - if denyIfNotAdmin(c) { + if !common.RequireAdmin(c, "keystone:identity-management") { return } // Authorization passed; validate the ids after it so 403 precedes the 404 (#515). diff --git a/internal/metadata/network_data_test.go b/internal/metadata/network_data_test.go index 9650d452..ecaf4901 100644 --- a/internal/metadata/network_data_test.go +++ b/internal/metadata/network_data_test.go @@ -8,7 +8,7 @@ import ( "github.com/google/uuid" ) -// Review 2026-10-13 N-7: GetNetworkDataJSON selected `p.fixed_ip`, a column +// GetNetworkDataJSON selected `p.fixed_ip`, a column // that does not exist — ports store `fixed_ips` as a JSONB array — so the // query failed and every guest fetching network_data.json got a 500, leaving // cloud-init to fall back to DHCP or configure nothing. This pins the fixed diff --git a/internal/neutron/allocate_free_ip_test.go b/internal/neutron/allocate_free_ip_test.go index e0ff8cea..2744079f 100644 --- a/internal/neutron/allocate_free_ip_test.go +++ b/internal/neutron/allocate_free_ip_test.go @@ -24,6 +24,47 @@ func TestAllocateFreeIPFindsLastAddress(t *testing.T) { } } +func TestReleaseInstancePortsPreservesTenantOwnedPorts(t *testing.T) { + requireDB(t) + networkID, _ := seedN4Subnet(t, "192.168.252.0/24", "192.168.252.1") + instanceID := uuid.NewString() + novaPortID, tenantPortID := uuid.NewString(), uuid.NewString() + for _, port := range []struct{ id, owner string }{ + {novaPortID, "compute:nova"}, + {tenantPortID, ""}, + } { + if _, err := testPool.Exec(t.Context(), ` + INSERT INTO ports (id, name, network_id, project_id, device_id, device_owner, mac_address, fixed_ips) + VALUES ($1, $2, $3, $4, $5, NULLIF($6, ''), $7, '[]') + `, port.id, "release-"+port.id[:8], networkID, seededProjectID, instanceID, port.owner, "52:54:00:aa:bb:cc"); err != nil { + t.Fatalf("seed port %s: %v", port.id, err) + } + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), "DELETE FROM ports WHERE id IN ($1, $2)", novaPortID, tenantPortID) + }) + + svc := NewService(testPool, "stub", nil) + if err := svc.ReleaseInstancePorts(t.Context(), instanceID); err != nil { + t.Fatalf("ReleaseInstancePorts: %v", err) + } + for _, tc := range []struct { + id string + want int + }{{novaPortID, 0}, {tenantPortID, 1}} { + var count int + if err := testPool.QueryRow(t.Context(), "SELECT count(*) FROM ports WHERE id = $1", tc.id).Scan(&count); err != nil { + t.Fatal(err) + } + if count != tc.want { + t.Errorf("port %s remains %d times; want %d", tc.id, count, tc.want) + } + } + if err := svc.ReleaseInstancePorts(t.Context(), instanceID); err != nil { + t.Errorf("second ReleaseInstancePorts call: %v", err) + } +} + func TestIPAvailableCanonicalIPv6(t *testing.T) { requireDB(t) networkID, subnetID := seedN4Subnet(t, "2001:db8::/64", "2001:0db8:0:0:0:0:0:1") @@ -72,7 +113,7 @@ func seedN4Port(t *testing.T, networkID, subnetID, ip string) { } } -// TestAllocateFreeIPAvoidsUsedAddresses pins review 2026-10-13 N-4: allocation +// TestAllocateFreeIPAvoidsUsedAddresses pins that allocation // used to be a purely random draw, so two ports in one subnet landed on the // same address at birthday-problem rates — two MACs claiming one IP. On a /28 // with 12 of 14 candidates already held, the old code duplicated an address diff --git a/internal/neutron/network.go b/internal/neutron/network.go index 125c587b..cb731e58 100644 --- a/internal/neutron/network.go +++ b/internal/neutron/network.go @@ -571,8 +571,8 @@ func (svc *Service) CreateNetwork(c *gin.Context) { } // Making a network shared exposes it to every tenant in the deployment — - // an operator decision, like router:external below (review 2026-10-13 - // M-4: a member-created shared network was visible to, and attachable by, + // an operator decision, like router:external below ( + // A member-created shared network was visible to, and attachable by, // every project). Same shape as the external gate: only `true` is gated, // matching upstream's explicit-set rule. shared := false diff --git a/internal/neutron/ports.go b/internal/neutron/ports.go index 64514004..cab4e8e7 100644 --- a/internal/neutron/ports.go +++ b/internal/neutron/ports.go @@ -45,7 +45,7 @@ func (svc *Service) CreatePort(c *gin.Context) { projectID := c.GetString("project_id") portID := uuid.New().String() - tapName := "tap-" + portID[:8] + tapName := tapNameFor(portID) macAddress := generateMAC() adminStateUp := true @@ -77,11 +77,11 @@ func (svc *Service) CreatePort(c *gin.Context) { } // Get subnet to allocate IP - var subnetID, cidr string + var subnetID string err = svc.pool.QueryRow(c.Request.Context(), - "SELECT id, cidr FROM subnets WHERE network_id = $1 LIMIT 1", + "SELECT id FROM subnets WHERE network_id = $1 LIMIT 1", req.Port.NetworkID, - ).Scan(&subnetID, &cidr) + ).Scan(&subnetID) if err != nil && !errors.Is(err, database.ErrNoRows) { log.Error().Err(err).Msg("port create: subnet lookup failed") @@ -130,7 +130,7 @@ func (svc *Service) CreatePort(c *gin.Context) { now := time.Now() err = database.WithTx(c.Request.Context(), svc.pool, func(tx database.Tx) error { if subnetID != "" { - cidr, err = LockSubnet(c.Request.Context(), tx, subnetID) + cidr, err := LockSubnet(c.Request.Context(), tx, subnetID) if err != nil { return err } @@ -141,7 +141,7 @@ func (svc *Service) CreatePort(c *gin.Context) { fixedIPs = append(fixedIPs, map[string]any{"subnet_id": subnetID, "ip_address": ip}) } fixedIPsJSON, _ := json.Marshal(fixedIPs) - _, err = tx.Exec(c.Request.Context(), ` + _, err := tx.Exec(c.Request.Context(), ` INSERT INTO ports (id, name, network_id, project_id, device_id, device_owner, mac_address, admin_state_up, status, fixed_ips, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) `, portID, req.Port.Name, req.Port.NetworkID, projectID, sql.NullString{String: req.Port.DeviceID, Valid: req.Port.DeviceID != ""}, @@ -544,48 +544,7 @@ func (svc *Service) DeletePort(c *gin.Context) { } portID = resolvedID - tapName := "tap-" + portID[:8] - - // Detach XDP program if eBPF mode - if svc.sgManager != nil && svc.mode == "ebpf" { - if err := svc.sgManager.DetachFromInterface(tapName); err != nil { - log.Warn().Err(err).Str("port_id", portID).Str("tap", tapName).Msg("failed to detach XDP program from interface") - } - - // Remove port from eBPF maps (need MAC address). A scan failure - // leaves macAddress empty, which skips the eBPF cleanup below — - // safe degradation; a leftover map entry will be reconciled on - // the next sync. - var macAddress string - if err := svc.pool.QueryRow(c.Request.Context(), - "SELECT mac_address FROM ports WHERE id = $1", - portID, - ).Scan(&macAddress); err != nil { - log.Warn().Err(err).Str("port_id", portID).Msg("failed to resolve port MAC during teardown") - } - - if macAddress != "" { - if mac, err := net.ParseMAC(macAddress); err == nil { - if err := svc.sgManager.RemoveSecurityGroupFromPort(portID, mac); err != nil { - log.Warn().Err(err).Str("port_id", portID).Str("mac", mac.String()).Msg("failed to remove security group from port") - } - } - } - } - - // Delete TAP device from the DEFAULT namespace, where CreatePort put it (F10). Best-effort: a - // leftover TAP is hygiene, not correctness — the port row is about to be deleted. - if err := svc.tapManager.DeleteTAPDevice(tapName); err != nil { - log.Warn().Err(err).Str("tap", tapName).Msg("failed to delete TAP device") - } - - // Remove FDB entry if VXLAN is enabled - if svc.vxlanCoordinator != nil { - if err := svc.vxlanCoordinator.RemoveFDBEntry(c.Request.Context(), portID); err != nil { - // Log but don't fail - log.Warn().Err(err).Str("port_id", portID).Msg("failed to remove FDB entry") - } - } + svc.teardownPortHost(c.Request.Context(), portID) // Delete from database. The row was resolved above, so this deletes by an id already known to // exist and to belong to the caller. @@ -795,7 +754,7 @@ func IPAvailable(ctx context.Context, pool subnetQuerier, subnetID, ip string) ( } // AllocateFreeIP picks a random usable address in the subnet that no port in -// it already holds (review 2026-10-13 N-4): allocateIP drew purely at random, +// it already holds: allocateIP drew purely at random, // so two ports in one subnet landed on the same address at birthday-problem // rates — two MACs claiming one IP, dnsmasq handing it to one of them, and a // guest that could not reliably reach anything. The subnet's gateway (when @@ -1707,7 +1666,7 @@ func (svc *Service) AllocatePortForInstance(ctx context.Context, networkID, proj // error return after the TAP exists must release it, or a retried boot leaves an // orphan tap- on the host per attempt (mirrors CreatePort's rollbackPort). // Deleting the TAP detaches it from the bridge; nothing else needs undoing. - tapName := "tap-" + portID[:8] + tapName := tapNameFor(portID) releaseTAP := func(reason string) { if svc.mode == "stub" { return @@ -1903,7 +1862,7 @@ func derefOr[T any](v *T, fallback T) T { // ReleaseInstancePorts deletes the ports Neutron allocated for an instance // (device_owner 'compute:nova'), with the same best-effort host cleanup // DeletePort does. It is the rollback for a boot that allocated ports and then -// failed on a later network (review 2026-10-13 N-6): the loop used to +// failed on a later network: the loop used to // markInstanceError and return with the earlier networks' ports still in the // database — rows the API still listed for a server that would never exist, // with leases still reserved, that only an operator could clean up. @@ -1938,19 +1897,7 @@ func (svc *Service) ReleaseInstancePorts(ctx context.Context, instanceID string) } for _, r := range toRelease { - // TAP device, same placement and derivation as DeletePort. - if svc.mode != "stub" { - tapName := "tap-" + r.portID[:8] - if err := svc.tapManager.DeleteTAPDevice(tapName); err != nil { - log.Warn().Err(err).Str("port_id", r.portID).Str("tap", tapName). - Msg("release: failed to delete TAP device") - } - } - if svc.vxlanCoordinator != nil { - if err := svc.vxlanCoordinator.RemoveFDBEntry(ctx, r.portID); err != nil { - log.Warn().Err(err).Str("port_id", r.portID).Msg("release: failed to remove FDB entry") - } - } + svc.teardownPortHost(ctx, r.portID) } if _, err := svc.pool.Exec(ctx, ` @@ -1969,3 +1916,40 @@ func (svc *Service) ReleaseInstancePorts(ctx context.Context, instanceID string) } return nil } + +func tapNameFor(portID string) string { + if len(portID) < 8 { + return "tap-" + portID + } + return "tap-" + portID[:8] +} + +// teardownPortHost removes host-side state for a port. Rollback and DeletePort +// use the same path so eBPF, TAP, and VXLAN cleanup cannot drift. +func (svc *Service) teardownPortHost(ctx context.Context, portID string) { + tapName := tapNameFor(portID) + if svc.sgManager != nil && svc.mode == "ebpf" { + if err := svc.sgManager.DetachFromInterface(tapName); err != nil { + log.Warn().Err(err).Str("port_id", portID).Str("tap", tapName).Msg("failed to detach XDP program from interface") + } + var macAddress string + if err := svc.pool.QueryRow(ctx, "SELECT mac_address FROM ports WHERE id = $1", portID).Scan(&macAddress); err != nil { + log.Warn().Err(err).Str("port_id", portID).Msg("failed to resolve port MAC during teardown") + } + if macAddress != "" { + if mac, err := net.ParseMAC(macAddress); err == nil { + if err := svc.sgManager.RemoveSecurityGroupFromPort(portID, mac); err != nil { + log.Warn().Err(err).Str("port_id", portID).Str("mac", mac.String()).Msg("failed to remove security group from port") + } + } + } + } + if err := svc.tapManager.DeleteTAPDevice(tapName); err != nil { + log.Warn().Err(err).Str("port_id", portID).Str("tap", tapName).Msg("failed to delete TAP device") + } + if svc.vxlanCoordinator != nil { + if err := svc.vxlanCoordinator.RemoveFDBEntry(ctx, portID); err != nil { + log.Warn().Err(err).Str("port_id", portID).Msg("failed to remove FDB entry") + } + } +} diff --git a/internal/neutron/shared_gate_test.go b/internal/neutron/shared_gate_test.go index 8691dfcc..152e5cf5 100644 --- a/internal/neutron/shared_gate_test.go +++ b/internal/neutron/shared_gate_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -// Review 2026-10-13 M-4: `shared` exposes a network to every tenant in the +// A `shared` network is visible to every tenant in the // deployment, but CreateNetwork accepted it from anyone — a member could open // their network to every project (and every project's guests to it). It is an // operator decision, gated exactly like router:external on create: only diff --git a/internal/nova/aggregates.go b/internal/nova/aggregates.go index 5ef1169e..cd972f7a 100644 --- a/internal/nova/aggregates.go +++ b/internal/nova/aggregates.go @@ -26,7 +26,7 @@ import ( // ListAggregates lists all host aggregates func (svc *Service) ListAggregates(c *gin.Context) { - if !requireAdmin(c, "os-aggregates:index") { + if !common.RequireAdmin(c, "os-aggregates:index") { return } rows, err := svc.pool.Query(c.Request.Context(), ` @@ -92,7 +92,7 @@ func (svc *Service) ListAggregates(c *gin.Context) { // CreateAggregate creates a new host aggregate func (svc *Service) CreateAggregate(c *gin.Context) { - if !requireAdmin(c, "os-aggregates:create") { + if !common.RequireAdmin(c, "os-aggregates:create") { return } var req struct { @@ -141,7 +141,7 @@ func (svc *Service) CreateAggregate(c *gin.Context) { // GetAggregate retrieves a specific host aggregate func (svc *Service) GetAggregate(c *gin.Context) { - if !requireAdmin(c, "os-aggregates:show") { + if !common.RequireAdmin(c, "os-aggregates:show") { return } aggregateID := c.Param("id") @@ -194,7 +194,7 @@ func (svc *Service) GetAggregate(c *gin.Context) { // UpdateAggregate updates a host aggregate func (svc *Service) UpdateAggregate(c *gin.Context) { - if !requireAdmin(c, "os-aggregates:update") { + if !common.RequireAdmin(c, "os-aggregates:update") { return } aggregateID := c.Param("id") @@ -256,7 +256,7 @@ func (svc *Service) UpdateAggregate(c *gin.Context) { // DeleteAggregate deletes a host aggregate func (svc *Service) DeleteAggregate(c *gin.Context) { - if !requireAdmin(c, "os-aggregates:delete") { + if !common.RequireAdmin(c, "os-aggregates:delete") { return } aggregateID := c.Param("id") @@ -285,7 +285,7 @@ func (svc *Service) AggregateAction(c *gin.Context) { // Gated here rather than in the three sub-handlers below: a gate placed on // the recognised actions only would leave the unknown-action branch — and any // action added later — reachable by any tenant (SPEC-023 FR-008f). - if !requireAdmin(c, "os-aggregates:action") { + if !common.RequireAdmin(c, "os-aggregates:action") { return } diff --git a/internal/nova/authz.go b/internal/nova/authz.go index 62e8576c..cffd5853 100644 --- a/internal/nova/authz.go +++ b/internal/nova/authz.go @@ -101,35 +101,6 @@ func resolveServerOr404(c *gin.Context, pool database.DB, ref string) (id, owner return id, owner, true } -// requireAdmin gates a surface that belongs to the DEPLOYMENT rather than to any -// project — host aggregates and the deployment-wide migration list (SPEC-023 -// FR-008e, FR-008f). It reports whether the caller may proceed, sending the 403 -// itself so call sites stay one line. -// -// These are deliberately NOT owner-or-administrator. Their tables carry no -// project_id, so there is no owner for the rule to fall back to; access is -// administrative or nothing. That missing column is also how they came to be -// ungated at all: every other handler in this package was written against a -// table with a project_id, so a scoping predicate was the obvious thing to -// write — here there was nothing obvious to write, and nothing was written. -// -// 403 rather than 404: unlike an instance, the existence of these surfaces is -// not a secret (they are in the API's own version document), so there is nothing -// to disclose by admitting they are there. -func requireAdmin(c *gin.Context, operation string) bool { - if common.HasRole(c, common.RoleAdmin) { - return true - } - log.Warn(). - Str("caller_project", c.GetString("project_id")). - Str("caller_user", c.GetString("user_id")). - Str("operation", operation). - Msg("refused operator-only compute operation") - common.SendError(c, common.NewForbiddenError( - "Policy doesn't allow "+operation+" to be performed. Admin role required.")) - return false -} - // denyCrossTenant records a refused cross-tenant compute access, naming the // parties (SPEC-023 FR-007b). // diff --git a/internal/nova/boot_failure_test.go b/internal/nova/boot_failure_test.go index 883ab775..2f9bf5cc 100644 --- a/internal/nova/boot_failure_test.go +++ b/internal/nova/boot_failure_test.go @@ -81,7 +81,7 @@ func (f *fakeNeutron) NetworkUsableBy(_ context.Context, networkID, _ string, ad return nil } -// ReleaseInstancePorts records the rollback calls (review 2026-10-13 N-6) so a +// ReleaseInstancePorts records the rollback calls so a // test can assert that a failed boot released the ports it had already // allocated. func (f *fakeNeutron) ReleaseInstancePorts(_ context.Context, instanceID string) error { @@ -529,7 +529,7 @@ func TestFailedBootLeavesADeletableServer(t *testing.T) { } } -// Review 2026-10-13 N-6: a multi-network boot that fails on a later network used +// A multi-network boot that fails on a later network used // to leak the ports already allocated for the earlier ones — rows the API kept // listing for a server that would never run, with leases still reserved. The // failure path must release what it allocated (the rollback is scoped to the diff --git a/internal/nova/console.go b/internal/nova/console.go index e8672307..fbdb8627 100644 --- a/internal/nova/console.go +++ b/internal/nova/console.go @@ -380,7 +380,7 @@ func unsupportedConsoleTypeMsg(consoleType string) string { // projection directory without asking us, so nothing about the console depends // on this endpoint working — but `openstack` and third-party tooling expect it. func (svc *Service) ShowConsoleAuthToken(c *gin.Context) { - if !requireAdmin(c, "os-console-auth-tokens:show") { + if !common.RequireAdmin(c, "os-console-auth-tokens:show") { return } if svc.consoleStore == nil { diff --git a/internal/nova/flavors.go b/internal/nova/flavors.go index 4bbfb54f..5e8ea820 100644 --- a/internal/nova/flavors.go +++ b/internal/nova/flavors.go @@ -13,10 +13,10 @@ import ( // CreateFlavor handles POST /v2.1/flavors func (svc *Service) CreateFlavor(c *gin.Context) { - // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // Admin-only: the flavor catalog is a shared, // deployment-wide resource — members must not create, delete, or // re-scope it (upstream: admin_api for all flavor management). - if !requireAdmin(c, "flavor-create") { + if !common.RequireAdmin(c, "flavor-create") { return } @@ -103,10 +103,10 @@ func (svc *Service) CreateFlavor(c *gin.Context) { // DeleteFlavor handles DELETE /v2.1/flavors/:id func (svc *Service) DeleteFlavor(c *gin.Context) { - // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // Admin-only: the flavor catalog is a shared, // deployment-wide resource — members must not create, delete, or // re-scope it (upstream: admin_api for all flavor management). - if !requireAdmin(c, "flavor-delete") { + if !common.RequireAdmin(c, "flavor-delete") { return } @@ -173,10 +173,10 @@ func (svc *Service) GetFlavorExtraSpecs(c *gin.Context) { // CreateFlavorExtraSpecs handles POST /v2.1/flavors/:id/os-extra_specs func (svc *Service) CreateFlavorExtraSpecs(c *gin.Context) { - // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // Admin-only: the flavor catalog is a shared, // deployment-wide resource — members must not create, delete, or // re-scope it (upstream: admin_api for all flavor management). - if !requireAdmin(c, "flavor-extra-specs-create") { + if !common.RequireAdmin(c, "flavor-extra-specs-create") { return } @@ -230,10 +230,10 @@ func (svc *Service) GetFlavorExtraSpecKey(c *gin.Context) { // UpdateFlavorExtraSpecKey handles PUT /v2.1/flavors/:id/os-extra_specs/:key func (svc *Service) UpdateFlavorExtraSpecKey(c *gin.Context) { - // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // Admin-only: the flavor catalog is a shared, // deployment-wide resource — members must not create, delete, or // re-scope it (upstream: admin_api for all flavor management). - if !requireAdmin(c, "flavor-extra-specs-update") { + if !common.RequireAdmin(c, "flavor-extra-specs-update") { return } @@ -271,10 +271,10 @@ func (svc *Service) UpdateFlavorExtraSpecKey(c *gin.Context) { // DeleteFlavorExtraSpecKey handles DELETE /v2.1/flavors/:id/os-extra_specs/:key func (svc *Service) DeleteFlavorExtraSpecKey(c *gin.Context) { - // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // Admin-only: the flavor catalog is a shared, // deployment-wide resource — members must not create, delete, or // re-scope it (upstream: admin_api for all flavor management). - if !requireAdmin(c, "flavor-extra-specs-delete") { + if !common.RequireAdmin(c, "flavor-extra-specs-delete") { return } @@ -302,10 +302,10 @@ func (svc *Service) DeleteFlavorExtraSpecKey(c *gin.Context) { // FlavorAction handles POST /v2.1/flavors/:id/action func (svc *Service) FlavorAction(c *gin.Context) { - // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // Admin-only: the flavor catalog is a shared, // deployment-wide resource — members must not create, delete, or // re-scope it (upstream: admin_api for all flavor management). - if !requireAdmin(c, "flavor-access-modify") { + if !common.RequireAdmin(c, "flavor-access-modify") { return } @@ -375,10 +375,10 @@ func (svc *Service) FlavorAction(c *gin.Context) { // GetFlavorAccess handles GET /v2.1/flavors/:id/os-flavor-access func (svc *Service) GetFlavorAccess(c *gin.Context) { - // Admin-only (review 2026-10-13 H-2): the flavor catalog is a shared, + // Admin-only: the flavor catalog is a shared, // deployment-wide resource — members must not create, delete, or // re-scope it (upstream: admin_api for all flavor management). - if !requireAdmin(c, "flavor-access-list") { + if !common.RequireAdmin(c, "flavor-access-list") { return } diff --git a/internal/nova/handlers.go b/internal/nova/handlers.go index 8d8bda0c..386626fc 100644 --- a/internal/nova/handlers.go +++ b/internal/nova/handlers.go @@ -62,7 +62,7 @@ type NeutronService interface { AllocatePortForInstance(ctx context.Context, networkID, projectID, instanceID string, securityGroupIDs []string, admin bool) (any, error) // ReleaseInstancePorts is AllocatePortForInstance's rollback: it deletes the // ports Neutron allocated for a boot that failed after allocating them - // (review 2026-10-13 N-6) — the port rows, their host plumbing and their + // — the port rows, their host plumbing and their // DHCP reservations. Scoped to nova-owned ports; a tenant-created port the // server was told to use is the tenant's, not the boot's. ReleaseInstancePorts(ctx context.Context, instanceID string) error @@ -1126,7 +1126,7 @@ func (svc *Service) CreateServer(c *gin.Context) { // Any failure from the first successful allocation onward (a later // network, the boot volume, libvirt itself) leaves the earlier ports // allocated for a server that will never run. Release them on the way - // out unless the boot completed (review 2026-10-13 N-6: they used to + // out unless the boot completed (: they used to // leak — still listed by the API, leases still reserved). defer func() { if portsAllocated && !bootSucceeded && svc.neutronSvc != nil { @@ -2150,7 +2150,7 @@ func (svc *Service) DeleteServer(c *gin.Context) { return } - // The instance's ports, split by who owns them (review 2026-10-13 N-8): + // The instance's ports, split by who owns them: // // - Ports Neutron allocated for the boot (device_owner 'compute:nova') are // Nova's to delete. Without this they linger after the server is gone and @@ -2963,7 +2963,7 @@ func (svc *Service) ListImagesDetail(c *gin.Context) { // ListHypervisors lists hypervisors (mock for Horizon) func (svc *Service) ListHypervisors(c *gin.Context) { - if !requireAdmin(c, "os-hypervisors:index") { + if !common.RequireAdmin(c, "os-hypervisors:index") { return } c.JSON(200, gin.H{"hypervisors": []gin.H{ @@ -2989,7 +2989,7 @@ func (svc *Service) ListHypervisors(c *gin.Context) { // ListHypervisorsDetail lists hypervisors with details (mock for Horizon) func (svc *Service) ListHypervisorsDetail(c *gin.Context) { - if !requireAdmin(c, "os-hypervisors:detail") { + if !common.RequireAdmin(c, "os-hypervisors:detail") { return } cpuInfoJSON := `{"arch":"x86_64","model":"Skylake-Server-IBRS","vendor":"Intel","features":[],"topology":{"cores":8,"threads":2,"sockets":1}}` @@ -3018,7 +3018,7 @@ func (svc *Service) ListHypervisorsDetail(c *gin.Context) { // GetHypervisorStatistics returns aggregated hypervisor statistics (for Horizon) func (svc *Service) GetHypervisorStatistics(c *gin.Context) { - if !requireAdmin(c, "os-hypervisors:statistics") { + if !common.RequireAdmin(c, "os-hypervisors:statistics") { return } // Count running instances. Scan failures degrade to zero (the stats @@ -3266,7 +3266,7 @@ func (svc *Service) GetLimits(c *gin.Context) { // ListServices returns list of compute services func (svc *Service) ListServices(c *gin.Context) { - if !requireAdmin(c, "os-services:index") { + if !common.RequireAdmin(c, "os-services:index") { return } // Format: "2006-01-02T15:04:05.000000" (without Z, microseconds) diff --git a/internal/nova/interface_attach.go b/internal/nova/interface_attach.go index 1796272c..fa6fdf14 100644 --- a/internal/nova/interface_attach.go +++ b/internal/nova/interface_attach.go @@ -136,22 +136,40 @@ func (svc *Service) AttachInterface(c *gin.Context) { portID = uuid.New().String() macAddress = generateMACAddress() - // Get subnet info for IP allocation - var subnetID, cidr string - err = svc.pool.QueryRow(c.Request.Context(), - "SELECT id, cidr FROM subnets WHERE network_id = $1 LIMIT 1", - networkID, - ).Scan(&subnetID, &cidr) - - if errors.Is(err, database.ErrNoRows) { - common.SendError(c, common.NewBadRequestError("network has no subnet")) - return - } + // Resolve the subnet containing an explicit address. Networks can have + // both IPv4 and IPv6 subnets, so an arbitrary first subnet can reject a + // valid address from the other family. + subnetRows, err := svc.pool.Query(c.Request.Context(), + "SELECT id, cidr FROM subnets WHERE network_id = $1 ORDER BY id", networkID) if err != nil { log.Error().Err(err).Str("operation", "allocate_ip").Msg("subnet lookup error") common.SendError(c, common.NewInternalServerError("failed to allocate IP")) return } + type subnetInfo struct{ id, cidr string } + var subnets []subnetInfo + for subnetRows.Next() { + var subnet subnetInfo + if err := subnetRows.Scan(&subnet.id, &subnet.cidr); err != nil { + subnetRows.Close() + log.Error().Err(err).Str("operation", "allocate_ip").Msg("subnet scan error") + common.SendError(c, common.NewInternalServerError("failed to allocate IP")) + return + } + subnets = append(subnets, subnet) + } + rowsErr := subnetRows.Err() + subnetRows.Close() + if rowsErr != nil { + log.Error().Err(rowsErr).Str("operation", "allocate_ip").Msg("subnet iteration error") + common.SendError(c, common.NewInternalServerError("failed to allocate IP")) + return + } + if len(subnets) == 0 { + common.SendError(c, common.NewBadRequestError("network has no subnet")) + return + } + subnetID, cidr := subnets[0].id, subnets[0].cidr // Use provided fixed_ip or allocate one if req.InterfaceAttachment.FixedIP != "" { @@ -163,8 +181,20 @@ func (svc *Service) AttachInterface(c *gin.Context) { // allocator covers for generated addresses. Parsing also keeps // arbitrary strings out of the JSONB build below. ip := net.ParseIP(fixedIP) - _, ipNet, cidrErr := net.ParseCIDR(cidr) - if ip == nil || cidrErr != nil || !ipNet.Contains(ip) { + if ip == nil { + common.SendError(c, common.NewBadRequestError("fixed_ip is not a valid address in the network's subnet")) + return + } + matchedSubnet := false + for _, subnet := range subnets { + _, ipNet, cidrErr := net.ParseCIDR(subnet.cidr) + if cidrErr == nil && ipNet.Contains(ip) { + subnetID, cidr = subnet.id, subnet.cidr + matchedSubnet = true + break + } + } + if !matchedSubnet { common.SendError(c, common.NewBadRequestError("fixed_ip is not a valid address in the network's subnet")) return } @@ -221,7 +251,7 @@ func (svc *Service) AttachInterface(c *gin.Context) { // Attach the port to the instance. device_owner is stamped // 'compute:nova' only on the port Nova itself just created — that stamp // is the ownership discriminator server delete and the boot-failure - // rollback key on (review 2026-10-13 N-8): a tenant's port attached by id + // rollback key on: a tenant's port attached by id // keeps its own device_owner, so deleting the server DETACHES it instead // of destroying it — upstream's deallocate_for_instance skips ports it // did not create for exactly this reason. The old blanket stamp is also diff --git a/internal/nova/interface_attach_portid_test.go b/internal/nova/interface_attach_portid_test.go index 71fc3b94..a4716d63 100644 --- a/internal/nova/interface_attach_portid_test.go +++ b/internal/nova/interface_attach_portid_test.go @@ -11,7 +11,7 @@ import ( "github.com/google/uuid" ) -// Round 3 of the 2026-10-13 review, F1/F2/F3: the port_id path of +// Regression coverage for F1/F2/F3: the port_id path of // os-interface. // // F1 — every free port has device_id NULL, and the lookup scanned it into a @@ -203,6 +203,60 @@ func TestAttachInterfaceExplicitFixedIPIsValidated(t *testing.T) { } } +func TestAttachInterfaceSelectsMatchingSubnetOnDualStackNetwork(t *testing.T) { + requireDB(t) + ctx := context.Background() + r := newNovaTestRouter() + server, _ := seedAuthzFixtures(t) + member := map[string]string{"X-Test-Project": authzProjectA, "X-Test-User": seededUserID, "X-Test-Roles": "member"} + netID := uuid.NewString() + v4ID, v6ID := uuid.NewString(), uuid.NewString() + if v6ID < v4ID { + v4ID, v6ID = v6ID, v4ID + } + if _, err := testPool.Exec(ctx, "INSERT INTO networks (id, name, project_id, status, admin_state_up) VALUES ($1, 'review-dual-stack', $2, 'ACTIVE', true)", netID, authzProjectA); err != nil { + t.Fatalf("seed network: %v", err) + } + for _, subnet := range []struct{ id, name, cidr, gateway string }{ + {v4ID, "review-dual-v4", "10.251.0.0/24", "10.251.0.1"}, + {v6ID, "review-dual-v6", "2001:db8:251::/64", "2001:db8:251::1"}, + } { + if _, err := testPool.Exec(ctx, "INSERT INTO subnets (id, name, network_id, project_id, cidr, gateway_ip) VALUES ($1, $2, $3, $4, $5, $6)", subnet.id, subnet.name, netID, authzProjectA, subnet.cidr, subnet.gateway); err != nil { + t.Fatalf("seed subnet %s: %v", subnet.id, err) + } + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), "DELETE FROM interface_attachments WHERE port_id IN (SELECT id FROM ports WHERE network_id = $1)", netID) + _, _ = testPool.Exec(context.Background(), "DELETE FROM ports WHERE network_id = $1", netID) + _, _ = testPool.Exec(context.Background(), "DELETE FROM subnets WHERE network_id = $1", netID) + _, _ = testPool.Exec(context.Background(), "DELETE FROM networks WHERE id = $1", netID) + }) + + const requestedIP = "2001:db8:251::42" + bodyJSON, err := json.Marshal(map[string]any{"interfaceAttachment": map[string]string{"net_id": netID, "fixed_ip": requestedIP}}) + if err != nil { + t.Fatalf("marshal interface attach request: %v", err) + } + body := string(bodyJSON) + if code, response := doReq(r, http.MethodPost, "/v2.1/servers/"+server+"/os-interface", body, member); code != http.StatusOK { + t.Fatalf("attach IPv6 address on dual-stack network: code=%d body=%s", code, response) + } + var fixedIPsJSON []byte + if err := testPool.QueryRow(ctx, "SELECT fixed_ips FROM ports WHERE network_id = $1 AND device_id = $2", netID, server).Scan(&fixedIPsJSON); err != nil { + t.Fatalf("read attached port addresses: %v", err) + } + var entries []struct { + SubnetID string `json:"subnet_id"` + IPAddress string `json:"ip_address"` + } + if err := json.Unmarshal(fixedIPsJSON, &entries); err != nil { + t.Fatalf("decode fixed_ips %s: %v", fixedIPsJSON, err) + } + if len(entries) != 1 || entries[0].SubnetID != v6ID || entries[0].IPAddress != requestedIP { + t.Errorf("attached fixed_ips = %s; want subnet %s with address %s", fixedIPsJSON, v6ID, requestedIP) + } +} + // Round 3, L-1: an attachment whose port has no addresses must appear in the // os-interface list with an empty fixed_ips — the list used to skip it, so an // attachment created a moment earlier was invisible, disagreeing with the diff --git a/internal/nova/keypairs.go b/internal/nova/keypairs.go index 0947917d..90a4a025 100644 --- a/internal/nova/keypairs.go +++ b/internal/nova/keypairs.go @@ -127,7 +127,7 @@ func (svc *Service) CreateKeypair(c *gin.Context) { var privateKey *string var fingerprint string - // Bound the import (review 2026-10-13 M-3): an imported public key is + // Bound the import: an imported public key is // parsed and stored verbatim — an unbounded string is an unbounded row. // Authorized-key blobs are a few hundred bytes; 64 KiB is generous. if len(req.Keypair.PublicKey) > 64<<10 { @@ -174,8 +174,8 @@ func (svc *Service) CreateKeypair(c *gin.Context) { // Insert into database. The name pre-check above is only a friendly fast // path — under concurrency the UNIQUE(user_id, name) constraint is the - // real guard, so its violation is the same 409, not a 500 (review - // 2026-10-13 L-3: check-then-insert raced into duplicate keys). + // real guard, so its violation is the same 409, not a 500 when concurrent + // check-then-insert requests collide. now := time.Now() _, err = svc.pool.Exec(c.Request.Context(), ` INSERT INTO keypairs (user_id, name, public_key, fingerprint, created_at) diff --git a/internal/nova/migrations.go b/internal/nova/migrations.go index 2a1affa5..88720027 100644 --- a/internal/nova/migrations.go +++ b/internal/nova/migrations.go @@ -18,7 +18,7 @@ import ( // doing. The comment below already said as much; the check was simply missing. func (svc *Service) ListMigrations(c *gin.Context) { // List all migrations (admin endpoint in real OpenStack) - if !requireAdmin(c, "os-migrations:index") { + if !common.RequireAdmin(c, "os-migrations:index") { return } rows, err := svc.pool.Query(c.Request.Context(), ` diff --git a/internal/nova/port_lifecycle_test.go b/internal/nova/port_lifecycle_test.go index 957a4f4c..540d10b3 100644 --- a/internal/nova/port_lifecycle_test.go +++ b/internal/nova/port_lifecycle_test.go @@ -9,15 +9,15 @@ import ( "github.com/cobaltcore-dev/lightstack/pkg/paths" ) -// Review 2026-10-13 N-8: DeleteServer used a blanket +// DeleteServer used a blanket // `DELETE FROM ports WHERE device_id = $1`, which destroyed ports the TENANT // created and attached (os-interface with port_id) along with the ones Nova // allocated for the boot. Upstream deletes only the ports it created and // detaches the rest — the tenant's port survives the server, parked DOWN with // no device, reusable or deletable by its owner. // -// The tenant port is attached through the real os-interface flow (round 2 of -// this review): the fix keys on device_owner='compute:nova' marking Nova's own +// The tenant port is attached through the real os-interface flow: the fix +// keys on device_owner='compute:nova' marking Nova's own // ports, and AttachInterface used to stamp that owner on a tenant's port too — // which would have made the discriminator, and this behaviour, unreachable. func TestDeleteServerDetachesTenantPortsButDeletesItsOwn(t *testing.T) { diff --git a/internal/nova/review_regressions_test.go b/internal/nova/review_regressions_test.go new file mode 100644 index 00000000..deb12de0 --- /dev/null +++ b/internal/nova/review_regressions_test.go @@ -0,0 +1,90 @@ +package nova + +import ( + "context" + "fmt" + "net/http" + "strings" + "testing" + + "github.com/google/uuid" +) + +func TestFlavorOperatorHandlersRequireAdmin(t *testing.T) { + requireDB(t) + r := newNovaTestRouter() + flavorID := uuid.NewString() + flavorName := "review-flavor-" + flavorID[:8] + if _, err := testPool.Exec(t.Context(), ` + INSERT INTO flavors (id, name, vcpus, ram_mb, disk_gb, is_public) + VALUES ($1, $2, 1, 512, 1, false) + `, flavorID, flavorName); err != nil { + t.Fatalf("seed flavor: %v", err) + } + t.Cleanup(func() { _, _ = testPool.Exec(context.Background(), "DELETE FROM flavors WHERE id = $1", flavorID) }) + + member := map[string]string{"X-Test-Project": seededProjectID, "X-Test-User": seededUserID, "X-Test-Roles": "member"} + admin := map[string]string{"X-Test-Project": seededProjectID, "X-Test-User": seededUserID, "X-Test-Roles": "admin"} + paths := []struct{ method, path, body string }{ + {http.MethodPost, "/v2.1/flavors", `{"flavor":{"name":"member-flavor","ram":512,"vcpus":1,"disk":1}}`}, + {http.MethodDelete, "/v2.1/flavors/" + flavorID, ""}, + {http.MethodPost, "/v2.1/flavors/" + flavorID + "/os-extra_specs", `{"extra_specs":{"review":"member"}}`}, + {http.MethodPut, "/v2.1/flavors/" + flavorID + "/os-extra_specs/review", `{"review":"member"}`}, + {http.MethodDelete, "/v2.1/flavors/" + flavorID + "/os-extra_specs/review", ""}, + {http.MethodPost, "/v2.1/flavors/" + flavorID + "/action", `{"addTenantAccess":{"tenant":"` + seededProjectID + `"}}`}, + {http.MethodGet, "/v2.1/flavors/" + flavorID + "/os-flavor-access", ""}, + } + for _, tc := range paths { + if code, body := doReq(r, tc.method, tc.path, tc.body, member); code != http.StatusForbidden { + t.Errorf("member %s %s: code=%d body=%s; want 403", tc.method, tc.path, code, body) + } + } + + createdName := "review-created-" + uuid.NewString()[:8] + if code, body := doReq(r, http.MethodPost, "/v2.1/flavors", + fmt.Sprintf(`{"flavor":{"name":%q,"ram":512,"vcpus":1,"disk":1}}`, createdName), admin); code != http.StatusOK { + t.Fatalf("admin create flavor: code=%d body=%s; want 200", code, body) + } + t.Cleanup(func() { _, _ = testPool.Exec(context.Background(), "DELETE FROM flavors WHERE name = $1", createdName) }) + checkAdmin := func(method, path, body string, want int) { + t.Helper() + if code, response := doReq(r, method, path, body, admin); code != want { + t.Errorf("admin %s %s: code=%d body=%s; want %d", method, path, code, response, want) + } + } + checkAdmin(http.MethodPost, "/v2.1/flavors/"+flavorID+"/os-extra_specs", `{"extra_specs":{"review":"admin"}}`, http.StatusOK) + checkAdmin(http.MethodPut, "/v2.1/flavors/"+flavorID+"/os-extra_specs/review", `{"review":"updated"}`, http.StatusOK) + checkAdmin(http.MethodDelete, "/v2.1/flavors/"+flavorID+"/os-extra_specs/review", "", http.StatusNoContent) + checkAdmin(http.MethodPost, "/v2.1/flavors/"+flavorID+"/action", `{"addTenantAccess":{"tenant":"`+seededProjectID+`"}}`, http.StatusOK) + checkAdmin(http.MethodGet, "/v2.1/flavors/"+flavorID+"/os-flavor-access", "", http.StatusOK) + checkAdmin(http.MethodDelete, "/v2.1/flavors/"+flavorID, "", http.StatusNoContent) +} + +func TestKeypairImportLimitAndDuplicateConflict(t *testing.T) { + requireDB(t) + r := newNovaTestRouter() + hdr := map[string]string{"X-Test-Project": seededProjectID, "X-Test-User": seededUserID, "X-Test-Roles": "member"} + oversized := fmt.Sprintf(`{"keypair":{"name":"too-large","public_key":%q}}`, strings.Repeat("x", (64<<10)+1)) + if code, body := doReq(r, http.MethodPost, "/v2.1/os-keypairs", oversized, hdr); code != http.StatusBadRequest { + t.Errorf("oversized keypair import: code=%d body=%s; want 400", code, body) + } + + publicKey, _, fingerprint, err := generateSSHKeyPair() + if err != nil { + t.Fatalf("generate test SSH key: %v", err) + } + name := "review-duplicate-" + uuid.NewString()[:8] + if _, err := testPool.Exec(t.Context(), ` + INSERT INTO keypairs (user_id, name, public_key, fingerprint) + VALUES ($1, $2, $3, $4) + `, seededUserID, name, publicKey, fingerprint); err != nil { + t.Fatalf("seed existing keypair: %v", err) + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), "DELETE FROM keypairs WHERE user_id = $1 AND name = $2", seededUserID, name) + }) + body := fmt.Sprintf(`{"keypair":{"name":%q,"public_key":%q}}`, name, publicKey) + if code, response := doReq(r, http.MethodPost, "/v2.1/os-keypairs", body, hdr); code != http.StatusConflict { + t.Errorf("duplicate keypair import: code=%d body=%s; want 409", code, response) + } +} diff --git a/internal/nova/tenant_usage.go b/internal/nova/tenant_usage.go index b2c60354..2aeabed6 100644 --- a/internal/nova/tenant_usage.go +++ b/internal/nova/tenant_usage.go @@ -19,7 +19,7 @@ import ( // by grouping instances and has no owner row of its own, which is how it came to // have no ownership check at all. func (svc *Service) ListTenantUsage(c *gin.Context) { - if !requireAdmin(c, "os-simple-tenant-usage:index") { + if !common.RequireAdmin(c, "os-simple-tenant-usage:index") { return } diff --git a/internal/nova/volume_attachment.go b/internal/nova/volume_attachment.go index d45b0978..df0a32a1 100644 --- a/internal/nova/volume_attachment.go +++ b/internal/nova/volume_attachment.go @@ -71,7 +71,6 @@ type VolumeAttachment struct { // answer with the right status instead of a generic 500. Mirrors cinder's // attachRefusal. type novaAttachRefusal struct { - status int message string } @@ -152,7 +151,7 @@ func (svc *Service) AttachVolume(c *gin.Context) { // Verify volume exists and is available, create the attachment and flip // the volume to in-use — all in one transaction with the volume row - // locked (review 2026-10-13 N-9): the old check-then-act let the loser of + // locked: the old check-then-act let the loser of // a concurrent double-attach surface as a 500 from the UNIQUE constraint, // and a failure between the INSERT and the UPDATE left the volume row // inconsistent with its attachment. Mirrors cinder's attach CAS. @@ -180,13 +179,12 @@ func (svc *Service) AttachVolume(c *gin.Context) { return err } if volumeStatus != "available" { - return &novaAttachRefusal{http.StatusBadRequest, - fmt.Sprintf("volume status is %s, must be available", volumeStatus)} + return &novaAttachRefusal{message: fmt.Sprintf("volume status is %s, must be available", volumeStatus)} } if attachedToInstance != nil { // Upstream answers a re-attach with 400 (Invalid input / // volume already attached), not 409 — round 2 of this review. - return &novaAttachRefusal{http.StatusBadRequest, "volume is already attached to an instance"} + return &novaAttachRefusal{message: "volume is already attached to an instance"} } // Auto-assign device if not provided. The instance lock above serializes @@ -218,11 +216,7 @@ func (svc *Service) AttachVolume(c *gin.Context) { if err != nil { var refusal *novaAttachRefusal if errors.As(err, &refusal) { - if refusal.status == http.StatusConflict { - common.SendError(c, common.NewConflictError(refusal.message)) - } else { - common.SendError(c, common.NewBadRequestError(refusal.message)) - } + common.SendError(c, common.NewBadRequestError(refusal.message)) return } if errors.Is(err, errNovaVolumeNotFound) { @@ -386,8 +380,8 @@ func (svc *Service) DetachVolume(c *gin.Context) { } } - // Delete the attachment and release the volume in one transaction (review - // 2026-10-13 N-10): as two statements, a failure after the DELETE left the + // Delete the attachment and release the volume in one transaction. As two + // statements, a failure after the DELETE left the // volume stranded in-use with no attachment row — unfixable through the // API, since detach could no longer find the attachment to remove. now := time.Now() diff --git a/internal/nova/volume_attachment_refusal_test.go b/internal/nova/volume_attachment_refusal_test.go new file mode 100644 index 00000000..2bd83ae6 --- /dev/null +++ b/internal/nova/volume_attachment_refusal_test.go @@ -0,0 +1,71 @@ +package nova + +import ( + "context" + "encoding/json" + "net/http" + "testing" + + "github.com/google/uuid" +) + +func TestVolumeAttachAndDetachRefusals(t *testing.T) { + requireDB(t) + r := newNovaTestRouter() + instanceID := seedConsoleInstance(t, seededProjectID) + inUseID, alreadyAttachedID := uuid.NewString(), uuid.NewString() + for _, volume := range []struct { + id, name, status string + attached bool + }{ + {inUseID, "review-in-use-" + inUseID[:8], "in-use", false}, + {alreadyAttachedID, "review-attached-" + alreadyAttachedID[:8], "available", true}, + } { + query := "INSERT INTO volumes (id, name, project_id, user_id, size_gb, status) VALUES ($1, $2, $3, $4, 1, $5)" + args := []any{volume.id, volume.name, seededProjectID, seededUserID, volume.status} + if volume.attached { + query = "INSERT INTO volumes (id, name, project_id, user_id, size_gb, status, attached_to_instance_id) VALUES ($1, $2, $3, $4, 1, $5, $6)" + args = append(args, instanceID) + } + if _, err := testPool.Exec(t.Context(), query, args...); err != nil { + t.Fatalf("seed volume %s: %v", volume.id, err) + } + } + t.Cleanup(func() { + _, _ = testPool.Exec(context.Background(), "DELETE FROM volume_attachments WHERE volume_id IN ($1, $2)", inUseID, alreadyAttachedID) + _, _ = testPool.Exec(context.Background(), "DELETE FROM volumes WHERE id IN ($1, $2)", inUseID, alreadyAttachedID) + }) + + headers := testAuthHdr(seededProjectID, seededUserID) + attach := func(volumeID string) (int, string) { + body, err := json.Marshal(map[string]any{"volumeAttachment": map[string]string{"volumeId": volumeID}}) + if err != nil { + t.Fatalf("marshal attach request: %v", err) + } + return doReq(r, http.MethodPost, "/v2.1/servers/"+instanceID+"/os-volume_attachments", string(body), headers) + } + for _, tc := range []struct { + name, volumeID string + want int + }{ + {"unknown volume", uuid.NewString(), http.StatusNotFound}, + {"wrong status", inUseID, http.StatusBadRequest}, + {"already attached", alreadyAttachedID, http.StatusBadRequest}, + } { + t.Run(tc.name, func(t *testing.T) { + if code, body := attach(tc.volumeID); code != tc.want { + t.Errorf("attach %s: code=%d body=%s; want %d", tc.name, code, body, tc.want) + } + }) + } + if code, body := doReq(r, http.MethodDelete, "/v2.1/servers/"+instanceID+"/os-volume_attachments/"+uuid.NewString(), "", headers); code != http.StatusNotFound { + t.Errorf("detach without an attachment: code=%d body=%s; want 404", code, body) + } + var count int + if err := testPool.QueryRow(t.Context(), "SELECT count(*) FROM volume_attachments WHERE volume_id IN ($1, $2)", inUseID, alreadyAttachedID).Scan(&count); err != nil { + t.Fatal(err) + } + if count != 0 { + t.Errorf("refused requests created %d volume attachments", count) + } +} diff --git a/pkg/storage/image_store.go b/pkg/storage/image_store.go index bee2dcc8..ac6a93a9 100644 --- a/pkg/storage/image_store.go +++ b/pkg/storage/image_store.go @@ -613,7 +613,7 @@ func (s *ImageStore) uploadLocalToS3(ctx context.Context, imageID string) error // Normalizing backends (local, local,rbd, local,s3) rewrite the upload — a // qcow2 becomes a raw base — so the checksum Glance advertises, downloads // serve and EnsureLocalBase verifies against must be taken from the stored -// object, not from the client's upload stream (review 2026-10-13 N3: the +// object, not from the client's upload stream (: the // stream hash made boot-from-image fail on any node without the local base, // because MD5(raw replica) never equals MD5(original qcow2)). For S3-only and // stub modes the stored bytes are the upload bytes, so the stream hash the diff --git a/pkg/storage/image_store_test.go b/pkg/storage/image_store_test.go index 11752720..0ac58fec 100644 --- a/pkg/storage/image_store_test.go +++ b/pkg/storage/image_store_test.go @@ -275,8 +275,8 @@ func makeQcow2(t *testing.T, dir string) string { return qcow } -// TestStoredChecksumMatchesStoredBytesNotUploadBytes pins review 2026-10-13 -// N3: normalizing backends rewrite the upload (qcow2 → raw base), so the +// TestStoredChecksumMatchesStoredBytesNotUploadBytes pins the stored-object +// checksum behavior: normalizing backends rewrite the upload (qcow2 → raw base), so the // checksum recorded for an image must describe the STORED bytes — downloads // serve them and EnsureLocalBase verifies a lazily fetched base against the // recorded value on every boot. Hashing the upload stream instead made diff --git a/scripts/deploy-single-node.sh b/scripts/deploy-single-node.sh index bde0f8d6..86564c58 100755 --- a/scripts/deploy-single-node.sh +++ b/scripts/deploy-single-node.sh @@ -469,7 +469,7 @@ clone_lightstack() { build_lightstack() { log_info "Building Lightstack binary..." - # Use the same patched toolchain as CI and the release image. + # Checksum-pinned toolchain by design; CI and the release image track latest stable instead. local go_version="1.27.3" local go_sha256="65efbc80fdc1fa2cd2631cad5b481777a68e8725f34e89f50fc6ac714e379b40" local installed_go="" From 98fb9058313c474061a601815b491545ea9244c2 Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Tue, 10 Nov 2026 17:26:07 +0100 Subject: [PATCH 24/26] fix(keystone): normalize authorized uuid path params Signed-off-by: Senol Colak --- internal/keystone/handlers.go | 6 +++++- internal/keystone/uuid_middleware.go | 15 +++++++++++++++ internal/keystone/uuid_middleware_test.go | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/internal/keystone/handlers.go b/internal/keystone/handlers.go index 0ba89319..eced00a7 100644 --- a/internal/keystone/handlers.go +++ b/internal/keystone/handlers.go @@ -53,7 +53,11 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup, authMiddleware gin.Handle // All management routes require authentication mgmt := v3.Group("") - mgmt.Use(authMiddleware) + // Canonicalize well-formed UUID path params after authentication. This keeps + // uppercase and compact UUIDs usable on SQLite text columns while leaving + // malformed params untouched so in-handler authorization can still return + // 403 before its 404 validation. + mgmt.Use(authMiddleware, canonicalizeUUIDParams) // Malformed UUID path ids (params named "id" or "*_id") get a Keystone-verbatim // 404 before the handler, on every management route EXCEPT the authz-gated ones diff --git a/internal/keystone/uuid_middleware.go b/internal/keystone/uuid_middleware.go index 114ced3e..b61ce8dc 100644 --- a/internal/keystone/uuid_middleware.go +++ b/internal/keystone/uuid_middleware.go @@ -42,6 +42,21 @@ func uuidParamMiddleware(c *gin.Context) { // advances to the next handler on its own. } +// canonicalizeUUIDParams normalizes only well-formed UUID path params. It is +// installed after authentication on management routes whose handlers perform +// authorization before rejecting malformed IDs. +func canonicalizeUUIDParams(c *gin.Context) { + for i := range c.Params { + p := &c.Params[i] + if !isUUIDParam(p.Key) { + continue + } + if id, err := uuid.Parse(p.Value); err == nil { + p.Value = id.String() + } + } +} + // isUUIDParam reports whether a gin path-param name addresses a UUID id. func isUUIDParam(name string) bool { return name == "id" || strings.HasSuffix(name, "_id") diff --git a/internal/keystone/uuid_middleware_test.go b/internal/keystone/uuid_middleware_test.go index e0f4cbbf..78d29aeb 100644 --- a/internal/keystone/uuid_middleware_test.go +++ b/internal/keystone/uuid_middleware_test.go @@ -242,3 +242,21 @@ func TestUUIDParamMiddlewareCanonicalisesID(t *testing.T) { } } } + +func TestCanonicalizeUUIDParamsLeavesMalformedIDsForHandlers(t *testing.T) { + gin.SetMode(gin.TestMode) + c, _ := gin.CreateTestContext(httptest.NewRecorder()) + c.Params = gin.Params{ + {Key: "id", Value: "11111111111111111111111111111111"}, + {Key: "cred_id", Value: "NOT-A-UUID"}, + {Key: "access", Value: "legacy-access-key"}, + } + + canonicalizeUUIDParams(c) + want := []string{"11111111-1111-1111-1111-111111111111", "NOT-A-UUID", "legacy-access-key"} + for i, expected := range want { + if got := c.Params[i].Value; got != expected { + t.Errorf("param %s = %q, want %q", c.Params[i].Key, got, expected) + } + } +} From 5615b7123b08d50f34bc3b6d06edacae35033dd1 Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Thu, 12 Nov 2026 14:46:55 +0100 Subject: [PATCH 25/26] fix: resolve remaining PR 104 review threads Signed-off-by: Senol Colak --- internal/common/fixed_ips.go | 1 + internal/keystone/ec2_credentials.go | 11 +-- internal/keystone/handlers.go | 14 ---- .../keystone/review_authz_regressions_test.go | 37 +++------- internal/keystone/routes_wiring_test.go | 6 +- internal/keystone/uuid_middleware.go | 17 ++--- internal/keystone/uuid_middleware_test.go | 6 +- internal/metadata/service.go | 73 ++----------------- internal/neutron/network.go | 8 +- internal/neutron/ports.go | 18 +---- internal/nova/handlers.go | 4 +- internal/nova/interface_attach.go | 47 +++--------- pkg/storage/image_store.go | 6 +- 13 files changed, 59 insertions(+), 189 deletions(-) diff --git a/internal/common/fixed_ips.go b/internal/common/fixed_ips.go index 311b6735..f8bcd8e6 100644 --- a/internal/common/fixed_ips.go +++ b/internal/common/fixed_ips.go @@ -5,6 +5,7 @@ package common // metadata service (instance-by-address lookup) read it to find which port holds an address. type FixedIPs []struct { IPAddress string `json:"ip_address"` + SubnetID string `json:"subnet_id"` } // Holds reports whether ip is one of the port's fixed addresses, compared as a whole string. diff --git a/internal/keystone/ec2_credentials.go b/internal/keystone/ec2_credentials.go index 656aeda4..81153975 100644 --- a/internal/keystone/ec2_credentials.go +++ b/internal/keystone/ec2_credentials.go @@ -52,16 +52,9 @@ func ec2CredentialView(userID, tenantID, access, secret string) gin.H { // API returns the secret, so every handler must be owner-or-admin only — otherwise any // authenticated tenant could read or mint another user's S3 credential and reach that // tenant's RGW namespace (SPEC-014 FR-007; mirrors the self-or-admin check in -// ChangePassword). On success it returns the id in canonical form, so the caller's -// queries match on SQLite's text ids. +// ChangePassword). The management group's canonicalization middleware puts valid ids +// in canonical form so the caller's queries match on SQLite's text ids. func denyIfNotSelfOrAdmin(c *gin.Context, userID string) (string, bool) { - // Canonicalise a well-formed id up front so the self-check compares like with like - // (the token's user_id is canonical) and the caller's query matches on SQLite's text - // ids. A malformed id is left as-is: it cannot match the caller's own id, so an - // unauthorised caller still gets 403 before the 404 below (#515). - if parsed, err := uuid.Parse(userID); err == nil { - userID = parsed.String() - } if !sameUser(c.GetString("user_id"), userID) && !common.HasRole(c, common.RoleAdmin) { common.SendError(c, common.NewForbiddenError("cannot manage another user's EC2 credentials")) return "", true diff --git a/internal/keystone/handlers.go b/internal/keystone/handlers.go index eced00a7..b7153adf 100644 --- a/internal/keystone/handlers.go +++ b/internal/keystone/handlers.go @@ -1476,12 +1476,6 @@ func (svc *Service) UpdateUser(c *gin.Context) { func (svc *Service) DeleteUser(c *gin.Context) { userID := c.Param("id") - // Canonicalise a well-formed id so the delete matches on SQLite's text ids; a - // malformed id is left for the uuid.Validate guard below to 404 after the authz check. - if parsed, err := uuid.Parse(userID); err == nil { - userID = parsed.String() - } - // Only admins may delete users if !common.HasRole(c, common.RoleAdmin) { common.SendError(c, common.NewForbiddenError("admin role required to delete users")) @@ -1519,14 +1513,6 @@ func (svc *Service) DeleteUser(c *gin.Context) { func (svc *Service) ChangePassword(c *gin.Context) { userID := c.Param("id") - // Canonicalise a well-formed id up front so the self-check compares like with like - // (the token's user_id is canonical) and the queries match on SQLite's text ids. A - // malformed id is left as-is: it can't match the caller's own id, so an unauthorised - // caller still gets 403 before the 404 below (#515). - if parsed, err := uuid.Parse(userID); err == nil { - userID = parsed.String() - } - // Users may only change their own password; admins may change any user's password callerID := c.GetString("user_id") if !sameUser(callerID, userID) && !common.HasRole(c, common.RoleAdmin) { diff --git a/internal/keystone/review_authz_regressions_test.go b/internal/keystone/review_authz_regressions_test.go index 7b845699..aec976fc 100644 --- a/internal/keystone/review_authz_regressions_test.go +++ b/internal/keystone/review_authz_regressions_test.go @@ -3,27 +3,12 @@ package keystone import ( "context" "net/http" - "net/http/httptest" "strings" "testing" - "github.com/gin-gonic/gin" "github.com/google/uuid" ) -func identityRequest(r *gin.Engine, method, path, body string, headers map[string]string) (int, string) { - req := httptest.NewRequest(method, path, strings.NewReader(body)) - if body != "" { - req.Header.Set("Content-Type", "application/json") - } - for key, value := range headers { - req.Header.Set(key, value) - } - w := httptest.NewRecorder() - r.ServeHTTP(w, req) - return w.Code, w.Body.String() -} - func identityUserHeaders(userID, roles string) map[string]string { return map[string]string{"X-Test-User": userID, "X-Test-Roles": roles} } @@ -50,7 +35,7 @@ func TestGenericCredentialOwnerAndAdminAuthorization(t *testing.T) { {http.MethodPatch, "/v3/credentials/" + ownerCredentialID, "{\"credential\":{\"blob\":\"outsider-secret\"}}"}, {http.MethodDelete, "/v3/credentials/" + ownerCredentialID, ""}, } { - if code, body := identityRequest(r, tc.method, tc.path, tc.body, outsider); code != http.StatusForbidden { + if code, body := doJSONReq(r, tc.method, tc.path, tc.body, outsider); code != http.StatusForbidden { t.Errorf("outsider %s %s: code=%d body=%s; want 403", tc.method, tc.path, code, body) } } @@ -58,27 +43,27 @@ func TestGenericCredentialOwnerAndAdminAuthorization(t *testing.T) { headers map[string]string wantID string }{{owner, ownerCredentialID}, {outsider, ""}, {admin, adminCredentialID}} { - code, body := identityRequest(r, http.MethodGet, "/v3/credentials", "", tc.headers) + code, body := doJSONReq(r, http.MethodGet, "/v3/credentials", "", tc.headers) if code != http.StatusOK || (tc.wantID != "" && !strings.Contains(body, tc.wantID)) || (tc.wantID == "" && strings.Contains(body, ownerCredentialID)) { t.Errorf("credential list: code=%d body=%s", code, body) } } - if code, body := identityRequest(r, http.MethodGet, "/v3/credentials/"+ownerCredentialID, "", owner); code != http.StatusOK || !strings.Contains(body, "review-secret") { + if code, body := doJSONReq(r, http.MethodGet, "/v3/credentials/"+ownerCredentialID, "", owner); code != http.StatusOK || !strings.Contains(body, "review-secret") { t.Errorf("owner get credential: code=%d body=%s", code, body) } - if code, body := identityRequest(r, http.MethodPatch, "/v3/credentials/"+ownerCredentialID, "{\"credential\":{\"blob\":\"owner-updated\"}}", owner); code != http.StatusOK || !strings.Contains(body, "owner-updated") { + if code, body := doJSONReq(r, http.MethodPatch, "/v3/credentials/"+ownerCredentialID, "{\"credential\":{\"blob\":\"owner-updated\"}}", owner); code != http.StatusOK || !strings.Contains(body, "owner-updated") { t.Errorf("owner update credential: code=%d body=%s", code, body) } - if code, body := identityRequest(r, http.MethodDelete, "/v3/credentials/"+ownerCredentialID, "", owner); code != http.StatusNoContent { + if code, body := doJSONReq(r, http.MethodDelete, "/v3/credentials/"+ownerCredentialID, "", owner); code != http.StatusNoContent { t.Errorf("owner delete credential: code=%d body=%s", code, body) } - if code, body := identityRequest(r, http.MethodGet, "/v3/credentials/"+adminCredentialID, "", admin); code != http.StatusOK { + if code, body := doJSONReq(r, http.MethodGet, "/v3/credentials/"+adminCredentialID, "", admin); code != http.StatusOK { t.Errorf("admin get credential: code=%d body=%s", code, body) } - if code, body := identityRequest(r, http.MethodPatch, "/v3/credentials/"+adminCredentialID, "{\"credential\":{\"blob\":\"admin-updated\"}}", admin); code != http.StatusOK || !strings.Contains(body, "admin-updated") { + if code, body := doJSONReq(r, http.MethodPatch, "/v3/credentials/"+adminCredentialID, "{\"credential\":{\"blob\":\"admin-updated\"}}", admin); code != http.StatusOK || !strings.Contains(body, "admin-updated") { t.Errorf("admin update credential: code=%d body=%s", code, body) } - if code, body := identityRequest(r, http.MethodDelete, "/v3/credentials/"+adminCredentialID, "", admin); code != http.StatusNoContent { + if code, body := doJSONReq(r, http.MethodDelete, "/v3/credentials/"+adminCredentialID, "", admin); code != http.StatusNoContent { t.Errorf("admin delete credential: code=%d body=%s", code, body) } } @@ -91,11 +76,11 @@ func TestApplicationCredentialMalformedIDsAreValidatedAfterAuthorization(t *test foreignUserID := "00000000-0000-0000-0000-000000000001" for _, method := range []string{http.MethodGet, http.MethodDelete} { path := "/v3/users/" + memberID + "/application_credentials/not-a-uuid" - if code, body := identityRequest(r, method, path, "", member); code != http.StatusNotFound || !strings.Contains(body, "Could not find Application Credential: not-a-uuid.") { + if code, body := doJSONReq(r, method, path, "", member); code != http.StatusNotFound || !strings.Contains(body, "Could not find Application Credential: not-a-uuid.") { t.Errorf("self %s malformed credential id: code=%d body=%s; want Keystone 404", method, code, body) } foreignPath := "/v3/users/" + foreignUserID + "/application_credentials/not-a-uuid" - if code, body := identityRequest(r, method, foreignPath, "", member); code != http.StatusForbidden { + if code, body := doJSONReq(r, method, foreignPath, "", member); code != http.StatusForbidden { t.Errorf("foreign %s malformed credential id: code=%d body=%s; want authz-first 403", method, code, body) } } @@ -115,7 +100,7 @@ func TestAssignRoleUnknownIDsReturnResourceNotFound(t *testing.T) { {"role", "/v3/projects/" + projectID + "/users/" + userID + "/roles/" + unknownID}, } { t.Run(tc.kind, func(t *testing.T) { - code, body := identityRequest(r, http.MethodPut, tc.path, "", admin) + code, body := doJSONReq(r, http.MethodPut, tc.path, "", admin) if code != http.StatusNotFound || !strings.Contains(body, "Could not find "+tc.kind+": "+unknownID+".") { t.Errorf("unknown %s: code=%d body=%s; want resource-specific 404", tc.kind, code, body) } diff --git a/internal/keystone/routes_wiring_test.go b/internal/keystone/routes_wiring_test.go index e6a3a8dc..c29882a3 100644 --- a/internal/keystone/routes_wiring_test.go +++ b/internal/keystone/routes_wiring_test.go @@ -81,9 +81,9 @@ func TestRegisterRoutesMiddlewareWiring(t *testing.T) { t.Fatal("GET /v3/domains/:id bypassed the auth middleware — it must be on mgmt, not the public v3 group") } - // The domain write/config routes moved to `mgmt` with the admin gate - //, so their malformed-id 404 is likewise produced - // in-handler, after the authz decision — same shape, different producer. + // The domain write/config routes moved to `mgmt` with the admin gate, so + // their malformed-id 404 is likewise produced in-handler, after the authz + // decision — same shape, different producer. for _, tc := range []struct{ method, path string }{ {http.MethodPatch, "/v3/domains/not-a-uuid"}, {http.MethodDelete, "/v3/domains/not-a-uuid"}, diff --git a/internal/keystone/uuid_middleware.go b/internal/keystone/uuid_middleware.go index b61ce8dc..14091b03 100644 --- a/internal/keystone/uuid_middleware.go +++ b/internal/keystone/uuid_middleware.go @@ -16,10 +16,10 @@ import ( // faithful behavior, and validating up front avoids the `22P02` cast that would // otherwise surface as 500 (cloud-storage#515). // -// A well-formed but non-canonical id — dash-free (the form rgw_compat_tenant_ids -// advertises) or upper-case — is rewritten to canonical form in place, so the handler's -// id comparison matches on SQLite, where ids are text, as it did on PostgreSQL's `uuid` -// columns. uuid.Parse accepts exactly the forms uuid.Validate did. +// Well-formed UUID params have already been canonicalized by the parent management +// group's canonicalizeUUIDParams middleware, so this middleware only rejects malformed +// values. That ordering keeps the id comparison compatible with SQLite's text columns +// while preserving the same 404 behavior as PostgreSQL's `uuid` columns. // // It validates only params named "id" or "_id"; the EC2 "access" key // and any other param pass through untouched (FR-005). It is registered only on @@ -31,20 +31,19 @@ func uuidParamMiddleware(c *gin.Context) { if !isUUIDParam(p.Key) { continue } - id, err := uuid.Parse(p.Value) - if err != nil { + if uuid.Validate(p.Value) != nil { common.AbortWithError(c, common.NewIdentityNotFoundError(resourceNoun(c.FullPath(), p.Key), p.Value)) return } - c.Params[i].Value = id.String() } // No c.Next() needed: when a middleware returns without aborting, gin's engine // advances to the next handler on its own. } // canonicalizeUUIDParams normalizes only well-formed UUID path params. It is -// installed after authentication on management routes whose handlers perform -// authorization before rejecting malformed IDs. +// installed after authentication on the management group, before uuidParamMiddleware +// on its validation-only subgroup, so authorization-gated handlers can still reject +// malformed IDs after their authorization check. func canonicalizeUUIDParams(c *gin.Context) { for i := range c.Params { p := &c.Params[i] diff --git a/internal/keystone/uuid_middleware_test.go b/internal/keystone/uuid_middleware_test.go index 78d29aeb..22cc467f 100644 --- a/internal/keystone/uuid_middleware_test.go +++ b/internal/keystone/uuid_middleware_test.go @@ -216,13 +216,13 @@ func TestUUIDParamMiddlewareAbortsBeforeHandler(t *testing.T) { } // A well-formed but non-canonical id (dash-free, as rgw_compat_tenant_ids advertises, or -// upper-case) is rewritten to canonical form, so the handler's id comparison matches on -// SQLite, where ids are text. +// upper-case) is canonicalized by the management middleware before validation, so the +// handler's id comparison matches on SQLite, where ids are text. func TestUUIDParamMiddlewareCanonicalisesID(t *testing.T) { gin.SetMode(gin.TestMode) r := gin.New() g := r.Group("/v3") - g.Use(uuidParamMiddleware) + g.Use(canonicalizeUUIDParams, uuidParamMiddleware) var seen string g.GET("/projects/:id", func(c *gin.Context) { diff --git a/internal/metadata/service.go b/internal/metadata/service.go index f654f5fd..83cc42fe 100644 --- a/internal/metadata/service.go +++ b/internal/metadata/service.go @@ -3,7 +3,6 @@ package metadata import ( "context" "database/sql" - "encoding/json" "errors" "fmt" "net" @@ -371,14 +370,14 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { } type networkDataPort struct { id, mac, networkID string - fixedIPsJSON []byte + fixedIPs common.FixedIPs } ports := []networkDataPort{} networkIDs := []string{} seenNetworkIDs := map[string]bool{} for portRows.Next() { var port networkDataPort - if err := portRows.Scan(&port.id, &port.mac, &port.fixedIPsJSON, &port.networkID); err != nil { + if err := portRows.Scan(&port.id, &port.mac, &port.fixedIPs, &port.networkID); err != nil { log.Error().Err(err).Str("instance_id", instanceID).Msg("failed to scan network data port") portRows.Close() common.SendError(c, common.NewInternalServerError("failed to get network data")) @@ -416,19 +415,15 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { } for subnetRows.Next() { var id, cidr, gateway string - var dnsValue any - if err := subnetRows.Scan(&id, &cidr, &gateway, &dnsValue); err != nil { + var dnsServers []string + if err := subnetRows.Scan(&id, &cidr, &gateway, &dnsServers); err != nil { subnetRows.Close() log.Error().Err(err).Msg("failed to scan network data subnet") common.SendError(c, common.NewInternalServerError("failed to get network data")) return } - dnsServers, err := decodeNetworkDataDNS(dnsValue) - if err != nil { - subnetRows.Close() - log.Error().Err(err).Str("subnet_id", id).Msg("failed to decode subnet DNS nameservers") - common.SendError(c, common.NewInternalServerError("failed to get network data")) - return + if dnsServers == nil { + dnsServers = []string{} } subnets[id] = networkDataSubnet{cidr: cidr, gateway: gateway, dnsServers: dnsServers} } @@ -454,18 +449,7 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { "mtu": 1500, }) - var fixedIPs []struct { - IPAddress string `json:"ip_address"` - SubnetID string `json:"subnet_id"` - } - if len(strings.TrimSpace(string(port.fixedIPsJSON))) > 0 { - if err := json.Unmarshal(port.fixedIPsJSON, &fixedIPs); err != nil { - log.Error().Err(err).Str("port_id", port.id).Msg("failed to decode port fixed addresses") - common.SendError(c, common.NewInternalServerError("failed to get network data")) - return - } - } - for _, fixedIP := range fixedIPs { + for _, fixedIP := range port.fixedIPs { ip := fixedIP.IPAddress if ip == "" { continue @@ -521,49 +505,6 @@ func (svc *Service) GetNetworkDataJSON(c *gin.Context) { }) } -// decodeNetworkDataDNS accepts PostgreSQL text[] values and SQLite JSON text. -func decodeNetworkDataDNS(value any) ([]string, error) { - switch v := value.(type) { - case nil: - return []string{}, nil - case []string: - return v, nil - case []any: - values := make([]string, 0, len(v)) - for i, item := range v { - switch s := item.(type) { - case string: - values = append(values, s) - case []byte: - values = append(values, string(s)) - default: - return nil, fmt.Errorf("unsupported DNS nameserver value at index %d: %T", i, item) - } - } - return values, nil - case []byte: - var values []string - if err := json.Unmarshal(v, &values); err != nil { - return nil, fmt.Errorf("decode DNS nameservers: %w", err) - } - if values == nil { - values = []string{} - } - return values, nil - case string: - var values []string - if err := json.Unmarshal([]byte(v), &values); err != nil { - return nil, fmt.Errorf("decode DNS nameservers: %w", err) - } - if values == nil { - values = []string{} - } - return values, nil - default: - return nil, fmt.Errorf("unsupported DNS nameserver value %T", value) - } -} - // GetVendorDataJSON returns vendor-specific data (usually empty) func (svc *Service) GetVendorDataJSON(c *gin.Context) { c.JSON(http.StatusOK, gin.H{}) diff --git a/internal/neutron/network.go b/internal/neutron/network.go index cb731e58..dae2cfcd 100644 --- a/internal/neutron/network.go +++ b/internal/neutron/network.go @@ -571,10 +571,10 @@ func (svc *Service) CreateNetwork(c *gin.Context) { } // Making a network shared exposes it to every tenant in the deployment — - // an operator decision, like router:external below ( - // A member-created shared network was visible to, and attachable by, - // every project). Same shape as the external gate: only `true` is gated, - // matching upstream's explicit-set rule. + // an operator decision, like router:external below. A member-created + // shared network was visible to, and attachable by, every project. Same + // shape as the external gate: only `true` is gated, matching upstream's + // explicit-set rule. shared := false if req.Network.Shared != nil { if *req.Network.Shared && !common.HasRole(c, common.RoleAdmin) { diff --git a/internal/neutron/ports.go b/internal/neutron/ports.go index cab4e8e7..c5f35b8c 100644 --- a/internal/neutron/ports.go +++ b/internal/neutron/ports.go @@ -695,23 +695,11 @@ func usedSubnetAddresses(ctx context.Context, pool subnetQuerier, subnetID strin } defer rows.Close() for rows.Next() { - var fixedIPsJSON []byte - if err := rows.Scan(&fixedIPsJSON); err != nil { + var fixedIPs common.FixedIPs + if err := rows.Scan(&fixedIPs); err != nil { return nil, fmt.Errorf("scanning a port's fixed addresses: %w", err) } - trimmed := bytes.TrimSpace(fixedIPsJSON) - if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("null")) { - // Legacy subnet-less ports can store JSON null. It has no addresses. - continue - } - var entries []struct { - SubnetID string `json:"subnet_id"` - IPAddress string `json:"ip_address"` - } - if err := json.Unmarshal(trimmed, &entries); err != nil { - return nil, fmt.Errorf("decoding a port's fixed addresses: %w", err) - } - for _, entry := range entries { + for _, entry := range fixedIPs { if entry.SubnetID != subnetID { continue } diff --git a/internal/nova/handlers.go b/internal/nova/handlers.go index 386626fc..1a5d878c 100644 --- a/internal/nova/handlers.go +++ b/internal/nova/handlers.go @@ -1126,8 +1126,8 @@ func (svc *Service) CreateServer(c *gin.Context) { // Any failure from the first successful allocation onward (a later // network, the boot volume, libvirt itself) leaves the earlier ports // allocated for a server that will never run. Release them on the way - // out unless the boot completed (: they used to - // leak — still listed by the API, leases still reserved). + // out unless the boot completed: they used to leak — still listed by + // the API, leases still reserved. defer func() { if portsAllocated && !bootSucceeded && svc.neutronSvc != nil { rctx, rcancel := context.WithTimeout(context.Background(), 15*time.Second) diff --git a/internal/nova/interface_attach.go b/internal/nova/interface_attach.go index fa6fdf14..987cc50a 100644 --- a/internal/nova/interface_attach.go +++ b/internal/nova/interface_attach.go @@ -62,7 +62,7 @@ func (svc *Service) AttachInterface(c *gin.Context) { // fixedIPEntries is what the response reports as the attachment's // fixed_ips: the port's actual entries (port_id path) or the one just // allocated (net_id path). Empty means none — not a fabricated one. - var fixedIPEntries []gin.H + var fixedIPEntries common.FixedIPs // novaCreatedPort is true when this request had Nova create the port // (net_id path); false when it attached a port the tenant already owned // (port_id path). It decides the device_owner stamp below (N-8). @@ -79,11 +79,11 @@ func (svc *Service) AttachInterface(c *gin.Context) { // "correct" only because a failed Scan invalidates the lookup, even if some earlier columns were read. Any real database error on this lookup surfaced the same way: // a bogus 200 with an empty net_id and MAC. var deviceID string - var fixedIPsJSON []byte + var fixedIPs common.FixedIPs err := svc.pool.QueryRow(c.Request.Context(), "SELECT network_id, fixed_ips, mac_address, COALESCE(device_id::text, '') FROM ports WHERE id = $1 AND project_id = $2", portID, projectID, - ).Scan(&networkID, &fixedIPsJSON, &macAddress, &deviceID) + ).Scan(&networkID, &fixedIPs, &macAddress, &deviceID) if errors.Is(err, database.ErrNoRows) { common.SendError(c, common.NewNotFoundError("port")) @@ -105,7 +105,7 @@ func (svc *Service) AttachInterface(c *gin.Context) { // to be reported instead (round 2 of this review). A port with no // addresses reports an empty list, as upstream does — not a fabricated // {"ip_address": ""}. - fixedIPEntries = parseFixedIPEntries(fixedIPsJSON) + fixedIPEntries = fixedIPs } else if req.InterfaceAttachment.NetID != "" { // Create new port on the specified network networkID = req.InterfaceAttachment.NetID @@ -222,7 +222,7 @@ func (svc *Service) AttachInterface(c *gin.Context) { return err } } - fixedIPEntries = []gin.H{{"ip_address": fixedIP, "subnet_id": subnetID}} + fixedIPEntries = common.FixedIPs{{IPAddress: fixedIP, SubnetID: subnetID}} fixedIPsJSON, _ := json.Marshal(fixedIPEntries) _, err = tx.Exec(c.Request.Context(), ` INSERT INTO ports (id, network_id, project_id, name, mac_address, fixed_ips, device_id, device_owner, status, created_at, updated_at) @@ -297,7 +297,7 @@ func (svc *Service) AttachInterface(c *gin.Context) { // empty list for a port with none (round 3 of this review: this used to // fabricate exactly one {"ip_address": ""}). if fixedIPEntries == nil { - fixedIPEntries = []gin.H{} + fixedIPEntries = common.FixedIPs{} } c.JSON(http.StatusOK, gin.H{ "interfaceAttachment": gin.H{ @@ -311,24 +311,6 @@ func (svc *Service) AttachInterface(c *gin.Context) { }) } -// parseFixedIPEntries reads a port's fixed_ips JSONB into response entries. -// NULL or malformed JSON yields no entries — a port with no addresses reports -// none, never a fabricated one. -func parseFixedIPEntries(fixedIPsJSON []byte) []gin.H { - var entries []struct { - IPAddress string `json:"ip_address"` - SubnetID string `json:"subnet_id"` - } - if err := json.Unmarshal(fixedIPsJSON, &entries); err != nil { - return nil - } - out := make([]gin.H, 0, len(entries)) - for _, e := range entries { - out = append(out, gin.H{"ip_address": e.IPAddress, "subnet_id": e.SubnetID}) - } - return out -} - // DetachInterface detaches a network interface from an instance func (svc *Service) DetachInterface(c *gin.Context) { instanceID, ownerProject, authorized := resolveServerOr404(c, svc.pool, c.Param("id")) @@ -422,10 +404,10 @@ func (svc *Service) ListInterfaceAttachments(c *gin.Context) { var attachments []gin.H for rows.Next() { var id, portID, networkID, macAddress string - var fixedIPsJSON []byte + var fixedIPs common.FixedIPs var portState *string - if err := rows.Scan(&id, &portID, &networkID, &fixedIPsJSON, &macAddress, &portState); err != nil { + if err := rows.Scan(&id, &portID, &networkID, &fixedIPs, &macAddress, &portState); err != nil { log.Warn().Err(err).Str("operation", "ListInterfaceAttachments").Msg("failed to scan row") continue } @@ -435,15 +417,10 @@ func (svc *Service) ListInterfaceAttachments(c *gin.Context) { state = *portState } - // Parse fixed_ips from JSONB. An attachment whose port has no - // addresses is still an attachment: it is listed with an empty - // fixed_ips, exactly as the attach response reports it (round 3 of - // this review — the guard here used to make an attachment invisible - // in the list the moment it was created). - fixedIPs := []map[string]any{} - _ = json.Unmarshal(fixedIPsJSON, &fixedIPs) - if fixedIPs == nil { // JSON null in the column - fixedIPs = []map[string]any{} + // An attachment with no addresses remains visible and reports an empty + // fixed_ips list, matching the attach response. + if fixedIPs == nil { + fixedIPs = common.FixedIPs{} } attachments = append(attachments, gin.H{ "id": id, diff --git a/pkg/storage/image_store.go b/pkg/storage/image_store.go index ac6a93a9..dc65bc12 100644 --- a/pkg/storage/image_store.go +++ b/pkg/storage/image_store.go @@ -613,9 +613,9 @@ func (s *ImageStore) uploadLocalToS3(ctx context.Context, imageID string) error // Normalizing backends (local, local,rbd, local,s3) rewrite the upload — a // qcow2 becomes a raw base — so the checksum Glance advertises, downloads // serve and EnsureLocalBase verifies against must be taken from the stored -// object, not from the client's upload stream (: the -// stream hash made boot-from-image fail on any node without the local base, -// because MD5(raw replica) never equals MD5(original qcow2)). For S3-only and +// object, not from the client's upload stream: the stream hash made +// boot-from-image fail on any node without the local base, because +// MD5(raw replica) never equals MD5(original qcow2). For S3-only and // stub modes the stored bytes are the upload bytes, so the stream hash the // caller already computed is the stored checksum and this method is not // consulted. A future RBD-only backend should hash its own object instead. From 00554e182a5cc6d57c3ec71580af9adc86fb9be0 Mon Sep 17 00:00:00 2001 From: Senol Colak Date: Thu, 12 Nov 2026 15:49:25 +0100 Subject: [PATCH 26/26] fix(neutron): remove unused bytes import Signed-off-by: Senol Colak --- internal/neutron/ports.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/neutron/ports.go b/internal/neutron/ports.go index c5f35b8c..85d07dc4 100644 --- a/internal/neutron/ports.go +++ b/internal/neutron/ports.go @@ -1,7 +1,6 @@ package neutron import ( - "bytes" "context" "crypto/rand" "database/sql"