Skip to content

Commit d5700c0

Browse files
authored
Merge pull request #107 from TDT-AG/tdt-openwrt-username-password
[feature] Add options username and password to interface settings in the OpenWRT backend
2 parents 307e426 + 5e5d025 commit d5700c0

File tree

6 files changed

+188
-13
lines changed

6 files changed

+188
-13
lines changed

CHANGES.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
Change log
22
==========
33

4+
Version 0.9.0 [unreleased]
5+
--------------------------
6+
7+
- [change] **Potentially backward incompatible**:
8+
added support for dialup interfaces (ppp, pppoe, pppoa,
9+
3g, qmi, ncm, wwan, pptp, 6in4, aiccu or l2tp) to openwrt backend.
10+
This change is backward incompatible if the same type of configuration
11+
was achieved using a workaround, in these cases the configuration
12+
will have to be upgraded to use the new format.
13+
414
Version 0.8.2 [2020-08-17]
515
--------------------------
616

docs/source/backends/openwrt.rst

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,12 @@ The network interface settings reside in the ``interfaces`` key of the
288288
`NetJSON interface objects <http://netjson.org/rfc.html#interfaces1>`_
289289
(see the link for the detailed specification).
290290

291-
There are 3 main type of interfaces:
291+
There are 4 main types of interfaces:
292292

293293
* **network interfaces**: may be of type ``ethernet``, ``virtual``, ``loopback`` or ``other``
294294
* **wireless interfaces**: must be of type ``wireless``
295295
* **bridge interfaces**: must be of type ``bridge``
296+
* **dialup interfaces**: must be of type ``dialup``
296297

297298
Interface object extensions
298299
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -376,7 +377,12 @@ Will be rendered as follows::
376377

377378
package network
378379

379-
config interface 'eth0' option ifname 'eth0' option ip6addr 'fdb4:5f35:e8fd::1/48' option ipaddr '10.27.251.1' option netmask '255.255.255.0' option proto 'static'
380+
config interface 'eth0'
381+
option ifname 'eth0'
382+
option ip6addr 'fdb4:5f35:e8fd::1/48'
383+
option ipaddr '10.27.251.1'
384+
option netmask '255.255.255.0'
385+
option proto 'static'
380386

381387
DNS servers and search domains
382388
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -431,7 +437,22 @@ Will return the following UCI output::
431437

432438
package network
433439

434-
config interface 'eth0' option dns '10.11.12.13 8.8.8.8' option dns_search 'openwisp.org netjson.org' option ifname 'eth0' option ipaddr '192.168.1.1' option netmask '255.255.255.0' option proto 'static' config interface 'eth1' option dns_search 'openwisp.org netjson.org' option ifname 'eth1' option proto 'dhcp' config interface 'eth1_31' option ifname 'eth1.31' option proto 'none'
440+
config interface 'eth0'
441+
option dns '10.11.12.13 8.8.8.8'
442+
option dns_search 'openwisp.org netjson.org'
443+
option ifname 'eth0'
444+
option ipaddr '192.168.1.1'
445+
option netmask '255.255.255.0'
446+
option proto 'static'
447+
448+
config interface 'eth1'
449+
option dns_search 'openwisp.org netjson.org'
450+
option ifname 'eth1'
451+
option proto 'dhcp'
452+
453+
config interface 'eth1_31'
454+
option ifname 'eth1.31'
455+
option proto 'none'
435456

436457
DHCP ipv6 ethernet interface
437458
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1190,6 +1211,54 @@ UCI Output::
11901211
option password 'test-password'
11911212
option ssid 'enterprise-client'
11921213

1214+
Dialup settings
1215+
---------------
1216+
1217+
Interfaces of type ``dialup`` contain a few options that are specific to dialup connections.
1218+
1219+
The ``OpenWrt`` backend NetJSON extensions for dialup interfaces:
1220+
1221+
+--------------+---------+-----------+------------------------------------------------------------------------------------------------------------+
1222+
| key name | type | default | allowed values |
1223+
+==============+=========+===========+============================================================================================================+
1224+
| ``proto`` | string | ``pppoe`` | ``3g``, ``6in4``, ``aiccu``, ``l2tp``, ``ncm``, ``ppp``, ``pppoa``, ``pppoe``, ``pptp``, ``qmi``, ``wwan`` |
1225+
+--------------+---------+-----------+------------------------------------------------------------------------------------------------------------+
1226+
| ``password`` | string | ``""`` | |
1227+
+--------------+---------+-----------+------------------------------------------------------------------------------------------------------------+
1228+
| ``username`` | string | ``""`` | |
1229+
+--------------+---------+-----------+------------------------------------------------------------------------------------------------------------+
1230+
1231+
Dialup interface example
1232+
~~~~~~~~~~~~~~~~~~~~~~~~
1233+
1234+
The following *configuration dictionary*:
1235+
1236+
.. code-block:: python
1237+
1238+
{
1239+
"interfaces": [
1240+
{
1241+
"name": "dsl0",
1242+
"network": "xdsl",
1243+
"proto": "pppoe",
1244+
"password": "jf93nf82o023$",
1245+
"username": "dsluser",
1246+
"mtu": 1448
1247+
}
1248+
]
1249+
}
1250+
1251+
Will be rendered as follows::
1252+
1253+
package network
1254+
1255+
config interface 'xdsl'
1256+
option ifname 'dsl0'
1257+
option proto 'pppoe'
1258+
option username 'dsluser'
1259+
option password 'jf93nf82o023$'
1260+
option mtu '1448'
1261+
11931262
Radio settings
11941263
--------------
11951264

netjsonconfig/backends/openwrt/converters/interfaces.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ def __netjson_addresses(self, interface):
248248
return interface
249249
if proto not in ['static', 'dhcp', 'dhcpv6', 'none']:
250250
interface['proto'] = proto
251-
interface['type'] = 'other'
251+
interface['type'] = self.__get_special_interface_type(interface)
252+
252253
addresses = []
253254
ipv4 = interface.pop('ipaddr', [])
254255
ipv6 = interface.pop('ip6addr', [])
@@ -272,6 +273,15 @@ def __netjson_addresses(self, interface):
272273
interface['addresses'] = addresses
273274
return interface
274275

276+
def __get_special_interface_type(self, interface):
277+
username = interface.get('username', False)
278+
password = interface.get('password', False)
279+
280+
if username and password:
281+
return 'dialup'
282+
283+
return 'other'
284+
275285
def __netjson_address(self, address, interface):
276286
ip = ip_interface(address)
277287
family = 'ipv{0}'.format(ip.version)

netjsonconfig/backends/openwrt/schema.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,51 @@
112112
}
113113
]
114114
},
115+
"dialup_interface": {
116+
"title": "Dialup interface",
117+
"required": ["proto", "username", "password"],
118+
"allOf": [
119+
{
120+
"properties": {
121+
"type": {
122+
"type": "string",
123+
"enum": ["dialup"],
124+
"default": "dialup",
125+
"propertyOrder": 1,
126+
},
127+
"proto": {
128+
"type": "string",
129+
"enum": [
130+
"3g",
131+
"6in4",
132+
"aiccu",
133+
"l2tp",
134+
"ncm",
135+
"ppp",
136+
"pppoa",
137+
"pppoe",
138+
"pptp",
139+
"qmi",
140+
"wwan",
141+
],
142+
"default": "pppoe",
143+
"propertyOrder": 8,
144+
},
145+
"username": {
146+
"type": "string",
147+
"description": "username for authentication in protocols like PPPoE",
148+
"propertyOrder": 9,
149+
},
150+
"password": {
151+
"type": "string",
152+
"description": "password for authentication in protocols like PPPoE",
153+
"propertyOrder": 10,
154+
},
155+
}
156+
},
157+
{"$ref": "#/definitions/interface_settings"},
158+
],
159+
},
115160
"base_radio_settings": {
116161
"properties": {
117162
"driver": {
@@ -165,6 +210,9 @@
165210
"timezone": {"enum": list(timezones.keys()), "default": "UTC"}
166211
}
167212
},
213+
"interfaces": {
214+
"items": {"oneOf": [{"$ref": "#/definitions/dialup_interface"}]}
215+
},
168216
"routes": {
169217
"items": {
170218
"properties": {

tests/openwrt/test_dialup.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import unittest
2+
3+
from netjsonconfig import OpenWrt
4+
from netjsonconfig.utils import _TabsMixin
5+
6+
7+
class TestDialup(unittest.TestCase, _TabsMixin):
8+
maxDiff = None
9+
10+
_dialup_interface_netjson = {
11+
"interfaces": [
12+
{
13+
"mtu": 1448,
14+
"network": "xdsl",
15+
"type": "dialup",
16+
"name": "dsl0",
17+
"password": "jf93nf82o023$",
18+
"username": "dsluser",
19+
"proto": "pppoe",
20+
},
21+
]
22+
}
23+
24+
_dialup_interface_uci = """package network
25+
26+
config interface 'xdsl'
27+
option ifname 'dsl0'
28+
option mtu '1448'
29+
option password 'jf93nf82o023$'
30+
option proto 'pppoe'
31+
option username 'dsluser'
32+
"""
33+
34+
def test_render_dialup_interface(self):
35+
result = OpenWrt(self._dialup_interface_netjson).render()
36+
expected = self._tabs(self._dialup_interface_uci)
37+
self.assertEqual(result, expected)
38+
39+
def test_parse_dialup_interface(self):
40+
result = OpenWrt(native=self._dialup_interface_uci).config
41+
expected = self._dialup_interface_netjson
42+
self.assertDictEqual(result, expected)

tests/openwrt/test_interfaces.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -601,25 +601,21 @@ def test_parse_custom_proto(self):
601601
native = self._tabs(
602602
"""package network
603603
604-
config interface 'ppp0'
604+
config interface 'custom_if0'
605605
option device '/dev/usb/modem1'
606-
option ifname 'ppp0'
606+
option ifname 'custom_if0'
607607
option ipv6 '1'
608608
option keepalive '3'
609-
option password 'pwd0123'
610-
option proto 'ppp'
611-
option username 'user1'
609+
option proto 'custom'
612610
"""
613611
)
614612
expected = {
615613
"interfaces": [
616614
{
617-
"name": "ppp0",
615+
"name": "custom_if0",
618616
"type": "other",
619-
"proto": "ppp",
617+
"proto": "custom",
620618
"device": "/dev/usb/modem1",
621-
"username": "user1",
622-
"password": "pwd0123",
623619
"keepalive": '3',
624620
"ipv6": '1',
625621
}

0 commit comments

Comments
 (0)