-
Notifications
You must be signed in to change notification settings - Fork 512
catalog: Allow for more builtin roles #14128
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
Changes from all commits
cb77bb1
0d8fdb6
db31af5
313e183
a84047d
76c9554
36020cb
65988bc
f500f55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,9 +7,10 @@ | |
| // the Business Source License, use of this software will be governed | ||
| // by the Apache License, Version 2.0. | ||
|
|
||
| use std::collections::{BTreeMap, HashMap}; | ||
| use std::collections::{BTreeMap, HashMap, HashSet}; | ||
| use std::hash::Hash; | ||
| use std::iter::once; | ||
| use std::str::FromStr; | ||
|
|
||
| use bytes::BufMut; | ||
| use itertools::{max, Itertools}; | ||
|
|
@@ -18,7 +19,6 @@ use serde_json::json; | |
| use timely::progress::Timestamp; | ||
| use uuid::Uuid; | ||
|
|
||
| use crate::catalog; | ||
| use mz_audit_log::{VersionedEvent, VersionedStorageMetrics}; | ||
| use mz_compute_client::command::ReplicaId; | ||
| use mz_compute_client::controller::ComputeInstanceId; | ||
|
|
@@ -31,14 +31,15 @@ use mz_repr::global_id::ProtoGlobalId; | |
| use mz_repr::GlobalId; | ||
| use mz_sql::catalog::CatalogError as SqlCatalogError; | ||
| use mz_sql::names::{ | ||
| DatabaseId, ObjectQualifiers, QualifiedObjectName, ResolvedDatabaseSpecifier, SchemaId, | ||
| DatabaseId, ObjectQualifiers, QualifiedObjectName, ResolvedDatabaseSpecifier, RoleId, SchemaId, | ||
| SchemaSpecifier, | ||
| }; | ||
| use mz_sql::plan::ComputeInstanceIntrospectionConfig; | ||
| use mz_stash::{Append, AppendBatch, Stash, StashError, TableTransaction, TypedCollection}; | ||
| use mz_storage::types::sources::Timeline; | ||
|
|
||
| use crate::catalog::builtin::BuiltinLog; | ||
| use crate::catalog; | ||
| use crate::catalog::builtin::{BuiltinLog, BUILTIN_ROLES}; | ||
| use crate::catalog::error::{Error, ErrorKind}; | ||
| use crate::catalog::SerializedComputeInstanceReplicaConfig; | ||
| use crate::catalog::SystemObjectMapping; | ||
|
|
@@ -57,7 +58,8 @@ const DEFAULT_REPLICA_ID: u64 = 1; | |
|
|
||
| const DATABASE_ID_ALLOC_KEY: &str = "database"; | ||
| const SCHEMA_ID_ALLOC_KEY: &str = "schema"; | ||
| const ROLE_ID_ALLOC_KEY: &str = "role"; | ||
| const USER_ROLE_ID_ALLOC_KEY: &str = "user_role"; | ||
| const SYSTEM_ROLE_ID_ALLOC_KEY: &str = "system_role"; | ||
| const COMPUTE_ID_ALLOC_KEY: &str = "compute"; | ||
| const REPLICA_ID_ALLOC_KEY: &str = "replica"; | ||
| pub(crate) const AUDIT_LOG_ID_ALLOC_KEY: &str = "auditlog"; | ||
|
|
@@ -112,12 +114,18 @@ async fn migrate<S: Append>( | |
| )?; | ||
| txn.id_allocator.insert( | ||
| IdAllocKey { | ||
| name: ROLE_ID_ALLOC_KEY.into(), | ||
| name: USER_ROLE_ID_ALLOC_KEY.into(), | ||
| }, | ||
| IdAllocValue { | ||
| next_id: MATERIALIZE_ROLE_ID + 1, | ||
| }, | ||
| )?; | ||
| txn.id_allocator.insert( | ||
| IdAllocKey { | ||
| name: SYSTEM_ROLE_ID_ALLOC_KEY.into(), | ||
| }, | ||
| IdAllocValue { next_id: 1 }, | ||
| )?; | ||
| txn.id_allocator.insert( | ||
| IdAllocKey { | ||
| name: COMPUTE_ID_ALLOC_KEY.into(), | ||
|
|
@@ -195,7 +203,7 @@ async fn migrate<S: Append>( | |
| )?; | ||
| txn.roles.insert( | ||
| RoleKey { | ||
| id: MATERIALIZE_ROLE_ID, | ||
| id: RoleId::User(MATERIALIZE_ROLE_ID).to_string(), | ||
| }, | ||
| RoleValue { | ||
| name: "materialize".into(), | ||
|
|
@@ -496,12 +504,33 @@ impl<S: Append> Connection<S> { | |
| .collect()) | ||
| } | ||
|
|
||
| pub async fn load_roles(&mut self) -> Result<Vec<(u64, String)>, Error> { | ||
| pub async fn load_roles(&mut self) -> Result<Vec<(RoleId, String)>, Error> { | ||
| // Add in any new builtin roles. | ||
| let mut tx = self.transaction().await?; | ||
| let role_names: HashSet<_> = tx | ||
| .roles | ||
| .items() | ||
| .into_values() | ||
| .map(|value| value.name) | ||
| .collect(); | ||
| for builtin_role in &*BUILTIN_ROLES { | ||
| if !role_names.contains(builtin_role.name) { | ||
| tx.insert_system_role(builtin_role.name)?; | ||
| } | ||
| } | ||
| tx.commit().await?; | ||
|
|
||
| Ok(COLLECTION_ROLE | ||
| .peek_one(&mut self.stash) | ||
| .await? | ||
| .into_iter() | ||
| .map(|(k, v)| (k.id, v.name)) | ||
| .map(|(k, v)| { | ||
| ( | ||
| RoleId::from_str(&k.id) | ||
| .unwrap_or_else(|_| panic!("Invalid persisted role id {}", k.id)), | ||
| v.name, | ||
| ) | ||
| }) | ||
| .collect()) | ||
| } | ||
|
|
||
|
|
@@ -945,10 +974,27 @@ impl<'a, S: Append> Transaction<'a, S> { | |
| } | ||
| } | ||
|
|
||
| pub fn insert_role(&mut self, role_name: &str) -> Result<u64, Error> { | ||
| let id = self.get_and_increment_id(ROLE_ID_ALLOC_KEY.to_string())?; | ||
| pub fn insert_user_role(&mut self, role_name: &str) -> Result<RoleId, Error> { | ||
| self.insert_role(role_name, USER_ROLE_ID_ALLOC_KEY, RoleId::User) | ||
| } | ||
|
|
||
| fn insert_system_role(&mut self, role_name: &str) -> Result<RoleId, Error> { | ||
| self.insert_role(role_name, SYSTEM_ROLE_ID_ALLOC_KEY, RoleId::System) | ||
| } | ||
|
|
||
| fn insert_role<F>( | ||
| &mut self, | ||
| role_name: &str, | ||
| id_alloc_key: &str, | ||
| role_id_variant: F, | ||
| ) -> Result<RoleId, Error> | ||
| where | ||
| F: Fn(u64) -> RoleId, | ||
| { | ||
| let id = self.get_and_increment_id(id_alloc_key.to_string())?; | ||
| let id = role_id_variant(id); | ||
| match self.roles.insert( | ||
| RoleKey { id }, | ||
| RoleKey { id: id.to_string() }, | ||
| RoleValue { | ||
| name: role_name.to_string(), | ||
| }, | ||
|
|
@@ -1654,8 +1700,8 @@ impl_codec!(ItemValue); | |
|
|
||
| #[derive(Clone, Message, PartialOrd, PartialEq, Eq, Ord, Hash)] | ||
| struct RoleKey { | ||
| #[prost(uint64)] | ||
| id: u64, | ||
| #[prost(string)] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oooh our first protobuf migration. The code here will cause any existing stash to fail on startup. We need to decide if we're ok with breaking the world again or not. I don't know.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per slack discussion, next release will have another breaking change, so we're going to piggy back off of that. |
||
| id: String, | ||
| } | ||
| impl_codec!(RoleKey); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is a breaking change to which we aren't providing an upgrade path, the cloud team should be flagged, unless there's some other breaking change they already know about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://materializeinc.slack.com/archives/CL68GT3AT/p1660169050996079