Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSH-key configuration utilities #176

Merged
merged 7 commits into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions tendrl/commons/utils/ssh/authorize_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from tendrl.commons.utils.ansible_module_runner import \
AnsibleExecutableGenerationFailed
from tendrl.commons.utils.ansible_module_runner import \
AnsibleRunner
from tendrl.commons.event import Event
from tendrl.commons.message import Message

ANSIBLE_MODULE_PATH = "core/system/authorized_key.py"


class AuthorizeKey(object):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Determine the output data structure for this utility. Make sure that all return values use the same data structure. Write error messages into logs at warning priority.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""AuthorizeKey class is used to copy the given ssh-key

to particular user. A default user is root.
Here ssh_key is mandatory and user is optional.
At the time of initalize it will take user and ssh-key as
parameter.

input:
ssh_key
user(optional)

output:
True/False, None/error
"""
def __init__(self, ssh_key, user="root"):
self.attributes = {}
self.attributes["user"] = user
self.attributes["key"] = ssh_key

def run(self):
"""This function is used to copy the given authorize ssh-key

output:
True/False, error
"""
try:
runner = AnsibleRunner(
ANSIBLE_MODULE_PATH,
tendrl_ns.config.data[
'tendrl_ansible_exec_file'],
**self.attributes
)
result, err = runner.run()
Event(
Message(
priority="debug",
publisher="commons",
payload={"message": "Authorize key: %s" % result}
)
)
except AnsibleExecutableGenerationFailed as e:
Event(
Message(
priority="warning",
publisher="commons",
payload={"message": "Copying authorize key failed %s. "
"Error: %s" % (
self.attributes["_raw_params"], str(e.message))}
)
)
if err is not "":
Event(
Message(
priority="warning",
publisher="commons",
payload={"message": "Unable to copy authorize key .err:%s" % err}
)
)
return False, err
else:
return True, err
75 changes: 75 additions & 0 deletions tendrl/commons/utils/ssh/generate_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from tendrl.commons.utils.ansible_module_runner import \
AnsibleExecutableGenerationFailed
from tendrl.commons.utils.ansible_module_runner import \
AnsibleRunner
from tendrl.commons.event import Event
from tendrl.commons.message import Message

ANSIBLE_MODULE_PATH = "core/system/user.py"


class GenerateKey(object):
"""GenerateKey is used to generate ssh-key

for the user. If the user is not exist then
it will create user with ssh-key.

At the time of initialize it takes user and
group as parameters.

input:
user (default is root)
group (optional)

output:
"some ssh-key", error/None
"""
def __init__(self, user="root", group=None):
self.attributes = {}
self.attributes["name"] = user
self.attributes["generate_ssh_key"] = "yes"
self.attributes["ssh_key_bits"] = 2048
if group is not None:
self.attributes["group"] = group

def run(self):
result = None
try:
runner = AnsibleRunner(
ANSIBLE_MODULE_PATH,
tendrl_ns.config.data[
'tendrl_ansible_exec_file'],
**self.attributes
)
out, err = runner.run()
Event(
Message(
priority="debug",
publisher="commons",
payload={"message": "SSH-key Generation: %s" % out}
)
)
except AnsibleExecutableGenerationFailed as e:
err = str(e.message)
Event(
Message(
priority="warning",
publisher="commons",
payload={"message": "SSH-Key Genertion failed %s. "
"Error: %s" % (
self.attributes["_raw_params"], err)}
)
)
if out is not None and "ssh_public_key" not in out:
err = out
Event(
Message(
priority="warning",
publisher="commons",
payload={"message":"Unable to generate ssh-key .err: %s" % err}
)
)
elif "ssh_public_key" in out:
result = out["ssh_public_key"]

return result, err
74 changes: 74 additions & 0 deletions tendrl/commons/utils/ssh/sshd_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import psutil
from tendrl.commons.event import Event
from tendrl.commons.message import Message
from tendrl.commons.utils import cmd_utils


def find_status():
"""This util is used to find the status of

sshd service. It will identify sshd status using
process id of sshd service.

input:
(No input required)

output:
{"name": "",
"port": "",
"status": ""}
"""

sshd = {"name": "",
"port": "",
"status": ""}
cmd = cmd_utils.Command("systemctl show sshd.service")
out, err, rc = cmd.run(tendrl_ns.config.data[
'tendrl_ansible_exec_file'])
if not err:
pid = _find_pid(out)
if pid != 0:
p = psutil.Process(pid)
result = [con for con in p.connections() if con.status ==
psutil.CONN_LISTEN and con.laddr[0] == "0.0.0.0"]
if result != []:
sshd["name"] = p.name()
sshd["port"] = int(result[0].laddr[1])
sshd["status"] = result[0].status
else:
err = "Unable to find port number"
Event(
Message(
priority="warning",
publisher="commons",
payload={"message": err}
)
)
else:
err = "sshd service is not running"
Event(
Message(
priority="warning",
publisher="commons",
payload={"message": err}
)
)

else:
Event(
Message(
priority="warning",
publisher="commons",
payload={"message": err}
)
)
return sshd, err

def _find_pid(out):
pid = 0 # 0 when sshd not run
out = out.split("\n")
for item in out:
item = item.split("=")
if "MainPID" == item[0]:
pid = int(item[1])
return pid