Skip to main content

SSH API Framework

Project description

Korv is an API framework that uses TCP sockets over SSH to exchange JSON data with a REST-like protocol. It's built on top of the asyncssh module, so it uses asyncio to manage the sockets and its callbacks. This allows you to build rich APIs with the session security of SSH and without the TCP overhead of HTTP.

Communications over this framework requires SSH keys like logging into a normal SSH server:

  • The server itself has a private key and a set of public keys for the authorized clients.
  • The client has a private key and a set of public keys for the servers it can connect to.

Verbs

There are 4 main verbs that indicate the intent of your request:

  • GET for retrieving information.
  • STORE for creating new objects.
  • UPDATE for changing existing objects.
  • DELETE for removing objects.

Keys

As discussed previously, you establish an SSH session with the server, so it's possible to reuse existing keys or generate them through any standard mechanism like the one below:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Server

Getting a server up and running is very simple:

from korv import KorvServer


def hello(request):
    """Callback for the /hello endpoint"""

    return 200, {'msg': 'Hello World!'}

def echo(request):
    """Callback for the /echo endpoint"""

    return 200, {'msg': f'{request}'}


# Create a server
k = KorvServer(host_keys=['PATH_TO_YOUR_SERVER_PRIVATE_KEY'], authorized_client_keys='PATH_TO_YOUR_AUTHORIZED_PUBLIC_KEYS')

# Register the callbacks
k.add_callback('GET', '/hello', hello)
k.add_callback('GET', '/echo', echo)

# Start listening for requests
k.start()

This will start a new SSH server with the specified private key that listens on port 8022 by default and will accept the clients listed in the authorized keys.

Client

Following is an example on how to communicate with this server.

>>> from korv import KorvClient
>>>
>>> # Create the client
>>> k = KorvClient(client_keys=['PATH_TO_YOUR_CLIENTS_PRIVATE_KEY'])
>>>
>>> # Issue a GET request and print the output
>>> k.get('/hello', callback=lambda response: print(response['body']))
>>> {'msg': 'Hello World!'}

Return Codes

We're using standard HTTP response codes:

  • 200 = Success.
  • 400 = Malformed request or missing parameters.
  • 404 = NotFound
  • 500 = Internal error.

Server exceptions map to a 500 return code ans will include a traceback in the response.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

korv-0.2.1.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file korv-0.2.1.tar.gz.

File metadata

  • Download URL: korv-0.2.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.1 CPython/3.6.5

File hashes

Hashes for korv-0.2.1.tar.gz
Algorithm Hash digest
SHA256 9f174e27a0eb032d661aea86f76ff1a49434e57969cc3eb9a0fad5c88cf52ae7
MD5 4cc83656ced6ac35ce5d6206977f628e
BLAKE2b-256 35cc329346d1c336696d5e8d8195b01d2cb9d785106ebda5b2d9283c762be9d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page