@@ -729,6 +729,142 @@ def build_bk_username_required() -> Dict[str, str]:
729729 }
730730
731731
732+ def build_bk_request_body_limit (
733+ max_body_size : int ,
734+ ) -> Dict [str , str ]:
735+ """generate bk-request-body-limit plugin config
736+
737+ Args:
738+ max_body_size (int): 最大请求体大小,最大值 33554432(32 M)。
739+
740+ Raises:
741+ ValueError: max_body_size must be between 1 byte and 33554432 bytes
742+
743+ Returns:
744+ {
745+ "type": "bk-request-body-limit",
746+ "yaml": "max_body_size: 1024\n "
747+ }
748+ """
749+
750+ if not (1 <= int (max_body_size ) <= 32 * 1024 * 1024 ):
751+ raise ValueError ("max_body_size must be between 1 byte and 33554432 bytes" )
752+
753+ return {
754+ "type" : "bk-request-body-limit" ,
755+ "yaml" : yaml_dump (
756+ {
757+ "max_body_size" : max_body_size ,
758+ }
759+ ),
760+ }
761+
762+
763+ def build_bk_user_restriction (
764+ whitelist : Optional [List [str ]] = None ,
765+ blacklist : Optional [List [str ]] = None ,
766+ ) -> Dict [str , str ]:
767+ """generate bk-user-restriction plugin config
768+
769+ Args:
770+ whitelist (Optional[List[str]], optional): 用户白名单列表。Defaults to None.
771+ blacklist (Optional[List[str]], optional): 用户黑名单列表。Defaults to None.
772+
773+ Raises:
774+ ValueError: whitelist and blacklist can not be set at the same time
775+ ValueError: whitelist or blacklist should be set
776+
777+ Returns:
778+ {
779+ "type": "bk-user-restriction",
780+ "yaml": "whitelist:\n - key: admin\n "
781+ }
782+ """
783+
784+ if whitelist and blacklist :
785+ raise ValueError ("whitelist and blacklist can not be set at the same time" )
786+
787+ if not (whitelist or blacklist ):
788+ raise ValueError ("whitelist or blacklist should be set" )
789+
790+ if whitelist :
791+ return {
792+ "type" : "bk-user-restriction" ,
793+ "yaml" : yaml_dump (
794+ {
795+ "whitelist" : [{"key" : k } for k in whitelist ],
796+ },
797+ ),
798+ }
799+
800+ if blacklist :
801+ return {
802+ "type" : "bk-user-restriction" ,
803+ "yaml" : yaml_dump (
804+ {
805+ "blacklist" : [{"key" : k } for k in blacklist ],
806+ },
807+ ),
808+ }
809+
810+
811+ def build_bk_legacy_invalid_params () -> Dict [str , str ]:
812+ """generate bk-legacy-invalid-params plugin config
813+
814+ Returns:
815+ {
816+ "type": "bk-legacy-invalid-params",
817+ "yaml": ""
818+ }
819+ """
820+
821+ return {
822+ "type" : "bk-legacy-invalid-params" ,
823+ "yaml" : "" ,
824+ }
825+
826+
827+ def build_proxy_cache (
828+ cache_method : List [str ],
829+ cache_ttl : int = 300 ,
830+ ) -> Dict [str , str ]:
831+ """generate proxy-cache plugin config
832+
833+ Args:
834+ cache_method (List[str]): 缓存方法,仅支持:GET, HEAD。Defaults to [GET].
835+ cache_ttl (int): 缓存时间,最大值 3600 秒。Defaults to 300.
836+
837+ Raises:
838+ ValueError: cache_method only supports GET and HEAD
839+ ValueError: cache_ttl must be between 1 and 3600 seconds
840+
841+ Returns:
842+ {
843+ "type": "proxy-cache",
844+ "yaml": "cache_method:\n - key: GET\n cache_ttl: 300\n "
845+ }
846+ """
847+
848+ cache_method_data = []
849+ for method in cache_method :
850+ if method not in ["GET" , "HEAD" ]:
851+ raise ValueError ("cache_method only supports GET and HEAD" )
852+ cache_method_data .append ({"key" : method })
853+
854+ if not (1 <= cache_ttl <= 3600 ):
855+ raise ValueError ("cache_ttl must be between 1 and 3600 seconds" )
856+
857+ return {
858+ "type" : "proxy-cache" ,
859+ "yaml" : yaml_dump (
860+ {
861+ "cache_method" : cache_method_data ,
862+ "cache_ttl" : cache_ttl ,
863+ }
864+ ),
865+ }
866+
867+
732868def _check_percentage (percentage : int , location : str ):
733869 if percentage and not (0 < percentage <= 100 ):
734870 raise ValueError (f"The percentage of { location } must be greater than 0 and less than or equal to 100" )
0 commit comments