fix(onprem): the cloud admin keeps tenant_status trial

Only on-prem passes the workspace's status to Keycloak for its first admin;
a cloud demo workspace with an admin wrote demo instead of trial (pinned by
a test).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit d8cb363d5c)
This commit is contained in:
Sharang Parnerkar committed 2026-11-20 17:08:48 +01:00
1 parent 90ae11ae42
commit 30cd577a33
4 files changed
+32 -2

No files matched your search

+1 -1
View File
@@ -56,7 +56,7 @@ func (s *Server) adminReprovision(w http.ResponseWriter, r *http.Request) {
_, lerr := s.Keycloak.ListOrgMembers(ctx, t.Slug)
switch {
case errors.Is(lerr, keycloak.ErrOrgNotFound):
res, err := s.Keycloak.CreateOrgAndInvite(ctx, keycloak.InviteInput{TenantID: t.ID, Slug: t.Slug, Name: t.Name, AdminEmail: email, TenantStatus: t.Status})
res, err := s.Keycloak.CreateOrgAndInvite(ctx, keycloak.InviteInput{TenantID: t.ID, Slug: t.Slug, Name: t.Name, AdminEmail: email, TenantStatus: s.onPremStatus(t)})
if err != nil {
if errors.Is(err, keycloak.ErrUserConflict) {
writeError(w, http.StatusConflict, "email_taken", "an account with this e-mail exists outside this tenant")
+10
View File
@@ -24,6 +24,16 @@ type kcInvite struct {
MailErr error
}
// onPremStatus is the tenant_status a new workspace admin gets in Keycloak:
// the workspace's own on-prem; empty in the cloud, where the adapter keeps
// writing "trial" as before.
func (s *Server) onPremStatus(t *store.Tenant) string {
if s.onPrem() {
return t.Status
}
return ""
}
func (s *Server) provisionKeycloak(ctx context.Context, t *store.Tenant, in keycloak.InviteInput) (kcInvite, error) {
if in.AdminEmail == "" {
// Skip silently — caller chose not to invite anyone yet (sales-led
+20
View File
@@ -364,3 +364,23 @@ type createResp struct {
Tenant *store.Tenant `json:"tenant"`
InviteURL string `json:"invite_url"`
}
// The cloud admin keeps the adapter's default tenant_status ("trial"); only
// on-prem passes the workspace's own.
func TestTenantStatusForTheAdminOnlyOnPrem(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
if resp, body := h.do("POST", "/v1/tenants", map[string]any{"slug": "cloud-demo", "name": "Demo", "kind": "demo", "admin_email": "demo@cloud.test"}); resp.StatusCode != http.StatusCreated {
t.Fatalf("cloud create = %d %s", resp.StatusCode, body)
}
if c := h.kcMock.Created; len(c) != 1 || c[0].TenantStatus != "" {
t.Fatalf("cloud: %+v", c)
}
onPremHarness(h, nil)
if resp, body := h.do("POST", "/v1/tenants", map[string]any{"slug": "werk", "name": "Werk", "admin_email": "anna.admin@onprem.test"}); resp.StatusCode != http.StatusCreated {
t.Fatalf("on-prem create = %d %s", resp.StatusCode, body)
}
if c := h.kcMock.Created; len(c) != 2 || c[1].TenantStatus != store.StatusActive {
t.Errorf("on-prem: %+v", c)
}
})
}
+1 -1
View File
@@ -165,7 +165,7 @@ func (s *Server) createTenantCore(ctx context.Context, r *http.Request, in creat
// tenant back — the operator can resend the invite via the KC admin UI.
// We emit an audit event regardless so the failure is traceable.
invite, kcErr := s.provisionKeycloak(ctx, t, keycloak.InviteInput{
TenantStatus: t.Status,
TenantStatus: s.onPremStatus(t),
AdminEmail: in.AdminEmail, AdminName: in.AdminName,
AdminFirst: in.adminFirst, AdminLast: in.adminLast, EmailVerified: in.emailVerified,
})