@@ -484,6 +484,41 @@ def test_merge_credentials_already_present(self):
484484 self .assertEqual (merged ['users' ], expected_users )
485485 self .assertEqual (merged ['current-context' ], obj2 ['current-context' ])
486486
487+ @unittest .skipIf (os .name == 'nt' , 'Symlink test not applicable on Windows' )
488+ def test_merge_credentials_rejects_symlink (self ):
489+ # Create a real kubeconfig file and a symlink pointing to it
490+ target = tempfile .NamedTemporaryFile (delete = False , suffix = '.kubeconfig' )
491+ target .close ()
492+ with open (target .name , 'w' ) as f :
493+ yaml .safe_dump ({'clusters' : [], 'contexts' : [], 'users' : [],
494+ 'current-context' : '' , 'kind' : 'Config' }, f )
495+ self .addCleanup (os .remove , target .name )
496+
497+ symlink_path = target .name + '.link'
498+ os .symlink (target .name , symlink_path )
499+ self .addCleanup (lambda : os .remove (symlink_path ) if os .path .islink (symlink_path ) else None )
500+
501+ addition = tempfile .NamedTemporaryFile (delete = False )
502+ addition .close ()
503+ obj = {
504+ 'clusters' : [{'cluster' : {'server' : 'https://test' }, 'name' : 'c1' }],
505+ 'contexts' : [{'context' : {'cluster' : 'c1' , 'user' : 'u1' }, 'name' : 'ctx1' }],
506+ 'users' : [{'name' : 'u1' , 'user' : {'token' : 'tok' }}],
507+ 'current-context' : 'ctx1' ,
508+ }
509+ with open (addition .name , 'w' ) as f :
510+ yaml .safe_dump (obj , f )
511+ self .addCleanup (os .remove , addition .name )
512+
513+ # Should raise CLIError when existing_file is a symlink
514+ with self .assertRaises (CLIError ):
515+ merge_kubernetes_configurations (symlink_path , addition .name , False )
516+
517+ # Verify the symlink target was not modified
518+ with open (target .name , 'r' ) as f :
519+ content = yaml .safe_load (f )
520+ self .assertEqual (content ['clusters' ], [])
521+
487522 @mock .patch ('azure.cli.command_modules.acs.addonconfiguration.get_rg_location' , return_value = 'eastus' )
488523 @mock .patch ('azure.cli.command_modules.acs.addonconfiguration.get_resource_groups_client' , autospec = True )
489524 @mock .patch ('azure.cli.command_modules.acs.addonconfiguration.get_resources_client' , autospec = True )
0 commit comments