forked from omnect/omnect-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
295 lines (260 loc) · 8.32 KB
/
mod.rs
File metadata and controls
295 lines (260 loc) · 8.32 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
pub mod compression;
pub mod functions;
mod partition;
use super::validators::{
device_update,
identity::{IdentityConfig, IdentityType, validate_identity},
ssh::validate_ssh_pub_key,
};
use crate::file::functions::{FileCopyFromParams, FileCopyToParams, Partition};
use anyhow::{Context, Result};
use log::warn;
use regex::Regex;
use std::{
fs,
path::{Path, PathBuf},
};
pub fn set_iotedge_gateway_config(
config_file: &Path,
image_file: &Path,
root_ca_file: &Path,
edge_device_identity_full_chain_file: &Path,
edge_device_identity_key_file: &Path,
) -> Result<()> {
validate_identity(IdentityType::Gateway, config_file, &None)?
.iter()
.for_each(|x| warn!("{}", x));
let mut file_copies = configure_hostname(config_file, image_file)?;
file_copies.append(&mut vec![
FileCopyToParams::new(
config_file,
Partition::factory,
Path::new("/etc/aziot/config.toml"),
),
FileCopyToParams::new(
root_ca_file,
Partition::cert,
Path::new("/ca/trust-bundle.pem.crt"),
),
FileCopyToParams::new(
edge_device_identity_full_chain_file,
Partition::cert,
Path::new("/priv/edge-ca.pem"),
),
FileCopyToParams::new(
edge_device_identity_key_file,
Partition::cert,
Path::new("/priv/edge-ca.key.pem"),
),
]);
copy_to_image(&file_copies, image_file)
}
pub fn set_iot_leaf_sas_config(
config_file: &Path,
image_file: &Path,
root_ca_file: &Path,
) -> Result<()> {
validate_identity(IdentityType::Leaf, config_file, &None)?
.iter()
.for_each(|x| warn!("{}", x));
let mut root_ca_out_file = PathBuf::from("ca");
root_ca_out_file.push(root_ca_file.file_name().unwrap());
root_ca_out_file.set_extension("crt");
let mut file_copies = configure_hostname(config_file, image_file)?;
file_copies.append(&mut vec![
FileCopyToParams::new(
config_file,
Partition::factory,
Path::new("/etc/aziot/config.toml"),
),
FileCopyToParams::new(root_ca_file, Partition::cert, &root_ca_out_file),
]);
copy_to_image(&file_copies, image_file)
}
pub fn set_ssh_tunnel_certificate(image_file: &Path, root_ca_file: &Path) -> Result<()> {
validate_ssh_pub_key(root_ca_file)?;
copy_to_image(
&[FileCopyToParams::new(
root_ca_file,
Partition::cert,
Path::new("/ssh/root_ca"),
)],
image_file,
)
}
pub fn set_identity_config(
config_file: &Path,
image_file: &Path,
payload: Option<&Path>,
) -> Result<()> {
validate_identity(IdentityType::Standalone, config_file, &payload)?
.iter()
.for_each(|x| warn!("{}", x));
let mut file_copies = configure_hostname(config_file, image_file)?;
file_copies.append(&mut vec![FileCopyToParams::new(
config_file,
Partition::factory,
Path::new("/etc/aziot/config.toml"),
)]);
if let Some(p) = payload {
file_copies.push(FileCopyToParams::new(
p,
Partition::factory,
Path::new("/etc/omnect/dps-payload.json"),
));
}
copy_to_image(&file_copies, image_file)
}
struct IntermediateFullChainCertDescr<'a> {
src: &'a Path,
name: &'a str,
}
struct CopyDescr<'a> {
src: &'a Path,
dest: &'a Path,
}
fn set_cert(
intermediate_full_chain_cert: Option<IntermediateFullChainCertDescr>,
cert: CopyDescr,
key: CopyDescr,
image_file: &Path,
) -> Result<()> {
let mut copy_params = vec![
FileCopyToParams::new(cert.src, Partition::cert, cert.dest),
FileCopyToParams::new(key.src, Partition::cert, key.dest),
];
if let Some(p) = intermediate_full_chain_cert {
copy_params.append(&mut vec![
FileCopyToParams::new(
p.src,
Partition::cert,
Path::new(&format!("/priv/{}.crt.pem", p.name)),
),
FileCopyToParams::new(
p.src,
Partition::cert,
Path::new(&format!("/ca/{}.crt", p.name)),
),
])
}
copy_to_image(©_params, image_file)
}
pub fn set_device_cert(
intermediate_full_chain_cert_path: Option<&Path>,
device_cert_path: &Path,
device_key_path: &Path,
image_file: &Path,
) -> Result<()> {
let full_chain_descr = intermediate_full_chain_cert_path
.map(|p| IntermediateFullChainCertDescr { src: p, name: "ca" });
set_cert(
full_chain_descr,
CopyDescr {
src: device_cert_path,
dest: Path::new("/priv/device_id_cert.pem"),
},
CopyDescr {
src: device_key_path,
dest: Path::new("/priv/device_id_cert_key.pem"),
},
image_file,
)
}
pub fn set_edge_ca_cert(
intermediate_full_chain_cert_path: Option<&Path>,
device_cert_path: &Path,
device_key_path: &Path,
image_file: &Path,
) -> Result<()> {
let full_chain_descr =
intermediate_full_chain_cert_path.map(|p| IntermediateFullChainCertDescr {
src: p,
name: "edge_ca",
});
set_cert(
full_chain_descr,
CopyDescr {
src: device_cert_path,
dest: Path::new("/priv/edge_ca_cert.pem"),
},
CopyDescr {
src: device_key_path,
dest: Path::new("/priv/edge_ca_cert_key.pem"),
},
image_file,
)
}
pub fn set_iot_hub_device_update_config(du_config_file: &Path, image_file: &Path) -> Result<()> {
device_update::validate_config(du_config_file)?;
copy_to_image(
&[FileCopyToParams::new(
du_config_file,
Partition::factory,
Path::new("/etc/adu/du-config.json"),
)],
image_file,
)
}
pub fn copy_to_image(file_copy_params: &[FileCopyToParams], image_file: &Path) -> Result<()> {
functions::copy_to_image(file_copy_params, image_file)
}
pub fn copy_from_image(file_copy_params: &[FileCopyFromParams], image_file: &Path) -> Result<()> {
functions::copy_from_image(file_copy_params, image_file)
}
fn configure_hostname(
identity_config_file: &Path,
image_file: &Path,
) -> Result<Vec<FileCopyToParams>> {
let hostname_file = get_file_path(image_file, "hostname")?;
let hosts_file = get_file_path(image_file, "hosts")?;
// get hostname from identity_config_file
let identity: IdentityConfig = serde_path_to_error::deserialize(
toml::Deserializer::parse(
fs::read_to_string(identity_config_file.to_str().unwrap())
.context("configure_hostname: cannot read identity file")?
.as_str(),
)
.context("configure_hostname: couldn't parse identity toml")?,
)
.context("configure_hostname: couldn't read identity")?;
fs::write(&hostname_file, &identity.hostname)
.context("configure_hostname: cannot write to hostname file")?;
// read /etc/hosts from rootA
copy_from_image(
&[FileCopyFromParams::new(
Path::new("/etc/hosts"),
Partition::rootA,
&hosts_file.to_path_buf(),
)],
image_file,
)
.context("configure_hostname: couldn't read /etc/hosts from rootA")?;
// patch /etc/hosts with hostname
let content =
fs::read_to_string(&hosts_file).context("configure_hostname: cannot read hosts file")?;
let reg =
Regex::new(r"(127\.0\.1\.1.*)").context("configure_hostname: create hostname regex")?;
let content = reg.replace_all(content.as_str(), format!("127.0.1.1 {}", identity.hostname));
fs::write(&hosts_file, content.to_string())
.context("configure_hostname: cannot write to hosts file")?;
Ok(vec![
FileCopyToParams::new(
&hostname_file.to_path_buf(),
Partition::factory,
Path::new("/etc/hostname"),
),
FileCopyToParams::new(
&hosts_file.to_path_buf(),
Partition::factory,
Path::new("/etc/hosts"),
),
])
}
pub(crate) fn get_file_path(image_path: &Path, file_name: &str) -> Result<PathBuf> {
let mut file_path = image_path
.parent()
.context("cannot get image directory")?
.to_path_buf();
file_path.push(file_name);
Ok(file_path)
}