-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsftpwrapper.py
executable file
·78 lines (64 loc) · 1.97 KB
/
sftpwrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
import getpass
import os
import sys
from lib import find_executable, get_hostvars, manage_conf_file
def main():
argv = list(sys.argv[1:])
bastion_user = None
bastion_host = None
bastion_port = None
remote_user = None
remote_port = 22
default_configuration_file = "/etc/ovh/bastion/config.yml"
iteration = enumerate(argv)
for i, e in iteration:
if e == "-o" and argv[i + 1].startswith("User="):
remote_user = argv[i + 1].split("=")[-1]
next(iteration)
elif e == "-o" and argv[i + 1].startswith("Port="):
remote_port = argv[i + 1].split("=")[-1]
next(iteration)
sftpcmd = argv.pop()
host = argv.pop()
# Playbook environment variables are not pushed to the sftp wrapper
# Skipping this source of configuration
# Read from configuration file
bastion_host, bastion_port, bastion_user = manage_conf_file(
os.getenv("BASTION_CONF_FILE", default_configuration_file),
bastion_host,
bastion_port,
bastion_user,
)
# Read from inventory and environment variables
if not bastion_host or not bastion_port or not bastion_user:
inventory = get_hostvars(host)
bastion_port = inventory.get("bastion_port", os.getenv("BASTION_PORT", 22))
bastion_user = inventory.get(
"bastion_user", os.getenv("BASTION_USER", getpass.getuser())
)
bastion_host = inventory.get("bastion_host", os.getenv("BASTION_HOST"))
args = [
"ssh",
"{}@{}".format(bastion_user, bastion_host),
"-p",
bastion_port,
"-o",
"StrictHostKeyChecking=no",
"-T",
"--",
"--user",
remote_user,
"--port",
remote_port,
"--host",
host,
"--osh",
"sftp",
]
os.execv(
find_executable("ssh"),
[str(e).strip() for e in args],
)
if __name__ == "__main__":
main()