@@ -38,12 +38,44 @@ class PolicyAdminException(Exception):
3838 """
3939
4040
41- class PolicyAdminTokenException (Exception ):
41+ class PolicyAdminTokenException (PolicyAdminException ):
4242 """
4343 A token check exception, indicating that a file is in unexpected state.
4444 """
4545
4646
47+ class PolicyAdminFileNotFoundException (PolicyAdminException ):
48+ """
49+ Policy cannot be found.
50+ """
51+
52+
53+ class PolicyAdminProtocolException (PolicyAdminException ):
54+ """
55+ Client sent an invalid request that it should have known to not send in the
56+ first place.
57+ """
58+
59+
60+ class PolicyAdminSyntaxException (PolicyAdminProtocolException ):
61+ """
62+ Client sent an invalid policy. That it should have known to not send in the
63+ first place. Clients should validate the policy before sending them.
64+ """
65+
66+
67+ class PolicyAdminInvalidFileNameException (PolicyAdminProtocolException ):
68+ """
69+ Client sent a policy with invalid name.
70+ """
71+
72+
73+ class PolicyAdminInvalidFilePathException (PolicyAdminProtocolException ):
74+ """
75+ Client sent a policy using a path out of bounds.
76+ """
77+
78+
4779def method (service_name , * , no_arg = False , no_payload = False ):
4880 def decorator (func ):
4981 func .api_service_name = service_name
@@ -89,29 +121,29 @@ def handle_request(
89121 """
90122
91123 if not all (char in RPCNAME_ALLOWED_CHARSET for char in arg ):
92- raise PolicyAdminException (
124+ raise PolicyAdminProtocolException (
93125 'Invalid argument: "{}"\n '
94126 "Valid characters are letters, numbers, dot, plus, hyphen and "
95127 "underline" .format (arg )
96128 )
97129
98130 func = self ._find_method (service_name )
99131 if not func :
100- raise PolicyAdminException (
132+ raise PolicyAdminProtocolException (
101133 "unrecognized method: {}" .format (service_name )
102134 )
103135
104136 args = []
105137
106138 if func .api_no_arg :
107139 if arg != "" :
108- raise PolicyAdminException ("Unexpected argument" )
140+ raise PolicyAdminProtocolException ("Unexpected argument" )
109141 else :
110142 args .append (arg )
111143
112144 if func .api_no_payload :
113145 if payload != b"" :
114- raise PolicyAdminException ("Unexpected payload" )
146+ raise PolicyAdminProtocolException ("Unexpected payload" )
115147 else :
116148 args .append (payload )
117149
@@ -178,7 +210,7 @@ def policy_include_get(self, arg):
178210
179211 def _common_get (self , path : Path ) -> bytes :
180212 if not path .is_file ():
181- raise PolicyAdminException ("Not found: {}" .format (path ))
213+ raise PolicyAdminFileNotFoundException ("Not found: {}" .format (path ))
182214
183215 data = path .read_bytes ()
184216 token = compute_token (data )
@@ -198,7 +230,7 @@ def policy_include_replace(self, arg, payload):
198230
199231 def _common_replace (self , path : Path , payload : bytes ) -> bytes :
200232 if b"\n " not in payload :
201- raise PolicyAdminException (
233+ raise PolicyAdminProtocolException (
202234 "Payload needs to include first line with token"
203235 )
204236 token , data = payload .split (b"\n " , 1 )
@@ -234,7 +266,7 @@ def _common_remove(self, path: Path, payload: str) -> None:
234266 self ._check_token (payload , path )
235267
236268 if not path .is_file ():
237- raise PolicyAdminException ("Not found: {}" .format (path ))
269+ raise PolicyAdminFileNotFoundException ("Not found: {}" .format (path ))
238270
239271 self ._validate (path , None )
240272
@@ -245,10 +277,10 @@ def _common_remove(self, path: Path, payload: str) -> None:
245277 @method ("policy.GetFiles" , no_payload = True )
246278 def policy_get_files (self , arg ):
247279 if not isinstance (arg , str ) or not arg :
248- raise PolicyAdminException ("Service cannot be empty." )
280+ raise PolicyAdminProtocolException ("Service cannot be empty." )
249281 invalid_chars = get_invalid_characters (arg , disallowed = "+" )
250282 if invalid_chars :
251- raise PolicyAdminException (
283+ raise PolicyAdminProtocolException (
252284 "Service {!r} contains invalid characters: {!r}" .format (
253285 arg , invalid_chars
254286 )
@@ -276,15 +308,15 @@ def policy_get_files(self, arg):
276308
277309 def _get_path (self , arg : str , dir_path : str , suffix : str ) -> Path :
278310 if not re .compile (r"^[\w-]+$" ).match (arg ):
279- raise PolicyAdminException (
311+ raise PolicyAdminInvalidFileNameException (
280312 f"Invalid policy file name: { arg } \n "
281313 "Names must contain only alphanumeric characters, "
282314 "underscore and hyphen."
283315 )
284316 path = dir_path / (arg + suffix )
285317 path = path .resolve ()
286318 if path .parent != dir_path :
287- raise PolicyAdminException (
319+ raise PolicyAdminInvalidFilePathException (
288320 "Expecting a path inside {}" .format (dir_path )
289321 )
290322
@@ -296,7 +328,7 @@ def _validate(self, path: Path, content: Optional[str]):
296328 policy_path = self .policy_path , overrides = {path : content }
297329 )
298330 except PolicySyntaxError as exc :
299- raise PolicyAdminException (
331+ raise PolicyAdminSyntaxException (
300332 "Policy change validation failed: {}" .format (exc )
301333 ) from exc
302334
@@ -312,7 +344,7 @@ def _check_token(self, token: bytes, path: Path):
312344 return
313345
314346 if not token .startswith (b"sha256:" ):
315- raise PolicyAdminException ("Unrecognized token" )
347+ raise PolicyAdminProtocolException ("Unrecognized token" )
316348
317349 if not path .exists ():
318350 raise PolicyAdminTokenException (
0 commit comments