Skip to content
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

feat: create secret catalog #16288

Merged
merged 50 commits into from
May 28, 2024
Merged

feat: create secret catalog #16288

merged 50 commits into from
May 28, 2024

Conversation

tabVersion
Copy link
Contributor

@tabVersion tabVersion commented Apr 12, 2024

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added test labels as necessary. See details.
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • My PR contains breaking changes. (If it deprecates some features, please create a tracking issue to remove them in the future).
  • All checks passed in ./risedev check (or alias, ./risedev c)
  • My PR changes performance-critical code. (Please run macro/micro-benchmarks and show the results.)
  • My PR contains critical fixes that are necessary to be merged into the latest release. (Please check out the details)

Documentation

  • My PR needs documentation updates. (Please use the Release note section below to summarize the impact on users)

Release note

If this PR includes changes that directly affect users or other significant modifications relevant to the community, kindly draft a release note to provide a concise summary of these changes. Please prioritize highlighting the impact these changes will have on users.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

license-eye has totally checked 5070 files.

Valid Invalid Ignored Fixed
2171 1 2898 0
Click to see the invalid file list
  • src/frontend/src/handler/create_secret.rs

src/frontend/src/handler/create_secret.rs Show resolved Hide resolved
Copy link

gitguardian bot commented May 2, 2024

⚠️ GitGuardian has uncovered 2 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
9425213 Triggered Generic Password 8e4637f e2e_test/source/cdc/cdc.validate.postgres.slt View secret
9425213 Triggered Generic Password 1a75e35 e2e_test/source/cdc/cdc.validate.postgres.slt View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Copy link
Member

@yezizp2012 yezizp2012 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is too large, can we separate it into two parts, like meta and frontend. Btw, we need to add some e2e to check if it works.
Btw, the backup and migration from etcd to sql backend are still missing, we need to add them as well. It's acceptable for me to add them in some other PRs.

src/common/src/config.rs Outdated Show resolved Hide resolved
proto/catalog.proto Outdated Show resolved Hide resolved
src/frontend/src/catalog/schema_catalog.rs Show resolved Hide resolved
src/frontend/src/handler/create_secret.rs Outdated Show resolved Hide resolved
src/sqlparser/src/ast/statement.rs Outdated Show resolved Hide resolved
secret_by_name: HashMap<String, Arc<SecretCatalog>>,
secret_by_id: HashMap<SecretId, Arc<SecretCatalog>>,

secret_source_ref: HashMap<SecretId, Vec<SourceId>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any usage of these two fields? We need to update them when issuing create/drop source/sink.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe they're necessary for command like show secrets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any usage of these two fields? We need to update them when issuing create/drop source/sink.

I am planning to impl this in the next pr. It is more intuitive to do it with ref secret syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe they're necessary for command like show secrets.

Yes, show secrets will be in this pr.

@@ -127,6 +129,13 @@ impl From<PbSink> for ActiveModel {
sink_from_name: Set(pb_sink.sink_from_name),
sink_format_desc: Set(pb_sink.format_desc.as_ref().map(|x| x.into())),
target_table: Set(pb_sink.target_table.map(|x| x as _)),
secret_ref: Set({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to define this column as json_binary in migration, then you can define a struct wrapping the map and use it here. Check examples that are using derive_from_json_struct for more details.

src/meta/src/manager/catalog/mod.rs Show resolved Hide resolved
src/meta/src/controller/catalog.rs Show resolved Hide resolved
let user_core = &mut core.user;
let mut secrets = BTreeMapTransaction::new(&mut database_core.secrets);

match database_core.relation_ref_count.get(&secret_id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, secret referring count is never updated when create/drop sink and source.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not resolved, you can remove the check and leave a TODO here.

Copy link
Member

@fuyufjh fuyufjh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM

let session = handler_args.session.clone();
let db_name = session.database();
let (schema_name, connection_name) =
Binder::resolve_schema_qualified_name(db_name, stmt.secret_name.clone())?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we follow the same rule of identifiers for secrets? That is,

  • foo will be converted to lower-case, so that Foo and foo will be the same
  • "foo" will be interpreted as case-sensitive, i.e. "foo" and "Foo" are different

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the secret name, yes. The impl leverages the existing funds and tries best to keep the same as other catalogs.

secret.schema_id,
SecretId::new(secret.id),
),
Operation::Update => unimplemented!(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering key rotation, I think it makes sense to provide an "update secret" command

@@ -184,6 +203,14 @@ impl MetaClient {
Ok(resp.version)
}

pub async fn drop_secret(&self, secret_id: SecretId) -> Result<CatalogVersion> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal but why not placing it with create_secret() together

Comment on lines 623 to 626
let encrypted_payload = simplestcrypt::encrypt_and_serialize(
self.env.opts.secret_store_private_key.as_slice(),
secret.get_value().as_slice(),
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do the encryption and the secret key here.
@yuhao-su

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using the implementation in our built-in function encrypt(bytea, bytea, varchar) -> bytea?

fn eval_inner(
&self,
input: &[u8],
operation: CipherMode,
) -> std::result::Result<Box<[u8]>, ErrorStack> {
let mut decrypter = Crypter::new(self.cipher, operation, self.crypt_key.as_ref(), None)?;
let enable_padding = match self.padding {
Padding::Pkcs => true,
Padding::None => false,
};
decrypter.pad(enable_padding);
let mut decrypt = vec![0; input.len() + self.cipher.block_size()];
let count = decrypter.update(input, &mut decrypt)?;
let rest = decrypter.finalize(&mut decrypt[count..])?;
decrypt.truncate(count + rest);
Ok(decrypt.into())
}

Benefits include

  • Avoid duplicates in dependencies
  • Easy to encrypt & decrypt with SQL interface when debugging

Copy link
Member

@fuyufjh fuyufjh May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with #16288 (comment)

Better to consider a more popular encryption lib

Such as openssl directly.

It's a bit risky to use a 3rd-party unpopular lib for decryption & encryption here. Considering the possibility that the lib author may discontinue its maintenance, we should either use ourselves's implementation, or ensure we know 100% how the lib works so that we can write an exactly same one. The former one is easier, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using the implementation in our built-in function encrypt(bytea, bytea, varchar) -> bytea?

Good point! Let me fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let encrypted_payload = {
let data = secret.get_value().as_slice();
let key = self.env.opts.secret_store_private_key.as_slice();
let encrypt_key = {
let mut k = key[..(std::cmp::min(key.len(), 32))].to_vec();
k.resize_with(32, || 0);
k
};
let mut rng = rand::thread_rng();
let mut nonce: [u8; 16] = [0; 16];
rng.fill_bytes(&mut nonce);
let nonce_array = GenericArray::from_slice(&nonce);
let cipher = Aes128SivAead::new(encrypt_key.as_slice().into());
let ciphertext = cipher.encrypt(nonce_array, data).map_err(|e| {
MetaError::from(MetaErrorInner::InvalidParameter(format!(
"failed to encrypt secret {}: {:?}",
secret.name, e
)))
})?;
bincode::serialize(&SecretEncryption { nonce, ciphertext }).map_err(|e| {
MetaError::from(MetaErrorInner::InvalidParameter(format!(
"failed to serialize secret {}: {:?}",
secret.name, e
)))
})?
};

Impl the encryption myself :-)

@@ -64,6 +64,7 @@ scopeguard = "1.2.0"
sea-orm = { workspace = true }
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
simplestcrypt = "0.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to consider a more popular encryption lib

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/TotalKrill/simplestcrypt/blob/master/src/lib.rs
Took a rough look at the crate, the owner just wrapped a basic usage of an audited encryption crate, instead of writing the real "encryption" part. Seems not that vital. What do you think?

Signed-off-by: tabVersion <[email protected]>
Copy link
Member

@yezizp2012 yezizp2012 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest generally LGTM. This PR is so large, rubber stamp for it. We need to add more e2e and secret usage support for it. After this PR, we also need to support it in migration script from etcd to sql backend.

src/sqlparser/src/ast/statement.rs Show resolved Hide resolved
@@ -1861,6 +1868,7 @@ impl fmt::Display for Statement {
Statement::DeclareCursor { stmt } => write!(f, "DECLARE {}", stmt,),
Statement::FetchCursor { stmt } => write!(f, "FETCH {}", stmt),
Statement::CloseCursor { stmt } => write!(f, "CLOSE {}", stmt),
Statement::CreateSecret { stmt } => write!(f, "CREATE SECRET {}", stmt, ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Statement::CreateSecret { stmt } => write!(f, "CREATE SECRET {}", stmt, ),
Statement::CreateSecret { stmt } => write!(f, "CREATE SECRET {}", stmt),

src/meta/src/rpc/ddl_controller.rs Show resolved Hide resolved
src/meta/src/manager/catalog/database.rs Show resolved Hide resolved
let user_core = &mut core.user;
let mut secrets = BTreeMapTransaction::new(&mut database_core.secrets);

match database_core.relation_ref_count.get(&secret_id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not resolved, you can remove the check and leave a TODO here.

src/meta/model_v2/migration/src/m20240422_090457_secret.rs Outdated Show resolved Hide resolved
@@ -24,6 +25,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240410_154406_session_params::Migration),
Box::new(m20240417_062305_subscription_internal_table_name::Migration),
Box::new(m20240418_142249_function_runtime::Migration),
Box::new(m20240422_090457_secret::Migration),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether a smaller date naming for this migration can be applied when upgrade from the version before this PR. Didn't check, if not work you can rename to a larger date for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

e2e_test/ddl/secret.slt Show resolved Hide resolved
src/meta/src/controller/mod.rs Outdated Show resolved Hide resolved
Signed-off-by: tabVersion <[email protected]>
Signed-off-by: tabVersion <[email protected]>
Copy link
Member

@yezizp2012 yezizp2012 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, thx for the PR.

src/meta/model_v2/migration/src/lib.rs Outdated Show resolved Hide resolved
@fuyufjh
Copy link
Member

fuyufjh commented May 28, 2024

Bump up #16288 (comment). Seems to be overlooked.

The rest LGTM

@tabVersion
Copy link
Contributor Author

Bump up #16288 (comment). Seems to be overlooked.

The rest LGTM

Not sure if it is ok to dep risingwave_expr from risingwave_meta. I impl the encrypting process using aes-siv.

Signed-off-by: tabVersion <[email protected]>
Copy link
Member

@fuyufjh fuyufjh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest LGTM

src/meta/Cargo.toml Outdated Show resolved Hide resolved
tabVersion and others added 2 commits May 28, 2024 21:52
Signed-off-by: tabVersion <[email protected]>
@tabVersion tabVersion enabled auto-merge May 28, 2024 21:08
@tabVersion tabVersion added this pull request to the merge queue May 28, 2024
Merged via the queue into main with commit 11aac57 May 28, 2024
29 of 31 checks passed
@tabVersion tabVersion deleted the tab/create-secret branch May 28, 2024 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants