Commit graph

144 commits

Author SHA1 Message Date
Gris Ge
c4643e56bb nm provider: Refactor the down action of network connection
When deactivating a profile in libNM, we should:

 * Check `NM.ActionConnection` existence
 * Check `NM.ActionConnection.props.state` not DEACTIVATED
 * Use signal `state-changed` of `NM.ActionConnection`.
 * Only invoke `NM.Client.deactivate_connection_async()` if not
   in DEACTIVATING state.
 * Ignore `NM.ManagerError.CONNECTIONNOTACTIVE` error.

This patch also introduced a new class `NetworkManagerProvider`
in `module_utils/network_lsr/nm`:

 * Independent from Ansible but need to use absolute import due to
   limitation of ansible 2.8.
 * Provide sync function wrapping async calls of libNM.
 * Use stable logging method of python.
 * Only load this module when provider is nm.

This patch also changed how logging is handling in
`Cmd_nm.run_action_down()` as initial step on isolate ansible log
mechanism from provider module.

By moving provider codes to `module_utils` folder, we can eventually
simplify the bloated `library/network_connections.py`.

Signed-off-by: Gris Ge <fge@redhat.com>
2020-11-19 18:58:26 +08:00
Noriko Hosoi
890af84071 RHELPLAN-56586 - sync collections related changes from template to network role
The task 56586 is for adding tests against the collection converted
format to the tox/travis CI to capture a problem before merging the
pr, if any. It'd be helpful to find out bugs in the conversion tool
lsr_role2collection.py, as well. The source of this commit is located
in linux-system-roles/template. They are synced by auto-maintenance/
sync-template.sh, then manually adjusted to the network role.

.travis/runcollection.sh is the test script. tox.ini is modified to run
it in the tox/travis CI. The script downloads lsr_role2collection.py,
then converts the network role into the conversion format in the working
directory .tox. In the collection, it runs a set of tests black, flake8,
yamllint, py38 to check the converted result.
2020-10-23 13:35:14 +08:00
Till Maas
9a6bdb5403 Update formatting according to new black version
Signed-off-by: Till Maas <opensource@till.name>
2020-08-28 09:11:56 +02:00
Elvira García Ruiz
c29a2bf708 Add Pytest integration tests
tests/integration/test_ethernet.py is a script capable of using the network
module without executing it from Ansible and using Pytest. The example test
sets an ip in a test interface. The changes in tox.ini adapt the testing to the
new script. The __init__.py files were added to allow python2.7 compatibility.

Signed-off-by: Elvira García Ruiz <elviragr@riseup.net>
2020-08-24 21:52:25 +02:00
Till Maas
0b2edc50d8 Forget unmanaged state in NetworkManager
When removing a profile with initscripts, also notify NetworkManager so
it can forget that the specified device was to be ignored.
2020-07-11 16:17:06 +02:00
Till Maas
e0c7d550a9 States: Ignore already removed profile for absent
When a profile is specified as absent, ignore state requests if the
profile is already removed or not completely specified to improve
idempotence.

Also restructure the states test. This introduces a clear structure for
the individual test steps, properly assigns tags for each test from the
file and provides a clear error message with a description of the test
that failed in case of errors. Support for tests that expect a failure
is still missing.
2020-06-26 20:08:51 +02:00
Till Maas
eb6c9c1cc8 library: Rename invocation return value
Since `invocation` is an Ansible special value that is set for
triple verbosity, it is now visible for smaller verbosity. Therefore
rename it to `_invocation`, which seems to work with less verbosity.
Also adjust the content to match the regular `invocation` style.
2020-06-25 14:54:13 +02:00
Till Maas
6a3b311461 Create all profiles before activating them
To reduce problems with profile specification order and to support
creating and activating bond profiles in one run with initscripts,
create all profiles first.
2020-06-18 13:58:36 +02:00
Jack Adolph
51f8e5b05f Add support for wireless connections
WPA-PSK and WPA-EAP are supported. Uses existing 802.1x features of the role.
Added extra functionality to ArgValidatorStr to enforce a min and max length.
2020-06-15 21:10:54 +02:00
Jack Adolph
dd4ae77cbf Add ca_path option for 802.1x connections
Special notes:
Hash the cacert in the ca_path as OpenSSL needs symlinks for ca
certificates from their symlinks to their filename if specifying a
directory.

In case ieee802_1x.ca_path is specified but not supported by
NetworkManager, fail to ensure the setting is not silently ignored.

References:
https://stackoverflow.com/questions/25889341/what-is-the-equivalent-of-unix-c-rehash-command-script-on-linux
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/448

Co-authored-by: Till Maas <opensource@till.name>
2020-06-15 18:26:34 +02:00
Till Maas
f72fc394ea library: Return module params for debugging
Set invocation as suggested in
https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#invocation
to report how the internal module was invoked.
2020-06-15 18:26:34 +02:00
Elvira Garcia Ruiz
ef20874f4d library: Change ethtool features to use underscores
Ethtool features should use underscores instead of dashes. A
warning shows in case dashes used, and it fails if underscore and dashes are
mixed. Unit tests and integration tests have been added. Since nm already
needed underscores, the string processing that was made in nm_provider is now
unneeded and therefore removed.
2020-06-15 14:13:23 +02:00
Till Maas
3fc15de068 Library: Introduce debug flags
Allow to disable the checkpoint feature with a debug flag to make
debugging easier.
2020-05-29 08:41:12 +02:00
Fernando Fernandez Mancera
2e5dd50852 ethtool: use GPERMADDR instead the ethtool command line tool
This patch implements the ETHTOOL_GPERMADDR command in order to retrieve
the permanent address from ethtool instead using command line tool.

Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
2020-05-15 16:11:56 +02:00
Fernando Fernandez Mancera
024ba709f6 library: all the imports must be on the top
According to PEP8 [1], the import are always put at the top of the file,
just after any module comments and docstrings, and before module globals
and constants.

[1] https://www.python.org/dev/peps/pep-0008/#imports

Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
2020-05-14 11:34:27 +02:00
Elvira Garcia Ruiz
4753c70c55 library: Separate debug and info logs from warnings
Logs are now separed by severity level. Warnings and failures are the only logs
that appear now on the output. All logs are saved into a new json parameter
called "stderr" that is later shown on a different task. In case of
failure, all logs are shown as output. Tests have been created and modified in
order to assure that this feature works.

Signed-off-by: Elvira Garcia Ruiz <elviragr@riseup.net>
2020-05-06 21:57:34 +02:00
Jack Adolph
939772fdc1 Add support for domain-suffix-match 802.1x option
Role now supports validating the domain name of the EAP server certificate.
Regenerated the CA certificate as the private key for the original CA has been lost.
Updated test certificates to include a domain name in the CN so the domain-suffix-match
feature can be tested.
2020-05-04 13:01:41 +02:00
Jack Adolph
330729c6dd Change 802.1x parameters to be valid python identifiers 2020-04-21 08:11:15 +02:00
Till Maas
20a20b60fb Module/NM: Try to reapply connections 2020-04-18 11:02:59 +02:00
Till Maas
1554c4a6a3 Library/NM: Always rollback checkpoint on failure
If there is a NetworkManager checkpoint, roll it back on any failure.
Otherwise future invocations of the role fail until the checkpoint timed
out.
2020-04-18 01:14:07 +02:00
Jack Adolph
4af8f23955 Add support for 802.1x wired connections (EAP-TLS only)
Only EAP-TLS method is supported. Must use NetworkManager as the network_provider.
Also fixed bug in do_connections_validate_nm() function.
2020-03-25 10:49:10 +01:00
Till Maas
f0aa5fd4de library: Use new-style classes with @property
Reference: https://lgtm.com/rules/10030086/
2019-10-17 18:48:00 +02:00
Till Maas
70aa9875aa Add support for ethtool features 2019-06-14 15:18:11 +02:00
Till Maas
225ba70a43 Modules/NM: Wrap changes in checkpoint
Create a checkpoint before changing NetworkManager profiles and rollback
on failures (destroy it on success).
2019-06-14 12:00:15 +02:00
Till Maas
a00fe15943 library: Make ignore_errors a real bool
This removes warnings like:

 [WARNING]: The value False (type bool) in a string field was converted
 to u'False' (type string). If this does not look like what you expect,
 quote the entire value to ensure it does not change.
2019-06-07 22:53:11 +02:00
Thomas Haller
7157713b14 module: fix setting NM connection-type for "vlan" and "macvlan"
Otherwise:

  failure: created connection failed to normalize: nm-connection-error-quark:
  connection.type: property is missing (6)
2019-02-08 07:30:54 +01:00
Thomas Haller
cff6989606 module: use introspection variables instead of plain strings for setting names 2019-02-08 07:30:54 +01:00
Thomas Haller
d2ce509320 module: rework polling in NMUtil.wait_till_connection_is_gone()
time.sleep() does not sleep an exact amount of time, hence, adding
up the sleep-times will not give the exact total wait-time.
An alternative would be to take a CLOCK_BOOTTIME timestamp (or similar)
and determine the elapsed time based on that.

Instead, do something different, and use a GLib timeout for polling.
2019-01-10 14:39:15 +01:00
Till Maas
1ca27dc09c Clarify missing type error 2019-01-10 14:39:15 +01:00
Till Maas
6fc00a0d43 Make connections volatile instead of removing them
This keeps the profile up in Network Manager for
persistent_state:absent.
2019-01-10 14:39:15 +01:00
Till Maas
23605615da Separate 'persistent_state' from 'state'
- persistent_state represents whether a profile is stored on disk
- persistent_state defaults to 'present'
- When there is no type specified for the profile, it is enough for a
profile with the same name to be stored on the target's systems file
system. Otherwise the role will fail
- state now represents the runtime state and can be up, down or
unspecified
- translate the state definitions into actions that will be performed.
The actions correspond to the previous states.
- add the possibility to write unit tests to only verify parts of the
resulting connection dictionary to only check for the expected changes
instead of the full connection that can also contain unrelated defaults
2018-08-15 10:04:47 +02:00
Till Maas
382c34197b Modularize role
Splitting the role in smaller parts helps to keep the overview and to
develop separate tests.
2018-08-15 10:04:47 +02:00
Till Maas
6bf70bcf60 Use profile name for interface name by default
* by default ```name``` is used as ```interface_name```
* if ```interface_name``` is set to the empty string, it will not be set
* if ```mac``` is specified, ```interface_name``` defaults to not being set

This fixes #41
2018-07-25 21:56:43 +02:00
Till Maas
e32033f995 Use black as formatter and enable flake8/pylint 2018-07-18 17:23:18 +02:00
Till Maas
459601c8e7 Fail when trying to down a non-existing NM profile 2018-07-11 10:06:45 +02:00
Till Maas
756be9a8de Allow to take down connections if not in config
Allow to take down connections regardless of whether they are defined in
the configuration. It should be enough that the connection is defined on
the system.
2018-07-09 14:39:40 +02:00
Thomas Haller
5b9e1331f8 library: fix try-count handling in SysUtil.link_infos()
https://github.com/linux-system-roles/network/issues/61
2018-06-18 21:56:37 +02:00
Till Maas
ed1d30a53d
Merge pull request #60 from linux-system-roles/th/main-function
th/main-function
2018-06-11 20:15:29 +02:00
Till Maas
ec65f5c78e
Merge pull request #59 from tyll/fix_global_connections
Use proper connections attribute
2018-06-11 15:36:37 +02:00
Thomas Haller
cf8522e5cc library: move main code to explicit main() function
Without it, we unintentionally declared 3 global variables
(connections, cmd, run_env_ansible). This was not intended,
and results in possibly wrong behavior by accidentally using
the global variables.
2018-06-11 08:49:35 +02:00
Till Maas
63314aeb92 Use proper connections attribute 2018-06-08 21:35:31 +02:00
Shannon Mascarenhas
5b45f9bc46 Fix handling of autoconnect parameter
This fixes a bug in the `network_connections` module when using the
`initscripts` provider and encountering a connection with `autoconnect`
set to false. It fails to write any `ONBOOT=` line to the interface's
`ifcfg-*` file, which RHEL treats as equivalent to `ONBOOT=yes`.

This explicitly sets `ONBOOT` for all interfaces defined, instead of
relying on what RHEL considers its default value.
2018-05-29 12:52:02 -05:00
Till Maas
dd24f020a9 Handle undefined type attribute
This fixes this exception (issue #40):

 [WARNING]: exception: Traceback (most recent call last):   File
"/tmp/ansible_FfjjGk/ansible_module_network_connections.py", line 2812, in <module>     cmd.run()   File
"/tmp/ansible_FfjjGk/ansible_module_network_connections.py", line 2398, in run     self.run_prepare()   File
"/tmp/ansible_FfjjGk/ansible_module_network_connections.py", line 2653, in run_prepare     if connection['type'] in [
'macvlan' ]: KeyError: 'type'
2018-05-25 19:52:10 +02:00
Till Maas
2185df2a81 Always run commands in C locale 2018-05-23 18:31:54 +02:00
Till Maas
83f2521d5e Fix python3 missing "cmp" keyword for list.sort()
This fixes this exception on Python3:
TypeError: 'cmp' is an invalid keyword argument for this function
2018-05-23 18:29:31 +02:00
Till Maas
fa13ea2388 Fix exception on Python3 in _link_read_permaddress
Bytestrings need to be decoded. Just use UTF-8 here, since everyone
should be using it. Also add a test case to catch this. The exception
was:
TypeError: cannot use a string pattern on a bytes-like object
2018-05-23 18:29:31 +02:00
Till Maas
8e6f1bf96b Move unit tests to tests dir 2018-05-16 20:18:05 +02:00
Till Maas
b3163cdf20 Test more boolean values 2018-05-14 16:53:39 +02:00
Pavel Cahyna
08187ae624 Unset the executable bit on Ansible module.
Per the policy defined in https://docs.ansible.com/ansible/latest/dev_guide/testing/sanity/shebang.html.
(Some RPM checks
f7e8f73ead/f/brp-mangle-shebangs
do not like an executable script to have a #!/usr/bin/python shebang.)
2018-05-10 18:28:13 +02:00
Till Maas
168bd43629 Add unittest for single IPv4 DNS server 2018-03-13 16:15:35 +01:00