Skip to content

Commit

Permalink
refactor: remove --sshkeyfile CLI argument
Browse files Browse the repository at this point in the history
Relates to JIRA: DISCOVERY-797
  • Loading branch information
infinitewarp committed Dec 16, 2024
1 parent 6f9df65 commit 9163ce5
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 118 deletions.
6 changes: 0 additions & 6 deletions qpc/cred/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ def __init__(self, subparsers):
action="store_true",
help=_(messages.CRED_PWD_HELP),
)
group.add_argument(
"--sshkeyfile",
dest="filename",
metavar="FILENAME",
help=_(messages.CRED_SSH_FILE_HELP),
)
group.add_argument(
"--sshkey",
dest="ssh_key",
Expand Down
7 changes: 0 additions & 7 deletions qpc/cred/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ def __init__(self, subparsers):
action="store_true",
help=_(messages.CRED_TOKEN_HELP),
)
group.add_argument(
"--sshkeyfile",
dest="filename",
metavar="FILENAME",
help=_(messages.CRED_SSH_FILE_HELP),
)
group.add_argument(
"--sshkey",
dest="ssh_key",
Expand Down Expand Up @@ -108,7 +102,6 @@ def _validate_args(self):
if not (
self.args.username
or self.args.password
or self.args.filename
or self.args.ssh_key
or self.args.ssh_passphrase
or self.args.become_method
Expand Down
4 changes: 0 additions & 4 deletions qpc/cred/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ def build_credential_payload(args, cred_type, add_none=True):
req_payload["become_method"] = args.become_method
if "become_user" in args and args.become_user:
req_payload["become_user"] = args.become_user
if "filename" in args and args.filename:
req_payload["ssh_keyfile"] = args.filename
elif add_none:
req_payload["ssh_keyfile"] = None

req_payload = get_password(args, req_payload, add_none)
return req_payload
Expand Down
42 changes: 5 additions & 37 deletions qpc/tests/cred/test_cred_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@
from qpc.utils import get_server_location


@pytest.fixture
def ssh_key(tmp_path):
"""Return the path to a fake ssh keyfile."""
sshkey = tmp_path / "ssh_key"
sshkey.write_text("fake ssh keyfile.")
return str(sshkey)


@pytest.mark.usefixtures("server_config")
class TestCredentialAddCli:
"""Class for testing the credential add commands for qpc."""
Expand Down Expand Up @@ -61,26 +53,7 @@ def test_add_no_type(self):
with pytest.raises(SystemExit), patch.object(sys, "argv", args):
CLI().main()

def test_add_bad_keyfile(self):
"""Testing the add credential command.
When providing an invalid path for the sshkeyfile.
"""
args = [
"/bin/qpc",
"credential",
"add",
"--name",
"credential1",
"--username",
"root",
"--sshkeyfile",
"bad_path",
]
with pytest.raises(SystemExit), patch.object(sys, "argv", args):
CLI().main()

def test_add_cred_name_dup(self, ssh_key):
def test_add_cred_name_dup(self):
"""Testing the add credential command duplicate name."""
url = get_server_location() + CREDENTIAL_URI
error = {"name": ["credential with this name already exists."]}
Expand All @@ -90,15 +63,14 @@ def test_add_cred_name_dup(self, ssh_key):
name="cred_dup",
username="root",
type=NETWORK_CRED_TYPE,
filename=ssh_key,
password=None,
become_password=None,
ssh_passphrase=None,
)
with pytest.raises(SystemExit):
self.command.main(args)

def test_add_cred_ssl_err(self, ssh_key):
def test_add_cred_ssl_err(self):
"""Testing the add credential command with a connection error."""
url = get_server_location() + CREDENTIAL_URI
with requests_mock.Mocker() as mocker:
Expand All @@ -107,15 +79,14 @@ def test_add_cred_ssl_err(self, ssh_key):
name="credential1",
username="root",
type=NETWORK_CRED_TYPE,
filename=ssh_key,
password=None,
become_password=None,
ssh_passphrase=None,
)
with pytest.raises(SystemExit):
self.command.main(args)

def test_add_cred_conn_err(self, ssh_key):
def test_add_cred_conn_err(self):
"""Testing the add credential command with a connection error."""
url = get_server_location() + CREDENTIAL_URI
with requests_mock.Mocker() as mocker:
Expand All @@ -124,15 +95,14 @@ def test_add_cred_conn_err(self, ssh_key):
name="credential1",
username="root",
type=NETWORK_CRED_TYPE,
filename=ssh_key,
password=None,
become_password=None,
ssh_passphrase=None,
)
with pytest.raises(SystemExit):
self.command.main(args)

def test_add_host_cred(self, caplog, ssh_key):
def test_add_host_cred(self, caplog):
"""Testing the add host cred command successfully."""
url = get_server_location() + CREDENTIAL_URI
with requests_mock.Mocker() as mocker:
Expand All @@ -141,7 +111,6 @@ def test_add_host_cred(self, caplog, ssh_key):
name="credential1",
username="root",
type=NETWORK_CRED_TYPE,
filename=ssh_key,
password=None,
ssh_passphrase=None,
become_method=None,
Expand Down Expand Up @@ -228,7 +197,7 @@ def test_add_host_cred_with_sshkey_and_passphrase(
expected_message = messages.CRED_ADDED % "credential1"
assert expected_message in caplog.text

def test_add_host_cred_with_become(self, caplog, ssh_key):
def test_add_host_cred_with_become(self, caplog):
"""Testing the add host cred command successfully."""
url = get_server_location() + CREDENTIAL_URI
with requests_mock.Mocker() as mocker:
Expand All @@ -237,7 +206,6 @@ def test_add_host_cred_with_become(self, caplog, ssh_key):
name="credential1",
username="root",
type=NETWORK_CRED_TYPE,
filename=ssh_key,
password=None,
ssh_passphrase=None,
become_method="sudo",
Expand Down
45 changes: 6 additions & 39 deletions qpc/tests/cred/test_cred_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@
from qpc.utils import get_server_location


@pytest.fixture
def ssh_key(tmp_path) -> str:
"""Return the path to a fake ssh keyfile."""
sshkey = tmp_path / "ssh_key"
sshkey.write_text("fake ssh keyfile.")
return str(sshkey)


@pytest.mark.usefixtures("server_config")
class TestCredentialEditCli:
"""Class for testing the credential edit commands for qpc."""
Expand All @@ -54,69 +46,49 @@ def test_edit_req_args_err(self):
with pytest.raises(SystemExit), patch.object(sys, "argv", args):
CLI().main()

def test_edit_bad_key(self):
"""Testing the credential edit command.
When providing an invalid path for the sshkeyfile.
"""
args = [
"/bin/qpc",
"credential",
"edit",
"--name",
"cred1",
"--sshkeyfile",
"bad_path",
]
with pytest.raises(SystemExit), patch.object(sys, "argv", args):
CLI().main()

def test_edit_cred_none(self, ssh_key):
def test_edit_cred_none(self):
"""Testing the edit credential command for non-existing credential."""
url = get_server_location() + CREDENTIAL_URI + "?name=cred_none"
with requests_mock.Mocker() as mocker:
mocker.get(url, status_code=200, json={"count": 0})
args = Namespace(
name="cred_none",
username="root",
filename=ssh_key,
password=None,
become_password=None,
)
with pytest.raises(SystemExit):
self.command.main(args)

def test_edit_cred_ssl_err(self, ssh_key):
def test_edit_cred_ssl_err(self):
"""Testing the edit credential command with a connection error."""
url = get_server_location() + CREDENTIAL_URI
with requests_mock.Mocker() as mocker:
mocker.get(url, exc=requests.exceptions.SSLError)
args = Namespace(
name="credential1",
username="root",
filename=ssh_key,
password=None,
become_password=None,
)
with pytest.raises(SystemExit):
self.command.main(args)

def test_edit_cred_conn_err(self, ssh_key):
def test_edit_cred_conn_err(self):
"""Testing the edit credential command with a connection error."""
url = get_server_location() + CREDENTIAL_URI
with requests_mock.Mocker() as mocker:
mocker.get(url, exc=requests.exceptions.ConnectTimeout)
args = Namespace(
name="credential1",
username="root",
filename=ssh_key,
password=None,
become_password=None,
)
with pytest.raises(SystemExit):
self.command.main(args)

def test_edit_host_cred(self, caplog, ssh_key):
def test_edit_host_cred(self, caplog):
"""Testing the edit credential command successfully."""
url_get = get_server_location() + CREDENTIAL_URI
url_patch = get_server_location() + CREDENTIAL_URI + "1/"
Expand All @@ -126,7 +98,6 @@ def test_edit_host_cred(self, caplog, ssh_key):
"name": "cred1",
"cred_type": NETWORK_CRED_TYPE,
"username": "root",
"sshkeyfile": "/foo/bar",
}
]
data = {"count": 1, "results": results}
Expand All @@ -136,8 +107,6 @@ def test_edit_host_cred(self, caplog, ssh_key):
args = Namespace(
name="cred1",
username="root",
filename=ssh_key,
sshkeyfile="/woot/ness",
become_password=None,
ssh_passphrase=None,
)
Expand All @@ -146,7 +115,7 @@ def test_edit_host_cred(self, caplog, ssh_key):
expected_message = messages.CRED_UPDATED % "cred1"
assert expected_message in caplog.text

def test_partial_edit_host_cred(self, caplog, ssh_key):
def test_partial_edit_host_cred(self, caplog):
"""Testing the edit credential command successfully."""
url_get = get_server_location() + CREDENTIAL_URI
url_patch = get_server_location() + CREDENTIAL_URI + "1/"
Expand All @@ -165,7 +134,6 @@ def test_partial_edit_host_cred(self, caplog, ssh_key):
args = Namespace(
name="cred1",
username="root",
filename=ssh_key,
password=None,
become_password=None,
ssh_passphrase=None,
Expand Down Expand Up @@ -256,7 +224,7 @@ def test_partial_edit_vcenter_cred(self, caplog):
expected_message = messages.CRED_UPDATED % "cred1"
assert expected_message in caplog.text

def test_edit_cred_get_error(self, ssh_key):
def test_edit_cred_get_error(self):
"""Testing the edit credential command server error occurs."""
StringIO()
url_get = get_server_location() + CREDENTIAL_URI
Expand All @@ -265,7 +233,6 @@ def test_edit_cred_get_error(self, ssh_key):
args = Namespace(
name="cred1",
username="root",
filename=ssh_key,
password=None,
become_password=None,
)
Expand Down
5 changes: 1 addition & 4 deletions qpc/tests/cred/test_openshift_cred_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ def test_add_no_token(
CLI().main()
out, err = capsys.readouterr()
assert out == ""
assert (
"one of the arguments --password --sshkeyfile"
" --sshkey --token is required" in err
)
assert "one of the arguments --password" " --sshkey --token is required" in err

def test_add_no_name(
self,
Expand Down
21 changes: 0 additions & 21 deletions qpc/tests/cred/test_openshift_cred_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,3 @@ def test_edit_with_token_and_password_as_args(
out, err = capsys.readouterr()
assert out == ""
assert "argument --token: not allowed with argument --password" in err

def test_edit_with_token_and_sshkeyfile_as_args(
self,
capsys,
):
"""Test openshift cred edit with token and sshkeyfile args."""
sys.argv = [
"/bin/qpc",
"cred",
"edit",
"--name",
"openshift_cred",
"--sshkeyfile",
"mock_path/test.pem",
"--token",
]
with pytest.raises(SystemExit):
CLI().main()
out, err = capsys.readouterr()
assert out == ""
assert "argument --token: not allowed with argument --sshkeyfile" in err

0 comments on commit 9163ce5

Please sign in to comment.