Skip to content

Commit

Permalink
chore: clean license message (databendlabs#14509)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhiHanZ authored Jan 29, 2024
1 parent a0a63c4 commit 5a76396
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/common/license/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ databend-common-base = { path = "../base" }
databend-common-exception = { path = "../exception" }
jwt-simple = "0.11.0"
serde = { workspace = true }
strum = "0.24.1"
strum_macros = "0.24.3"
23 changes: 23 additions & 0 deletions src/common/license/src/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ use std::fmt::Formatter;

use serde::Deserialize;
use serde::Serialize;
use strum::IntoEnumIterator;
use strum_macros::EnumIter;

// All enterprise features are defined here.
#[derive(Debug, PartialEq, EnumIter)]
pub enum Feature {
LicenseInfo,
Vacuum,
Expand Down Expand Up @@ -77,3 +80,23 @@ pub struct LicenseInfo {
pub tenants: Option<Vec<String>>,
pub features: Option<Vec<String>>,
}

impl LicenseInfo {
pub fn display_features(&self) -> String {
// sort all features in alphabet order and ignore test feature
let mut binding = self.features.clone().unwrap_or_default();
if binding.is_empty() {
binding = Feature::iter().map(|f| f.to_string()).collect::<Vec<_>>();
}
let mut features = binding
.iter()
.filter(|f| *f != &Feature::Test.to_string())
.collect::<Vec<_>>();
features.sort();
features
.iter()
.map(|f| f.to_string())
.collect::<Vec<_>>()
.join(",")
}
}
5 changes: 3 additions & 2 deletions src/query/ee/src/license/license_mgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ impl RealLicenseManager {
let features = l.custom.features.as_ref().unwrap();
if !features.contains(&feature.to_string()) {
return Err(ErrorCode::LicenseKeyInvalid(format!(
"license key does not support feature {}, supported features: {:?}",
feature, features
"license key does not support feature {}, supported features: {}",
feature,
l.custom.display_features()
)));
}
Ok(())
Expand Down
4 changes: 4 additions & 0 deletions src/query/service/src/table_functions/others/license_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl LicenseInfoTable {
TableField::new("expire_at", TableDataType::Timestamp),
// formatted string calculate the available time from now to expiry of license
TableField::new("available_time_until_expiry", TableDataType::String),
TableField::new("features", TableDataType::String),
])
}

Expand Down Expand Up @@ -156,6 +157,8 @@ impl LicenseInfoSource {
let available_time = info.expires_at.unwrap_or_default().sub(now).as_micros();
let human_readable_available_time =
HumanDuration::from(Duration::from_micros(available_time)).to_string();

let feature_str = info.custom.display_features();
Ok(DataBlock::new(
vec![
BlockEntry::new(
Expand Down Expand Up @@ -192,6 +195,7 @@ impl LicenseInfoSource {
DataType::String,
Value::Scalar(Scalar::String(human_readable_available_time)),
),
BlockEntry::new(DataType::String, Value::Scalar(Scalar::String(feature_str))),
],
1,
))
Expand Down
5 changes: 3 additions & 2 deletions tests/suites/5_ee/00_check/00_0014_license_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ def get_license():
organization = license[0][2]
issue_at = license[0][3]
expire_at = license[0][4]
features = license[0][6]
now = datetime.utcnow()

if now < issue_at or now > expire_at:
print(
f"License is invalid: issuer: {issuer}, type: {type_}, "
f"org: {organization}, issue_at: {issue_at}, expire_at: {expire_at}"
f"org: {organization}, issue_at: {issue_at}, expire_at: {expire_at}, features: {features}"
)
sys.exit(1)

print(features)
mycursor = mydb.cursor()
mycursor.execute(
"select count(*) from system.processes where type = 'FlightSQL';"
Expand Down
1 change: 1 addition & 0 deletions tests/suites/5_ee/00_check/00_0014_license_info.result
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[]
aggregate_index,background_service,computed_column,data_mask,license_info,storage_encryption,stream,vacuum,virtual_column
[(0,)]

0 comments on commit 5a76396

Please sign in to comment.