regent-sdk 0.8.1

Multi-paradigm configuration management system as a library
Documentation
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! YUM/DNF package management attribute
//!
//! This module provides the `YumDnfBlockExpectedState` type for managing RHEL/CentOS/Fedora
//! packages using the YUM or DNF package manager.
//!
//! # Examples
//!
//! ## Rust API
//!
//! ```no_run
//! use regent_sdk::state::attribute::package::yumdnf::{YumDnfBlockExpectedState, PackageExpectedState};
//! use regent_sdk::{Attribute, ExpectedState, Privilege};
//!
//! // Install httpd package
//! let httpd = YumDnfBlockExpectedState::builder()
//!     .with_package_state("httpd", PackageExpectedState::Present)
//!     .build()
//!     .unwrap();
//!
//! let expected_state = ExpectedState::new()
//!     .with_attribute(Attribute::yumdnf(httpd, Privilege::WithSudo, None))
//!     .build();
//! ```
//!
//! ## YAML API
//!
//! ```yaml
//! Attributes:
//!   - Detail: !YumDnf
//!       Package: httpd
//!       State: !Present
//!       Privilege: !WithSudo
//! ```

use crate::error::RegentError;
use crate::hosts::managed_host::InternalApiCallOutcome;
use crate::hosts::managed_host::{AssessCompliance, ReachCompliance, Timeout};
use crate::hosts::properties::HostProperties;
use crate::secrets::SecretProvidersPool;
use crate::state::Check;
use crate::state::attribute::HostHandler;
use crate::state::attribute::Privilege;
use crate::state::attribute::Remediation;
use crate::state::compliance::AttributeComplianceAssessment;
use serde::{Deserialize, Serialize};
use std::time::Duration;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum YumDnfModuleInternalApiCall {
    Install(String),
    Remove(String),
    Upgrade,
}

impl std::fmt::Display for YumDnfModuleInternalApiCall {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            YumDnfModuleInternalApiCall::Install(package) => write!(f, "install {}", package),
            YumDnfModuleInternalApiCall::Remove(package) => write!(f, "remove {}", package),
            YumDnfModuleInternalApiCall::Upgrade => write!(f, "upgrade"),
        }
    }
}

/// Desired state of a package
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum PackageExpectedState {
    /// Package should be installed
    Present,
    /// Package should be removed
    Absent,
}

/// Configuration for YUM/DNF package management
/// 
/// Use the builder to specify package state (Present/Absent) and optionally trigger
/// a system upgrade.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "PascalCase")]
pub struct YumDnfBlockExpectedState {
    /// Desired state of the package(s)
    state: Option<PackageExpectedState>,
    /// Package name to manage
    package: Option<String>,
    /// Whether to perform a full system upgrade
    upgrade: Option<bool>,
}

impl Timeout for YumDnfBlockExpectedState {
    fn default_timeout(&self) -> Duration {
        Duration::from_secs(30)
    }
}

impl YumDnfBlockExpectedState {
    pub fn builder() -> YumDnfBlockExpectedState {
        YumDnfBlockExpectedState {
            state: None,
            package: None,
            upgrade: None,
        }
    }

    pub fn with_system_upgrade(&mut self) -> &mut Self {
        self.upgrade = Some(true);
        self
    }

    pub fn with_package_state(
        &mut self,
        package_name: &str,
        package_state: PackageExpectedState,
    ) -> &mut Self {
        self.package = Some(package_name.to_string());
        self.state = Some(package_state);
        self
    }

    pub fn build(&self) -> Result<YumDnfBlockExpectedState, RegentError> {
        if let Err(details) = self.check() {
            return Err(details);
        }
        Ok(self.clone())
    }
}

impl Check for YumDnfBlockExpectedState {
    fn check(&self) -> Result<(), RegentError> {
        if let (None, None, None) = (&self.state, &self.package, self.upgrade) {
            return Err(RegentError::IncoherentExpectedState(format!(
                "All parameters are unset. Please describe the expected state."
            )));
        }
        if let (None, Some(package_name)) = (&self.state, &self.package) {
            return Err(RegentError::IncoherentExpectedState(format!(
                "Missing 'state' parameter. What is the expected state of the package ({}) ?",
                package_name
            )));
        }
        if let (Some(package_expected_state), None) = (&self.state, &self.package) {
            return Err(RegentError::IncoherentExpectedState(format!(
                "Missing 'package' parameter. Which package should be {:?} ?",
                package_expected_state
            )));
        }
        Ok(())
    }
}

#[allow(unused_assignments)] // 'package_manager' is never actually read, only borrowed
impl<Handler: HostHandler> AssessCompliance<Handler> for YumDnfBlockExpectedState {
    async fn assess_compliance(
        &self,
        host_handler: &mut Handler,

        _host_properties: &Option<HostProperties>,
        privilege: &Privilege,
        _optional_secret_provider: &Option<SecretProvidersPool>,
    ) -> Result<AttributeComplianceAssessment, RegentError> {
        let package_manager: RedHatFlavoredPackageManager;

        if host_handler
            .is_this_command_available("dnf", &Privilege::None)
            .await
            .unwrap()
        {
            package_manager = RedHatFlavoredPackageManager::Dnf;
        } else if host_handler
            .is_this_command_available("yum", &Privilege::None)
            .await
            .unwrap()
        {
            package_manager = RedHatFlavoredPackageManager::Yum;
        } else {
            return Err(RegentError::FailedDryRunEvaluation(
                "Neither YUM nor DNF work on this host".to_string(),
            ));
        }

        let mut remediations: Vec<Remediation> = Vec::new();

        match &self.state {
            None => {}
            Some(state) => {
                match state {
                    PackageExpectedState::Present => {
                        // Check is package is already installed or needs to be
                        if is_package_installed(
                            host_handler,
                            &package_manager,
                            self.package.clone().unwrap(),
                            privilege.clone(),
                        )
                        .await
                        {
                            remediations.push(Remediation::None(format!(
                                "{} already present",
                                self.package.clone().unwrap()
                            )));
                        } else {
                            // Package is absent and needs to be installed
                            remediations.push(Remediation::YumDnf(YumDnfApiCall::from(
                                YumDnfModuleInternalApiCall::Install(self.package.clone().unwrap()),
                                package_manager.clone(),
                                privilege.clone(),
                            )));
                        }
                    }
                    PackageExpectedState::Absent => {
                        // Check is package is already absent or needs to be removed
                        if is_package_installed(
                            host_handler,
                            &package_manager,
                            self.package.clone().unwrap(),
                            privilege.clone(),
                        )
                        .await
                        {
                            // Package is present and needs to be removed
                            remediations.push(Remediation::YumDnf(YumDnfApiCall::from(
                                YumDnfModuleInternalApiCall::Remove(self.package.clone().unwrap()),
                                package_manager.clone(),
                                privilege.clone(),
                            )));
                        } else {
                            remediations.push(Remediation::None(format!(
                                "{} already absent",
                                self.package.clone().unwrap()
                            )));
                        }
                    }
                }
            }
        }
        // TODO : have this to do a "dnf check-update" only
        // If updates available -> ApiCall, if not, Matched
        if let Some(value) = self.upgrade {
            if value {
                remediations.push(Remediation::YumDnf(YumDnfApiCall::from(
                    YumDnfModuleInternalApiCall::Upgrade,
                    package_manager,
                    privilege.clone(),
                )));
            }
        }

        // If remediations are only None, it means a Match. If only one change is not a None, return the whole list.
        for remediation in remediations.iter() {
            match remediation {
                Remediation::None(_) => {}
                _ => {
                    return Ok(AttributeComplianceAssessment::NonCompliant(remediations));
                }
            }
        }
        return Ok(AttributeComplianceAssessment::Compliant);
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
enum RedHatFlavoredPackageManager {
    Yum,
    Dnf,
}

impl RedHatFlavoredPackageManager {
    fn command_name(&self) -> &str {
        match self {
            RedHatFlavoredPackageManager::Dnf => "dnf",
            RedHatFlavoredPackageManager::Yum => "yum",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct YumDnfApiCall {
    pub api_call: YumDnfModuleInternalApiCall,
    package_manager: RedHatFlavoredPackageManager,
    privilege: Privilege,
}

impl YumDnfApiCall {
    pub fn display(&self) -> String {
        match &self.api_call {
            YumDnfModuleInternalApiCall::Install(package_name) => {
                return format!("Install - {}", package_name);
            }
            YumDnfModuleInternalApiCall::Remove(package_name) => {
                return format!("Remove - {}", package_name);
            }
            YumDnfModuleInternalApiCall::Upgrade => {
                return String::from("Upgrade");
            }
        }
    }
}

impl<Handler: HostHandler> ReachCompliance<Handler> for YumDnfApiCall {
    async fn call(
        &self,
        host_handler: &mut Handler,
        _host_properties: &Option<HostProperties>,
        _optional_secret_provider: &Option<SecretProvidersPool>,
    ) -> Result<InternalApiCallOutcome, RegentError> {
        let (cmd, privilege) = match &self.api_call {
            YumDnfModuleInternalApiCall::Install(package_name) => (
                format!(
                    "{} install -y {}",
                    self.package_manager.command_name(),
                    package_name
                ),
                &self.privilege,
            ),
            YumDnfModuleInternalApiCall::Remove(package_name) => (
                format!(
                    "{} remove -y {}",
                    self.package_manager.command_name(),
                    package_name
                ),
                &self.privilege,
            ),
            YumDnfModuleInternalApiCall::Upgrade => (
                format!(
                    "{} update -y --refresh",
                    self.package_manager.command_name()
                ),
                &self.privilege,
            ),
        };

        let cmd_result = host_handler
            .run_command(cmd.as_str(), privilege)
            .await
            .unwrap();

        if cmd_result.return_code == 0 {
            Ok(InternalApiCallOutcome::Success(None))
        } else {
            Ok(InternalApiCallOutcome::Failure(format!(
                "RC : {}, STDOUT : {}, STDERR : {}",
                cmd_result.return_code, cmd_result.stdout, cmd_result.stderr
            )))
        }
    }
}

impl YumDnfApiCall {
    fn from(
        api_call: YumDnfModuleInternalApiCall,
        package_manager: RedHatFlavoredPackageManager,
        privilege: Privilege,
    ) -> YumDnfApiCall {
        YumDnfApiCall {
            api_call,
            package_manager,
            privilege,
        }
    }
}

async fn is_package_installed<Handler: HostHandler>(
    host_handler: &mut Handler,
    package_manager: &RedHatFlavoredPackageManager,
    package_name: String,
    privilege: Privilege,
) -> bool {
    let test = host_handler
        .run_command(
            format!(
                "{} list installed {}",
                package_manager.command_name(),
                package_name
            )
            .as_str(),
            &privilege,
        )
        .await
        .unwrap();

    if test.return_code == 0 {
        return true;
    } else {
        return false;
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn parsing_yumdnf_module_block_from_yaml_str() {
        let raw_attributes = "---
- Package: httpd
  State: !Present

- Package: httpd
  State: !Absent

- Upgrade: true
    ";

        let attributes: Vec<YumDnfBlockExpectedState> =
            yaml_serde::from_str(raw_attributes).unwrap();

        assert_eq!(attributes[0].package, Some("httpd".to_string()));
        assert_eq!(attributes[0].state, Some(PackageExpectedState::Present));
        assert_eq!(attributes[0].upgrade, None);

        assert_eq!(attributes[1].package, Some("httpd".to_string()));
        assert_eq!(attributes[1].state, Some(PackageExpectedState::Absent));
        assert_eq!(attributes[1].upgrade, None);

        assert_eq!(attributes[2].package, None);
        assert_eq!(attributes[2].state, None);
        assert_eq!(attributes[2].upgrade, Some(true));
    }

    #[test]
    fn rejecting_incorrect_yumdnf_module_block_from_yaml_str() {
        let raw_attribute = "---
Package: httpd
    ";
        let yaml_part = yaml_serde::from_str::<YumDnfBlockExpectedState>(raw_attribute);
        assert!(yaml_part.is_ok());
        assert!(yaml_part.unwrap().check().is_err());

        let raw_attribute = "---
Package:
State: !Absent
    ";
        let yaml_part = yaml_serde::from_str::<YumDnfBlockExpectedState>(raw_attribute);
        assert!(yaml_part.is_ok());
        assert!(yaml_part.unwrap().check().is_err());

        let raw_attribute = "---
Package: httpd
State: !Absent
unknown_key: unknown_value
    ";
        assert!(yaml_serde::from_str::<YumDnfBlockExpectedState>(raw_attribute).is_err());
    }
}