<reponame>fossabot/kraken-3
# Copyright 2020 The Kraken Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import logging
import subprocess

log = logging.getLogger(__name__)


class SshAgent:
    def __init__(self):
        self.process = subprocess.run(['ssh-agent', '-s'], stdout=subprocess.PIPE, universal_newlines=True, check=True)

        pattern = re.compile(r'SSH_AUTH_SOCK=(?P<socket>[^;]+).*SSH_AGENT_PID=(?P<pid>\d+)', re.MULTILINE | re.DOTALL)
        match = pattern.search(self.process.stdout)
        if match is None:
            raise Exception('Could not parse ssh-agent output. It was: %s' % self.process.stdout)
        data = match.groupdict()
        self.sock = data['socket']
        self.pid = data['pid']

    def shutdown(self):
        subprocess.run(['kill', self.pid], check=True)

    def add_key(self, key):
        env = os.environ.copy()
        env['SSH_AUTH_SOCK'] = self.sock
        env['SSH_AGENT_PID'] = self.pid

        key = key.strip() + '\n'  # key must be finished with new line
        key = bytes(key, 'ascii')

        process = subprocess.run('ssh-add -', shell=True, input=key, env=env)  # pylint: disable=subprocess-run-check
        if process.returncode != 0:
            raise Exception('failed to add the key')


def test():
    sa = SshAgent()
    sa.add_key('''-----BEGIN RSA PRIVATE KEY-----
<KEY>''')
    sa.shutdown()


if __name__ == '__main__':
    test()

# Copyright 2020 The Kraken Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import logging
import subprocess

log = logging.getLogger(__name__)


class SshAgent:
    def __init__(self):
        self.process = subprocess.run(['ssh-agent', '-s'], stdout=subprocess.PIPE, universal_newlines=True, check=True)

        pattern = re.compile(r'SSH_AUTH_SOCK=(?P<socket>[^;]+).*SSH_AGENT_PID=(?P<pid>\d+)', re.MULTILINE | re.DOTALL)
        match = pattern.search(self.process.stdout)
        if match is None:
            raise Exception('Could not parse ssh-agent output. It was: %s' % self.process.stdout)
        data = match.groupdict()
        self.sock = data['socket']
        self.pid = data['pid']

    def shutdown(self):
        subprocess.run(['kill', self.pid], check=True)

    def add_key(self, key):
        env = os.environ.copy()
        env['SSH_AUTH_SOCK'] = self.sock
        env['SSH_AGENT_PID'] = self.pid

        key = key.strip() + '\n'  # key must be finished with new line
        key = bytes(key, 'ascii')

        process = subprocess.run('ssh-add -', shell=True, input=key, env=env)  # pylint: disable=subprocess-run-check
        if process.returncode != 0:
            raise Exception('failed to add the key')


def test():
    sa = SshAgent()
    sa.add_key('''-----BEGIN RSA PRIVATE KEY-----
<KEY>''')
    sa.shutdown()


if __name__ == '__main__':
    test()
# Copyright 2020 The Kraken Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #     http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # key must be finished with new line # pylint: disable=subprocess-run-check -----BEGIN RSA PRIVATE KEY----- <KEY>