Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: cap the tenant IDs that can be allocated via create_tenant #101928

Merged
merged 2 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/tenant_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,8 @@ query I
SELECT crdb_internal.create_tenant('{"name":"tenant-number-ten", "if_not_exists": true}'::JSONB)
----
NULL

subtest avoid_too_large_ids

query error tenant ID 10000000000 out of range
SELECT crdb_internal.create_tenant(10000000000)
14 changes: 12 additions & 2 deletions pkg/sql/tenant_creation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"context"
gojson "encoding/json"
"fmt"
"math"
"strings"
"time"

Expand Down Expand Up @@ -62,6 +63,14 @@ func (p *planner) CreateTenant(
}
}

if ctcfg.ID != nil && *ctcfg.ID > math.MaxUint32 {
// Tenant creation via this interface (which includes
// crdb_internal.create_tenant) should be prevented from gobbling
// up the entire tenant ID space by asking for too large values.
// Otherwise, CREATE TENANT will not be possible any more.
return tid, pgerror.Newf(pgcode.ProgramLimitExceeded, "tenant ID %d out of range", *ctcfg.ID)
}

configTemplate := mtinfopb.TenantInfoWithUsage{}

return p.createTenantInternal(ctx, ctcfg, &configTemplate)
Expand Down Expand Up @@ -608,6 +617,8 @@ HAVING ($1 = '' OR NOT EXISTS (SELECT 1 FROM system.tenants t WHERE t.name = $1)
return roachpb.MakeTenantID(nextID)
}

var tenantIDSequenceFQN = tree.MakeTableNameWithSchema(catconstants.SystemDatabaseName, tree.PublicSchemaName, tree.Name(catconstants.TenantIDSequenceTableName))

// getTenantIDSequenceDesc retrieves a leased descriptor for the
// sequence system.tenant_id_seq.
func getTenantIDSequenceDesc(ctx context.Context, txn isql.Txn) (catalog.TableDescriptor, error) {
Expand All @@ -626,9 +637,8 @@ func getTenantIDSequenceDesc(ctx context.Context, txn isql.Txn) (catalog.TableDe
coll := itxn.Descriptors()

// Full name of the sequence.
tn := tree.MakeTableNameWithSchema(catconstants.SystemDatabaseName, tree.PublicSchemaName, tree.Name(catconstants.TenantIDSequenceTableName))
// Look up the sequence by name with lease.
_, desc, err := descs.PrefixAndTable(ctx, coll.ByNameWithLeased(txn.KV()).Get(), &tn)
_, desc, err := descs.PrefixAndTable(ctx, coll.ByNameWithLeased(txn.KV()).Get(), &tenantIDSequenceFQN)
if err != nil {
return nil, err
}
Expand Down