def testGetPreferredAuthForClusterMissingClusterOrConfig(self): self.assertIsNone( anthoscli_backend.GetPreferredAuthForCluster( cluster=None, login_config=self.v2_ex1_path)[0]) self.assertIsNone( anthoscli_backend.GetPreferredAuthForCluster(cluster='mycluster', login_config=None)[0])
def testURLContentsGetPreferredAuthForClusterNoPrompt(self): config_contents = files.ReadFileContents(self.v2_ex1_path) auth_method, _, _ = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster', login_config='https://www.example.com', config_contents=config_contents, is_url=True) self.assertEqual(auth_method, 'oidc1')
def testGetPreferredAuthForCluster(self): # Make a Multi V2 File self.WriteInput('1') auth_method, _, _ = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster-3', login_config=self.v2_ex2_multi_path) self.assertEqual(auth_method, 'basic') self.AssertFileContains('preferredAuthentication: basic', self.v2_ex2_multi_path) self.AssertErrContains('PROMPT_CHOICE')
def testURLContentsGetPreferredAuthForClusterBadCluster(self): config_contents = files.ReadFileContents(self.v2_ex1_path) with self.assertRaises(anthoscli_backend.AnthosAuthException): anthoscli_backend.GetPreferredAuthForCluster( cluster='non-cluster', login_config='https://www.example.com', config_contents=config_contents, is_url=True)
def testURLContentsGetPreferredAuthForClusterOldVersion(self): config_contents = files.ReadFileContents(self.v1_ex1_path) self.assertIsNone( anthoscli_backend.GetPreferredAuthForCluster( cluster='mycluster', login_config='https://www.example.com', config_contents=config_contents, is_url=True)[0])
def testGetPreferredAuthForClusterOneOption(self): auth_method, _, _ = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster', login_config=self.v2_ex3_1p, force_update=True) self.assertEqual(auth_method, 'oidc1') self.AssertFileContains('preferredAuthentication: oidc1', self.v2_ex3_1p) self.AssertErrContains('Setting Preferred Authentication option to') self.AssertErrNotContains('PROMPT_CHOICE')
def testURLContentsGetPreferredAuthForClusterMissingProviders(self): config_contents = files.ReadFileContents(self.v2_ex2_missing_providers) with self.assertRaisesRegexp(anthoscli_backend.AnthosAuthException, r'No Authentication Providers found'): anthoscli_backend.GetPreferredAuthForCluster( cluster='test-cluster', login_config='https://www.example.com', config_contents=config_contents, is_url=True)
def testURLContentsGetPreferredAuthForClusterOneOption(self): config_contents = files.ReadFileContents(self.v2_ex3_1p) auth_method, _, _ = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster', login_config='https://www.example.com', config_contents=config_contents, is_url=True, force_update=True) self.assertEqual(auth_method, 'oidc1') self.AssertErrContains('Setting Preferred Authentication option to') self.AssertErrNotContains('PROMPT_CHOICE')
def testGetPreferredAuthForClusterForceUpdate(self): self.WriteInput('3') auth_method, _, _ = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster', login_config=self.v2_ex1_path, force_update=True) self.assertEqual(auth_method, 'oidc2') self.AssertFileContains('preferredAuthentication: oidc2', self.v2_ex1_path) self.AssertErrContains( 'This will overwrite current preferred auth method') self.AssertErrContains('PROMPT_CHOICE')
def testURLContentsGetPreferredAuthForCluster(self): config_contents = files.ReadFileContents(self.v2_ex2_multi_path) self.WriteInput('1') auth_method, _, _ = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster-3', login_config='https://www.example.com', config_contents=config_contents, is_url=True) self.assertEqual(auth_method, 'basic') self.AssertErrNotContains( 'This will overwrite current preferred auth method') self.AssertErrContains('PROMPT_CHOICE')
def testGetPreferredAuthForClusterOneOptionLDAP(self): self.StartObjectPatch(getpass, 'getpass').return_value = 'password' self.WriteInput('user') auth_method, username, passwd = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster', login_config=self.v2_ex4_1p_ldap, force_update=True) self.assertEqual(auth_method, 'ldap2') self.assertEqual(username, 'dXNlcg==') self.assertEqual(passwd, 'cGFzc3dvcmQ=') self.AssertFileContains('preferredAuthentication: ldap2', self.v2_ex4_1p_ldap) self.AssertErrContains('Setting Preferred Authentication option to') self.AssertErrNotContains('PROMPT_CHOICE')
def testGetPreferredAuthForClusterWithLdap(self): self.StartObjectPatch(getpass, 'getpass').return_value = 'password' self.WriteInput('5') self.WriteInput('user') auth_method, username, passwd = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster', login_config=self.v2_ex1_path, force_update=True) self.assertEqual(auth_method, 'ldap2') self.assertEqual(username, 'dXNlcg==') self.assertEqual(passwd, 'cGFzc3dvcmQ=') self.AssertFileContains('preferredAuthentication: ldap2', self.v2_ex1_path) self.AssertErrContains( 'This will overwrite current preferred auth method') self.AssertErrContains('PROMPT_CHOICE')
def testURLContentsGetPreferredAuthForClusterOneOptionLDAP(self): config_contents = files.ReadFileContents(self.v2_ex4_1p_ldap) self.StartObjectPatch(getpass, 'getpass').return_value = 'password' self.WriteInput('user') auth_method, username, passwd = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster', login_config='https://www.example.com', config_contents=config_contents, is_url=True, force_update=True) self.assertEqual(auth_method, 'ldap2') self.assertEqual(username, 'dXNlcg==') self.assertEqual(passwd, 'cGFzc3dvcmQ=') self.AssertErrContains('Setting Preferred Authentication option to') self.AssertErrNotContains('PROMPT_CHOICE')
def testURLContentsGetPreferredAuthForClusterWithLdap(self): config_contents = files.ReadFileContents(self.v2_ex1_path) self.StartObjectPatch(getpass, 'getpass').return_value = 'password' self.WriteInput('5') self.WriteInput('user') auth_method, username, passwd = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster', login_config='https://www.example.com', config_contents=config_contents, is_url=True, force_update=True) self.assertEqual(auth_method, 'ldap2') self.assertEqual(username, 'dXNlcg==') self.assertEqual(passwd, 'cGFzc3dvcmQ=') self.AssertErrNotContains( 'This will overwrite current preferred auth method') self.AssertErrContains('PROMPT_CHOICE')
def Run(self, args): cluster = args.CLUSTER config_file = args.login_config force_update = args.set_preferred_auth anthoscli_backend.GetPreferredAuthForCluster(cluster, config_file, force_update) command_executor = anthoscli_backend.AnthosAuthWrapper() log.status.Print(self._LOGIN_CONFIG_MESSAGE) response = command_executor( command='login', cluster=cluster, kube_config=args.kubeconfig, user=args.user, login_config=config_file, login_config_cert=args.login_config_cert, dry_run=args.dry_run, show_exec_error=args.show_exec_error, env=anthoscli_backend.GetEnvArgsForCommand()) return self._LoginResponseHandler(response)
def Run(self, args): command_executor = anthoscli_backend.AnthosAuthWrapper() cluster = args.CLUSTER # Get Default Path if flag not provided. login_config = args.login_config or command_executor.default_config_path # Get contents of config, parsing either URL or File. login_config, config_contents, is_url = anthoscli_backend.GetFileOrURL( login_config, args.login_config_cert) # Get Preferred Auth Method and handle prompting. force_update = args.set_preferred_auth authmethod, ldapuser, ldappass = anthoscli_backend.GetPreferredAuthForCluster( cluster=cluster, login_config=login_config, config_contents=config_contents, force_update=force_update, is_url=is_url) # Log and execute binary command with flags. log.status.Print(messages.LOGIN_CONFIG_MESSAGE) response = command_executor( command='login', cluster=cluster, kube_config=args.kubeconfig, user=args.user, login_config=login_config, login_config_cert=args.login_config_cert, dry_run=args.dry_run, show_exec_error=args.show_exec_error, ldap_user=ldapuser, ldap_pass=ldappass, preferred_auth=authmethod, env=anthoscli_backend.GetEnvArgsForCommand( extra_vars={'GCLOUD_AUTH_PLUGIN': 'true'})) return anthoscli_backend.LoginResponseHandler( response, list_clusters_only=(cluster is None))
def testGetPreferredAuthFromURLFails(self): with self.assertRaises(anthoscli_backend.AnthosAuthException): _, _, _ = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster', login_config='https://www.example.com', is_url=True)
def testGetPreferredAuthForClusterNoPrompt(self): # Make a Multi V2 File auth_method, _, _ = anthoscli_backend.GetPreferredAuthForCluster( cluster='testcluster', login_config=self.v2_ex1_path) self.assertEqual(auth_method, 'oidc1')
def testGetPreferredAuthForClusterMissingProviders(self): with self.assertRaisesRegexp(anthoscli_backend.AnthosAuthException, r'No Authentication Providers found'): anthoscli_backend.GetPreferredAuthForCluster( cluster='test-cluster', login_config=self.v2_ex2_missing_providers)
def testGetPreferredAuthForClusterOldVersion(self): self.assertIsNone( anthoscli_backend.GetPreferredAuthForCluster( cluster='mycluster', login_config=self.v1_ex1_path)[0])
def testGetPreferredAuthForClusterBadCluster(self): with self.assertRaises(anthoscli_backend.AnthosAuthException): anthoscli_backend.GetPreferredAuthForCluster( cluster='non-cluster', login_config=self.v2_ex1_path)
def testGetPreferredAuthForClusterBadConfigFile(self): with self.assertRaises(file_parsers.YamlConfigFileError): anthoscli_backend.GetPreferredAuthForCluster( cluster='mycluster', login_config='BAD_PATH')