Skip to content

Commit 9412101

Browse files
committed
Merge branch 'main' of https://github.com/ideal-world/bios
2 parents 06df3dc + 0e58912 commit 9412101

File tree

101 files changed

+2442
-1521
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+2442
-1521
lines changed

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ run_script = { version = "0.10" }
5959
testcontainers-modules = { version = "0.3", features = ["redis"] }
6060
strum = { version = "0.26", features = ["derive"] }
6161
# tardis
62-
tardis = { version = "0.1.0-rc.10" }
62+
# tardis = { version = "0.1.0-rc.10" }
6363
# tardis = { path = "../tardis/tardis" }
64-
# tardis = { git = "https://github.com/ideal-world/tardis.git", rev = "6312e3305c322d48b084e14c23a8de2a879a738a" }
64+
tardis = { git = "https://github.com/ideal-world/tardis.git", rev = "191f3ec" }
6565
#spacegate
6666

6767
# spacegate-kernel = { git = "https://github.com/ideal-world/spacegate.git", rev = "f37a81a", features = [
@@ -74,8 +74,10 @@ tardis = { version = "0.1.0-rc.10" }
7474
# "k8s",
7575
# "ext-redis",
7676
# ] }
77-
spacegate-shell = { git = "https://github.com/ideal-world/spacegate.git", branch = "master", features = [
77+
spacegate-shell = { git = "https://github.com/ideal-world/spacegate.git", branch = "master", features = [
7878
"cache",
7979
"k8s",
8080
"ext-redis",
8181
] }
82+
83+
spacegate-plugin = { git = "https://github.com/ideal-world/spacegate.git", branch = "master" }

basic/src/helper.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod bios_ctx_helper;
12
pub mod db_helper;
23
pub mod request_helper;
34
pub mod url_helper;

basic/src/helper/bios_ctx_helper.rs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
use crate::rbum::rbum_config::RbumConfigApi;
2+
use tardis::{
3+
basic::{dto::TardisContext, error::TardisError, result::TardisResult},
4+
web::poem::Request,
5+
TardisFuns, TardisFunsInst,
6+
};
7+
8+
fn unsafe_check_ctx<F>(request: &Request, f: F, check: bool, funs: &TardisFunsInst, ctx: &mut TardisContext) -> TardisResult<()>
9+
where
10+
F: FnOnce(TardisContext, &mut TardisContext),
11+
{
12+
if check && !ctx.owner.is_empty() {
13+
return Ok(());
14+
}
15+
let bios_ctx = if let Some(bios_ctx) = request.header(&funs.rbum_head_key_bios_ctx()).or_else(|| request.header(&funs.rbum_head_key_bios_ctx().to_lowercase())) {
16+
TardisFuns::json.str_to_obj::<TardisContext>(&TardisFuns::crypto.base64.decode_to_string(bios_ctx)?)?
17+
} else {
18+
if ctx.owner.is_empty() && ctx.ak.is_empty() && ctx.own_paths.is_empty() && ctx.roles.is_empty() && ctx.groups.is_empty() {
19+
return Err(TardisError::unauthorized(
20+
&format!("[Basic] Request is not legal, missing header [{}]", funs.rbum_head_key_bios_ctx()),
21+
"401-auth-req-ak-not-exist",
22+
));
23+
} else {
24+
return Ok(());
25+
}
26+
};
27+
28+
if bios_ctx.own_paths.contains(&ctx.own_paths) {
29+
f(bios_ctx, ctx);
30+
31+
Ok(())
32+
} else {
33+
Err(TardisError::forbidden(
34+
&format!("[Basic] Request is not legal from head [{}]", funs.rbum_head_key_bios_ctx()),
35+
"403-auth-req-permission-denied",
36+
))
37+
}
38+
}
39+
40+
// xxx_check_own function will check the owner is empty or not.
41+
pub fn check_own_fill_ctx(request: &Request, funs: &TardisFunsInst, ctx: &mut TardisContext) -> TardisResult<()> {
42+
unsafe_check_ctx(
43+
request,
44+
|bios_ctx, ctx| {
45+
let mut roles = bios_ctx.roles.clone();
46+
for role in bios_ctx.roles.clone() {
47+
if role.contains(':') {
48+
let extend_role = role.split(':').collect::<Vec<_>>()[0];
49+
roles.push(extend_role.to_string());
50+
}
51+
}
52+
ctx.owner = bios_ctx.owner.clone();
53+
ctx.roles = roles;
54+
ctx.groups = bios_ctx.groups;
55+
ctx.own_paths = bios_ctx.own_paths;
56+
},
57+
true,
58+
funs,
59+
ctx,
60+
)
61+
}
62+
63+
pub fn unsafe_fill_ctx(request: &Request, funs: &TardisFunsInst, ctx: &mut TardisContext) -> TardisResult<()> {
64+
unsafe_check_ctx(
65+
request,
66+
|bios_ctx, ctx| {
67+
let mut roles = bios_ctx.roles.clone();
68+
for role in bios_ctx.roles.clone() {
69+
if role.contains(':') {
70+
let extend_role = role.split(':').collect::<Vec<_>>()[0];
71+
roles.push(extend_role.to_string());
72+
}
73+
}
74+
ctx.owner = bios_ctx.owner.clone();
75+
ctx.roles = roles;
76+
ctx.groups = bios_ctx.groups;
77+
ctx.own_paths = bios_ctx.own_paths;
78+
},
79+
false,
80+
funs,
81+
ctx,
82+
)
83+
}
84+
85+
pub fn unsfae_fill_owner_only(request: &Request, funs: &TardisFunsInst, ctx: &mut TardisContext) -> TardisResult<()> {
86+
unsafe_check_ctx(
87+
request,
88+
|bios_ctx, ctx| {
89+
ctx.owner = bios_ctx.owner.clone();
90+
},
91+
false,
92+
funs,
93+
ctx,
94+
)
95+
}
96+
97+
pub fn unsfae_fill_own_paths_only(request: &Request, funs: &TardisFunsInst, ctx: &mut TardisContext) -> TardisResult<()> {
98+
unsafe_check_ctx(
99+
request,
100+
|bios_ctx, ctx| {
101+
ctx.own_paths = bios_ctx.own_paths;
102+
},
103+
false,
104+
funs,
105+
ctx,
106+
)
107+
}
108+
109+
pub fn unsfae_fill_roles_only(request: &Request, funs: &TardisFunsInst, ctx: &mut TardisContext) -> TardisResult<()> {
110+
unsafe_check_ctx(
111+
request,
112+
|bios_ctx, ctx| {
113+
let mut roles = bios_ctx.roles.clone();
114+
for role in bios_ctx.roles.clone() {
115+
if role.contains(':') {
116+
let extend_role = role.split(':').collect::<Vec<_>>()[0];
117+
roles.push(extend_role.to_string());
118+
}
119+
}
120+
ctx.roles = roles;
121+
},
122+
false,
123+
funs,
124+
ctx,
125+
)
126+
}
127+
128+
pub fn unsfae_fill_groups_only(request: &Request, funs: &TardisFunsInst, ctx: &mut TardisContext) -> TardisResult<()> {
129+
unsafe_check_ctx(
130+
request,
131+
|bios_ctx, ctx| {
132+
ctx.groups = bios_ctx.groups;
133+
},
134+
false,
135+
funs,
136+
ctx,
137+
)
138+
}

basic/src/rbum/domain/rbum_cert.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ use tardis::chrono::{self, Utc};
33
use tardis::db::reldb_client::TardisActiveModel;
44
use tardis::db::sea_orm;
55
use tardis::db::sea_orm::prelude::*;
6-
use tardis::db::sea_orm::sea_query::{ColumnDef, Index, IndexCreateStatement, Table, TableCreateStatement};
6+
use tardis::db::sea_orm::sea_query::{ColumnDef, IndexCreateStatement, Table, TableCreateStatement};
77
use tardis::db::sea_orm::*;
8+
use tardis::TardisCreateIndex;
89

910
/// Credential or authentication instance model
1011
///
1112
/// Uniform use of cert refers to credentials or authentication
12-
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
13+
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, TardisCreateIndex)]
1314
#[sea_orm(table_name = "rbum_cert")]
1415
pub struct Model {
1516
#[sea_orm(primary_key, auto_increment = false)]
@@ -40,6 +41,7 @@ pub struct Model {
4041
/// Associated [cert configuration](crate::rbum::domain::rbum_cert_conf::Model) id
4142
pub rel_rbum_cert_conf_id: String,
4243
/// Associated [resource kind](crate::rbum::rbum_enumeration::RbumCertRelKind) id
44+
#[index(index_id = "id")]
4345
pub rel_rbum_kind: i16,
4446
/// Associated resource id
4547
///
@@ -54,20 +56,25 @@ pub struct Model {
5456
///
5557
/// * if rel_rbum_kind == Rel
5658
/// - In the CMDB service, a resource can be sliced (E.g. DB instance), we can specify slice information of association
59+
#[index(index_id = "id")]
5760
pub rel_rbum_id: String,
5861

5962
pub own_paths: String,
6063
pub owner: String,
6164
pub create_time: chrono::DateTime<Utc>,
6265
pub update_time: chrono::DateTime<Utc>,
66+
pub create_by: String,
67+
pub update_by: String,
6368
}
6469

6570
impl TardisActiveModel for ActiveModel {
6671
fn fill_ctx(&mut self, ctx: &TardisContext, is_insert: bool) {
6772
if is_insert {
6873
self.own_paths = Set(ctx.own_paths.to_string());
6974
self.owner = Set(ctx.owner.to_string());
75+
self.create_by = Set(ctx.owner.to_string());
7076
}
77+
self.update_by = Set(ctx.owner.to_string());
7178
}
7279

7380
fn create_table_statement(db: DbBackend) -> TableCreateStatement {
@@ -90,7 +97,9 @@ impl TardisActiveModel for ActiveModel {
9097
// Basic
9198
.col(ColumnDef::new(Column::OwnPaths).not_null().string())
9299
.col(ColumnDef::new(Column::Owner).not_null().string())
93-
.col(ColumnDef::new(Column::Status).not_null().small_integer());
100+
.col(ColumnDef::new(Column::Status).not_null().small_integer())
101+
.col(ColumnDef::new(Column::CreateBy).not_null().string())
102+
.col(ColumnDef::new(Column::UpdateBy).not_null().string());
94103
if db == DatabaseBackend::Postgres {
95104
builder
96105
.col(ColumnDef::new(Column::StartTime).not_null().timestamp_with_time_zone())
@@ -111,23 +120,7 @@ impl TardisActiveModel for ActiveModel {
111120
}
112121

113122
fn create_index_statement() -> Vec<IndexCreateStatement> {
114-
vec![
115-
// todo delete index
116-
// Index::create()
117-
// .name(&format!("idx-{}-ak", Entity.table_name()))
118-
// .table(Entity)
119-
// .col(Column::OwnPaths)
120-
// .col(Column::RelRbumKind)
121-
// .col(Column::RelRbumCertConfId)
122-
// .col(Column::Ak)
123-
// .to_owned(),
124-
Index::create()
125-
.name(&format!("idx-{}-{}", Entity.table_name(), Column::RelRbumKind.to_string()))
126-
.table(Entity)
127-
.col(Column::RelRbumKind)
128-
.col(Column::RelRbumId)
129-
.to_owned(),
130-
]
123+
tardis_create_index_statement()
131124
}
132125
}
133126

basic/src/rbum/domain/rbum_cert_conf.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ use tardis::chrono::{self, Utc};
33
use tardis::db::reldb_client::TardisActiveModel;
44
use tardis::db::sea_orm;
55
use tardis::db::sea_orm::prelude::*;
6-
use tardis::db::sea_orm::sea_query::{ColumnDef, Index, IndexCreateStatement, Table, TableCreateStatement};
6+
use tardis::db::sea_orm::sea_query::{ColumnDef, IndexCreateStatement, Table, TableCreateStatement};
77
use tardis::db::sea_orm::*;
8+
use tardis::TardisCreateIndex;
89

910
/// Credential or authentication configuration model
1011
///
1112
/// Uniform use of cert refers to credentials or authentication
12-
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
13+
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, TardisCreateIndex)]
1314
#[sea_orm(table_name = "rbum_cert_conf")]
1415
pub struct Model {
1516
#[sea_orm(primary_key, auto_increment = false)]
1617
pub id: String,
18+
#[index(index_id = "id_2", unique)]
1719
pub kind: String,
20+
#[index(index_id = "id_2", unique)]
1821
pub supplier: String,
1922
pub name: String,
2023
pub note: String,
@@ -59,22 +62,29 @@ pub struct Model {
5962
/// see [status][crate::rbum::rbum_enumeration::RbumCertConfStatusKind]
6063
pub status: i16,
6164
/// Associated [resource domain](crate::rbum::domain::rbum_domain::Model) id
65+
#[index(index_id = "id_2", unique)]
6266
pub rel_rbum_domain_id: String,
6367
/// Associated [resource](crate::rbum::domain::rbum_item::Model) id
68+
#[index(index_id = "id_2", unique)]
6469
pub rel_rbum_item_id: String,
6570

71+
#[index()]
6672
pub own_paths: String,
6773
pub owner: String,
6874
pub create_time: chrono::DateTime<Utc>,
6975
pub update_time: chrono::DateTime<Utc>,
76+
pub create_by: String,
77+
pub update_by: String,
7078
}
7179

7280
impl TardisActiveModel for ActiveModel {
7381
fn fill_ctx(&mut self, ctx: &TardisContext, is_insert: bool) {
7482
if is_insert {
7583
self.own_paths = Set(ctx.own_paths.to_string());
7684
self.owner = Set(ctx.owner.to_string());
85+
self.create_by = Set(ctx.owner.to_string());
7786
}
87+
self.update_by = Set(ctx.owner.to_string());
7888
}
7989

8090
fn create_table_statement(db: DbBackend) -> TableCreateStatement {
@@ -111,7 +121,9 @@ impl TardisActiveModel for ActiveModel {
111121
.col(ColumnDef::new(Column::Status).not_null().small_integer())
112122
// Basic
113123
.col(ColumnDef::new(Column::OwnPaths).not_null().string())
114-
.col(ColumnDef::new(Column::Owner).not_null().string());
124+
.col(ColumnDef::new(Column::Owner).not_null().string())
125+
.col(ColumnDef::new(Column::CreateBy).not_null().string())
126+
.col(ColumnDef::new(Column::UpdateBy).not_null().string());
115127
if db == DatabaseBackend::Postgres {
116128
builder
117129
.col(ColumnDef::new(Column::CreateTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp_with_time_zone())
@@ -128,18 +140,7 @@ impl TardisActiveModel for ActiveModel {
128140
}
129141

130142
fn create_index_statement() -> Vec<IndexCreateStatement> {
131-
vec![
132-
Index::create().name(&format!("idx-{}-{}", Entity.table_name(), Column::OwnPaths.to_string())).table(Entity).col(Column::OwnPaths).to_owned(),
133-
Index::create()
134-
.name(&format!("idx-{}-{}", Entity.table_name(), Column::Kind.to_string()))
135-
.table(Entity)
136-
.col(Column::Kind)
137-
.col(Column::Supplier)
138-
.col(Column::RelRbumDomainId)
139-
.col(Column::RelRbumItemId)
140-
.unique()
141-
.to_owned(),
142-
]
143+
tardis_create_index_statement()
143144
}
144145
}
145146

0 commit comments

Comments
 (0)