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

Explicitly set REALM when querying workgroup #2671 #2672

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
25 changes: 11 additions & 14 deletions src/rockstor/system/directory_services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Copyright (c) 2012-2021 RockStor, Inc. <http://rockstor.com>
Copyright (c) 2012-2023 RockStor, Inc. <https://rockstor.com>
This file is part of RockStor.

RockStor is free software; you can redistribute it and/or modify
Expand All @@ -13,7 +13,7 @@
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""

import os
Expand Down Expand Up @@ -295,28 +295,25 @@ def leave_domain(config, method="sssd"):
run_command(cmd, log=True)


def domain_workgroup(domain=None, method="sssd"):
def domain_workgroup(domain: str, method: str = "sssd") -> str:
"""
Fetches the Workgroup value from an Active Directory domain
to be fed to Samba configuration.
:param domain: String - Active Directory domain
:param method: String - SSSD or Winbind (default is sssd)
:return:
"""
cmd = [NET, "ads", "workgroup", "-S", domain]
if method == "winbind":
cmd = [ADCLI, "info", domain]
o, e, rc = run_command(cmd)
cmd = [NET, "ads", "workgroup", f"--realm={domain.upper()}"]
match_str = "Workgroup:"
if method == "winbind":
cmd = [ADCLI, "info", domain]
match_str = "domain-short = "
for l in o:
l = l.strip()
if re.match(match_str, l) is not None:
return l.split(match_str)[1].strip()
raise Exception(
"Failed to retrieve Workgroup. out: {} err: {} rc: {}".format(o, e, rc)
)
o, e, rc = run_command(cmd, log=True)
for line in o:
line = line.strip()
if re.match(match_str, line) is not None:
return line.split(match_str)[1].strip()
raise Exception(f"Failed to retrieve Workgroup. out: {o} err: {e} rc: {rc}")


def validate_idmap_range(config):
Expand Down
11 changes: 6 additions & 5 deletions src/rockstor/system/tests/test_directory_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
class SystemDirectoryServicesTests(unittest.TestCase):
"""
The tests in this suite can be run via the following command:
cd <root dir of rockstor ie /opt/rockstor>
./bin/test --settings=test-settings -v 3 -p test_directory_services*
cd /opt/rockstor/src/rockstor
export DJANGO_SETTINGS_MODULE=settings
poetry run django-admin test -p test_directory_services.py -v 2
"""

def setUp(self):
Expand All @@ -50,8 +51,8 @@ def test_domain_workgroup(self):
returned,
expected,
msg="Un-expected domain_workgroup() result:\n "
"returned = ({}).\n "
"expected = ({}).".format(returned, expected),
f"returned = {returned}.\n "
f"expected = {expected}.",
)

def test_domain_workgroup_invalid(self):
Expand All @@ -62,7 +63,7 @@ def test_domain_workgroup_invalid(self):
domain = "bogusad.bogusdomain.com"
self.mock_run_command.side_effect = CommandException(
err=["Didn't find the cldap server!", ""],
cmd=["/usr/bin/net", "ads", "workgroup", "-S", domain],
cmd=["/usr/bin/net", "ads", "workgroup", f"--realm={domain.upper()}"],
out=[""],
rc=255,
)
Expand Down