Skip to content

Commit 94b3d3f

Browse files
author
Ritwick DSouza
committed
[raspbian] Reduce _get_encryption complexity
1 parent 4b878e5 commit 94b3d3f

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

netjsonconfig/backends/raspbian/converters/wireless.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ def to_intermediate(self):
1111
new_interface = {}
1212
for interface in interfaces:
1313
if interface.get('type') == 'wireless' and interface.get('wireless').get('mode') is not 'adhoc':
14+
wireless = interface.get('wireless')
1415
new_interface.update({
1516
'ifname': interface.get('name'),
1617
'iftype': interface.get('type'),
17-
})
18-
wireless = interface.get('wireless')
19-
new_interface.update({
2018
'ssid': wireless.get('ssid'),
2119
'radio': wireless.get('radio'),
2220
'mode': wireless.get('mode'),
@@ -55,13 +53,9 @@ def _get_hwmode(self, radio):
5553
def _get_encryption(self, wireless):
5654
encryption = wireless.get('encryption', None)
5755
new_encryption = {}
58-
if encryption is None:
56+
if encryption is None or encryption.get('disabled', False) or encryption.get('protocol') == 'none':
5957
return new_encryption
60-
disabled = encryption.get('disabled', False)
61-
protocol = encryption.get('protocol')
62-
if disabled or protocol == 'none':
63-
return new_encryption
64-
protocol, method = protocol.split("_")
58+
protocol, method = encryption.get('protocol').split("_")
6559
if 'wpa' in protocol:
6660
if 'personal' in method:
6761
new_encryption.update({
@@ -71,10 +65,8 @@ def _get_encryption(self, wireless):
7165
'wpa': '1' if protocol == 'wpa' else '2',
7266
'wpa_key_mgmt': 'WPA-PSK',
7367
'wpa_passphrase': encryption.get('key'),
68+
'wpa_pairwise': self._get_cipher(encryption),
7469
})
75-
if encryption.get('cipher'):
76-
wpa_pairwise = str(encryption.get('cipher').replace('+', ' ')).upper()
77-
new_encryption.update({'wpa_pairwise': wpa_pairwise})
7870
elif method == 'enterprise':
7971
if wireless.get('mode') == 'access_point':
8072
new_encryption.update({
@@ -88,15 +80,13 @@ def _get_encryption(self, wireless):
8880
'auth_server_shared_secret': encryption.get('key', None),
8981
})
9082
elif wireless.get('mode') == 'station':
91-
if encryption.get('cipher'):
92-
cipher = str(encryption.get('cipher').replace('+', ' ').upper())
93-
new_encryption.update({'cipher': cipher})
9483
if encryption.get('eap_type'):
9584
eap_type = encryption.get('eap_type').upper()
9685
new_encryption.update({'eap_type': eap_type})
9786
new_encryption.update({
9887
'protocol': 'wpa',
9988
'method': 'enterprise',
89+
'wpa_pairwise': self._get_cipher(encryption),
10090
'identity': encryption.get('identity', None),
10191
'password': encryption.get('password', None),
10292
'ca_cert': encryption.get('ca_cert', None),
@@ -111,3 +101,9 @@ def _get_encryption(self, wireless):
111101
'key': encryption.get('key', None)
112102
})
113103
return new_encryption
104+
105+
def _get_cipher(self, encryption):
106+
if encryption.get('cipher'):
107+
return str(encryption.get('cipher').replace('+', ' ')).upper()
108+
else:
109+
return None

0 commit comments

Comments
 (0)