#!/usr/bin/env python3
"""
Generate SSH key pair

Verify that 'guest' user can fetch data using only the 'public' key
"""

import infamy
import os
import tempfile
from infamy import ssh, until, netutil

USER = "guest"
KEY = {
    "private": """-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn
NhAAAAAwEAAQAAAQEAn3zL3StVQZLsP9dnrXxK/PT4m2tjW0M+2GN6JdiIUZyKI9KKHWaf
2uHNdXNUPo0JGwgyj1geOcN2TEdkPd113mO9qrS87wgSzs3gpF93MzACSy8c55dxF5g50w
FnrRNZ1JgciyzryncNQjxYkvbbrIcHX4+ojn3sdx70MJbVeYv60M9IeyXksBENF5+rAYA8
VpQ+k7cGJeHIdMx6MURwo6nUIoOzTKJ4WhedwTCkWGlEixE98PsynyvB2Ih8a8uiX/nXLb
PT0MsaQohFt0aFOuZ8J6taRu1wHZk/w3hikkKl5HZ5l74GZK0GGfuB1tr3kJR88MBCAITE
7HEvkIjl/wAAA8hbDYq6Ww2KugAAAAdzc2gtcnNhAAABAQCffMvdK1VBkuw/12etfEr89P
iba2NbQz7YY3ol2IhRnIoj0oodZp/a4c11c1Q+jQkbCDKPWB45w3ZMR2Q93XXeY72qtLzv
CBLOzeCkX3czMAJLLxznl3EXmDnTAWetE1nUmByLLOvKdw1CPFiS9tushwdfj6iOfex3Hv
QwltV5i/rQz0h7JeSwEQ0Xn6sBgDxWlD6TtwYl4ch0zHoxRHCjqdQig7NMonhaF53BMKRY
aUSLET3w+zKfK8HYiHxry6Jf+dcts9PQyxpCiEW3RoU65nwnq1pG7XAdmT/DeGKSQqXkdn
mXvgZkrQYZ+4HW2veQlHzwwEIAhMTscS+QiOX/AAAAAwEAAQAAAQAOYsviyc9ZaF7ODWiJ
Mg5zjcdFAaVHLKQlEagJdOQq9GNTguC5cTHXJQoK35nIQKGDIjSpUGn9jN+FVuU4XVsN8d
JAbSgjqYdExzZNrVzLrbdv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0zHoxRHCjqdQig7NMonhaF53BMKRYaUSLET3w+zKfK8HYiHxry6Jf+dcts9PQyxpCiEW3RoU65nwnq1pG7XAdmT/DeGKSQqXkdnmXvgZkrQYZ+4HW2veQlHzwwEIAhMTscS+QiOX/",
}

with infamy.Test() as test:
    with test.step("Connect to the target device"):
        env = infamy.Env()
        target = env.attach("target", "mgmt")

    with test.step("Create a guest user on the target device"):
        # Upon attachment, target reverts to `test-config`, in which
        # the SSH service is enabled, hence we do not have to
        # explicitly enable it here.
        target.put_config_dict("ietf-system", {
            "system": {
                "authentication": {
                    "user": [
                        {
                            "name": USER,
                            "authorized-key": [
                                {
                                    "name":"guest-ssh-key",
                                    "algorithm": "ssh-rsa",
                                    "key-data": KEY["public"]
                                }
                            ],
                            "infix-system:shell":"bash"
                        }
                    ]
                }
            }
        })

    with test.step("Wait until SSH server is ready to accept connections"):
        until(lambda: netutil.tcp_port_is_open(target.get_mgmt_ip(), 22))

    with test.step("Write private key to a temporary file"):
        with tempfile.NamedTemporaryFile(delete=False, mode='w') as key_file:
            key_file.write(KEY["private"])
            key_file_name = key_file.name

        os.chmod(key_file_name, 0o600)

    with test.step("Verify it is possible to fetch syslog data using public key"):
        address = target.get_mgmt_ip()
        syslog_file = "./syslog_copy"

        ssh.fetch_file(
            remote_user=USER,
            remote_address=address,
            remote_file="/var/log/syslog",
            local_file=syslog_file,
            key_file=key_file_name,
            check=True,
            remove=True
        )

    test.succeed()
