Package pulp :: Package client :: Package core :: Module core_auth
[hide private]
[frames] | no frames]

Source Code for Module pulp.client.core.core_auth

  1  #!/usr/bin/python 
  2  # 
  3  # Pulp Repo management module 
  4  # 
  5  # Copyright (c) 2010 Red Hat, Inc. 
  6  # 
  7  # This software is licensed to you under the GNU General Public License, 
  8  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  9  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
 10  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 11  # along with this software; if not, see 
 12  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 13  # 
 14  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 15  # granted to use or replicate Red Hat trademarks that are incorporated 
 16  # in this software or its documentation. 
 17   
 18  import os 
 19  import sys 
 20   
 21  import pulp.client.auth_utils as auth_utils 
 22  from pulp.client.logutil import getLogger 
 23  from pulp.client.config import Config 
 24  from pulp.client.connection import UserConnection 
 25  from pulp.client.core.basecore import BaseCore 
 26   
 27  import gettext 
 28  _ = gettext.gettext 
 29  log = getLogger(__name__) 
 30   
 31   
 32  CFG = Config() 
 33   
 34   
35 -class auth(BaseCore):
36
37 - def __init__(self):
38 usage = 'usage: %prog auth [OPTIONS]' 39 shortdesc = 'stores authentication credentials for the user on the machine.' 40 desc = '' 41 42 self.name = 'auth' 43 self.actions = {'login' : 'Stores user credentials on this machine', 44 'logout': 'Removes stored user credentials on this machine',} 45 self.is_admin = True 46 47 BaseCore.__init__(self, 'auth', usage, shortdesc, desc)
48
49 - def load_server(self):
50 self.authconn = UserConnection(host=CFG.server.host or "localhost", 51 port=CFG.server.port or 443, 52 username=self.username, 53 password=self.password, 54 cert_file=self.cert_filename, 55 key_file=self.key_filename)
56
57 - def generate_options(self):
58 usage = 'auth' 59 self.setup_option_parser(usage, '', True)
60
61 - def _do_core(self):
62 self.action = self._get_action() 63 if self.action == 'login': 64 self._login() 65 if self.action == 'logout': 66 self._logout()
67
68 - def _login(self):
69 if not self.options.username and not self.options.password: 70 print _("username and password are required. Try --help") 71 sys.exit(1) 72 73 # Retrieve the certificate information from the server 74 cert_dict = self.authconn.admin_certificate() 75 76 # Determine the destination and store the cert information there 77 if not os.path.exists(auth_utils.admin_cert_dir()): 78 os.makedirs(auth_utils.admin_cert_dir()) 79 80 # Write the certificate data 81 cert_filename, key_filename = auth_utils.admin_cert_paths() 82 83 f = open(cert_filename, 'w') 84 f.write(cert_dict['certificate']) 85 f.close() 86 87 f = open(key_filename, 'w') 88 f.write(cert_dict['private_key']) 89 f.close() 90 91 print('User credentials successfully stored at [%s]' % auth_utils.admin_cert_dir())
92
93 - def _logout(self):
94 # Determine the destination and store the cert information there 95 cert_filename, key_filename = auth_utils.admin_cert_paths() 96 97 # Remove the certificate and private key files 98 if os.path.exists(cert_filename): 99 os.remove(cert_filename) 100 101 if os.path.exists(key_filename): 102 os.remove(key_filename) 103 104 print('User credentials removed from [%s]' % auth_utils.admin_cert_dir())
105