A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.1 KiB

7 years ago
  1. use std::borrow::{Borrow, Cow};
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::ops::Deref;
  5. use std::path::PathBuf;
  6. use resources::Resource;
  7. use command_runner::CommandRunner;
  8. use symbols::{Action, OwnedSymbolAction, SymbolAction, SymbolRunner, Symbol};
  9. use symbols::acme::AcmeAccountKey;
  10. use symbols::dir::Dir;
  11. use symbols::file::File;
  12. use symbols::list::List;
  13. use symbols::owner::Owner;
  14. pub struct AcmeUser<'a>(Cow<'a, str>);
  15. impl<'a> AcmeUser<'a> {
  16. pub fn new<S: Into<Cow<'a, str>>>(user_name: S) -> Self {
  17. AcmeUser(user_name.into())
  18. }
  19. }
  20. impl<'a> Symbol for AcmeUser<'a> {
  21. fn target_reached(&self) -> Result<bool, Box<Error>> {
  22. Ok(false)
  23. }
  24. fn execute(&self) -> Result<(), Box<Error>> {
  25. Ok(())
  26. }
  27. fn get_prerequisites(&self) -> Vec<Resource> {
  28. vec![]
  29. }
  30. fn provides(&self) -> Option<Vec<Resource>> {
  31. None
  32. }
  33. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  34. Box::new(SymbolAction::new(runner, self))
  35. }
  36. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  37. Box::new(OwnedSymbolAction::new(runner, *self))
  38. }
  39. }
  40. impl<'a> fmt::Display for AcmeUser<'a> {
  41. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  42. write!(f, "AcmeUser {}", self.0)
  43. }
  44. }
  45. pub fn new<'a, S: Into<Cow<'a, str>>, C: CommandRunner, P: 'a + Deref<Target=str>>(command_runner: &'a C, cert: P, user_name: S) -> Box<Symbol + 'a> { // impl trait
  46. let user_name_cow = user_name.into();
  47. let account_key_file: PathBuf = ["/home", user_name_cow.borrow(), "account.key"].iter().collect();
  48. Box::new(List::new(vec![
  49. Box::new(AcmeAccountKey::new(account_key_file.clone().into(), command_runner)),
  50. Box::new(Owner::new(account_key_file.to_string_lossy().into_owned(), user_name_cow.clone(), command_runner)),
  51. Box::new(Dir::new("/home/acme/challenges")),
  52. Box::new(Owner::new("/home/acme/challenges", user_name_cow.clone(), command_runner)),
  53. Box::new(Dir::new("/etc/ssl/local_certs")),
  54. Box::new(Owner::new("/etc/ssl/local_certs", user_name_cow, command_runner)),
  55. Box::new(File::new("/home/acme/lets_encrypt_x3_cross_signed.pem", cert))
  56. ]))
  57. }