Skip to content

Commit 797ccd0

Browse files
committed
feat: manage TargetedInstanceCreation via TenantAccount siteCapabilities
Move privileged tenant capability configuration to TenantAccount.config and TenantSite.config, expose it on TenantAccount GET/PATCH, and enforce effective site-scoped checks on instance, machine, and VPC actions while deprecating the legacy Tenant.capabilities field.
1 parent d2408d5 commit 797ccd0

55 files changed

Lines changed: 1455 additions & 189 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

rest-api/api/pkg/api/handler/instance.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,12 @@ func (cih CreateInstanceHandler) Handle(c echo.Context) error {
892892

893893
// Begin validating Machine ID
894894
if apiRequest.MachineID != nil {
895-
if tenant.Config == nil || !tenant.Config.TargetedInstanceCreation {
895+
enabledForSite, derr := common.EffectiveTargetedInstanceCreation(ctx, tx, cih.dbSession, tenant, site.ID)
896+
if derr != nil {
897+
logger.Error().Err(derr).Msg("error checking effective targeted instance creation for Site")
898+
return cutil.NewAPIError(http.StatusInternalServerError, "Failed to verify capability for Site", nil)
899+
}
900+
if !enabledForSite {
896901
logger.Warn().Msg("tenant does not have capability to create instances from specific machine")
897902
return cutil.NewAPIError(http.StatusForbidden, "Tenant does not have capability to create Instances using specific Machine ID", nil)
898903
}
@@ -4900,8 +4905,13 @@ func (dih DeleteInstanceHandler) Handle(c echo.Context) error {
49004905
// capability. By the time `ToProto` runs the request is safe
49014906
// to trust.
49024907
if apiRequest.IsRepairTenant != nil && *apiRequest.IsRepairTenant {
4903-
if instance.Tenant.Config == nil || !instance.Tenant.Config.TargetedInstanceCreation {
4904-
logger.Warn().Msg("tenant does not have capability to set IsRepairTenant")
4908+
enabledForSite, derr := common.EffectiveTargetedInstanceCreation(ctx, tx, dih.dbSession, instance.Tenant, instance.SiteID)
4909+
if derr != nil {
4910+
logger.Error().Err(derr).Msg("error checking effective targeted instance creation for Instance's Site")
4911+
return cutil.NewAPIError(http.StatusInternalServerError, "Failed to verify capability for Instance's Site", nil)
4912+
}
4913+
if !enabledForSite {
4914+
logger.Warn().Msg("tenant does not have capability to set IsRepairTenant for the Instance's Site")
49054915
return cutil.NewAPIError(http.StatusForbidden, "Tenant does not have capability to set IsRepairTenant", nil)
49064916
}
49074917
}

rest-api/api/pkg/api/handler/machine.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (gamh GetAllMachineHandler) Handle(c echo.Context) error {
242242

243243
if tenant != nil {
244244
// Check if Tenant is privileged
245-
if tenant.Config.TargetedInstanceCreation {
245+
if common.TenantHasTargetedInstanceCreation(tenant) {
246246
// Get IDs for all Providers the privileged Tenant has an account with
247247
taDAO := cdbm.NewTenantAccountDAO(gamh.dbSession)
248248
tas, _, serr := taDAO.GetAll(ctx, nil, cdbm.TenantAccountFilterInput{
@@ -615,7 +615,7 @@ func (gmh GetMachineHandler) Handle(c echo.Context) error {
615615
isProviderOrPrivilegedTenant = true
616616
} else if tenant != nil {
617617
// Check if Tenant is privileged
618-
if tenant.Config.TargetedInstanceCreation {
618+
if common.TenantHasTargetedInstanceCreation(tenant) {
619619
// Check if privileged Tenant has an account with Infrastructure Provider
620620
taDAO := cdbm.NewTenantAccountDAO(gmh.dbSession)
621621
_, taCount, serr := taDAO.GetAll(ctx, nil, cdbm.TenantAccountFilterInput{
@@ -759,22 +759,13 @@ func (umh UpdateMachineHandler) Handle(c echo.Context) error {
759759
// Validate if Tenant is allowed to update Machine
760760
if tenant != nil {
761761
// Check if Tenant is privileged
762-
if tenant.Config.TargetedInstanceCreation {
763-
// Check if privileged Tenant has an account with Infrastructure Provider
764-
taDAO := cdbm.NewTenantAccountDAO(umh.dbSession)
765-
_, taCount, serr := taDAO.GetAll(ctx, nil, cdbm.TenantAccountFilterInput{
766-
InfrastructureProviderID: &machine.InfrastructureProviderID,
767-
TenantIDs: []uuid.UUID{tenant.ID},
768-
}, cdbp.PageInput{}, []string{})
769-
if serr != nil {
770-
logger.Error().Err(serr).Msg("error retrieving Tenant Accounts for org's Tenant")
771-
return cutil.NewAPIErrorResponse(c, http.StatusInternalServerError, "Error retrieving Tenant Accounts for org's Tenant, DB error", nil)
772-
}
773-
if taCount == 0 {
774-
logger.Error().Msg("privileged Tenant doesn't have an account with Infrastructure Provider")
775-
} else {
776-
isPrivilegedTenant = true
777-
}
762+
enabledForSite, serr := common.EffectiveTargetedInstanceCreation(ctx, nil, umh.dbSession, tenant, machine.SiteID)
763+
if serr != nil {
764+
logger.Error().Err(serr).Msg("error checking effective targeted instance creation for Machine's Site")
765+
return cutil.NewAPIErrorResponse(c, http.StatusInternalServerError, "Error verifying capability for Machine's Site, DB error", nil)
766+
}
767+
if enabledForSite {
768+
isPrivilegedTenant = true
778769
}
779770
}
780771

rest-api/api/pkg/api/handler/serviceaccount.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,11 @@ func (gcsah GetCurrentServiceAccountHandler) Handle(c echo.Context) error {
9797
}
9898

9999
if len(tns) == 0 {
100-
// Create Tenant
101-
tenantConfig := &cdbm.TenantConfig{
102-
// Enable targeted instance creation for org
103-
TargetedInstanceCreation: true,
104-
}
100+
// Create Tenant without legacy capability flags; capability is managed on TenantAccount.
105101
tn, serr = tnDAO.Create(ctx, nil, cdbm.TenantCreateInput{
106102
Name: org,
107103
Org: org,
108104
OrgDisplayName: cutil.GetPtr(org),
109-
Config: tenantConfig,
110105
CreatedBy: dbUser.ID,
111106
})
112107
if serr != nil {
@@ -115,23 +110,10 @@ func (gcsah GetCurrentServiceAccountHandler) Handle(c echo.Context) error {
115110
}
116111
} else {
117112
tn = &tns[0]
118-
119-
// Check if Tenant has targeted instance creation enabled
120-
if !tn.Config.TargetedInstanceCreation {
121-
// Update Tenant to enable targeted instance creation
122-
tenantConfig := tn.Config
123-
tenantConfig.TargetedInstanceCreation = true
124-
tn, serr = tnDAO.Update(ctx, nil, cdbm.TenantUpdateInput{
125-
TenantID: tn.ID,
126-
Config: tenantConfig,
127-
})
128-
if serr != nil {
129-
logger.Error().Err(serr).Msg("error updating Tenant DB entity")
130-
return cutil.NewAPIErrorResponse(c, http.StatusInternalServerError, "Failed to update Tenant capabilities for org, DB error", nil)
131-
}
132-
}
133113
}
134114

115+
accountConfig := &cdbm.TenantAccountConfig{TargetedInstanceCreation: true}
116+
135117
// Create Tenant Account if it doesn't exist
136118
taDAO := cdbm.NewTenantAccountDAO(gcsah.dbSession)
137119
tas, _, err := taDAO.GetAll(ctx, nil, cdbm.TenantAccountFilterInput{
@@ -153,6 +135,7 @@ func (gcsah GetCurrentServiceAccountHandler) Handle(c echo.Context) error {
153135
InfrastructureProviderID: ip.ID,
154136
InfrastructureProviderOrg: ip.Org,
155137
Status: cdbm.TenantAccountStatusReady,
138+
Config: accountConfig,
156139
CreatedBy: dbUser.ID,
157140
})
158141
if derr != nil {
@@ -175,6 +158,17 @@ func (gcsah GetCurrentServiceAccountHandler) Handle(c echo.Context) error {
175158
if serr != nil {
176159
return common.HandleTxError(c, logger, serr, "Failed to create Tenant Account, DB transaction error")
177160
}
161+
} else if !tas[0].Config.TargetedInstanceCreation {
162+
_, serr = taDAO.Update(ctx, nil, cdbm.TenantAccountUpdateInput{
163+
TenantAccountID: tas[0].ID,
164+
Config: &cdbm.TenantAccountConfigUpdateInput{
165+
TargetedInstanceCreation: cutil.GetPtr(true),
166+
},
167+
})
168+
if serr != nil {
169+
logger.Error().Err(serr).Msg("error updating Tenant Account capabilities for org")
170+
return cutil.NewAPIErrorResponse(c, http.StatusInternalServerError, "Failed to update Tenant Account capabilities for org, DB error", nil)
171+
}
178172
}
179173

180174
// Create response

0 commit comments

Comments
 (0)