Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: instance permissions definition (#9431)

p0ny8be73e879112
diff --git a/.golangci.yaml b/.golangci.yaml
index 4a338f7942..24f6116710 100644
--- a/.golangci.yaml
+++ b/.golangci.yaml
@@ -17,6 +17,7 @@ linters:
- forbidigo
- mirror
- bodyclose
+ - exhaustive
# golangci-lint run --exclude="Rollback,logger.Sync,pgInstance.Stop"
issues:
@@ -78,6 +79,9 @@ linters-settings:
disabled: true
- name: early-return
disabled: true
+ - name: comment-spacings
+ arguments:
+ - exhaustive
gocritic:
disabled-checks:
- ifElseChain
@@ -90,6 +94,8 @@ linters-settings:
disable:
- fieldalignment
- shadow
+ exhaustive:
+ explicit-exhaustive-switch: true
forbidigo:
forbid:
- 'fmt\.Errorf(# Please use errors\.Wrap\|Wrapf\|Errorf instead)?'
diff --git a/backend/component/iam/acl.yaml b/backend/component/iam/acl.yaml
new file mode 100644
index 0000000000..1941f00611
--- /dev/null
+++ b/backend/component/iam/acl.yaml
@@ -0,0 +1,33 @@
+roles:
+ - name: roles/workspaceAdmin
+ permissions:
+ - bb.instance.list
+ - bb.instance.get
+ - bb.instance.create
+ - bb.instance.update
+ - bb.instance.delete
+ - bb.instance.undelete
+ - bb.instance.sync
+ - name: roles/workspaceDBA
+ permissions:
+ - bb.instance.list
+ - bb.instance.get
+ - bb.instance.create
+ - bb.instance.update
+ - bb.instance.delete
+ - bb.instance.undelete
+ - bb.instance.sync
+ - name: roles/workspaceMember
+ permissions: []
+ - name: roles/projectOwner
+ permissions: []
+ - name: roles/projectDeveloper
+ permissions: []
+ - name: roles/projectQuerier
+ permissions: []
+ - name: roles/projectExporter
+ permissions: []
+ - name: roles/projectReleaser
+ permissions: []
+ - name: roles/projectViewer
+ permissions: []
diff --git a/backend/component/iam/manager.go b/backend/component/iam/manager.go
new file mode 100644
index 0000000000..4c008f3871
--- /dev/null
+++ b/backend/component/iam/manager.go
@@ -0,0 +1,42 @@
+package iam
+
+import (
+ _ "embed"
+
+ "github.com/pkg/errors"
+ "gopkg.in/yaml.v3"
+)
+
+//go:embed acl.yaml
+var aclYaml []byte
+
+type acl struct {
+ Roles []Role `yaml:"roles"`
+}
+
+type Role struct {
+ Name string `yaml:"name"`
+ Permissions []string `yaml:"permissions"`
+}
+
+type Manager struct {
+ roles map[string][]Permission
+}
+
+func NewManager() (*Manager, error) {
+ predefinedACL := new(acl)
+ if err := yaml.Unmarshal(aclYaml, predefinedACL); err != nil {
+ return nil, errors.Wrapf(err, "failed to unmarshal predefined acl")
+ }
+
+ roles := make(map[string][]Permission)
+ for _, binding := range predefinedACL.Roles {
+ for _, permission := range binding.Permissions {
+ roles[binding.Name] = append(roles[binding.Name], Permission(permission))
+ }
+ }
+
+ return &Manager{
+ roles: roles,
+ }, nil
+}
diff --git a/backend/component/iam/permission.go b/backend/component/iam/permission.go
new file mode 100644
index 0000000000..8b339a4949
--- /dev/null
+++ b/backend/component/iam/permission.go
@@ -0,0 +1,13 @@
+package iam
+
+type Permission string
+
+const (
+ PermissionInstanceList Permission = "bb.instance.list"
+ PermissionInstanceGet Permission = "bb.instance.get"
+ PermissionInstanceCreate Permission = "bb.instance.create"
+ PermissionInstanceUpdate Permission = "bb.instance.update"
+ PermissionInstanceDelete Permission = "bb.instance.delete"
+ PermissionInstanceUndelete Permission = "bb.instance.undelete"
+ PermissionInstanceSync Permission = "bb.instance.sync"
+)
diff --git a/backend/component/iam/permission_test.go b/backend/component/iam/permission_test.go
new file mode 100644
index 0000000000..4da25c7f07
--- /dev/null
+++ b/backend/component/iam/permission_test.go
@@ -0,0 +1,44 @@
+package iam
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+// Test that every permission in the yaml is also defined in permission.go as a constant.
+func TestPermissionExists(t *testing.T) {
+ a := require.New(t)
+
+ m, err := NewManager()
+ a.NoError(err)
+
+ for _, permissions := range m.roles {
+ for _, p := range permissions {
+ exist := permissionExist(p)
+ a.True(exist, "permission %s is not defined as a constant", p)
+ }
+ }
+}
+
+func permissionExist(p Permission) bool {
+ //exhaustive:enforce
+ switch p {
+ case PermissionInstanceList:
+ return true
+ case PermissionInstanceGet:
+ return true
+ case PermissionInstanceCreate:
+ return true
+ case PermissionInstanceUpdate:
+ return true
+ case PermissionInstanceDelete:
+ return true
+ case PermissionInstanceUndelete:
+ return true
+ case PermissionInstanceSync:
+ return true
+ default:
+ return false
+ }
+}