File: secret-agent.py

package info (click to toggle)
network-manager 1.54.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 71,424 kB
  • sloc: ansic: 483,661; python: 11,632; xml: 8,546; sh: 5,555; perl: 596; cpp: 178; javascript: 130; ruby: 107; makefile: 60; lisp: 22
file content (92 lines) | stat: -rwxr-xr-x 2,825 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# SPDX-License-Identifier: LGPL-2.1-or-later

import gi

gi.require_version("NM", "1.0")
from gi.repository import GLib, NM, Gio

# This example shows how to implement a very simple secret agent for
# NetworkManager. The secret agent registers to the NM daemon and can
# provide missing secrets like Wi-Fi or VPN passwords. Set environment
# variable "LIBNM_CLIENT_DEBUG=trace" to enable libnm verbose logging.


class SecretAgent(NM.SecretAgentOld):
    def __init__(self):
        super().__init__(identifier="MySecretAgent")
        super().init()

    def do_get_secrets(
        self,
        connection,
        connection_path,
        setting_name,
        hints,
        flags,
        callback,
        callback_data,
    ):
        print(
            "get_secrets for '{}', interface '{}', setting '{}'".format(
                connection.get_id(), connection.get_interface_name(), setting_name
            )
        )

        # Implement here the logic to retrieve the secrets.
        # As an example, we return a hardcoded Wi-Fi PSK.
        if (
            connection.get_connection_type() == "802-11-wireless"
            and setting_name == "802-11-wireless-security"
        ):
            s_wifi = connection.get_setting_wireless()
            ssid = NM.utils_ssid_to_utf8(s_wifi.get_ssid().get_data())

            if ssid == "home":
                secrets = GLib.Variant(
                    "a{sa{sv}}",
                    {
                        "802-11-wireless-security": {
                            "psk": GLib.Variant("s", "abcd1234")
                        }
                    },
                )
                print("Sending secrets {}".format(secrets))
                callback(self, connection, secrets, None)
                return

        # We don't have the secret, NM will ask to another agent or fail
        callback(
            self,
            connection,
            None,
            GLib.GError.new_literal(
                NM.SecretAgentError.quark(),
                "No secrets found",
                NM.SecretAgentError.NOSECRETS,
            ),
        )

    def do_cancel_get_secrets(self, connection_path, connection_name):
        pass

    def do_save_secrets(self, connection, connection_path, callback, callback_data):
        # Implement this if you want to store "agent-owned" secrets
        callback(self, connection, None)

    def do_delete_secrets(self, connection, connection_path, callback, callback_data):
        # Implement this if you want to store "agent-owned" secrets
        callback(self, connection, None)


def main():
    agent = SecretAgent()
    loop = GLib.MainLoop()
    try:
        loop.run()
    except KeyboardInterrupt:
        print("Exiting Secret Agent...")


if __name__ == "__main__":
    main()