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.

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