From e0dfc25717cffa0c5bdfc36271a01672bf57d650 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Tue, 20 Aug 2019 02:05:48 -0500 Subject: [PATCH] Added test folder --- .gitignore | 2 +- tests/constants.py | 3 + tests/sample_configs/config_test | 111 ++++++++++++++++++++ tests/sample_configs/crucial | 15 +++ tests/sample_configs/gist1 | 65 ++++++++++++ tests/sample_configs/gist2 | 73 +++++++++++++ tests/sample_configs/linuxize | 21 ++++ tests/sample_configs/nerderati | 11 ++ tests/sample_configs/nixcraft | 45 ++++++++ tests/test_class.py | 19 ++++ tests/test_config | 110 +++++++++++++++++++ tests/test_config_file_read.py | 8 ++ tests/test_config_file_read_write.py | 17 +++ tests/test_config_file_write.py | 9 ++ tests/test_host_class_from_parsed_config.py | 34 ++++++ tests/test_ssh_config_file_parse.py | 12 +++ 16 files changed, 554 insertions(+), 1 deletion(-) create mode 100644 tests/constants.py create mode 100644 tests/sample_configs/config_test create mode 100644 tests/sample_configs/crucial create mode 100644 tests/sample_configs/gist1 create mode 100644 tests/sample_configs/gist2 create mode 100644 tests/sample_configs/linuxize create mode 100644 tests/sample_configs/nerderati create mode 100644 tests/sample_configs/nixcraft create mode 100644 tests/test_class.py create mode 100644 tests/test_config create mode 100644 tests/test_config_file_read.py create mode 100644 tests/test_config_file_read_write.py create mode 100644 tests/test_config_file_write.py create mode 100644 tests/test_host_class_from_parsed_config.py create mode 100644 tests/test_ssh_config_file_parse.py diff --git a/.gitignore b/.gitignore index 944cfd2..1d51b31 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/tests __pycache__ +/tests/write_test diff --git a/tests/constants.py b/tests/constants.py new file mode 100644 index 0000000..0249e7e --- /dev/null +++ b/tests/constants.py @@ -0,0 +1,3 @@ +import os + +user_ssh_config_path = os.path.join(os.environ["HOME"], ".ssh", "config") diff --git a/tests/sample_configs/config_test b/tests/sample_configs/config_test new file mode 100644 index 0000000..79caec0 --- /dev/null +++ b/tests/sample_configs/config_test @@ -0,0 +1,111 @@ +Host attachpoint + Hostname 192.168.1.25 + IdentityFile ~/.ssh/keys/attachpoint + Port 333 + User root + +Host camille + Hostname localhost + IdentityFile ~/.ssh/keys/virtual + Port 2223 + User raphael + +Host camille_tablet + Hostname 192.168.1.115 + IdentityFile ~/.ssh/keys/camille_tablet + Port 8022 + +Host cloud + Hostname rlbr-gcloud.ddns.net + IdentityFile ~/.ssh/keys/cloud + User raphael_roberts48 + +Host dbox + Hostname localhost + IdentityFile ~/.ssh/keys/vbox + Port 2225 + User raphael + +Host downstairs + Hostname 192.168.1.140 + IdentityFile ~/.ssh/keys/downstairs-wsl + Port 2222 + User server + +Host downstairs-windows + Hostname 192.168.1.140 + IdentityFile ~/.ssh/keys/downstairs + User raphael + +Host mac_vm + Hostname localhost + IdentityFile ~/.ssh/keys/virtual + Port 1984 + User fuckboi + +Host moto + Hostname localhost + IdentityFile ~/.ssh/keys/moto + Port 8023 + +Host room + Hostname rlbrhost.ddns.net + IdentityFile ~/.ssh/keys/room + Port 22 + User raphael + +Host room-local + Hostname 192.168.1.120 + IdentityFile ~/.ssh/keys/room + User raphael + +Host router + Hostname rlbrhost.ddns.net + IdentityFile ~/.ssh/keys/router + Port 1025 + User root + +Host router-local + Hostname 192.168.1.1 + IdentityFile ~/.ssh/keys/router + Port 1025 + User root + +Host termux + Hostname localhost + IdentityFile ~/.ssh/keys/termux + Port 8022 + +Host termux-lan + Hostname 192.168.1.121 + IdentityFile ~/.ssh/keys/termux + Port 8022 + +Host termux-remote + Hostname rlbr.phone + IdentityFile ~/.ssh/keys/termux + ProxyCommand ssh -W %h:%p serveo.net + +Host ubuntu + Hostname localhost + IdentityFile ~/.ssh/keys/virtual + Port 2226 + User raphael + +Host vbox + Hostname localhost + IdentityFile ~/.ssh/keys/vbox + Port 5139 + User booooooooi + +Host wsl + Hostname rlbrhost.ddns.net + IdentityFile ~/.ssh/keys/wsl + Port 2222 + User raphael + +Host wsl-local + Hostname localhost + IdentityFile ~/.ssh/keys/wsl + Port 2222 + User raphael \ No newline at end of file diff --git a/tests/sample_configs/crucial b/tests/sample_configs/crucial new file mode 100644 index 0000000..c04d52b --- /dev/null +++ b/tests/sample_configs/crucial @@ -0,0 +1,15 @@ +# My Web Server +Host web +User example +Port 22 +HostName www.example.com +ServerAliveInterval 60 +ServerAliveCountMax 30 + +# My Database Server +Host db +User example +Port 22 +HostName db.example.com +ServerAliveInterval 60 +ServerAliveCountMax 30 diff --git a/tests/sample_configs/gist1 b/tests/sample_configs/gist1 new file mode 100644 index 0000000..c461649 --- /dev/null +++ b/tests/sample_configs/gist1 @@ -0,0 +1,65 @@ +#### Example SSH config file + +# Host = a list of domains, IPs and personalized aliases that use this config +# entry. These alias names follow "Host" and are space delimited. +# e.g. Host 192.168.1.1 example.com example1 myalias1 +# Hostname = the IP or real hostname +# e.g. Hostname 192.168.1.1 +# User = the username +# e.g. User jdoe +# Port = if this is a non standard port, i.e. not 22 +# e.g. Port 2345 + +# The above would allow you to use 'ssh exp1' or 'ssh myalias1' which are now +# both equivalent commands for 'ssh jdoe@192.168.1.1 -p2345' + +#### Example entries + +# Example 1 +Host 192.168.1.1 example1 + Hostname 192.168.1.1 + # Non standard port + Port 2222 + # Enable forwarding + ForwardAgent yes + # Proxy forwarding of the connection via 192.168.1.3 + ProxyCommand ssh jdoe@192.168.1.3 nc %h %p 2> /dev/null + +# Example 2 +Host 192.168.1.2 example2 + Hostname 192.168.1.2 + # Enable forwarding + ForwardAgent yes + +###################################### +# Defaults / catchall options. Add this block to the end of your config file to +# enable these options for all unspecified connections. +Host * + # Default user + User jdoe + + # Default identity, usually id_rsa which will be a sys default anyway. + IdentityFile ~/.ssh/id_rsa + + # Enable compression. + Compression yes + + # Hash known-host names for additional security. + HashKnownHosts yes + + # Keep connection alive may benefit some connections. + ServerAliveInterval 15 + ServerAliveCountMax 3 + + # Allow local and proxy commands + PermitLocalCommand yes + + # Forward agent for pushing your ssh-agent. You will need to add your ssh key + # to the authorized_keys file on the remote system. + # Normally not a great idea to make this a global always on option unless you + # are sure the remote systems are secure. + #ForwardAgent yes + + # Multiplexing shared connections to improve speed. + #ControlPath ~/.ssh/master-%l-%r@%h:%p + #ControlMaster auto \ No newline at end of file diff --git a/tests/sample_configs/gist2 b/tests/sample_configs/gist2 new file mode 100644 index 0000000..71cfed9 --- /dev/null +++ b/tests/sample_configs/gist2 @@ -0,0 +1,73 @@ +# ssh(1) obtains configuration data from the following sources in the following order: +# +# 1. command-line options +# 2. user's configuration file (~/.ssh/config) +# 3. system-wide configuration file (/etc/ssh/ssh_config) +# +# For each parameter, the first obtained value will be used. The configuration files contain sections separated +# by “Host” specifications, and that section is only applied for hosts that match one of the patterns given in the +# specification. The matched host name is the one given on the command line. +# +# Since the first obtained value for each parameter is used, more host-specific declarations should be given near +# the beginning of the file, and general defaults at the end. + +# Example for one that uses ssh keys +# Without specifying IdentityFile it will use the default list +Host reallysecure reallysecure.example.com + HostName reallysecure.example.com + User mysecureuser + PreferredAuthentications publickey + PasswordAuthentication no + KbdInteractiveAuthentication no + +# This is used to rsync data over lan +Host backupserver + HostName 10.20.30.40 + User backupuser + BatchMode yes + Ciphers arcfour,blowfish-cbc,aes128-ctr + IdentityFile ~/.ssh/backupserver.pem + # Generally compression over lan slows things down + Compression no + + +# See man ssh_config for all options and descriptions, this just lists what may +# be useful to do on a per-host basis. Defaults from Ubuntu 12.04 +Host * + # Can speed up logins to some servers + GSSAPIAuthentication no + GSSAPIKeyExchange no + ChallengeResponseAuthentication no + HashKnownHosts no + Protocol 2 + # Some servers will force log out if you haven't typed anything in a while + # This means send a packet every 60 seconds, after 3 of those without a + # response then drop the connection + ServerAliveCountMax 3 + ServerAliveInterval 60 + TCPKeepAlive yes + #BatchMode [yes|no] # Def: no + #CheckHostIP [yes|no] # Def: yes - no is useful on a host who's key changes (rebuilding a dev machine) + #Ciphers (see ssh_config(5)) # (sshv2) Def: (see man page) - (speed: arcfour > blowfish > aes) + #Compression [yes|no] # Def: no + #ControlMaster [yes|ask|auto|autoask] # Best to just google ControlMaster or check man pages + #ControlPath + #ControlPersist [yes|no] + #DynamicForward <[bind_addres:]port> # sets up a local socks proxy over ssh + #ExitOnForwardFailure [yes|no] # Def: no + #ForwardX11 [yes|no] # Def: no + #ForwardX11Timeout (see sshd_config "Time Formats") # Def: 20 minutes + #ForwardX11Trust [yes|no] # Def: yes (debian specific) + #HostKeyAlias # Useful if have several Host sections for single server + #HostName [|] # Also supports %h for given on command line (eg: %h.example.com) + #IdentityFile # Def: (see ssh_config(5)) - several escape sequences are supported, see man page + #KbdInteractiveAuthentication [yes|no] # Def: yes - can disable if only using ssh keys + #LocalForward <[local bind_address:]local_port> : + # An example that forwards local 8080 to port 8443 on remote server: + # LocalForward localhost:8080 127.0.0.1:8443 + #PasswordAuthentication [yes|no] # Def: yes - Another thing to disable if using keys + #Port # Def: 22 + #PreferredAuthentications # Can set this to 'publickey' to disable all other methods + #StrictHostKeyChecking [yes|no|ask] # Def: ask + #VerifyHostKeyDNS [yes|no|ask] # Def: no - useful but rarely used, google it for info + diff --git a/tests/sample_configs/linuxize b/tests/sample_configs/linuxize new file mode 100644 index 0000000..60ee362 --- /dev/null +++ b/tests/sample_configs/linuxize @@ -0,0 +1,21 @@ +Host targaryen + HostName 192.168.1.10 + User daenerys + Port 7654 + IdentityFile ~/.ssh/targaryen.key + +Host tyrell + HostName 192.168.10.20 + +Host martell + HostName 192.168.10.50 + +Host *ell + user oberyn + +Host * !martell + LogLevel INFO + +Host * + User root + Compression yes \ No newline at end of file diff --git a/tests/sample_configs/nerderati b/tests/sample_configs/nerderati new file mode 100644 index 0000000..0bb4568 --- /dev/null +++ b/tests/sample_configs/nerderati @@ -0,0 +1,11 @@ +Host github-project1 + User git + HostName github.com + IdentityFile ~/.ssh/github.project1.key +Host github-org + User git + HostName github.com + IdentityFile ~/.ssh/github.org.key +Host github.com + User git + IdentityFile ~/.ssh/github.key diff --git a/tests/sample_configs/nixcraft b/tests/sample_configs/nixcraft new file mode 100644 index 0000000..fb67c55 --- /dev/null +++ b/tests/sample_configs/nixcraft @@ -0,0 +1,45 @@ +### default for all ## +Host * + ForwardAgent no + ForwardX11 no + ForwardX11Trusted yes + User nixcraft + Port 22 + Protocol 2 + ServerAliveInterval 60 + ServerAliveCountMax 30 + +## override as per host ## +Host server1 + HostName server1.cyberciti.biz + User nixcraft + Port 4242 + IdentityFile /nfs/shared/users/nixcraft/keys/server1/id_rsa + +## Home nas server ## +Host nas01 + HostName 192.168.1.100 + User root + IdentityFile ~/.ssh/nas01.key + +## Login AWS Cloud ## +Host aws.apache + HostName 1.2.3.4 + User wwwdata + IdentityFile ~/.ssh/aws.apache.key + +## Login to internal lan server at 192.168.0.251 via our public uk office ssh based gateway using ## +## $ ssh uk.gw.lan ## +Host uk.gw.lan uk.lan + HostName 192.168.0.251 + User nixcraft + ProxyCommand ssh nixcraft@gateway.uk.cyberciti.biz nc %h %p 2> /dev/null + +## Our Us Proxy Server ## +## Forward all local port 3128 traffic to port 3128 on the remote vps1.cyberciti.biz server ## +## $ ssh -f -N proxyus ## +Host proxyus + HostName vps1.cyberciti.biz + User breakfree + IdentityFile ~/.ssh/vps1.cyberciti.biz.key + LocalForward 3128 127.0.0.1:3128 diff --git a/tests/test_class.py b/tests/test_class.py new file mode 100644 index 0000000..19eb970 --- /dev/null +++ b/tests/test_class.py @@ -0,0 +1,19 @@ +class DictThing: + def __init__(self, init_dict): + self.d = init_dict + + def __setattr__(self, name, value): + if name == "d": + super().__setattr__("d", value) + else: + + self.d[name] = value + + def __getattr__(self, name): + if name == "d": + return super().__getattribute__("d") + return self.d[name] + + +if __name__ == "__main__": + e = DictThing(dict(a="a", b="b", c="c")) diff --git a/tests/test_config b/tests/test_config new file mode 100644 index 0000000..77fe84b --- /dev/null +++ b/tests/test_config @@ -0,0 +1,110 @@ +Host attachpoint + Hostname 192.168.1.25 + IdentityFile ~/.ssh/keys/attachpoint + Port 333 + User root + +Host camille + Hostname localhost + IdentityFile ~/.ssh/keys/virtual + Port 2223 + User raphael + +Host camille_tablet + Hostname 192.168.1.115 + IdentityFile ~/.ssh/keys/camille_tablet + Port 8022 + +Host cloud + Hostname rlbr-gcloud.ddns.net + IdentityFile ~/.ssh/keys/cloud + User raphael_roberts48 + +Host dbox + Hostname localhost + IdentityFile ~/.ssh/keys/vbox + Port 2225 + User raphael + +Host downstairs + Hostname 192.168.1.140 + IdentityFile ~/.ssh/keys/downstairs-wsl + Port 2222 + User server + +Host downstairs-windows + Hostname 192.168.1.140 + IdentityFile ~/.ssh/keys/downstairs + User raphael + +Host mac_vm + Hostname localhost + IdentityFile ~/.ssh/keys/virtual + Port 1984 + User fuckboi + +Host moto + Hostname localhost + IdentityFile ~/.ssh/keys/moto + Port 8023 + +Host room + Hostname rlbrhost.ddns.net + IdentityFile ~/.ssh/keys/room + User raphael + +Host room-local + Hostname 192.168.1.120 + IdentityFile ~/.ssh/keys/room + User raphael + +Host router + Hostname rlbrhost.ddns.net + IdentityFile ~/.ssh/keys/router + Port 1025 + User root + +Host router-local + Hostname 192.168.1.1 + IdentityFile ~/.ssh/keys/router + Port 1025 + User root + +Host termux + Hostname localhost + IdentityFile ~/.ssh/keys/termux + Port 8022 + +Host termux-lan + Hostname 192.168.1.121 + IdentityFile ~/.ssh/keys/termux + Port 8022 + +Host termux-remote + Hostname rlbr.phone + IdentityFile ~/.ssh/keys/termux + ProxyCommand ssh -W %h:%p serveo.net + +Host ubuntu + Hostname localhost + IdentityFile ~/.ssh/keys/virtual + Port 2226 + User raphael + +Host vbox + Hostname localhost + IdentityFile ~/.ssh/keys/vbox + Port 5139 + User booooooooi + +Host wsl + Hostname rlbrhost.ddns.net + IdentityFile ~/.ssh/keys/wsl + Port 2222 + User raphael + +Host wsl-local + Hostname localhost + IdentityFile ~/.ssh/keys/wsl + Port 2222 + User raphael \ No newline at end of file diff --git a/tests/test_config_file_read.py b/tests/test_config_file_read.py new file mode 100644 index 0000000..fcddf67 --- /dev/null +++ b/tests/test_config_file_read.py @@ -0,0 +1,8 @@ +import sys + +sys.path.append("..") +from ssh_config_utils.config_file import ConfigFile + +from constants import user_ssh_config_path + +config_file = ConfigFile.read_file(user_ssh_config_path) diff --git a/tests/test_config_file_read_write.py b/tests/test_config_file_read_write.py new file mode 100644 index 0000000..01d4722 --- /dev/null +++ b/tests/test_config_file_read_write.py @@ -0,0 +1,17 @@ +import sys + +sys.path.append("..") + +import os +from ssh_config_utils.config_file import ConfigFile + +base = "./sample_configs" +write_base = "./write_test" +for file in os.listdir(base): + print(file) + fullpath = os.path.join(base, file) + with open(fullpath, encoding="utf-8") as read_file: + config_file = ConfigFile.read_file(read_file) + + with open(os.path.join(write_base, file), "w", encoding="utf-8") as write_file: + config_file.write_file(write_file) diff --git a/tests/test_config_file_write.py b/tests/test_config_file_write.py new file mode 100644 index 0000000..8563e4f --- /dev/null +++ b/tests/test_config_file_write.py @@ -0,0 +1,9 @@ +import sys + +sys.path.append("..") +from ssh_config_utils.config_file import ConfigFile + +from constants import user_ssh_config_path + +config_file = ConfigFile.read_file(user_ssh_config_path) +config_file.write_file("test_config") diff --git a/tests/test_host_class_from_parsed_config.py b/tests/test_host_class_from_parsed_config.py new file mode 100644 index 0000000..d388b6f --- /dev/null +++ b/tests/test_host_class_from_parsed_config.py @@ -0,0 +1,34 @@ +import sys + +sys.path.append("..") +import os + +from ssh_config_utils.parser import parse_config_text +from ssh_config_utils.host import Host, GlobalHost + +from constants import user_ssh_config_path + +with open(user_ssh_config_path) as your_ssh_config_file: + text = your_ssh_config_file.read() +data = parse_config_text(text) + + +def from_parsed_config_file(data): + global_host = None + hosts = [] + + for host_data in data: + for key, values in host_data.items(): + if len(values) == 1: + host_data[key] = values[0] + if host_data["host"] == "*": + if global_host is None: + del host_data["host"] + global_host = GlobalHost(host_data) + else: + name = host_data.pop("host") + hosts.append(Host(name, host_data)) + return global_host, hosts + + +global_host, hosts = from_parsed_config_file(data) diff --git a/tests/test_ssh_config_file_parse.py b/tests/test_ssh_config_file_parse.py new file mode 100644 index 0000000..bcfd8bd --- /dev/null +++ b/tests/test_ssh_config_file_parse.py @@ -0,0 +1,12 @@ +import sys + +sys.path.append("..") +import os + +from ssh_config_utils.parser import parse_config_text + +from constants import user_ssh_config_path + +with open(user_ssh_config_path) as your_ssh_config_file: + text = your_ssh_config_file.read() + data = parse_config_text(text)