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.

56 lines
1.3 KiB

7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
7 years ago
5 years ago
  1. use crate::command_runner::CommandRunner;
  2. use crate::symbols::Symbol;
  3. use async_trait::async_trait;
  4. use std::error::Error;
  5. #[derive(Debug)]
  6. pub struct User<'a, U, C> {
  7. user_name: U,
  8. command_runner: &'a C,
  9. }
  10. impl<'a, U: AsRef<str>, C: CommandRunner> User<'a, U, C> {
  11. pub fn new(user_name: U, command_runner: &'a C) -> Self {
  12. Self {
  13. user_name,
  14. command_runner,
  15. }
  16. }
  17. async fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
  18. let b = self
  19. .command_runner
  20. .get_output("mariadb", args!["--skip-column-names", "-B", "-e", sql])
  21. .await?;
  22. Ok(String::from_utf8(b)?)
  23. }
  24. }
  25. #[async_trait(?Send)]
  26. impl<U: AsRef<str>, C: CommandRunner> Symbol for User<'_, U, C> {
  27. async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  28. Ok(
  29. self
  30. .run_sql(&format!(
  31. "SELECT User FROM mysql.user WHERE User = '{}' AND plugin = 'unix_socket'",
  32. self.user_name.as_ref()
  33. ))
  34. .await?
  35. .trim_end()
  36. == self.user_name.as_ref(),
  37. )
  38. }
  39. async fn execute(&self) -> Result<(), Box<dyn Error>> {
  40. self
  41. .run_sql(&format!(
  42. "GRANT ALL ON {0}.* TO {0} IDENTIFIED VIA unix_socket",
  43. self.user_name.as_ref()
  44. ))
  45. .await?;
  46. Ok(())
  47. }
  48. }
  49. #[cfg(test)]
  50. mod test {}