-
Notifications
You must be signed in to change notification settings - Fork 23
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4da3200
SSH-key configuration utilities
GowthamShanmugam c2e7960
ssh utils are moves to utils/ssh directory
GowthamShanmugam b3db86e
SSH-key configuration utilities
GowthamShanmugam d8d8b94
SSH-key configuration utilities
GowthamShanmugam 7f7b766
Merge branch 'master' into ssh_utils
r0h4n 36f73d3
Merge branch 'master' into ssh_utils
r0h4n 1ab3a11
Update authorize_key.py
r0h4n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
"""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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brainfunked ok