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.

82 lines
2.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. use resources::Resource;
  2. use std::borrow::{Borrow, Cow};
  3. use std::error::Error;
  4. use std::fmt;
  5. use std::ops::Deref;
  6. use std::path::PathBuf;
  7. use command_runner::CommandRunner;
  8. use symbols::acme::AcmeAccountKey;
  9. use symbols::dir::Dir;
  10. use symbols::file::File;
  11. use symbols::list::List;
  12. use symbols::owner::Owner;
  13. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  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<dyn Error>> {
  22. Ok(false)
  23. }
  24. fn execute(&self) -> Result<(), Box<dyn 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 dyn SymbolRunner) -> Box<dyn Action + 'b> {
  34. Box::new(SymbolAction::new(runner, self))
  35. }
  36. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  37. where
  38. Self: 'b,
  39. {
  40. Box::new(OwnedSymbolAction::new(runner, *self))
  41. }
  42. }
  43. impl<'a> fmt::Display for AcmeUser<'a> {
  44. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  45. write!(f, "AcmeUser {}", self.0)
  46. }
  47. }
  48. pub fn new<'a, S: Into<Cow<'a, str>>, C: CommandRunner, P: 'a + Deref<Target = str>>(
  49. command_runner: &'a C,
  50. cert: P,
  51. user_name: S,
  52. ) -> impl Symbol + 'a {
  53. // impl trait
  54. let user_name_cow = user_name.into();
  55. let account_key_file: PathBuf = ["/home", user_name_cow.borrow(), "account.key"]
  56. .iter()
  57. .collect();
  58. List::from((
  59. AcmeAccountKey::new(account_key_file.clone().into(), command_runner),
  60. Owner::new(
  61. account_key_file.to_string_lossy().into_owned(),
  62. user_name_cow.clone(),
  63. command_runner,
  64. ),
  65. Dir::new("/home/acme/challenges"),
  66. Owner::new(
  67. "/home/acme/challenges",
  68. user_name_cow.clone(),
  69. command_runner,
  70. ),
  71. Dir::new("/etc/ssl/local_certs"),
  72. Owner::new("/etc/ssl/local_certs", user_name_cow, command_runner),
  73. File::new("/home/acme/lets_encrypt_x3_cross_signed.pem", cert),
  74. ))
  75. }