Skip to main content

risingwave_frontend/catalog/system_catalog/rw_catalog/
rw_sources.rs

1// Copyright 2023 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use risingwave_common::id::{ConnectionId, SchemaId, SourceId, TableId, UserId};
16use risingwave_common::types::{Fields, JsonbVal, Timestamptz};
17use risingwave_frontend_macro::system_catalog;
18use serde_json::{Map as JsonMap, json};
19
20use crate::WithOptionsSecResolved;
21use crate::catalog::catalog_service::CatalogReadGuard;
22use crate::catalog::system_catalog::{SysCatalogReaderImpl, get_acl_items};
23use crate::error::Result;
24use crate::handler::create_source::UPSTREAM_SOURCE_KEY;
25
26#[derive(Fields)]
27struct RwSource {
28    #[primary_key]
29    id: SourceId,
30    name: String,
31    schema_id: SchemaId,
32    owner: UserId,
33    connector: String,
34    columns: Vec<String>,
35    format: Option<String>,
36    row_encode: Option<String>,
37    append_only: bool,
38    associated_table_id: Option<TableId>,
39    connection_id: Option<ConnectionId>,
40    definition: String,
41    acl: Vec<String>,
42    initialized_at: Option<Timestamptz>,
43    created_at: Option<Timestamptz>,
44    initialized_at_cluster_version: Option<String>,
45    created_at_cluster_version: Option<String>,
46    is_shared: bool,
47    // connector properties in json format
48    connector_props: JsonbVal,
49    // format-encode options in json format
50    format_encode_options: JsonbVal,
51}
52
53#[system_catalog(table, "rw_catalog.rw_sources")]
54fn read_rw_sources_info(reader: &SysCatalogReaderImpl) -> Result<Vec<RwSource>> {
55    let catalog_reader = reader.catalog_reader.read_guard();
56    let schemas = catalog_reader.iter_schemas(&reader.auth_context.database)?;
57    let user_reader = reader.user_info_reader.read_guard();
58    let current_user = user_reader
59        .get_user_by_name(&reader.auth_context.user_name)
60        .expect("user not found");
61    let users = user_reader.get_all_users();
62    let username_map = user_reader.get_user_name_map();
63
64    Ok(schemas
65        .flat_map(|schema| {
66            schema.iter_source_with_acl(current_user).map(|source| {
67                let format_encode_props_with_secrets = WithOptionsSecResolved::new(
68                    source.info.format_encode_options.clone(),
69                    source.info.format_encode_secret_refs.clone(),
70                );
71                RwSource {
72                    id: source.id,
73                    name: source.name.clone(),
74                    schema_id: schema.id(),
75                    owner: source.owner,
76                    connector: source
77                        .with_properties
78                        .get(UPSTREAM_SOURCE_KEY)
79                        .cloned()
80                        .unwrap_or("".to_owned())
81                        .to_uppercase(),
82                    columns: source.columns.iter().map(|c| c.name().into()).collect(),
83                    format: source
84                        .info
85                        .get_format()
86                        .ok()
87                        .map(|format| format.as_str_name().into()),
88                    row_encode: source
89                        .info
90                        .get_row_encode()
91                        .ok()
92                        .map(|row_encode| row_encode.as_str_name().into()),
93                    append_only: source.append_only,
94                    associated_table_id: source.associated_table_id,
95                    connection_id: source.connection_id,
96                    definition: source.create_sql_purified(),
97                    acl: get_acl_items(source.id, false, &users, username_map),
98                    initialized_at: source.initialized_at_epoch.map(|e| e.as_timestamptz()),
99                    created_at: source.created_at_epoch.map(|e| e.as_timestamptz()),
100                    initialized_at_cluster_version: source.initialized_at_cluster_version.clone(),
101                    created_at_cluster_version: source.created_at_cluster_version.clone(),
102                    is_shared: source.info.is_shared(),
103
104                    connector_props: serialize_props_with_secret(
105                        &catalog_reader,
106                        &reader.auth_context.database,
107                        source.with_properties.clone(),
108                    )
109                    .into(),
110                    format_encode_options: serialize_props_with_secret(
111                        &catalog_reader,
112                        &reader.auth_context.database,
113                        format_encode_props_with_secrets,
114                    )
115                    .into(),
116                }
117            })
118        })
119        .collect())
120}
121
122pub fn serialize_props_with_secret(
123    catalog_reader: &CatalogReadGuard,
124    db_name: &str,
125    props_with_secret: WithOptionsSecResolved,
126) -> jsonbb::Value {
127    let (inner, secret_ref) = props_with_secret.into_parts();
128    // if not secret, {"some key": {"type": "plaintext", "value": "xxxx"}}
129    // if secret, {"some key": {"type": "secret", "value": {"value": "<secret name>"}}}
130    let mut result: JsonMap<String, serde_json::Value> = JsonMap::new();
131
132    for (k, v) in inner {
133        result.insert(k, json!({"type": "plaintext", "value": v}));
134    }
135    for (k, v) in secret_ref {
136        let secret = catalog_reader
137            .iter_schemas(db_name)
138            .unwrap()
139            .find_map(|schema| schema.get_secret_by_id(v.secret_id));
140        let secret_name = secret
141            .map(|s| s.name.clone())
142            .unwrap_or("not found".to_owned());
143        result.insert(
144            k,
145            json!({"type": "secret", "value": {"value": secret_name}}),
146        );
147    }
148
149    jsonbb::Value::from(serde_json::Value::Object(result))
150}