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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# vi: ts=4 expandtab
#
# Copyright (C) 2009-2010 Canonical Ltd.
# Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
#
# Author: Scott Moser <scott.moser@canonical.com>
# Author: Juerg Haefliger <juerg.haefliger@hp.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, 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/>.
import os
import sys
import ast
from ipaddress import IPv4Network
from cloudinit import util
from cloudinit.distros import ug_util
from cloudinit.settings import PER_INSTANCE
from vyos.configtree import ConfigTree
frequency = PER_INSTANCE
class VyosError(Exception):
"""Raised when the distro runs into an exception when setting vyos config.
This may happen when the ssh pub key format is wrong.
"""
pass
def set_pass_login(config, user, password, encrypted_pass):
if encrypted_pass:
config.set(['system', 'login', 'user', user, 'authentication', 'encrypted-password'], value=password, replace=True)
else:
config.set(['system', 'login', 'user', user, 'authentication', 'plaintext-password'], value=password, replace=True)
config.set_tag(['system', 'login', 'user'])
config.set(['system', 'login', 'user', user, 'level'], value='admin', replace=True)
def set_ssh_login(config, user, key_string, key_x):
key_type = None
key_data = None
key_name = None
key_parts = key_string.split(None)
for key in key_parts:
if 'ssh-dss' in key or 'ssh-rsa' in key:
key_type = key
if key.startswith('AAAAB3NzaC1yc2E') or key.startswith('AAAAB3NzaC1kc3M'):
key_data = key
if not key_type:
util.logexc(log, 'Key type not defined, wrong ssh key format.')
raise VyosError('Key type not defined, wrong ssh key format.')
if not key_data:
util.logexc(log, 'Key base64 not defined, wrong ssh key format.')
raise VyosError('Key base64 not defined, wrong ssh key format.')
if len(key_parts) > 2:
if key_parts[2] != key_type or key_parts[2] != key_data:
key_name = key_parts[2]
else:
key_name = "cloud-init-%s" % key_x
else:
key_name = "cloud-init-%s" % key_x
config.set(['system', 'login', 'user', user, 'authentication', 'public-keys', key_name , 'key'], value=key_data, replace=True)
config.set(['system', 'login', 'user', user, 'authentication', 'public-keys', key_name , 'type'], value=key_type, replace=True)
config.set_tag(['system', 'login', 'user'])
config.set_tag(['system', 'login', 'user', user, 'authentication', 'public-keys'])
config.set(['system', 'login', 'user', user, 'level'], value='admin', replace=True)
def set_config_cloud(config, hostname):
config.set(['service', 'ssh'], replace=True)
config.set(['service', 'ssh', 'port'], value='22', replace=True)
config.set(['service', 'ssh', 'client-keepalive-interval'], value='180', replace=True)
config.set(['interfaces', 'ethernet', 'eth0', 'address'], value='dhcp', replace=True)
config.set_tag(['interfaces', 'ethernet'])
config.set(['system', 'host-name'], value=hostname, replace=True)
def set_config_ovf(config, hostname, metadata):
ip_0 = metadata['ip0']
mask_0 = metadata['netmask0']
gateway = metadata['gateway']
DNS = list(metadata['DNS'].replace(" ", "").split(","))
NTP = list(metadata['NTP'].replace(" ", "").split(","))
if ip_0 != '' and mask_0 != '' and gateway != '':
cidr = str(IPv4Network('0.0.0.0/' + mask_0).prefixlen)
ipcidr = ip_0 + '/' + cidr
config.set(['interfaces', 'ethernet', 'eth0', 'address'], value=ipcidr, replace=True)
config.set_tag(['interfaces', 'ethernet'])
config.set(['protocols', 'static', 'route', '0.0.0.0/0', 'next-hop'], value=gateway, replace=True)
config.set_tag(['protocols', 'static', 'route'])
config.set_tag(['protocols', 'static', 'route', '0.0.0.0/0', 'next-hop'])
else:
config.set(['interfaces', 'ethernet', 'eth0', 'address'], value='dhcp', replace=True)
config.set_tag(['interfaces', 'ethernet'])
DNS = [ server for server in DNS if server != "" ]
if DNS:
for server in DNS:
config.set(['system', 'name-server'], value=server, replace=False)
NTP = [ server for server in NTP if server != "" ]
if NTP:
for server in NTP:
config.set(['system', 'ntp', 'server'], value=server, replace=False)
config.set_tag(['system', 'ntp', 'server'])
config.set(['service', 'ssh'], replace=True)
config.set(['service', 'ssh', 'port'], value='22', replace=True)
config.set(['system', 'host-name'], value=hostname, replace=True)
def handle(name, cfg, cloud, log, _args):
cfg_file_name = '/opt/vyatta/etc/config/config.boot'
bak_file_name = '/opt/vyatta/etc/config.boot.default'
metadata = cloud.datasource.metadata
(users, groups) = ug_util.normalize_users_groups(cfg, cloud.distro)
(hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
encrypted_pass = False
key_x = 1
if not os.path.exists(cfg_file_name):
file_name = bak_file_name
else:
file_name = cfg_file_name
with open(file_name, 'r') as f:
config_file = f.read()
config = ConfigTree(config_file)
if 'Azure' in str(cloud.datasource):
encrypted_pass = True
for key, val in users.items():
user = key
if 'passwd' in val:
password = val.get('passwd')
set_pass_login(config, user, password, encrypted_pass)
vyos_keys = metadata['public-keys']
for ssh_key in vyos_keys:
set_ssh_login(config, user, ssh_key, key_x)
key_x = key_x + 1
set_config_cloud(config, hostname)
elif 'OVF' in str(cloud.datasource):
for user in users:
password = util.get_cfg_option_str(cfg, "password", None)
if password:
set_pass_login(config, user, password, encrypted_pass)
vyos_keys = cloud.get_public_ssh_keys() or []
if "ssh_authorized_keys" in cfg:
cfgkeys = cfg["ssh_authorized_keys"]
vyos_keys.extend(cfgkeys)
for ssh_key in vyos_keys:
set_ssh_login(config, user, ssh_key, key_x)
key_x = key_x + 1
set_config_ovf(config, hostname, metadata)
else:
for user in users:
password = util.get_cfg_option_str(cfg, "passwd", None)
if password:
set_pass_login(config, user, password, encrypted_pass)
vyos_keys = cloud.get_public_ssh_keys() or []
if "ssh_authorized_keys" in cfg:
cfgkeys = cfg["ssh_authorized_keys"]
vyos_keys.extend(cfgkeys)
for ssh_key in vyos_keys:
set_ssh_login(config, user, ssh_key, key_x)
key_x = key_x + 1
set_config_cloud(config, hostname)
try:
with open(cfg_file_name, 'w') as f:
f.write(config.to_string())
except:
util.logexc(log, "Failed to write configs into file %s", file_name)
|