summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyo Nakamura <upa@haeena.net>2024-02-06 10:35:38 +0900
committerRyo Nakamura <upa@haeena.net>2024-02-06 10:35:38 +0900
commitff45d9d71b85a618aed6d3d5e5056bada6ff81f9 (patch)
tree8f5989f7f30f8e8a9320128617fa63e22115d65a
parent9908fb309d9388481769e1a558a1f8dc47c4dabf (diff)
add two env vars to pass password/keyphrase (#9)
MSCP_SSH_AUTH_PASSWORD passes a password, and MSCP_SSH_AUTH_PASSPHRASE passes a passphrase for publickey auth. They enable avoiding interactive password input. Test cases are also added.
-rw-r--r--Dockerfile/almalinux-9.3.Dockerfile15
-rw-r--r--Dockerfile/alpine-3.19.Dockerfile18
-rw-r--r--Dockerfile/rocky-8.9.Dockerfile16
-rw-r--r--Dockerfile/rocky-9.3.Dockerfile15
-rw-r--r--Dockerfile/ubuntu-20.04.Dockerfile14
-rw-r--r--Dockerfile/ubuntu-22.04.Dockerfile17
-rw-r--r--doc/mscp.1.in18
-rw-r--r--doc/mscp.rst15
-rw-r--r--include/mscp.h11
-rw-r--r--src/main.c4
-rw-r--r--test/test_e2e.py49
11 files changed, 172 insertions, 20 deletions
diff --git a/Dockerfile/almalinux-9.3.Dockerfile b/Dockerfile/almalinux-9.3.Dockerfile
index 516fbd2..3286f72 100644
--- a/Dockerfile/almalinux-9.3.Dockerfile
+++ b/Dockerfile/almalinux-9.3.Dockerfile
@@ -8,10 +8,21 @@ RUN python3 -m pip install pytest
# preparation for sshd
-RUN mkdir /var/run/sshd \
+RUN mkdir /var/run/sshd \
&& ssh-keygen -A \
&& ssh-keygen -f /root/.ssh/id_rsa -N "" \
- && mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
+ && cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys
+
+# create test user
+RUN useradd -m -d /home/test test \
+ && echo "test:userpassword" | chpasswd \
+ && mkdir -p /home/test/.ssh \
+ && ssh-keygen -f /home/test/.ssh/id_rsa_test -N "keypassphrase" \
+ && cat /home/test/.ssh/id_rsa_test.pub >> /home/test/.ssh/authorized_keys \
+ && chown -R test:test /home/test \
+ && chown -R test:test /home/test/.ssh
+
+RUN rm -rf /run/nologin
ARG mscpdir="/mscp"
diff --git a/Dockerfile/alpine-3.19.Dockerfile b/Dockerfile/alpine-3.19.Dockerfile
index a19957b..a338161 100644
--- a/Dockerfile/alpine-3.19.Dockerfile
+++ b/Dockerfile/alpine-3.19.Dockerfile
@@ -9,10 +9,20 @@ RUN apk add --no-cache \
RUN pip3 install --break-system-packages conan
# preparation for sshd
-RUN ssh-keygen -A
-RUN mkdir /var/run/sshd \
- && ssh-keygen -f /root/.ssh/id_rsa -N "" \
- && mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
+RUN ssh-keygen -A \
+ && mkdir /var/run/sshd \
+ && ssh-keygen -f /root/.ssh/id_rsa -N "" \
+ && cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys
+
+# create test user
+RUN addgroup -S test \
+ && adduser -S test -G test \
+ && echo "test:userpassword" | chpasswd \
+ && mkdir -p /home/test/.ssh \
+ && ssh-keygen -f /home/test/.ssh/id_rsa_test -N "keypassphrase" \
+ && cat /home/test/.ssh/id_rsa_test.pub >> /home/test/.ssh/authorized_keys \
+ && chown -R test:test /home/test \
+ && chown -R test:test /home/test/.ssh
# Build mscp as a single binary
diff --git a/Dockerfile/rocky-8.9.Dockerfile b/Dockerfile/rocky-8.9.Dockerfile
index cb2de9f..df23d83 100644
--- a/Dockerfile/rocky-8.9.Dockerfile
+++ b/Dockerfile/rocky-8.9.Dockerfile
@@ -8,10 +8,22 @@ RUN python3 -m pip install pytest
# preparation for sshd
-RUN mkdir /var/run/sshd \
+RUN mkdir /var/run/sshd \
&& ssh-keygen -A \
&& ssh-keygen -f /root/.ssh/id_rsa -N "" \
- && mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
+ && cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys
+
+# create test user
+RUN useradd -m -d /home/test test \
+ && echo "test:userpassword" | chpasswd \
+ && mkdir -p /home/test/.ssh \
+ && ssh-keygen -f /home/test/.ssh/id_rsa_test -N "keypassphrase" \
+ && cat /home/test/.ssh/id_rsa_test.pub >> /home/test/.ssh/authorized_keys \
+ && chown -R test:test /home/test \
+ && chown -R test:test /home/test/.ssh
+
+RUN rm -rf /run/nologin
+
ARG mscpdir="/mscp"
diff --git a/Dockerfile/rocky-9.3.Dockerfile b/Dockerfile/rocky-9.3.Dockerfile
index 84f6b11..225aafa 100644
--- a/Dockerfile/rocky-9.3.Dockerfile
+++ b/Dockerfile/rocky-9.3.Dockerfile
@@ -8,10 +8,21 @@ RUN python3 -m pip install pytest
# preparation for sshd
-RUN mkdir /var/run/sshd \
+RUN mkdir /var/run/sshd \
&& ssh-keygen -A \
&& ssh-keygen -f /root/.ssh/id_rsa -N "" \
- && mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
+ && cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys
+
+# create test user
+RUN useradd -m -d /home/test test \
+ && echo "test:userpassword" | chpasswd \
+ && mkdir -p /home/test/.ssh \
+ && ssh-keygen -f /home/test/.ssh/id_rsa_test -N "keypassphrase" \
+ && cat /home/test/.ssh/id_rsa_test.pub >> /home/test/.ssh/authorized_keys \
+ && chown -R test:test /home/test \
+ && chown -R test:test /home/test/.ssh
+
+RUN rm -rf /run/nologin
ARG mscpdir="/mscp"
diff --git a/Dockerfile/ubuntu-20.04.Dockerfile b/Dockerfile/ubuntu-20.04.Dockerfile
index a2dda99..5a75295 100644
--- a/Dockerfile/ubuntu-20.04.Dockerfile
+++ b/Dockerfile/ubuntu-20.04.Dockerfile
@@ -12,9 +12,19 @@ RUN python3 -m pip install pytest
# preparation for sshd
-RUN mkdir /var/run/sshd \
+RUN mkdir /var/run/sshd \
+ && ssh-keygen -A \
&& ssh-keygen -f /root/.ssh/id_rsa -N "" \
- && mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
+ && cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys
+
+# create test user
+RUN useradd -m -d /home/test test \
+ && echo "test:userpassword" | chpasswd \
+ && mkdir -p /home/test/.ssh \
+ && ssh-keygen -f /home/test/.ssh/id_rsa_test -N "keypassphrase" \
+ && cat /home/test/.ssh/id_rsa_test.pub >> /home/test/.ssh/authorized_keys \
+ && chown -R test:test /home/test \
+ && chown -R test:test /home/test/.ssh
ARG mscpdir="/mscp"
diff --git a/Dockerfile/ubuntu-22.04.Dockerfile b/Dockerfile/ubuntu-22.04.Dockerfile
index 1a8fb12..dff3f7f 100644
--- a/Dockerfile/ubuntu-22.04.Dockerfile
+++ b/Dockerfile/ubuntu-22.04.Dockerfile
@@ -12,9 +12,20 @@ RUN python3 -m pip install pytest
# preparation for sshd
-RUN mkdir /var/run/sshd \
- && ssh-keygen -f /root/.ssh/id_rsa -N "" \
- && mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
+RUN mkdir /var/run/sshd \
+ && ssh-keygen -A \
+ && ssh-keygen -f /root/.ssh/id_rsa -N "" \
+ && cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys
+
+# create test user
+RUN useradd -m -d /home/test test \
+ && echo "test:userpassword" | chpasswd \
+ && mkdir -p /home/test/.ssh \
+ && ssh-keygen -f /home/test/.ssh/id_rsa_test -N "keypassphrase" \
+ && cat /home/test/.ssh/id_rsa_test.pub >> /home/test/.ssh/authorized_keys \
+ && chown -R test:test /home/test \
+ && chown -R test:test /home/test/.ssh
+
ARG mscpdir="/mscp"
diff --git a/doc/mscp.1.in b/doc/mscp.1.in
index 545ea09..b329c27 100644
--- a/doc/mscp.1.in
+++ b/doc/mscp.1.in
@@ -236,6 +236,24 @@ Prints help.
.SH EXIT STATUS
Exit status is 0 on success, and >0 if an error occurs.
+.SH ENVIRONMENT
+
+.PP
+.B mscp
+recognizes the following environment variables.
+
+.TP
+.B MSCP_SSH_AUTH_PASSWORD
+This environment variable passes a password for password
+authentication to establish SSH connections.
+
+.TP
+.B MSCP_SSH_AUTH_PASSPHRASE
+This environment variable passes a passphrase for public-key
+authentication for establishing SSH connections.
+
+
+
.SH NOTES
.PP
diff --git a/doc/mscp.rst b/doc/mscp.rst
index ca4cf44..c4ed35b 100644
--- a/doc/mscp.rst
+++ b/doc/mscp.rst
@@ -2,7 +2,7 @@
MSCP
====
-:Date: v0.1.3
+:Date: v0.1.3-15-g9908fb3
NAME
====
@@ -147,6 +147,19 @@ EXIT STATUS
Exit status is 0 on success, and >0 if an error occurs.
+ENVIRONMENT
+===========
+
+**mscp** recognizes the following environment variables.
+
+**MSCP_SSH_AUTH_PASSWORD**
+ This environment variable passes a password for password
+ authentication to establish SSH connections.
+
+**MSCP_SSH_AUTH_PASSPHRASE**
+ This environment variable passes a passphrase for public-key
+ authentication for establishing SSH connections.
+
NOTES
=====
diff --git a/include/mscp.h b/include/mscp.h
index 92f6e9e..f8ff0df 100644
--- a/include/mscp.h
+++ b/include/mscp.h
@@ -72,6 +72,17 @@ struct mscp_ssh_opts {
bool enable_nagle; /** enable Nagle's algorithm if true */
};
+/** @def
+ * Environment variable that passes password for ssh password auth
+ */
+#define ENV_SSH_AUTH_PASSWORD "MSCP_SSH_AUTH_PASSWORD"
+
+/** @def
+ * Environment vraible that passes passphrase for private key
+ */
+#define ENV_SSH_AUTH_PASSPHRASE "MSCP_SSH_AUTH_PASSPHRASE"
+
+
/**
* @struct mscp_stats
* @brief Structure to get mscp statistics
diff --git a/src/main.c b/src/main.c
index 282837a..9dc7d7b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -344,6 +344,10 @@ int main(int argc, char **argv)
}
}
+ s.password = getenv(ENV_SSH_AUTH_PASSWORD);
+ s.passphrase = getenv(ENV_SSH_AUTH_PASSPHRASE);
+
+
if (argc - optind < 2) {
/* mscp needs at lease 2 (src and target) argument */
usage(false);
diff --git a/test/test_e2e.py b/test/test_e2e.py
index a73d896..fa17a6a 100644
--- a/test/test_e2e.py
+++ b/test/test_e2e.py
@@ -12,16 +12,16 @@ from subprocess import check_call, CalledProcessError, PIPE
from util import File, check_same_md5sum
-def run2ok(args):
+def run2ok(args, env = None):
cmd = list(map(str, args))
print("cmd: {}".format(" ".join(cmd)))
- check_call(cmd)
+ check_call(cmd, env = env)
-def run2ng(args):
+def run2ng(args, env = None):
cmd = list(map(str, args))
print("cmd: {}".format(" ".join(cmd)))
with pytest.raises(CalledProcessError) as e:
- check_call(cmd)
+ check_call(cmd, env = env)
""" usage test """
@@ -401,3 +401,44 @@ def test_config_ng(mscp, src_prefix, dst_prefix):
os.remove(config)
src.cleanup()
dst.cleanup()
+
+# username test assumes that this test runs inside a container, see Dockerfiles
+def test_specify_passphrase_via_env(mscp):
+ src = File(os.getcwd() + "/src", size = 1024).make()
+ dst = File("/home/test/dst")
+ env = os.environ
+ env["MSCP_SSH_AUTH_PASSPHRASE"] = "keypassphrase"
+ run2ok([mscp, "-H", "-vvv", "-l", "test", "-i", "/home/test/.ssh/id_rsa_test",
+ src.path, "localhost:" + dst.path], env = env)
+ assert check_same_md5sum(src, dst)
+ src.cleanup()
+ dst.cleanup()
+
+def test_specify_invalid_passphrase_via_env(mscp):
+ src = File(os.getcwd() + "/src", size = 1024).make()
+ dst = File("/home/test/dst")
+ env = os.environ
+ env["MSCP_SSH_AUTH_PASSPHRASE"] = "invalid-keypassphrase"
+ run2ng([mscp, "-H", "-vvv", "-l", "test", "-i", "/home/test/.ssh/id_rsa_test",
+ src.path, "localhost:" + dst.path], env = env)
+ src.cleanup()
+
+def test_specify_password_via_env(mscp):
+ src = File(os.getcwd() + "/src", size = 1024).make()
+ dst = File("/home/test/dst")
+ env = os.environ
+ env["MSCP_SSH_AUTH_PASSWORD"] = "userpassword"
+ run2ok([mscp, "-H", "-vvv", "-l", "test",
+ src.path, "localhost:" + dst.path], env = env)
+ assert check_same_md5sum(src, dst)
+ src.cleanup()
+ dst.cleanup()
+
+def test_specify_invalid_password_via_env(mscp):
+ src = File(os.getcwd() + "/src", size = 1024).make()
+ dst = File("/home/test/dst")
+ env = os.environ
+ env["MSCP_SSH_AUTH_PASSWORD"] = "invalid-userpassword"
+ run2ng([mscp, "-H", "-vvv", "-l", "test",
+ src.path, "localhost:" + dst.path], env = env)
+ src.cleanup()