Ejemplo n.º 1
0
    def sshKeys(self, request, user):
        """
        Manage list of authorized keys for SSH access.
        """
        cors.config_cors(request)
        request.setHeader('Content-Type', 'application/json')

        if request.method == "GET":
            try:
                if GovernorClient.isAvailable():
                    client = GovernorClient()
                    keys = client.listAuthorizedKeys(user)
                else:
                    keys = ssh_keys.getAuthorizedKeys(user)
                return json.dumps(keys)
            except Exception as e:
                out.warn(str(e))
                request.setResponseCode(404)
                return json.dumps({'message': str(e)})
        else:
            body = str2json(request.content.read())
            key = body['key'].strip()

            try:
                if GovernorClient.isAvailable():
                    client = GovernorClient()
                    client.addAuthorizedKey(key, user)
                else:
                    ssh_keys.addAuthorizedKey(key, user)
                return json.dumps(body)
            except Exception as e:
                out.warn(str(e))
                request.setResponseCode(404)
                return json.dumps({'message': str(e)})
Ejemplo n.º 2
0
    def sshKeys(self, request, user):
        """
        Manage list of authorized keys for SSH access.
        """
        cors.config_cors(request)
        request.setHeader('Content-Type', 'application/json')

        if request.method == "GET":
            try:
                if GovernorClient.isAvailable():
                    client = GovernorClient()
                    keys = client.listAuthorizedKeys(user)

                    # Move keys to the root because the API consumer is
                    # expecting a list, not an object.
                    if 'keys' in keys:
                        keys = keys['keys']
                else:
                    keys = ssh_keys.getAuthorizedKeys(user)
                return json.dumps(keys)
            except Exception as e:
                out.warn(str(e))
                request.setResponseCode(404)
                return json.dumps({'message': str(e)})
        else:
            body = json.loads(request.content.read().decode('utf-8'))
            key = body['key'].strip()

            try:
                if GovernorClient.isAvailable():
                    client = GovernorClient()
                    client.addAuthorizedKey(key, user)
                else:
                    ssh_keys.addAuthorizedKey(key, user)
                return json.dumps(body)
            except Exception as e:
                out.warn(str(e))
                request.setResponseCode(404)
                return json.dumps({'message': str(e)})