@@ -242,15 +242,10 @@ def __init__(self):
242
242
self ._original_run_command = shellutil .run_command
243
243
self ._run_command_patcher = patch ("azurelinuxagent.ga.firewall_manager.shellutil.run_command" , side_effect = self ._mock_run_command )
244
244
#
245
- # Return values for each nft command-line indexed by command name ("add", " delete", "list") . Each item is a (exit_code, stdout) tuple.
246
- # These default values indicate success, and can be overridden with the set_*_return_values () methods .
245
+ # Return values for the " delete" and "list" options of the nft command . Each item is a (exit_code, stdout) tuple.
246
+ # The default values below indicate success, and can be overridden with the set_return_value () method .
247
247
#
248
248
self ._return_values = {
249
- "add" : {
250
- "table" : (0 , '' ), # nft add table ip walinuxagent
251
- "chain" : (0 , '' ), # nft add chain ip walinuxagent output { type filter hook output priority 0 ; policy accept ; }
252
- "rule" : (0 , '' ), # nft add rule ip walinuxagent output ip daddr 168.63.129.16 tcp dport != 53 skuid != 0 ct state invalid,new counter drop
253
- },
254
249
"delete" : {
255
250
"table" : (0 , '' ), # nft delete table walinuxagent
256
251
},
@@ -300,15 +295,17 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
300
295
def _mock_run_command (self , command , * args , ** kwargs ):
301
296
if command [0 ] == 'nft' :
302
297
command_string = " " .join (command )
298
+ if command_string == "nft --version" :
299
+ # return a hardcoded version string and don't add the command to the call list
300
+ return self ._original_run_command (['echo' , 'nftables v1.0.2 (Lester Gooch)' ], * args , ** kwargs )
301
+ elif command_string == 'nft -f -' :
302
+ # if we are executing an nft script, add the script to the call list and return success with no stdout (empty string)
303
+ script = self ._original_run_command (['cat' ], * args , ** kwargs )
304
+ self ._call_list .append (script )
305
+ return self ._original_run_command (['echo' , '-n' ], * args , ** kwargs )
306
+ # get the exit code and stdout from the pre-defined table of return values and add the command to the call list
303
307
exit_code , stdout = self .get_return_value (command_string )
304
- script = \
305
- """
306
- cat << ..
307
- {0}
308
- ..
309
- exit {1}
310
- """ .format (stdout , exit_code )
311
- command = ['sh' , '-c' , script ]
308
+ command = ['sh' , '-c' , "echo '{0}'; exit {1}" .format (stdout , exit_code )]
312
309
self ._call_list .append (command_string )
313
310
return self ._original_run_command (command , * args , ** kwargs )
314
311
@@ -323,29 +320,18 @@ def set_return_value(self, command, target, return_value):
323
320
"""
324
321
Changes the return values for the mocked command
325
322
"""
323
+ if command not in self ._return_values or target not in self ._return_values [command ]:
324
+ raise Exception ("Unexpected command: {0} {1}" .format (command , target ))
326
325
self ._return_values [command ][target ] = return_value
327
326
328
327
def get_return_value (self , command ):
329
328
"""
330
329
Possible commands are:
331
330
332
- nft add table ip walinuxagent
333
- nft add chain ip walinuxagent output { type filter hook output priority 0 ; policy accept ; }
334
- nft add rule ip walinuxagent output ip daddr 168.63.129.16 tcp dport != 53 skuid != 0 ct state invalid,new counter drop
335
331
nft delete table walinuxagent
336
332
nft --json list tables
337
333
nft --json list table walinuxagent
338
334
"""
339
- r = r"nft add (?P<target>table|chain|rule)" + \
340
- r"(ip walinuxagent output " + \
341
- r"(\{ type filter hook output priority 0 ; policy accept ; })" + \
342
- r"|" + \
343
- r"(ip daddr 168.63.129.16 tcp dport != 53 skuid != \d+ ct state invalid,new counter drop)" + \
344
- r")?"
345
- match = re .match (r , command )
346
- if match is not None :
347
- target = match .group ("target" )
348
- return self ._return_values ["add" ][target ]
349
335
if command == "nft delete table walinuxagent" :
350
336
return self ._return_values ["delete" ]["table" ]
351
337
match = re .match (r"nft --json list (?P<target>tables|table)( walinuxagent)?" , command )
@@ -354,20 +340,6 @@ def get_return_value(self, command):
354
340
return self ._return_values ["list" ][target ]
355
341
raise Exception ("Unexpected command: {0}" .format (command ))
356
342
357
- @staticmethod
358
- def get_add_command (target ):
359
- if target == "table" :
360
- return "nft add table ip walinuxagent"
361
- if target == "chain" :
362
- return "nft add chain ip walinuxagent output { type filter hook output priority 0 ; policy accept ; }"
363
- if target == "rule" :
364
- return "nft add rule ip walinuxagent output ip daddr 168.63.129.16 tcp dport != 53 skuid != {0} ct state invalid,new counter drop" .format (os .getuid ())
365
- raise Exception ("Unexpected command target: {0}" .format (target ))
366
-
367
- @staticmethod
368
- def get_delete_command ():
369
- return "nft delete table walinuxagent"
370
-
371
343
@staticmethod
372
344
def get_list_command (target ):
373
345
if target == "tables" :
0 commit comments