Skip to content

Commit dee2ef5

Browse files
dhruvibmbradbishop
authored andcommitted
Upload and activate new certificate file.
Handle the rest interface to activate new certificate. Change-Id: Ida636a129a042eaa03c754f57fe1bb134446e086 Signed-off-by: Dhruvaraj Subhashchandran <[email protected]>
1 parent ba04364 commit dee2ef5

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

module/obmc/wsgi/apps/rest_dbus.py

+70
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,74 @@ def setup(self, **kw):
876876
pass
877877

878878

879+
class CertificateHandler:
880+
file_loc = '/tmp'
881+
file_suffix = '.pem'
882+
file_prefix = 'cert_'
883+
CERT_BUSNAME = 'xyz.openbmc_project.Certs.Manager'
884+
CERT_PATH = '/xyz/openbmc_project/certs'
885+
CERT_IFACE = 'xyz.openbmc_project.Certs.Install'
886+
887+
def do_upload(cls, cert_type, service):
888+
def cleanup():
889+
if os.path.exists(temp.name):
890+
os.remove(temp.name)
891+
892+
if not service:
893+
abort(500, "Missing service")
894+
if not cert_type:
895+
abort(500, "Missing certificate type")
896+
897+
with tempfile.NamedTemporaryFile(
898+
suffix=cls.file_suffix,
899+
prefix=cls.file_prefix,
900+
delete=False) as temp:
901+
try:
902+
file_contents = request.body.read()
903+
request.body.close()
904+
temp.write(file_contents)
905+
except (IOError, ValueError) as e:
906+
cleanup()
907+
abort(500, str(e))
908+
except Exception:
909+
cleanup()
910+
abort(500, "Unexpected Error")
911+
912+
try:
913+
bus = dbus.SystemBus()
914+
busName = cls.CERT_BUSNAME + "." + cert_type.capitalize() + "." \
915+
+ service.capitalize()
916+
certPath = cls.CERT_PATH + "/" + cert_type + "/" + service
917+
obj = bus.get_object(busName, certPath)
918+
iface = dbus.Interface(obj, cls.CERT_IFACE)
919+
iface.Install(temp.name)
920+
except dbus.exceptions.DBusException as e:
921+
cleanup()
922+
abort(503, str(e))
923+
cleanup()
924+
925+
926+
class CertificatePutHandler(RouteHandler):
927+
''' Handles the /xyz/openbmc_project/certs/<cert_type>/<service> route. '''
928+
929+
verbs = ['PUT']
930+
rules = ['/xyz/openbmc_project/certs/<cert_type>/<service>']
931+
content_type = 'application/octet-stream'
932+
933+
def __init__(self, app, bus):
934+
super(CertificatePutHandler, self).__init__(
935+
app, bus, self.verbs, self.rules, self.content_type)
936+
937+
def do_put(self, cert_type, service):
938+
return CertificateHandler().do_upload(cert_type, service)
939+
940+
def find(self, **kw):
941+
pass
942+
943+
def setup(self, **kw):
944+
pass
945+
946+
879947
class EventNotifier:
880948
keyNames = {}
881949
keyNames['event'] = 'event'
@@ -1647,6 +1715,7 @@ def create_handlers(self):
16471715
self.image_upload_post_handler = ImagePostHandler(self, self.bus)
16481716
self.image_upload_put_handler = ImagePutHandler(self, self.bus)
16491717
self.download_dump_get_handler = DownloadDumpHandler(self, self.bus)
1718+
self.certificate_put_handler = CertificatePutHandler(self, self.bus)
16501719
if self.have_wsock:
16511720
self.event_handler = EventHandler(self, self.bus)
16521721
self.host_console_handler = HostConsoleHandler(self, self.bus)
@@ -1664,6 +1733,7 @@ def install_handlers(self):
16641733
self.image_upload_post_handler.install()
16651734
self.image_upload_put_handler.install()
16661735
self.download_dump_get_handler.install()
1736+
self.certificate_put_handler.install()
16671737
if self.have_wsock:
16681738
self.event_handler.install()
16691739
self.host_console_handler.install()

0 commit comments

Comments
 (0)