# sshproxy

## Description

SSHProxy Charm example for Open Source MANO.

The purpose of this charm is to operate a VNF via SSH. For this, the charm should know the hostname of the VNF (ip address), and the username. The charm will be in a blocked state until it has the hostname, username, and the credentials for SSH-ing to the VNF. Both hostname and username are set via config, with `ssh-hostname` and `ssh-username` respectively.

There are two ways of specifying the credentials: password, keys. (See next section)


## Usage

This charm works for both LXD and K8s. By default, it will work on LXD. To make it work in K8s, just change the following in the `metadata.yaml`

```yaml
series:
# - focal
# - bionic
# - xenial
 - kubernetes
 deployment:
    mode: operator
```

### Prepare the environment:

- LXD:

```bash
sudo snap install juju --classic
juju bootstrap lxd
juju add-model test
```

- K8s:

```bash
sudo snap install juju --classic
sudo snap install microk8s --classic
sudo microk8s.status --wait-ready
sudo microk8s.enable storage dns
juju bootstrap microk8s
juju add-model test
```

### Deploying charm:

```bash
charmcraft build
juju deploy ./sshproxy.charm
```

### Configuring the charm:

First of all, set the username and hostname of the VNF:

```bash
juju config sshproxy ssh-hostname=<hostname> \
                     ssh-username=<username>
```

#### Mirrors

To workaround this [bug](https://bugs.launchpad.net/juju/+bug/1929399), use the following configurations of the charm to specify the urls of apt and security mirrors.

```bash
juju config sshproxy apt-mirror=<apt-mirror> \
                     security-apt-mirror=<security-apt-mirror>
```

### Credentials

There are two ways to set up the credentials for the charm to be able to SSH the VNF.

With password:

```bash
juju config sshproxy ssh-password=<password>
```

With public keys:

1. First get the public key from the charm

```bash
$ juju run-action sshproxy/0 get-ssh-public-key --wait
unit-sshproxy-0:
  UnitId: sshproxy/0
  id: "12"
  results:
    pubkey: |
      ssh-rsa   root@juju-73fac6-2
  status: completed
  timing:
    completed: 2020-11-18 15:42:03 +0000 UTC
    enqueued: 2020-11-18 15:42:00 +0000 UTC
    started: 2020-11-18 15:42:03 +0000 UTC
```
2. Inject that key in `~/.ssh/authorized_keys` at the VNF
3. Verify the ssh credentials

```bash
$ juju run-action sshproxy/0 verify-ssh-credentials --wait
unit-sshproxy-0:
  UnitId: sshproxy/0
  id: "14"
  results:
    verified: "True"
  status: completed
  timing:
    completed: 2020-11-18 15:39:30 +0000 UTC
    enqueued: 2020-11-18 15:39:29 +0000 UTC
    started: 2020-11-18 15:39:29 +0000 UTC
```


## Developing

Create and activate a virtualenv with the development requirements:

    virtualenv -p python3 venv
    source venv/bin/activate
    pip install -r requirements-dev.txt

## Testing

The Python operator framework includes a very nice harness for testing
operator behaviour without full deployment. Just `run_tests`:

    ./run_tests
