Skip to content

Commit 640e03e

Browse files
authored
feat(apigw_manager/plugin): 新增 bk-access-token-source 插件 (#238)
1 parent 1c9cbe5 commit 640e03e

2 files changed

Lines changed: 78 additions & 10 deletions

File tree

sdks/apigw-manager/src/apigw_manager/plugin/config.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ class HeadersConfig:
572572
remove: List[str]
573573

574574

575-
def build_response_rewrite_validation(
575+
def build_response_rewrite(
576576
status_code: int,
577577
body: str,
578578
vars: Optional[str],
@@ -648,7 +648,7 @@ def build_response_rewrite_validation(
648648
}
649649

650650

651-
def build_redirect_validation(
651+
def build_redirect(
652652
uri: str,
653653
ret_code: int = 302,
654654
) -> Dict[str, str]:
@@ -682,6 +682,37 @@ def build_redirect_validation(
682682
}
683683

684684

685+
def build_bk_access_token_source(
686+
source: str,
687+
) -> Dict[str, str]:
688+
"""generate bk-access-token-source plugin config
689+
690+
Args:
691+
source (str): access_token 来源。Defaults to bearer.
692+
693+
Raises:
694+
ValueError: source must be bearer or api_key
695+
696+
Returns:
697+
{
698+
"type": "bk-access-token-source",
699+
"yaml": "source: bearer\n"
700+
}
701+
"""
702+
703+
if source not in ["bearer", "api_key"]:
704+
raise ValueError("source must be bearer or api_key.")
705+
706+
return {
707+
"type": "bk-access-token-source",
708+
"yaml": yaml_dump(
709+
{
710+
"source": source,
711+
}
712+
),
713+
}
714+
715+
685716
def _check_percentage(percentage: int, location: str):
686717
if percentage and not (0 < percentage <= 100):
687718
raise ValueError(f"The percentage of {location} must be greater than 0 and less than or equal to 100")

sdks/apigw-manager/tests/apigw_manager/plugin/test_config.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
build_api_breaker,
1919
build_fault_injection,
2020
build_request_validation,
21-
build_response_rewrite_validation,
22-
build_redirect_validation,
21+
build_response_rewrite,
22+
build_redirect,
23+
build_bk_access_token_source,
2324
build_stage_plugin_config_for_definition_yaml,
2425
UnhealthyConfig,
2526
HealthyConfig,
@@ -565,15 +566,15 @@ def test_build_request_validation(
565566
),
566567
]
567568
)
568-
def test_build_response_rewrite_validation(
569+
def test_build_response_rewrite(
569570
self, status_code, body, vars, body_base64, headers, will_error, expected
570571
):
571572
if will_error:
572573
with pytest.raises(ValueError):
573-
build_response_rewrite_validation(status_code, body, vars, body_base64, headers)
574+
build_response_rewrite(status_code, body, vars, body_base64, headers)
574575
return
575576

576-
assert build_response_rewrite_validation(status_code, body, vars, body_base64, headers) == expected
577+
assert build_response_rewrite(status_code, body, vars, body_base64, headers) == expected
577578

578579
@pytest.mark.parametrize(
579580
"uri, ret_code, will_error, expected",
@@ -595,15 +596,51 @@ def test_build_response_rewrite_validation(
595596
),
596597
]
597598
)
598-
def test_build_redirect_validation(
599+
def test_build_redirect(
599600
self, uri, ret_code, will_error, expected
600601
):
601602
if will_error:
602603
with pytest.raises(ValueError):
603-
build_redirect_validation(uri, ret_code)
604+
build_redirect(uri, ret_code)
604605
return
605606

606-
assert build_redirect_validation(uri, ret_code) == expected
607+
assert build_redirect(uri, ret_code) == expected
608+
609+
@pytest.mark.parametrize(
610+
"source, will_error, expected",
611+
[
612+
(
613+
"bearer",
614+
False,
615+
{
616+
"type": "bk-access-token-source",
617+
"yaml": "source: bearer\n"
618+
},
619+
),
620+
(
621+
"api_key",
622+
False,
623+
{
624+
"type": "bk-access-token-source",
625+
"yaml": "source: api_key\n"
626+
},
627+
),
628+
(
629+
"test",
630+
True,
631+
None,
632+
),
633+
]
634+
)
635+
def test_build_redirect(
636+
self, source, will_error, expected
637+
):
638+
if will_error:
639+
with pytest.raises(ValueError):
640+
build_bk_access_token_source(source)
641+
return
642+
643+
assert build_bk_access_token_source(source) == expected
607644

608645

609646
class TestBuildStagePluginConfigForDefinitionYaml:

0 commit comments

Comments
 (0)