7927c555e388e199ed3876b57a1f017c524acbd7
2 Copyright 2007-2008, Red Hat, Inc
5 This software may be freely redistributed under the terms of the GNU
6 general public license.
8 You should have received a copy of the GNU General Public License
9 along with this program; if not, write to the Free Software
10 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 from config
import read_config
25 from commonconfig
import MinionConfig
28 # FIXME: module needs better pydoc
31 # FIXME: can remove this constant?
32 REMOTE_ERROR
= "REMOTE_ERROR"
36 x
= traceback
.extract_stack()
37 bar
= string
.join(traceback
.format_list(x
))
40 def daemonize(pidfile
=None):
42 Daemonize this process with the UNIX double-fork trick.
43 Writes the new PID to the provided file name if not None.
55 if pidfile
is not None:
56 open(pidfile
, "w").write(str(pid
))
59 def nice_exception(etype
, evalue
, etb
):
60 # FIXME: I believe we can remove this function
62 lefti
= etype
.index("'") + 1
63 righti
= etype
.rindex("'")
64 nicetype
= etype
[lefti
:righti
]
65 nicestack
= string
.join(traceback
.format_list(traceback
.extract_tb(etb
)))
66 return [ REMOTE_ERROR
, nicetype
, str(evalue
), nicestack
]
69 # FIXME: I believe we can remove this function
70 if type(result
) != list:
74 if result
[0] == REMOTE_ERROR
:
80 "localhost" is a lame hostname to use for a key, so try to get
81 a more meaningful hostname. We do this by connecting to the certmaster
82 and seeing what interface/ip it uses to make that connection, and looking
83 up the hostname for that.
85 # FIXME: this code ignores http proxies (which granted, we don't
86 # support elsewhere either. It also hardcodes the port number
87 # for the certmaster for now
89 hostname
= socket
.gethostname()
91 ip
= socket
.gethostbyname(hostname
)
98 config_file
= '/etc/certmaster/minion.conf'
99 config
= read_config(config_file
, MinionConfig
)
101 server
= config
.certmaster
107 s
.connect((server
, port
))
108 (intf
, port
) = s
.getsockname()
109 hostname
= socket
.gethostbyaddr(intf
)[0]
119 def create_minion_keys():
120 # FIXME: paths should not be hard coded here, move to settings universally
121 config_file
= '/etc/certmaster/minion.conf'
122 config
= read_config(config_file
, MinionConfig
)
123 cert_dir
= config
.cert_dir
124 master_uri
= 'http://%s:51235/' % config
.certmaster
125 print "DEBUG: acquiring hostname"
127 print "DEBUG: hostname = %s\n" % hn
130 raise codes
.CMException("Could not determine a hostname other than localhost")
132 key_file
= '%s/%s.pem' % (cert_dir
, hn
)
133 csr_file
= '%s/%s.csr' % (cert_dir
, hn
)
134 cert_file
= '%s/%s.cert' % (cert_dir
, hn
)
135 ca_cert_file
= '%s/ca.cert' % cert_dir
138 if os
.path
.exists(cert_file
) and os
.path
.exists(ca_cert_file
):
139 print "DEBUG: err, no cert_file"
144 if not os
.path
.exists(cert_dir
):
145 os
.makedirs(cert_dir
)
146 if not os
.path
.exists(key_file
):
147 keypair
= certs
.make_keypair(dest
=key_file
)
148 if not os
.path
.exists(csr_file
):
150 keypair
= certs
.retrieve_key_from_file(key_file
)
151 csr
= certs
.make_csr(keypair
, dest
=csr_file
)
153 traceback
.print_exc()
154 raise codes
.FuncException
, "Could not create local keypair or csr for session"
157 log
= logger
.Logger().logger
160 print "DEBUG: submitting CSR to certmaster: %s" % master_uri
161 log
.debug("submitting CSR to certmaster %s" % master_uri
)
162 result
, cert_string
, ca_cert_string
= submit_csr_to_master(csr_file
, master_uri
)
163 except socket
.gaierror
, e
:
164 raise codes
.FuncException
, "Could not locate certmaster at %s" % master_uri
166 # logging here would be nice
168 print "DEBUG: no response from certmaster, sleeping 10 seconds"
169 log
.warning("no response from certmaster %s, sleeping 10 seconds" % master_uri
)
174 print "DEBUG: recieved certificate from certmaster"
175 log
.debug("received certificate from certmaster %s, storing" % master_uri
)
176 cert_fd
= os
.open(cert_file
, os
.O_RDWR|os
.O_CREAT
, 0644)
177 os
.write(cert_fd
, cert_string
)
180 ca_cert_fd
= os
.open(ca_cert_file
, os
.O_RDWR|os
.O_CREAT
, 0644)
181 os
.write(ca_cert_fd
, ca_cert_string
)
184 def submit_csr_to_master(csr_file
, master_uri
):
186 gets us our cert back from the certmaster.wait_for_cert() method
187 takes csr_file as path location and master_uri
188 returns Bool, str(cert), str(ca_cert)
193 s
= xmlrpclib
.ServerProxy(master_uri
)
195 print "DEBUG: waiting for cert"
196 return s
.wait_for_cert(csr
)