Skip to content

Commit 05ca1f8

Browse files
authored
chore: update access control policy & sql review policy (#47)
* feat: support policy data source * chore: support CRUD policy resource * chore: support config sql review rules * chore: update docs * chore: update version * chore: update docs * chore: update docs * chore: update docs * chore: optimize * chore: update * chore: update version * chore: add tests * chore: update access control policy & sql review policy
1 parent f66e7c5 commit 05ca1f8

File tree

13 files changed

+229
-101
lines changed

13 files changed

+229
-101
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.7-alpha.6
1+
0.0.7-alpha.7

api/common.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,23 @@ const (
99
// Deleted is the state for an removed row.
1010
Deleted State = "DELETED"
1111
)
12+
13+
// EngineType is the type of the instance engine.
14+
type EngineType string
15+
16+
const (
17+
// EngineTypeMySQL is the database type for MYSQL.
18+
EngineTypeMySQL EngineType = "MYSQL"
19+
// EngineTypePostgres is the database type for POSTGRES.
20+
EngineTypePostgres EngineType = "POSTGRES"
21+
// EngineTypeTiDB is the database type for TiDB.
22+
EngineTypeTiDB EngineType = "TIDB"
23+
// EngineTypeSnowflake is the database type for SNOWFLAKE.
24+
EngineTypeSnowflake EngineType = "SNOWFLAKE"
25+
// EngineTypeClickHouse is the database type for CLICKHOUSE.
26+
EngineTypeClickHouse EngineType = "CLICKHOUSE"
27+
// EngineTypeMongoDB is the database type for MongoDB.
28+
EngineTypeMongoDB EngineType = "MONGODB"
29+
// EngineTypeSQLite is the database type for SQLite.
30+
EngineTypeSQLite EngineType = "SQLITE"
31+
)

api/instance.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
package api
22

3-
// EngineType is the type of the instance engine.
4-
type EngineType string
5-
6-
const (
7-
// EngineTypeMySQL is the database type for MYSQL.
8-
EngineTypeMySQL EngineType = "MYSQL"
9-
// EngineTypePostgres is the database type for POSTGRES.
10-
EngineTypePostgres EngineType = "POSTGRES"
11-
// EngineTypeTiDB is the database type for TiDB.
12-
EngineTypeTiDB EngineType = "TIDB"
13-
// EngineTypeSnowflake is the database type for SNOWFLAKE.
14-
EngineTypeSnowflake EngineType = "SNOWFLAKE"
15-
// EngineTypeClickHouse is the database type for CLICKHOUSE.
16-
EngineTypeClickHouse EngineType = "CLICKHOUSE"
17-
// EngineTypeMongoDB is the database type for MongoDB.
18-
EngineTypeMongoDB EngineType = "MONGODB"
19-
// EngineTypeSQLite is the database type for SQLite.
20-
EngineTypeSQLite EngineType = "SQLITE"
21-
)
22-
233
// InstanceMessage is the API message for an instance.
244
type InstanceMessage struct {
255
UID string `json:"uid"`

api/policy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ type PolicyMessage struct {
128128
type PolicyPatchMessage struct {
129129
InheritFromParent *bool `json:"inheritFromParent"`
130130
Type PolicyType `json:"type"`
131+
Enforce *bool `json:"enforce"`
131132

132133
// The policy payload
133134
DeploymentApprovalPolicy *DeploymentApprovalPolicy `json:"deploymentApprovalPolicy"`

api/sql_review.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,6 @@ type NumberTypeRulePayload struct {
156156
type SQLReviewRule struct {
157157
Type SQLReviewRuleType `json:"type"`
158158
Level SQLReviewRuleLevel `json:"level"`
159+
Engine EngineType `json:"engine"`
159160
Payload string `json:"payload"`
160161
}

docs/resources/policy.md

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ Access Control Policy is the policy configuration for database access control. I
163163
Must set the `access_control_policy` if the policy type if `ACCESS_CONTROL`. It contains following attributes:
164164

165165
- `disallow_rules` (List of Object) The object contains following attribute:
166-
- `full_database` (Boolean) will apply to the full database.
166+
- `all_databases` (Boolean) will apply to all databases.
167167

168168
For example:
169169

@@ -183,7 +183,7 @@ resource "bytebase_policy" "access_control" {
183183
184184
access_control_policy {
185185
disallow_rules {
186-
full_database = true
186+
all_databases = true
187187
}
188188
}
189189
}
@@ -194,12 +194,6 @@ resource "bytebase_policy" "access_control" {
194194
environment = bytebase_environment.prod.resource_id
195195
instance = "<instance resource id for the database>"
196196
database = "employee"
197-
198-
access_control_policy {
199-
disallow_rules {
200-
full_database = true
201-
}
202-
}
203197
}
204198
```
205199

@@ -219,6 +213,10 @@ The rule should a object contains:
219213
- `ERROR`
220214
- `WARNING`
221215
- `DISABLED`
216+
- `engine` (String) The database engine for the rule. Should be one of:
217+
- `MYSQL`
218+
- `POSTGRES`
219+
- `TIDB`
222220
- `payload` (Object) The payload for SQL review rule.
223221

224222
Please check the doc for details: https://www.bytebase.com/docs/sql-review/review-rules/supported-rules
@@ -233,27 +231,31 @@ resource "bytebase_policy" "sql_review" {
233231
sql_review_policy {
234232
title = "SQL Review Policy for Test environment"
235233
rules {
236-
type = "statement.select.no-select-all"
237-
level = "ERROR"
234+
type = "statement.select.no-select-all"
235+
level = "ERROR"
236+
engine = "MYSQL"
238237
}
239238
rules {
240-
type = "naming.table"
241-
level = "ERROR"
239+
type = "naming.table"
240+
level = "ERROR"
241+
engine = "POSTGRES"
242242
payload {
243243
max_length = 99
244244
format = "^[a-z]+$"
245245
}
246246
}
247247
rules {
248-
type = "column.required"
249-
level = "WARNING"
248+
type = "column.required"
249+
level = "WARNING"
250+
engine = "TIDB"
250251
payload {
251252
list = ["id", "created_ts", "updated_ts"]
252253
}
253254
}
254255
rules {
255-
type = "column.auto-increment-initial-value"
256-
level = "DISABLED"
256+
type = "column.auto-increment-initial-value"
257+
level = "DISABLED"
258+
engine = "MYSQL"
257259
payload {
258260
number = 1
259261
}
@@ -288,8 +290,9 @@ resource "bytebase_policy" "sql_review" {
288290
sql_review_policy {
289291
title = "SQL Review Policy for Test environment"
290292
rules {
291-
type = "naming.table"
292-
level = "ERROR"
293+
type = "naming.table"
294+
level = "ERROR"
295+
engine = "MYSQL"
293296
payload {
294297
max_length = 99
295298
format = "^[a-z]+$"
@@ -316,8 +319,9 @@ resource "bytebase_policy" "sql_review" {
316319
sql_review_policy {
317320
title = "SQL Review Policy for Test environment"
318321
rules {
319-
type = "column.comment"
320-
level = "WARNING"
322+
type = "column.comment"
323+
level = "WARNING"
324+
engine = "MYSQL"
321325
payload {
322326
max_length = 99
323327
required = true
@@ -350,8 +354,9 @@ resource "bytebase_policy" "sql_review" {
350354
sql_review_policy {
351355
title = "SQL Review Policy for Test environment"
352356
rules {
353-
type = "column.auto-increment-initial-value"
354-
level = "WARNING"
357+
type = "column.auto-increment-initial-value"
358+
level = "WARNING"
359+
engine = "MYSQL"
355360
payload {
356361
number = 1
357362
}
@@ -382,8 +387,9 @@ resource "bytebase_policy" "sql_review" {
382387
sql_review_policy {
383388
title = "SQL Review Policy for Test environment"
384389
rules {
385-
type = "column.required"
386-
level = "WARNING"
390+
type = "column.required"
391+
level = "WARNING"
392+
engine = "MYSQL"
387393
payload {
388394
list = ["id", "created_ts", "updated_ts"]
389395
}

examples/environments/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
terraform {
33
required_providers {
44
bytebase = {
5-
version = "0.0.7-alpha.6"
5+
version = "0.0.7-alpha.7"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

examples/instances/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
terraform {
33
required_providers {
44
bytebase = {
5-
version = "0.0.7-alpha.6"
5+
version = "0.0.7-alpha.7"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

examples/policies/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
terraform {
33
required_providers {
44
bytebase = {
5-
version = "0.0.7-alpha.6"
5+
version = "0.0.7-alpha.7"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

examples/roles/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
bytebase = {
4-
version = "0.0.7-alpha.6"
4+
version = "0.0.7-alpha.7"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

examples/setup/main.tf

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
bytebase = {
4-
version = "0.0.7-alpha.6"
4+
version = "0.0.7-alpha.7"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}
@@ -149,47 +149,54 @@ resource "bytebase_policy" "sql_review" {
149149
sql_review_policy {
150150
title = "SQL Review Policy for Test environment"
151151
rules {
152-
type = "statement.select.no-select-all"
153-
level = "ERROR"
152+
type = "statement.select.no-select-all"
153+
level = "ERROR"
154+
engine = "MYSQL"
154155
}
155156
rules {
156-
type = "statement.where.no-leading-wildcard-like"
157-
level = "DISABLED"
157+
type = "statement.where.no-leading-wildcard-like"
158+
level = "DISABLED"
159+
engine = "MYSQL"
158160
}
159161
rules {
160-
type = "column.comment"
161-
level = "ERROR"
162+
type = "column.comment"
163+
level = "ERROR"
164+
engine = "MYSQL"
162165
payload {
163166
max_length = 99
164167
required = true
165168
}
166169
}
167170
rules {
168-
type = "table.comment"
169-
level = "WARNING"
171+
type = "table.comment"
172+
level = "WARNING"
173+
engine = "MYSQL"
170174
payload {
171175
max_length = 30
172176
required = false
173177
}
174178
}
175179
rules {
176-
type = "naming.table"
177-
level = "ERROR"
180+
type = "naming.table"
181+
level = "ERROR"
182+
engine = "MYSQL"
178183
payload {
179184
max_length = 99
180185
format = "^[a-z]+$"
181186
}
182187
}
183188
rules {
184-
type = "column.required"
185-
level = "WARNING"
189+
type = "column.required"
190+
level = "WARNING"
191+
engine = "MYSQL"
186192
payload {
187193
list = ["id", "created_ts", "updated_ts"]
188194
}
189195
}
190196
rules {
191-
type = "column.auto-increment-initial-value"
192-
level = "WARNING"
197+
type = "column.auto-increment-initial-value"
198+
level = "WARNING"
199+
engine = "MYSQL"
193200
payload {
194201
number = 1
195202
}

provider/data_source_policy.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func flattenAccessControlPolicy(p *api.AccessControlPolicy) []interface{} {
239239
rules := []interface{}{}
240240
for _, rule := range p.DisallowRules {
241241
raw := map[string]interface{}{}
242-
raw["full_database"] = rule.FullDatabase
242+
raw["all_databases"] = rule.FullDatabase
243243
rules = append(rules, raw)
244244
}
245245
policy := map[string]interface{}{
@@ -254,6 +254,7 @@ func flattenSQLReviewPolicy(p *api.SQLReviewPolicy) ([]interface{}, error) {
254254
raw := map[string]interface{}{}
255255
raw["type"] = rule.Type
256256
raw["level"] = rule.Level
257+
raw["engine"] = rule.Engine
257258

258259
payload, err := unamrshalSQLReviewRulePayload(rule.Type, rule.Payload)
259260
if err != nil {
@@ -425,7 +426,7 @@ func getAccessControlPolicy(computed bool) *schema.Schema {
425426
Type: schema.TypeList,
426427
Elem: &schema.Resource{
427428
Schema: map[string]*schema.Schema{
428-
"full_database": {
429+
"all_databases": {
429430
Type: schema.TypeBool,
430431
Computed: computed,
431432
Optional: true,
@@ -525,6 +526,17 @@ func getSQLReviewPolicy(computed bool) *schema.Schema {
525526
string(api.SQLReviewRuleLevelDisabled),
526527
}, false),
527528
},
529+
"engine": {
530+
Type: schema.TypeString,
531+
Computed: computed,
532+
Optional: true,
533+
ValidateFunc: validation.StringInSlice([]string{
534+
string(api.EngineTypeMySQL),
535+
string(api.EngineTypePostgres),
536+
string(api.EngineTypeTiDB),
537+
}, false),
538+
Description: "The engine for this rule.",
539+
},
528540
"payload": {
529541
Computed: computed,
530542
Type: schema.TypeList,

0 commit comments

Comments
 (0)