Skip to content

Commit 56d69c5

Browse files
authored
iam: test fix && api debug (#643)
* iam: miss open-api * iam: test fix && api debug
1 parent 4305bc8 commit 56d69c5

File tree

8 files changed

+181
-15
lines changed

8 files changed

+181
-15
lines changed

support/iam/src/basic/dto/iam_open_dto.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ pub struct IamOpenAddSpecReq {
3232

3333
#[derive(poem_openapi::Object, Serialize, Deserialize, Debug)]
3434
pub struct IamOpenBindAkProductReq {
35-
pub product_id: String,
36-
pub spec_id: String,
35+
pub product_code: String,
36+
pub spec_code: String,
3737
pub start_time: Option<chrono::DateTime<Utc>>,
3838
pub end_time: Option<chrono::DateTime<Utc>>,
3939
pub api_call_frequency: Option<u32>,
@@ -43,10 +43,24 @@ pub struct IamOpenBindAkProductReq {
4343
#[derive(Serialize, Deserialize, Debug, poem_openapi::Object)]
4444
pub struct IamOpenRuleInfo {
4545
pub cert_id: String,
46-
pub spec_id: String,
46+
pub spec_code: String,
4747
pub start_time: Option<chrono::DateTime<Utc>>,
4848
pub end_time: Option<chrono::DateTime<Utc>>,
4949
pub api_call_frequency: Option<u32>,
5050
pub api_call_count: Option<u32>,
5151
pub api_call_cumulative_count: Option<u32>,
5252
}
53+
54+
55+
#[derive(poem_openapi::Object, Serialize, Deserialize, Debug)]
56+
pub struct IamOpenAkSkAddReq {
57+
pub tenant_id: String,
58+
pub app_id: Option<String>,
59+
}
60+
61+
#[derive(poem_openapi::Object, Serialize, Deserialize, Debug)]
62+
pub struct IamOpenAkSkResp {
63+
pub id: String,
64+
pub ak: String,
65+
pub sk: String,
66+
}

support/iam/src/basic/serv/iam_open_serv.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use bios_basic::rbum::{
1010
use bios_sdk_invoke::clients::spi_kv_client::SpiKvClient;
1111
use itertools::Itertools;
1212
use tardis::{
13-
basic::{dto::TardisContext, result::TardisResult},
14-
TardisFunsInst,
13+
basic::{dto::TardisContext, field::TrimString, result::TardisResult}, TardisFuns, TardisFunsInst
1514
};
1615
use tardis::{
1716
chrono::{self, Utc},
@@ -20,15 +19,13 @@ use tardis::{
2019

2120
use crate::{
2221
basic::dto::{
23-
iam_filer_dto::IamResFilterReq,
24-
iam_open_dto::{IamOpenAddProductReq, IamOpenBindAkProductReq},
25-
iam_res_dto::IamResAddReq,
22+
iam_cert_conf_dto::IamCertConfAkSkAddOrModifyReq, iam_cert_dto::IamCertAkSkAddReq, iam_filer_dto::IamResFilterReq, iam_open_dto::{IamOpenAddProductReq, IamOpenAkSkAddReq, IamOpenAkSkResp, IamOpenBindAkProductReq}, iam_res_dto::IamResAddReq
2623
},
2724
iam_config::IamConfig,
28-
iam_enumeration::{IamRelKind, IamResKind},
25+
iam_enumeration::{IamCertKernelKind, IamRelKind, IamResKind},
2926
};
3027

31-
use super::{iam_key_cache_serv::IamIdentCacheServ, iam_rel_serv::IamRelServ, iam_res_serv::IamResServ};
28+
use super::{iam_cert_aksk_serv::IamCertAkSkServ, iam_cert_serv::IamCertServ, iam_key_cache_serv::IamIdentCacheServ, iam_rel_serv::IamRelServ, iam_res_serv::IamResServ, iam_tenant_serv::IamTenantServ};
3229

3330
pub struct IamOpenServ;
3431

@@ -83,10 +80,39 @@ impl IamOpenServ {
8380
RbumRelServ::delete_rbum(&rel.id, funs, ctx).await?;
8481
}
8582

86-
Self::bind_cert_product(cert_id, &bind_req.product_id, None, funs, ctx).await?;
83+
let product_id = IamResServ::find_one_detail_item(
84+
&IamResFilterReq {
85+
basic: RbumBasicFilterReq {
86+
code: Some(format!("{}/*/{}", IamResKind::Product.to_int(), &bind_req.product_code)),
87+
..Default::default()
88+
},
89+
..Default::default()
90+
},
91+
funs,
92+
ctx,
93+
)
94+
.await?
95+
.ok_or_else(|| funs.err().internal_error("iam_open", "bind_cert_product_and_spec", "illegal response", "404-iam-res-not-exist"))?
96+
.id;
97+
let spec_id = IamResServ::find_one_detail_item(
98+
&IamResFilterReq {
99+
basic: RbumBasicFilterReq {
100+
code: Some(format!("{}/*/{}", IamResKind::Spec.to_int(), &bind_req.spec_code)),
101+
..Default::default()
102+
},
103+
..Default::default()
104+
},
105+
funs,
106+
ctx,
107+
)
108+
.await?
109+
.ok_or_else(|| funs.err().internal_error("iam_open", "bind_cert_product_and_spec", "illegal response", "404-iam-res-not-exist"))?
110+
.id;
111+
112+
Self::bind_cert_product(cert_id, &product_id, None, funs, ctx).await?;
87113
Self::bind_cert_spec(
88114
cert_id,
89-
&bind_req.spec_id,
115+
&spec_id,
90116
None,
91117
bind_req.start_time,
92118
bind_req.end_time,
@@ -240,6 +266,25 @@ impl IamOpenServ {
240266
Ok(())
241267
}
242268

269+
pub async fn general_cert(add_req: IamOpenAkSkAddReq, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<IamOpenAkSkResp> {
270+
let rel_iam_item_id = IamTenantServ::get_id_by_ctx(ctx, funs)?;
271+
let cert_conf_id = IamCertServ::get_cert_conf_id_by_kind(IamCertKernelKind::AkSk.to_string().as_str(), Some(rel_iam_item_id.clone()), funs).await
272+
.unwrap_or(
273+
IamCertAkSkServ::add_cert_conf(&IamCertConfAkSkAddOrModifyReq {
274+
name: TrimString(format!("AkSk-{}", &rel_iam_item_id)),
275+
expire_sec: None,
276+
}, Some(IamTenantServ::get_id_by_ctx(ctx, funs)?), funs, ctx).await?
277+
);
278+
let ak = TardisFuns::crypto.key.generate_ak()?;
279+
let sk = TardisFuns::crypto.key.generate_sk(&ak)?;
280+
281+
let cert_id = IamCertAkSkServ::add_cert(&IamCertAkSkAddReq {
282+
tenant_id:add_req.tenant_id,
283+
app_id:add_req.app_id,
284+
}, &ak, &sk, &cert_conf_id, funs, ctx).await?;
285+
Ok(IamOpenAkSkResp { id: cert_id, ak, sk })
286+
}
287+
243288
pub async fn refresh_cert_cumulative_count(funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
244289
let cert_ids = RbumRelServ::find_rels(
245290
&RbumRelFilterReq {

support/iam/src/console_interface/api/iam_ci_open_api.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tardis::web::poem_openapi::param::Path;
77
use tardis::web::poem_openapi::payload::Json;
88
use tardis::web::web_resp::{TardisApiResult, TardisResp, Void};
99

10-
use crate::basic::dto::iam_open_dto::{IamOpenAddProductReq, IamOpenBindAkProductReq};
10+
use crate::basic::dto::iam_open_dto::{IamOpenAddProductReq, IamOpenAkSkAddReq, IamOpenAkSkResp, IamOpenBindAkProductReq};
1111
use crate::basic::serv::iam_open_serv::IamOpenServ;
1212
use crate::iam_constants;
1313

@@ -42,9 +42,21 @@ impl IamCiOpenApi {
4242
TardisResp::ok(Void {})
4343
}
4444

45+
/// Add aksk Cert by open platform / 生成AKSK通过开放平台
46+
#[oai(path = "/aksk", method = "post")]
47+
async fn add_aksk(&self, add_req: Json<IamOpenAkSkAddReq>, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<IamOpenAkSkResp> {
48+
add_remote_ip(request, &ctx.0).await?;
49+
let mut funs = iam_constants::get_tardis_inst();
50+
funs.begin().await?;
51+
let result = IamOpenServ::general_cert(add_req.0, &funs, &ctx.0).await?;
52+
funs.commit().await?;
53+
ctx.0.execute_task().await?;
54+
TardisResp::ok(result)
55+
}
56+
4557
/// Refresh cumulative number of api calls / 刷新API累计调用数 (定时任务)
4658
#[oai(path = "/refresh_cert_cumulative_count", method = "post")]
47-
async fn refresh_cert_cumulative_count(&self, request: &Request) -> TardisApiResult<Void> {
59+
async fn refresh_cert_cumulative_count(&self, _request: &Request) -> TardisApiResult<Void> {
4860
let mut funs = iam_constants::get_tardis_inst();
4961
let ctx = TardisContext::default();
5062
funs.begin().await?;

support/iam/tests/test_cc_res.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ async fn test_single_level(context: &TardisContext, another_context: &TardisCont
7272
double_auth_msg: None,
7373
need_login: None,
7474
bind_api_res: None,
75+
ext: None,
7576
},
7677
&funs,
7778
context,
@@ -96,6 +97,7 @@ async fn test_single_level(context: &TardisContext, another_context: &TardisCont
9697
double_auth_msg: None,
9798
need_login: None,
9899
bind_api_res: None,
100+
ext: None,
99101
},
100102
&funs,
101103
another_context,
@@ -121,6 +123,7 @@ async fn test_single_level(context: &TardisContext, another_context: &TardisCont
121123
double_auth_msg: None,
122124
need_login: None,
123125
bind_api_res: None,
126+
ext: None,
124127
},
125128
&funs,
126129
another_context
@@ -146,6 +149,7 @@ async fn test_single_level(context: &TardisContext, another_context: &TardisCont
146149
double_auth_msg: None,
147150
need_login: None,
148151
bind_api_res: None,
152+
ext: None,
149153
},
150154
&funs,
151155
context,
@@ -232,6 +236,7 @@ async fn test_multi_level_add<'a>(
232236
double_auth: Some(false),
233237
need_login: Some(false),
234238
bind_api_res: None,
239+
ext: None,
235240
double_auth_msg: None,
236241
},
237242
funs,
@@ -257,6 +262,7 @@ async fn test_multi_level_add<'a>(
257262
double_auth_msg: None,
258263
need_login: None,
259264
bind_api_res: None,
265+
ext: None,
260266
},
261267
funs,
262268
sys_context,
@@ -281,6 +287,7 @@ async fn test_multi_level_add<'a>(
281287
double_auth_msg: None,
282288
need_login: None,
283289
bind_api_res: None,
290+
ext: None,
284291
},
285292
funs,
286293
t1_context,
@@ -305,6 +312,7 @@ async fn test_multi_level_add<'a>(
305312
need_login: Some(false),
306313
double_auth_msg: None,
307314
bind_api_res: None,
315+
ext: None,
308316
},
309317
funs,
310318
t2_context,
@@ -329,6 +337,7 @@ async fn test_multi_level_add<'a>(
329337
double_auth_msg: None,
330338
need_login: None,
331339
bind_api_res: None,
340+
ext: None,
332341
},
333342
funs,
334343
t2_context,
@@ -353,6 +362,7 @@ async fn test_multi_level_add<'a>(
353362
double_auth_msg: None,
354363
need_login: None,
355364
bind_api_res: None,
365+
ext: None,
356366
},
357367
funs,
358368
t2_a1_context,
@@ -377,6 +387,7 @@ async fn test_multi_level_add<'a>(
377387
double_auth_msg: None,
378388
need_login: None,
379389
bind_api_res: None,
390+
ext: None,
380391
},
381392
funs,
382393
t2_a2_context,
@@ -437,6 +448,7 @@ pub async fn test_multi_level_by_sys_context(
437448
double_auth_msg: None,
438449
need_login: None,
439450
bind_api_res: None,
451+
ext: None,
440452
},
441453
&funs,
442454
sys_context,
@@ -460,6 +472,7 @@ pub async fn test_multi_level_by_sys_context(
460472
double_auth_msg: None,
461473
need_login: None,
462474
bind_api_res: None,
475+
ext: None,
463476
},
464477
&funs,
465478
sys_context,
@@ -483,6 +496,7 @@ pub async fn test_multi_level_by_sys_context(
483496
double_auth_msg: None,
484497
need_login: None,
485498
bind_api_res: None,
499+
ext: None,
486500
},
487501
&funs,
488502
sys_context,
@@ -612,6 +626,7 @@ pub async fn test_multi_level_by_tenant_context(
612626
double_auth_msg: None,
613627
need_login: None,
614628
bind_api_res: None,
629+
ext: None,
615630
},
616631
&funs,
617632
t1_context,
@@ -636,6 +651,7 @@ pub async fn test_multi_level_by_tenant_context(
636651
double_auth_msg: None,
637652
need_login: None,
638653
bind_api_res: None,
654+
ext: None,
639655
},
640656
&funs,
641657
t1_context,
@@ -660,6 +676,7 @@ pub async fn test_multi_level_by_tenant_context(
660676
double_auth_msg: None,
661677
need_login: None,
662678
bind_api_res: None,
679+
ext: None,
663680
},
664681
&funs,
665682
t2_context,
@@ -684,6 +701,7 @@ pub async fn test_multi_level_by_tenant_context(
684701
double_auth_msg: None,
685702
need_login: None,
686703
bind_api_res: None,
704+
ext: None,
687705
},
688706
&funs,
689707
t2_context,
@@ -707,6 +725,7 @@ pub async fn test_multi_level_by_tenant_context(
707725
double_auth_msg: None,
708726
need_login: None,
709727
bind_api_res: None,
728+
ext: None,
710729
},
711730
&funs,
712731
t2_context,
@@ -826,6 +845,7 @@ pub async fn test_multi_level_by_app_context(
826845
double_auth_msg: None,
827846
need_login: None,
828847
bind_api_res: None,
848+
ext: None,
829849
},
830850
&funs,
831851
t2_a1_context,
@@ -850,6 +870,7 @@ pub async fn test_multi_level_by_app_context(
850870
double_auth_msg: None,
851871
need_login: None,
852872
bind_api_res: None,
873+
ext: None,
853874
},
854875
&funs,
855876
t2_a1_context,
@@ -874,6 +895,7 @@ pub async fn test_multi_level_by_app_context(
874895
double_auth_msg: None,
875896
need_login: None,
876897
bind_api_res: None,
898+
ext: None,
877899
},
878900
&funs,
879901
t2_a1_context,
@@ -898,6 +920,7 @@ pub async fn test_multi_level_by_app_context(
898920
double_auth_msg: None,
899921
need_login: None,
900922
bind_api_res: None,
923+
ext: None,
901924
},
902925
&funs,
903926
t2_a1_context,
@@ -922,6 +945,7 @@ pub async fn test_multi_level_by_app_context(
922945
double_auth_msg: None,
923946
need_login: None,
924947
bind_api_res: None,
948+
ext: None,
925949
},
926950
&funs,
927951
t2_a1_context,
@@ -946,6 +970,7 @@ pub async fn test_multi_level_by_app_context(
946970
double_auth_msg: None,
947971
need_login: None,
948972
bind_api_res: None,
973+
ext: None,
949974
},
950975
&funs,
951976
t2_a1_context,

support/iam/tests/test_cc_role.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ async fn test_single_level(context: &TardisContext, account_name: &str, another_
175175
double_auth_msg: None,
176176
need_login: None,
177177
bind_api_res: None,
178+
ext: None,
178179
},
179180
&funs,
180181
context,

0 commit comments

Comments
 (0)