forked from cockroachdb/cockroach
/
system.go
181 lines (156 loc) · 5.01 KB
/
system.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
//
// Author: Marc Berhault (marc@cockroachlabs.com)
package sql
import (
"github.com/cockroachdb/cockroach/keys"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/security"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/sql/privilege"
"github.com/cockroachdb/cockroach/util/log"
)
const (
// sql CREATE commands and full schema for each system table.
namespaceTableSchema = `
CREATE TABLE system.namespace (
parentID INT,
name CHAR,
id INT,
PRIMARY KEY (parentID, name)
);`
descriptorTableSchema = `
CREATE TABLE system.descriptor (
id INT PRIMARY KEY,
descriptor BLOB
);`
usersTableSchema = `
CREATE TABLE system.users (
username CHAR PRIMARY KEY,
hashedPassword BLOB
);`
// Zone settings per DB/Table.
zonesTableSchema = `
CREATE TABLE system.zones (
id INT PRIMARY KEY,
config BLOB
);`
)
var (
// SystemDB is the descriptor for the system database.
SystemDB = DatabaseDescriptor{
Name: "system",
ID: keys.SystemDatabaseID,
// Assign max privileges to root user.
Privileges: NewPrivilegeDescriptor(security.RootUser,
SystemAllowedPrivileges[keys.SystemDatabaseID]),
}
// NamespaceTable is the descriptor for the namespace table.
NamespaceTable = createSystemTable(keys.NamespaceTableID, namespaceTableSchema)
// DescriptorTable is the descriptor for the descriptor table.
DescriptorTable = createSystemTable(keys.DescriptorTableID, descriptorTableSchema)
// UsersTable is the descriptor for the users table.
UsersTable = createSystemTable(keys.UsersTableID, usersTableSchema)
// ZonesTable is the descriptor for the zones table.
ZonesTable = createSystemTable(keys.ZonesTableID, zonesTableSchema)
// SystemAllowedPrivileges describes the privileges allowed for each
// system object. No user may have more than those privileges, and
// the root user must have exactly those privileges.
// CREATE|DROP|ALL should always be denied.
SystemAllowedPrivileges = map[ID]privilege.List{
keys.SystemDatabaseID: privilege.ReadData,
keys.NamespaceTableID: privilege.ReadData,
keys.DescriptorTableID: privilege.ReadData,
keys.UsersTableID: privilege.ReadWriteData,
keys.ZonesTableID: privilege.ReadWriteData,
}
// NumUsedSystemIDs is only used in tests that need to know the
// number of system objects created at initialization.
// It gets automatically set to "number of created system tables"
// + 1 (for system database).
NumUsedSystemIDs = 1
)
func createSystemTable(id ID, cmd string) TableDescriptor {
stmts, err := parser.ParseTraditional(cmd)
if err != nil {
log.Fatal(err)
}
desc, err := makeTableDesc(stmts[0].(*parser.CreateTable), keys.SystemDatabaseID)
if err != nil {
log.Fatal(err)
}
// Assign max privileges to root user.
desc.Privileges = NewPrivilegeDescriptor(security.RootUser,
SystemAllowedPrivileges[id])
desc.ID = id
if err := desc.AllocateIDs(); err != nil {
log.Fatal(err)
}
NumUsedSystemIDs++
return desc
}
// GetInitialSystemValues returns a list of key/value pairs.
// They are written at cluster bootstrap time (see storage/node.go:BootstrapCLuster).
func GetInitialSystemValues() []roachpb.KeyValue {
systemData := []struct {
parentID ID
desc descriptorProto
}{
{keys.RootNamespaceID, &SystemDB},
{SystemDB.ID, &NamespaceTable},
{SystemDB.ID, &DescriptorTable},
{SystemDB.ID, &UsersTable},
{SystemDB.ID, &ZonesTable},
}
// Initial kv pairs:
// - ID generator
// - 2 per table/database
numEntries := 1 + len(systemData)*2
ret := make([]roachpb.KeyValue, numEntries, numEntries)
i := 0
// Descriptor ID generator.
value := roachpb.Value{}
value.SetInt(int64(keys.MaxReservedDescID + 1))
ret[i] = roachpb.KeyValue{
Key: keys.DescIDGenerator,
Value: value,
}
i++
// System database and tables.
for _, d := range systemData {
value = roachpb.Value{}
value.SetInt(int64(d.desc.GetID()))
ret[i] = roachpb.KeyValue{
Key: MakeNameMetadataKey(d.parentID, d.desc.GetName()),
Value: value,
}
i++
value = roachpb.Value{}
if err := value.SetProto(d.desc); err != nil {
log.Fatalf("could not marshal %v", d.desc)
}
ret[i] = roachpb.KeyValue{
Key: MakeDescMetadataKey(d.desc.GetID()),
Value: value,
}
i++
}
return ret
}
// IsSystemID returns true if this ID is reserved for system objects.
func IsSystemID(id ID) bool {
return id > 0 && id <= keys.MaxReservedDescID
}