summaryrefslogtreecommitdiff
path: root/src/migration-scripts/interfaces/22-to-23
blob: 3fd5998a0ad950c04c777c6c4a6f31023b4e4104 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
#
# Copyright (C) 2021 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Migrate Wireguard to store keys in CLI
# Migrate EAPoL to PKI configuration

import os
import sys
from vyos.configtree import ConfigTree
from vyos.pki import load_certificate
from vyos.pki import load_private_key
from vyos.pki import encode_certificate
from vyos.pki import encode_private_key

def wrapped_pem_to_config_value(pem):
    return "".join(pem.strip().split("\n")[1:-1])

if (len(sys.argv) < 1):
    print("Must specify file name!")
    sys.exit(1)

file_name = sys.argv[1]

with open(file_name, 'r') as f:
    config_file = f.read()

config = ConfigTree(config_file)

# Wireguard
base = ['interfaces', 'wireguard']

if config.exists(base):
    for interface in config.list_nodes(base):
        private_key_path = base + [interface, 'private-key']
        
        key_file = 'default'
        if config.exists(private_key_path):
            key_file = config.return_value(private_key_path)

        full_key_path = f'/config/auth/wireguard/{key_file}/private.key'

        if not os.path.exists(full_key_path):
            print(f'Could not find wireguard private key for migration on interface "{interface}"')
            continue

        with open(full_key_path, 'r') as f:
            key_data = f.read().strip()
            config.set(private_key_path, value=key_data)

        for peer in config.list_nodes(base + [interface, 'peer']):
            config.rename(base + [interface, 'peer', peer, 'pubkey'], 'public-key')

# Ethernet EAPoL
base = ['interfaces', 'ethernet']

if config.exists(base):
    AUTH_DIR = '/config/auth'
    pki_base = ['pki']

    for interface in config.list_nodes(base):
        if not config.exists(base + [interface, 'eapol']):
            continue

        x509_base = base + [interface, 'eapol']
        pki_name = f'eapol_{interface}'

        if config.exists(x509_base + ['ca-cert-file']):
            if not config.exists(pki_base + ['ca']):
                config.set(pki_base + ['ca'])
                config.set_tag(pki_base + ['ca'])

            cert_file = config.return_value(x509_base + ['ca-cert-file'])
            cert_path = os.path.join(AUTH_DIR, cert_file)
            cert = None

            if os.path.isfile(cert_path):
                if not os.access(cert_path, os.R_OK):
                    run(f'sudo chmod 644 {cert_path}')

                with open(cert_path, 'r') as f:
                    cert_data = f.read()
                    cert = load_certificate(cert_data, wrap_tags=False)

            if cert:
                cert_pem = encode_certificate(cert)
                config.set(pki_base + ['ca', pki_name, 'certificate'], value=wrapped_pem_to_config_value(cert_pem))
                config.set(x509_base + ['ca-certificate'], value=pki_name)
            else:
                print(f'Failed to migrate CA certificate on eapol config for interface {interface}')

            config.delete(x509_base + ['ca-cert-file'])

        if config.exists(x509_base + ['cert-file']):
            if not config.exists(pki_base + ['certificate']):
                config.set(pki_base + ['certificate'])
                config.set_tag(pki_base + ['certificate'])

            cert_file = config.return_value(x509_base + ['cert-file'])
            cert_path = os.path.join(AUTH_DIR, cert_file)
            cert = None

            if os.path.isfile(cert_path):
                if not os.access(cert_path, os.R_OK):
                    run(f'sudo chmod 644 {cert_path}')

                with open(cert_path, 'r') as f:
                    cert_data = f.read()
                    cert = load_certificate(cert_data, wrap_tags=False)

            if cert:
                cert_pem = encode_certificate(cert)
                config.set(pki_base + ['certificate', pki_name, 'certificate'], value=wrapped_pem_to_config_value(cert_pem))
                config.set(x509_base + ['certificate'], value=pki_name)
            else:
                print(f'Failed to migrate certificate on eapol config for interface {interface}')

            config.delete(x509_base + ['cert-file'])

        if config.exists(x509_base + ['key-file']):
            key_file = config.return_value(x509_base + ['key-file'])
            key_path = os.path.join(AUTH_DIR, key_file)
            key = None

            if os.path.isfile(key_path):
                if not os.access(key_path, os.R_OK):
                    run(f'sudo chmod 644 {key_path}')

                with open(key_path, 'r') as f:
                    key_data = f.read()
                    key = load_private_key(key_data, passphrase=None, wrap_tags=False)

            if key:
                key_pem = encode_private_key(key, passphrase=None)
                config.set(pki_base + ['certificate', pki_name, 'private', 'key'], value=wrapped_pem_to_config_value(key_pem))
            else:
                print(f'Failed to migrate private key on eapol config for interface {interface}')
                
            config.delete(x509_base + ['key-file'])

try:
    with open(file_name, 'w') as f:
        f.write(config.to_string())
except OSError as e:
    print("Failed to save the modified config: {}".format(e))
    sys.exit(1)