Debian Codegraph @

src/plugins/fd/kubernetes-backend/baculak8s/services/handshake_service.py

raw
1 # Bacula(R) - The Network Backup Solution
2 #
3 # Copyright (C) 2000-2022 Kern Sibbald
4 #
5 # The original author of Bacula is Kern Sibbald, with contributions
6 # from many others, a complete list can be found in the file AUTHORS.
7 #
8 # You may use this file and others of this release according to the
9 # license defined in the LICENSE file, which includes the Affero General
10 # Public License, v3.0 ("AGPLv3") and some additional permissions and
11 # terms pursuant to its AGPLv3 Section 7.
12 #
13 # This notice must be preserved when any source code is
14 # conveyed and/or propagated.
15 #
16 # Bacula(R) is a registered trademark of Kern Sibbald.
17 import sys
18 from baculak8s.io.default_io import DefaultIO
19 from baculak8s.services.service import Service
20
21 INVALID_HANDSHAKE_PACKET = "Invalid Handshake Packet"
22 INVALID_PLUGIN_NAME = "Invalid Plugin Name"
23 INVALID_PLUGIN_API = "Invalid Plugin API Version"
24 HANDSHAKE_OK = "Hello Bacula"
25
26
27 class HandshakeService(Service):
28 """
29 Service that contains the business logic
30 related to the Backend Handshake.
31 """
32
33 def __init__(self):
34 self.io = DefaultIO()
35
36 def execute(self, params=None):
37 """
38 Reads and parses the Handshake Packet sent to the Backend.
39 The Handshake Packet should have the format:
40 "Hello $pluginname$ $pluginAPI$".
41
42 :raise SystemExit, in case of a invalid HandshakePacket or in case
43 of unsupported $pluginname$, $pluginAPI$
44 or if the Backend received an invalid Handshake Packet.
45
46 :returns The $pluginname$.
47 """
48
49 handshake_data = self.__read_handshake_packet()
50 self.__verify_packet_data(handshake_data)
51 self.io.send_command(HANDSHAKE_OK)
52 return handshake_data[1]
53
54 def __read_handshake_packet(self):
55 _, packet = self.io.read_packet()
56 if not packet:
57 self.io.send_abort(INVALID_HANDSHAKE_PACKET)
58 sys.exit(0)
59
60 packet = packet.lower()
61 packet_data = packet.split(" ")
62
63 if len(packet_data) != 3 or packet_data[0] != "hello":
64 self.io.send_abort(INVALID_HANDSHAKE_PACKET)
65 sys.exit(0)
66 return packet_data
67
68 def __verify_packet_data(self, packet_data):
69 valid_plugins = {
70 "kubernetes": "3",
71 "openshift": "3",
72 }
73
74 plugin_name = packet_data[1]
75 plugin_api = packet_data[2]
76
77 if plugin_name not in valid_plugins:
78 self.io.send_abort(INVALID_PLUGIN_NAME)
79 sys.exit(0)
80
81 if plugin_api is not valid_plugins[plugin_name]:
82 self.io.send_abort(INVALID_PLUGIN_API)
83 sys.exit(0)