Skip to content

Commit

Permalink
Remove changes on ssh part
Browse files Browse the repository at this point in the history
  • Loading branch information
agoscinski committed Sep 11, 2024
1 parent d2cfd84 commit 92b3cde
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 103 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ jobs:
env:
AIIDA_TEST_PROFILE: test_aiida
AIIDA_WARN_v3: 1
AIIDA_PYTEST_SSH_KEY: ${{ github.workspace }}/.ssh/id_rsa_aiida_pytest
# Python 3.12 has a performance regression when running with code coverage
# so run code coverage only for python 3.9.
run: pytest -v tests -m 'not nightly' ${{ matrix.python-version == '3.9' && '--cov aiida' || '' }}
Expand Down Expand Up @@ -140,7 +139,6 @@ jobs:
- name: Run test suite
env:
AIIDA_WARN_v3: 0
AIIDA_PYTEST_SSH_KEY: ${{ github.workspace }}/.ssh/id_rsa_aiida_pytest
run: pytest -m 'presto'


Expand Down
7 changes: 0 additions & 7 deletions .github/workflows/setup_ssh.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#!/usr/bin/env bash
# Sets up ssh keys to allow a ssh connection to localhost. This is needed
# because localhost is used as remote address to run the tests locally.
set -ev

mkdir -p ${PWD}/.ssh
mkdir -p ${HOME}/.ssh
ssh-keygen -q -t rsa -b 4096 -N "" -f "${PWD}/.ssh/id_rsa_aiida_pytest"
ssh-keygen -y -f "${PWD}/.ssh/id_rsa_aiida_pytest" >> "${HOME}/.ssh/authorized_keys"
# for the ssh_auto the tests still require the default key
ssh-keygen -q -t rsa -b 4096 -N "" -f "${HOME}/.ssh/id_rsa"
ssh-keygen -y -f "${HOME}/.ssh/id_rsa" >> "${HOME}/.ssh/authorized_keys"
ssh-keyscan -H localhost >> "${HOME}/.ssh/known_hosts"
Expand Down
4 changes: 0 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -430,23 +430,19 @@ deps =
py310: -rrequirements/requirements-py-3.10.txt
py311: -rrequirements/requirements-py-3.11.txt
py312: -rrequirements/requirements-py-3.12.txt
setenv =
AIIDA_PYTEST_SSH_KEY = $HOME/.ssh/id_rsa_aiida_pytest
[testenv:py{39,310,311,312}-presto]
passenv =
PYTHONASYNCIODEBUG
setenv =
AIIDA_WARN_v3 =
AIIDA_PYTEST_SSH_KEY = $HOME/.ssh/id_rsa_aiida_pytest
commands = pytest -m 'presto' {posargs}
[testenv:py{39,310,311,312}]
passenv =
PYTHONASYNCIODEBUG
setenv =
AIIDA_WARN_v3 =
AIIDA_PYTEST_SSH_KEY = $HOME/.ssh/id_rsa_aiida_pytest
commands = pytest {posargs}
[testenv:py{39,310,311,312}-verdi]
Expand Down
66 changes: 30 additions & 36 deletions src/aiida/manage/tests/pytest_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,50 +504,44 @@ def get_code(entry_point, executable, computer=aiida_localhost, label=None, **kw

@pytest.fixture(scope='session')
def ssh_key(tmp_path_factory) -> t.Generator[pathlib.Path, None, None]:
"""Returns a SSH key for the test session.
If the environment variable ``AIIDA_PYTEST_SSH_KEY`` is set we take the key from this path otherwise we generate a
temporary SSH key pair for the test session and return the filepath of the private key.
"""Generate a temporary SSH key pair for the test session and return the filepath of the private key.
The filepath of the public key is the same as the private key, but it adds the ``.pub`` file extension.
"""
if (ssh_key_path := os.environ.get('AIIDA_PYTEST_SSH_KEY')) is not None:
yield pathlib.Path(ssh_key_path)
else:
from cryptography.hazmat.backends import default_backend as crypto_default_backend
from cryptography.hazmat.primitives import serialization as crypto_serialization
from cryptography.hazmat.primitives.asymmetric import rsa

key = rsa.generate_private_key(
backend=crypto_default_backend(),
public_exponent=65537,
key_size=2048,
)
from cryptography.hazmat.backends import default_backend as crypto_default_backend
from cryptography.hazmat.primitives import serialization as crypto_serialization
from cryptography.hazmat.primitives.asymmetric import rsa

key = rsa.generate_private_key(
backend=crypto_default_backend(),
public_exponent=65537,
key_size=2048,
)

private_key = key.private_bytes(
crypto_serialization.Encoding.PEM,
crypto_serialization.PrivateFormat.PKCS8,
crypto_serialization.NoEncryption(),
)
private_key = key.private_bytes(
crypto_serialization.Encoding.PEM,
crypto_serialization.PrivateFormat.PKCS8,
crypto_serialization.NoEncryption(),
)

public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.OpenSSH,
crypto_serialization.PublicFormat.OpenSSH,
)
public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.OpenSSH,
crypto_serialization.PublicFormat.OpenSSH,
)

dirpath = tmp_path_factory.mktemp('keys')
filename = uuid.uuid4().hex
filepath_private_key = dirpath / filename
filepath_public_key = dirpath / f'{filename}.pub'
dirpath = tmp_path_factory.mktemp('keys')
filename = uuid.uuid4().hex
filepath_private_key = dirpath / filename
filepath_public_key = dirpath / f'{filename}.pub'

filepath_private_key.write_bytes(private_key)
filepath_public_key.write_bytes(public_key)
filepath_private_key.write_bytes(private_key)
filepath_public_key.write_bytes(public_key)

try:
yield filepath_private_key
finally:
filepath_private_key.unlink(missing_ok=True)
filepath_public_key.unlink(missing_ok=True)
try:
yield filepath_private_key
finally:
filepath_private_key.unlink(missing_ok=True)
filepath_public_key.unlink(missing_ok=True)


@pytest.fixture
Expand Down
83 changes: 37 additions & 46 deletions src/aiida/tools/pytest_fixtures/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,48 @@

@pytest.fixture(scope='session')
def ssh_key(tmp_path_factory) -> t.Generator[pathlib.Path, None, None]:
"""Returns a SSH key for the test session.
If the environment variable ``AIIDA_PYTEST_SSH_KEY`` is set we take the key
from this path otherwise we generate a temporary SSH key pair for the test
session and return the filepath of the private key.
"""Generate a temporary SSH key pair for the test session and return the filepath of the private key.
The filepath of the public key is the same as the private key, but it adds the ``.pub`` file extension.
:returns: The filepath of the generated private key.
"""
import os

if (ssh_key_path := os.environ.get('AIIDA_PYTEST_SSH_KEY')) is not None:
yield pathlib.Path(ssh_key_path)
else:
from uuid import uuid4

from cryptography.hazmat.backends import default_backend as crypto_default_backend
from cryptography.hazmat.primitives import serialization as crypto_serialization
from cryptography.hazmat.primitives.asymmetric import rsa

key = rsa.generate_private_key(
backend=crypto_default_backend(),
public_exponent=65537,
key_size=2048,
)

private_key = key.private_bytes(
crypto_serialization.Encoding.PEM,
crypto_serialization.PrivateFormat.PKCS8,
crypto_serialization.NoEncryption(),
)

public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.OpenSSH,
crypto_serialization.PublicFormat.OpenSSH,
)

dirpath = tmp_path_factory.mktemp('keys')
filename = uuid4().hex
filepath_private_key = dirpath / filename
filepath_public_key = dirpath / f'{filename}.pub'

filepath_private_key.write_bytes(private_key)
filepath_public_key.write_bytes(public_key)

try:
yield filepath_private_key
finally:
filepath_private_key.unlink(missing_ok=True)
filepath_public_key.unlink(missing_ok=True)
from uuid import uuid4

from cryptography.hazmat.backends import default_backend as crypto_default_backend
from cryptography.hazmat.primitives import serialization as crypto_serialization
from cryptography.hazmat.primitives.asymmetric import rsa

key = rsa.generate_private_key(
backend=crypto_default_backend(),
public_exponent=65537,
key_size=2048,
)

private_key = key.private_bytes(
crypto_serialization.Encoding.PEM,
crypto_serialization.PrivateFormat.PKCS8,
crypto_serialization.NoEncryption(),
)

public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.OpenSSH,
crypto_serialization.PublicFormat.OpenSSH,
)

dirpath = tmp_path_factory.mktemp('keys')
filename = uuid4().hex
filepath_private_key = dirpath / filename
filepath_public_key = dirpath / f'{filename}.pub'

filepath_private_key.write_bytes(private_key)
filepath_public_key.write_bytes(public_key)

try:
yield filepath_private_key
finally:
filepath_private_key.unlink(missing_ok=True)
filepath_public_key.unlink(missing_ok=True)


@pytest.fixture
Expand Down
10 changes: 2 additions & 8 deletions tests/transports/test_all_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,12 @@


@pytest.fixture(scope='function', params=entry_point.get_entry_point_names('aiida.transports'))
def custom_transport(request, tmp_path, monkeypatch, ssh_key) -> Transport:
def custom_transport(request, tmp_path, monkeypatch) -> Transport:
"""Fixture that parametrizes over all the registered implementations of the ``CommonRelaxWorkChain``."""
plugin = TransportFactory(request.param)

if request.param == 'core.ssh':
kwargs = {
'machine': 'localhost',
'timeout': 30,
'load_system_host_keys': True,
'key_policy': 'AutoAddPolicy',
'key_filename': str(ssh_key),
}
kwargs = {'machine': 'localhost', 'timeout': 30, 'load_system_host_keys': True, 'key_policy': 'AutoAddPolicy'}
elif request.param == 'core.ssh_auto':
kwargs = {'machine': 'localhost'}
filepath_config = tmp_path / 'config'
Expand Down

0 comments on commit 92b3cde

Please sign in to comment.