Automatically manage firewall rules (iptables) for services #1

Merged
quentin merged 7 commits from add-firewall-rules into master 2020-07-04 15:16:23 +00:00
Showing only changes of commit a2d2582098 - Show all commits

View file

@ -1,6 +1,8 @@
use iptables; use iptables;
use regex::Regex; use regex::Regex;
use std::collections::HashSet; use std::collections::HashSet;
use std::io;

Could you add the anyhow crate to handle errors please:

use anyhow::{Result,Context}
Could you add the anyhow crate to handle errors please: ```rust use anyhow::{Result,Context} ```
#[derive(PartialEq,Eq,Debug,Hash)] #[derive(PartialEq,Eq,Debug,Hash)]
pub struct Port { pub struct Port {
@ -8,22 +10,35 @@ pub struct Port {
number: u16, number: u16,
} }
pub fn setup(ipt: &iptables::IPTables) { #[derive(Debug)]
ipt.new_chain("filter", "DIPLONAT").unwrap(); pub struct FirewallError(String);
ipt.insert("filter", "INPUT", "-j DIPLONAT", 1).unwrap();

anyhow will override your Result<> object, taking only a return value, error will be generic then.
eg:

pub fn setup(ipt: &iptables::IPTables) -> Result<()> {

This tip applies for the whole document, and in any case build will fail as soon as you will have added the anyhow use statement.

anyhow will override your Result<> object, taking only a return value, error will be generic then. eg: ```rust pub fn setup(ipt: &iptables::IPTables) -> Result<()> { ``` This tip applies for the whole document, and in any case build will fail as soon as you will have added the anyhow use statement.
impl From<iptables::error::IPTError> for FirewallError {
fn from(error: iptables::error::IPTError) -> Self {

It's very clever to put the rules in a separate chain, well done ;)

It's very clever to put the rules in a separate chain, well done ;)
FirewallError(error.to_string())
} }
pub fn open_ports(ipt: &iptables::IPTables, ports: Vec<Port>) { }
pub fn setup(ipt: &iptables::IPTables) -> Result<(), FirewallError> {
ipt.new_chain("filter", "DIPLONAT")?;
ipt.insert("filter", "INPUT", "-j DIPLONAT", 1)?;
Ok(())

Same as before, you can just use Result<()>

Same as before, you can just use `Result<()>`
}
pub fn open_ports(ipt: &iptables::IPTables, ports: Vec<Port>) -> Result<(), FirewallError> {
for p in ports { for p in ports {
ipt.append("filter", "DIPLONAT", &format!("-p {} --dport {} -j ACCEPT", p.proto, p.number)).unwrap(); ipt.append("filter", "DIPLONAT", &format!("-p {} --dport {} -j ACCEPT", p.proto, p.number))?;
}
} }
pub fn get_opened_ports(ipt: &iptables::IPTables) -> HashSet<Port> { Ok(())
}
pub fn get_opened_ports(ipt: &iptables::IPTables) -> Result<HashSet<Port>, FirewallError> {

Same as before, you can just use Result<messages::PublicExposedPorts>

Same as before, you can just use `Result<messages::PublicExposedPorts>`
let mut opened_ports: HashSet<Port> = HashSet::new(); let mut opened_ports: HashSet<Port> = HashSet::new();
let list = ipt.list("filter", "DIPLONAT").unwrap(); let list = ipt.list("filter", "DIPLONAT")?;
let re = Regex::new(r"\-A.*? \-p (\w+).*\-\-dport (\d+).*?\-j ACCEPT").unwrap(); let re = Regex::new(r"\-A.*? \-p (\w+).*\-\-dport (\d+).*?\-j ACCEPT").unwrap();
for i in list { for i in list {
let caps = re.captures(&i); let caps = re.captures(&i);
@ -41,13 +56,14 @@ pub fn get_opened_ports(ipt: &iptables::IPTables) -> HashSet<Port> {
} }
} }
opened_ports Ok(opened_ports)
} }
pub fn cleanup(ipt: &iptables::IPTables) { pub fn cleanup(ipt: &iptables::IPTables) -> Result<(), FirewallError> {
ipt.flush_chain("filter", "DIPLONAT").unwrap(); ipt.flush_chain("filter", "DIPLONAT")?;
ipt.delete("filter", "INPUT", "-j DIPLONAT").unwrap(); ipt.delete("filter", "INPUT", "-j DIPLONAT")?;
ipt.delete_chain("filter", "DIPLONAT").unwrap(); ipt.delete_chain("filter", "DIPLONAT")?;
Ok(())
} }
/* /*