pyadm-toolkit 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyadm-toolkit-0.0.1/LICENSE +10 -0
- pyadm-toolkit-0.0.1/PKG-INFO +81 -0
- pyadm-toolkit-0.0.1/README.md +63 -0
- pyadm-toolkit-0.0.1/VERSION +1 -0
- pyadm-toolkit-0.0.1/pyproject.toml +22 -0
- pyadm-toolkit-0.0.1/requirements.txt +3 -0
- pyadm-toolkit-0.0.1/setup.cfg +37 -0
- pyadm-toolkit-0.0.1/src/pyadm/__init__.py +0 -0
- pyadm-toolkit-0.0.1/src/pyadm/config.py +6 -0
- pyadm-toolkit-0.0.1/src/pyadm/ldapcli/__init__.py +0 -0
- pyadm-toolkit-0.0.1/src/pyadm/ldapcli/click_commands.py +220 -0
- pyadm-toolkit-0.0.1/src/pyadm/main.py +39 -0
- pyadm-toolkit-0.0.1/src/pyadm_toolkit.egg-info/PKG-INFO +81 -0
- pyadm-toolkit-0.0.1/src/pyadm_toolkit.egg-info/SOURCES.txt +18 -0
- pyadm-toolkit-0.0.1/src/pyadm_toolkit.egg-info/dependency_links.txt +1 -0
- pyadm-toolkit-0.0.1/src/pyadm_toolkit.egg-info/entry_points.txt +2 -0
- pyadm-toolkit-0.0.1/src/pyadm_toolkit.egg-info/requires.txt +2 -0
- pyadm-toolkit-0.0.1/src/pyadm_toolkit.egg-info/top_level.txt +1 -0
- pyadm-toolkit-0.0.1/src/pyadm_toolkit.egg-info/zip-safe +1 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
Copyright 2023 Ole Kleinschmidt <ok@datenreisende.org>
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
9
|
+
|
|
10
|
+
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyadm-toolkit
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: PyADM - Python Admin Toolkit. The swiss army knife for every engineer.
|
|
5
|
+
Home-page: https://github.com/okleinschmidt/pyadm
|
|
6
|
+
Author: Ole Kleinschmidt
|
|
7
|
+
Author-email: Ole Kleinschmidt <ok@datenreisende.org>
|
|
8
|
+
License: MIT License
|
|
9
|
+
Project-URL: Homepage, https://github.com/okleinschmidt/pyadm
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/okleinschmidt/pyadm/issues
|
|
11
|
+
Keywords: python admin toolkit pyadm ldap activedirectory directoryserver
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Requires-Python: >=3.10.8
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
|
|
19
|
+
# pyadm - Swiss Army Knife for Engineers and Administrators
|
|
20
|
+
|
|
21
|
+
**pyadm** is a versatile command-line tool designed as a Swiss Army Knife for engineers and administrators.\
|
|
22
|
+
It provides modular functionality to perform various tasks efficiently.
|
|
23
|
+
|
|
24
|
+
**Currently, the only available module is `ldap`, which enables LDAP-related operations.**
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
To install `pyadm`, use the following command (soon!):
|
|
29
|
+
|
|
30
|
+
```shell
|
|
31
|
+
pip install pyadm-toolkit
|
|
32
|
+
```
|
|
33
|
+
## Usage
|
|
34
|
+
The general command structure for pyadm is as follows:
|
|
35
|
+
```shell
|
|
36
|
+
pyadm MODULE SUBCOMMAND [OPTIONS]
|
|
37
|
+
```
|
|
38
|
+
To use the LDAP module, execute the pyadm ldap command followed by the desired subcommand to perform specific LDAP operations.
|
|
39
|
+
|
|
40
|
+
## LDAP Module
|
|
41
|
+
The LDAP module within pyadm allows you to interact with LDAP servers and perform common operations, such as retrieving user information, showing group associations, and displaying group members.
|
|
42
|
+
|
|
43
|
+
### Examples
|
|
44
|
+
* Retrieve information for a user in the LDAP directory:
|
|
45
|
+
```shell
|
|
46
|
+
pyadm ldap user USERNAME
|
|
47
|
+
```
|
|
48
|
+
* Show groups associated with a user in the LDAP directory:
|
|
49
|
+
```shell
|
|
50
|
+
pyadm ldap groups USERNAME
|
|
51
|
+
```
|
|
52
|
+
* Show members of a group in the LDAP directory:
|
|
53
|
+
```shell
|
|
54
|
+
pyadm ldap members GROUP_CN
|
|
55
|
+
```
|
|
56
|
+
For more information on each subcommand, you can use the --help option, as shown in the examples below:
|
|
57
|
+
```shell
|
|
58
|
+
pyadm ldap user --help
|
|
59
|
+
pyadm ldap groups --help
|
|
60
|
+
pyadm ldap members --hel
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Configuration
|
|
64
|
+
The pyadm tool allows you to customize its behavior through a configuration file. By default, the configuration file is located at `~/.config/pyadm/pyadm.conf`.
|
|
65
|
+
|
|
66
|
+
To use a custom configuration file, create a file in the following format:
|
|
67
|
+
```ini
|
|
68
|
+
[LDAP]
|
|
69
|
+
server = ldaps://dc.example.org
|
|
70
|
+
base_dn = dc=example,dc=org
|
|
71
|
+
bind_username = administrator@example.org
|
|
72
|
+
bind_password = s3cr3t-p455w0rd!
|
|
73
|
+
```
|
|
74
|
+
Specify the desired values for the LDAP server, base DN, bind username, and bind password in the configuration file.
|
|
75
|
+
|
|
76
|
+
## Contributing
|
|
77
|
+
Contributions are welcome! If you encounter any issues, have suggestions, or would like to add new features, please submit an issue or a pull request.
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).\
|
|
81
|
+
Feel free to copy and use this markdown source as needed for your `README.md` file.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# pyadm - Swiss Army Knife for Engineers and Administrators
|
|
2
|
+
|
|
3
|
+
**pyadm** is a versatile command-line tool designed as a Swiss Army Knife for engineers and administrators.\
|
|
4
|
+
It provides modular functionality to perform various tasks efficiently.
|
|
5
|
+
|
|
6
|
+
**Currently, the only available module is `ldap`, which enables LDAP-related operations.**
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
To install `pyadm`, use the following command (soon!):
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
pip install pyadm-toolkit
|
|
14
|
+
```
|
|
15
|
+
## Usage
|
|
16
|
+
The general command structure for pyadm is as follows:
|
|
17
|
+
```shell
|
|
18
|
+
pyadm MODULE SUBCOMMAND [OPTIONS]
|
|
19
|
+
```
|
|
20
|
+
To use the LDAP module, execute the pyadm ldap command followed by the desired subcommand to perform specific LDAP operations.
|
|
21
|
+
|
|
22
|
+
## LDAP Module
|
|
23
|
+
The LDAP module within pyadm allows you to interact with LDAP servers and perform common operations, such as retrieving user information, showing group associations, and displaying group members.
|
|
24
|
+
|
|
25
|
+
### Examples
|
|
26
|
+
* Retrieve information for a user in the LDAP directory:
|
|
27
|
+
```shell
|
|
28
|
+
pyadm ldap user USERNAME
|
|
29
|
+
```
|
|
30
|
+
* Show groups associated with a user in the LDAP directory:
|
|
31
|
+
```shell
|
|
32
|
+
pyadm ldap groups USERNAME
|
|
33
|
+
```
|
|
34
|
+
* Show members of a group in the LDAP directory:
|
|
35
|
+
```shell
|
|
36
|
+
pyadm ldap members GROUP_CN
|
|
37
|
+
```
|
|
38
|
+
For more information on each subcommand, you can use the --help option, as shown in the examples below:
|
|
39
|
+
```shell
|
|
40
|
+
pyadm ldap user --help
|
|
41
|
+
pyadm ldap groups --help
|
|
42
|
+
pyadm ldap members --hel
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Configuration
|
|
46
|
+
The pyadm tool allows you to customize its behavior through a configuration file. By default, the configuration file is located at `~/.config/pyadm/pyadm.conf`.
|
|
47
|
+
|
|
48
|
+
To use a custom configuration file, create a file in the following format:
|
|
49
|
+
```ini
|
|
50
|
+
[LDAP]
|
|
51
|
+
server = ldaps://dc.example.org
|
|
52
|
+
base_dn = dc=example,dc=org
|
|
53
|
+
bind_username = administrator@example.org
|
|
54
|
+
bind_password = s3cr3t-p455w0rd!
|
|
55
|
+
```
|
|
56
|
+
Specify the desired values for the LDAP server, base DN, bind username, and bind password in the configuration file.
|
|
57
|
+
|
|
58
|
+
## Contributing
|
|
59
|
+
Contributions are welcome! If you encounter any issues, have suggestions, or would like to add new features, please submit an issue or a pull request.
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).\
|
|
63
|
+
Feel free to copy and use this markdown source as needed for your `README.md` file.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.0.1
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools", "black"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pyadm-toolkit"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Ole Kleinschmidt", email="ok@datenreisende.org" },
|
|
10
|
+
]
|
|
11
|
+
description = "PyADM - Python Admin Toolkit. The swiss army knife for every engineer."
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.10.8"
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
"Homepage" = "https://github.com/okleinschmidt/pyadm"
|
|
22
|
+
"Bug Tracker" = "https://github.com/okleinschmidt/pyadm/issues"
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[metadata]
|
|
2
|
+
name = pyadm-toolkit
|
|
3
|
+
version = file: VERSION
|
|
4
|
+
author = Ole Kleinschmidt
|
|
5
|
+
author_email = ok@datenreisende.org
|
|
6
|
+
url = https://github.com/okleinschmidt/pyadm
|
|
7
|
+
description = PyADM - Python Admin Toolkit. The swiss army knife for every engineer.
|
|
8
|
+
long_description = file: README.md
|
|
9
|
+
long_description_content_type = text/markdown
|
|
10
|
+
keywords = python admin toolkit pyadm ldap activedirectory directoryserver
|
|
11
|
+
license = MIT License
|
|
12
|
+
classifiers =
|
|
13
|
+
License :: OSI Approved :: MIT License
|
|
14
|
+
Programming Language :: Python :: 3
|
|
15
|
+
Operating System :: OS Independent
|
|
16
|
+
|
|
17
|
+
[options]
|
|
18
|
+
packages = find:
|
|
19
|
+
zip_safe = True
|
|
20
|
+
include_package_data = True
|
|
21
|
+
install_requires = file: requirements.txt
|
|
22
|
+
python_requires = >=3.10.8
|
|
23
|
+
|
|
24
|
+
[options.package_data]
|
|
25
|
+
* = README.md, LICENSE, VERSION
|
|
26
|
+
|
|
27
|
+
[options.packages.find]
|
|
28
|
+
where = src
|
|
29
|
+
|
|
30
|
+
[options.entry_points]
|
|
31
|
+
console_scripts =
|
|
32
|
+
pyadm = pyadm.main:cli
|
|
33
|
+
|
|
34
|
+
[egg_info]
|
|
35
|
+
tag_build =
|
|
36
|
+
tag_date = 0
|
|
37
|
+
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import click
|
|
2
|
+
import json
|
|
3
|
+
import ldap3
|
|
4
|
+
|
|
5
|
+
from ldap3.core.exceptions import LDAPException
|
|
6
|
+
from pyadm.config import config
|
|
7
|
+
|
|
8
|
+
defaults = {
|
|
9
|
+
"server": "ldap://localhost",
|
|
10
|
+
"base_dn": "dc=example,dc=org",
|
|
11
|
+
"username": "root@example.org",
|
|
12
|
+
"password": "s3cur3_p455w0rd",
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
click_options = {}
|
|
16
|
+
|
|
17
|
+
def ldap_search(click_options, search_filter, attributes=[]):
|
|
18
|
+
try:
|
|
19
|
+
server_url = click_options["server"]
|
|
20
|
+
bind_dn = click_options["username"]
|
|
21
|
+
bind_password = click_options["password"]
|
|
22
|
+
|
|
23
|
+
server = ldap3.Server(server_url)
|
|
24
|
+
conn = ldap3.Connection(server, user=bind_dn, password=bind_password)
|
|
25
|
+
conn.open()
|
|
26
|
+
conn.bind()
|
|
27
|
+
|
|
28
|
+
base_dn = click_options["base_dn"]
|
|
29
|
+
|
|
30
|
+
conn.search(base_dn, search_filter, attributes=attributes)
|
|
31
|
+
|
|
32
|
+
result_entries = conn.entries
|
|
33
|
+
return result_entries
|
|
34
|
+
except LDAPException as e:
|
|
35
|
+
raise click.ClickException(f"LDAP search failed: {e}")
|
|
36
|
+
|
|
37
|
+
# define click commands
|
|
38
|
+
@click.group("ldap")
|
|
39
|
+
@click.option("--server", "-s", help="Specify the LDAP server.")
|
|
40
|
+
@click.option("--base_dn", "-b", help="Specify the base DN (Distinguished Name).")
|
|
41
|
+
@click.option("--username", "-u", help="Specify the username for authentication.")
|
|
42
|
+
@click.option("--password", "-p", help="Specify the password for authentication. It will prompt if the password is empty.", prompt=False, hide_input=True)
|
|
43
|
+
def ldapcli(server, base_dn, username, password):
|
|
44
|
+
"""
|
|
45
|
+
Query LDAP/Active Directory.
|
|
46
|
+
|
|
47
|
+
This command provides various subcommands to interact with
|
|
48
|
+
an LDAP or Active Directory server. It allows you to perform queries,
|
|
49
|
+
retrieve information, and manage users and groups within the directory.
|
|
50
|
+
|
|
51
|
+
\b
|
|
52
|
+
To see the available subcommands, run 'pyadm ldap --help'.\r
|
|
53
|
+
Get help for subcommands, run 'pyadm ldap COMMAND --help'.
|
|
54
|
+
|
|
55
|
+
\b
|
|
56
|
+
Configuration File:\r
|
|
57
|
+
The configuration file allows you to customize various settings for the
|
|
58
|
+
command, such as the LDAP server, base DN, username, password, and other options.
|
|
59
|
+
|
|
60
|
+
The default location for the configuration file is `/home/user/.config/pyadm/pyadm.conf`.
|
|
61
|
+
|
|
62
|
+
Example configuration file contents:
|
|
63
|
+
|
|
64
|
+
\b
|
|
65
|
+
[LDAP]
|
|
66
|
+
server = ldaps://dc.example.org
|
|
67
|
+
base_dn = dc=example,dc=org
|
|
68
|
+
bind_username = administrator@example.org
|
|
69
|
+
bind_password = s3cr3t-p455w0rd!
|
|
70
|
+
|
|
71
|
+
"""
|
|
72
|
+
if not (server or base_dn or username or password):
|
|
73
|
+
click_options["server"] = config["LDAP"]["server"] or defaults["server"]
|
|
74
|
+
click_options["base_dn"] = config["LDAP"]["base_dn"] or defaults["base_dn"]
|
|
75
|
+
click_options["username"] = config["LDAP"]["bind_username"] or defaults["username"]
|
|
76
|
+
click_options["password"] = config["LDAP"]["bind_password"] or defaults["password"]
|
|
77
|
+
else:
|
|
78
|
+
click_options["server"] = server or defaults["server"]
|
|
79
|
+
click_options["base_dn"] = base_dn or defaults["base_dn"]
|
|
80
|
+
click_options["username"] = username or defaults["username"]
|
|
81
|
+
click_options["password"] = password
|
|
82
|
+
|
|
83
|
+
# show information about a user
|
|
84
|
+
@ldapcli.command("user")
|
|
85
|
+
@click.argument("username", metavar="[UID, CN, MAIL]")
|
|
86
|
+
@click.option("--all", "-a", is_flag=True, default=None, help="Show all attributes")
|
|
87
|
+
@click.option("--json", "-j", "json_output", is_flag=True, default=None, help="Output as JSON")
|
|
88
|
+
def user(username, json_output, all):
|
|
89
|
+
"""Show information about a user specified by [UID], [CN], or [MAIL].
|
|
90
|
+
|
|
91
|
+
This command allows you to retrieve detailed information about a user
|
|
92
|
+
in the LDAP directory. You can specify the username using their UID,
|
|
93
|
+
CN (Common Name), or MAIL (Email Address).
|
|
94
|
+
|
|
95
|
+
\b
|
|
96
|
+
Examples:
|
|
97
|
+
$ pyadm ldap user jdoe # Retrieve information for user with UID 'jdoe'
|
|
98
|
+
$ pyadm ldap user "John Doe" # Retrieve information for user with CN 'John Doe'
|
|
99
|
+
$ pyadm ldap user jdoe@example.com # Retrieve information for user with MAIL 'jdoe@example.com'
|
|
100
|
+
"""
|
|
101
|
+
search_filter = f"(|(uid={username})(cn={username})(mail={username}))"
|
|
102
|
+
try:
|
|
103
|
+
if all:
|
|
104
|
+
attributes = ["*"]
|
|
105
|
+
result = ldap_search(click_options, search_filter, attributes)
|
|
106
|
+
else:
|
|
107
|
+
attributes = ["cn", "mail", "memberOf"]
|
|
108
|
+
result = ldap_search(click_options, search_filter, attributes)
|
|
109
|
+
|
|
110
|
+
if result:
|
|
111
|
+
if json_output:
|
|
112
|
+
user_info = result[0].entry_to_json()
|
|
113
|
+
print(user_info)
|
|
114
|
+
else:
|
|
115
|
+
user_info = result[0].entry_attributes_as_dict
|
|
116
|
+
user_info = {str(attr): [str(value) for value in values] for attr, values in user_info.items()}
|
|
117
|
+
for attr, values in sorted(user_info.items()):
|
|
118
|
+
if attr == "memberOf" or attr == "objectClass":
|
|
119
|
+
print(f"{attr}:")
|
|
120
|
+
for group in values:
|
|
121
|
+
print(f" - {group}")
|
|
122
|
+
else:
|
|
123
|
+
print(f"{attr}: {', '.join(values)}")
|
|
124
|
+
else:
|
|
125
|
+
raise click.ClickException(f"No user found with UID '{username}'.")
|
|
126
|
+
except click.ClickException as e:
|
|
127
|
+
raise e
|
|
128
|
+
except Exception as e:
|
|
129
|
+
raise click.ClickException(f"An error occurred: {e}")
|
|
130
|
+
|
|
131
|
+
# show groups a user belongs to
|
|
132
|
+
@ldapcli.command("groups")
|
|
133
|
+
@click.argument("username", metavar="[UID, CN, MAIL]")
|
|
134
|
+
@click.option("--json", "-j", "json_output", is_flag=True, default=None, help="Output as JSON")
|
|
135
|
+
def groups(username, json_output):
|
|
136
|
+
"""
|
|
137
|
+
Show groups associated with a user specified by [UID], [CN], or [MAIL].
|
|
138
|
+
|
|
139
|
+
This command allows you to retrieve the groups associated with a user
|
|
140
|
+
in the LDAP directory. You can specify the username using their UID,
|
|
141
|
+
CN (Common Name), or MAIL (Email Address).
|
|
142
|
+
|
|
143
|
+
\b
|
|
144
|
+
Examples:
|
|
145
|
+
$ pyadm ldap groups jdoe # Retrieve groups for user with UID 'jdoe'
|
|
146
|
+
$ pyadm ldap groups "John Doe" # Retrieve groups for user with CN 'John Doe'
|
|
147
|
+
$ pyadm ldap groups jdoe@example.com # Retrieve groups for user with MAIL 'jdoe@example.com'
|
|
148
|
+
"""
|
|
149
|
+
search_filter = f"(|(uid={username})(cn={username}))"
|
|
150
|
+
try:
|
|
151
|
+
attributes = ["memberOf"]
|
|
152
|
+
result = ldap_search(click_options, search_filter, attributes)
|
|
153
|
+
|
|
154
|
+
if result:
|
|
155
|
+
if json_output:
|
|
156
|
+
group_info = result[0].entry_to_json()
|
|
157
|
+
print(group_info)
|
|
158
|
+
else:
|
|
159
|
+
group_info = result[0].entry_attributes_as_dict
|
|
160
|
+
group_info = {str(attr): [str(value) for value in values] for attr, values in group_info.items()}
|
|
161
|
+
for attr, values in sorted(group_info.items()):
|
|
162
|
+
if attr == "memberOf" or attr == "objectClass":
|
|
163
|
+
print(f"{attr}:")
|
|
164
|
+
for group in values:
|
|
165
|
+
print(f" - {group}")
|
|
166
|
+
else:
|
|
167
|
+
print(f"{attr}: {', '.join(values)}")
|
|
168
|
+
else:
|
|
169
|
+
raise click.ClickException(f"No user found with UID '{uid}'.")
|
|
170
|
+
except click.ClickException as e:
|
|
171
|
+
raise e
|
|
172
|
+
except Exception as e:
|
|
173
|
+
raise click.ClickException(f"An error occurred: {e}")
|
|
174
|
+
|
|
175
|
+
# show members of a group
|
|
176
|
+
@ldapcli.command("members")
|
|
177
|
+
@click.argument("group_cn", metavar="[GROUP]")
|
|
178
|
+
@click.option("--all", "-a", is_flag=True, default=None, help="Show all attributes")
|
|
179
|
+
@click.option("--json", "-j", "json_output", is_flag=True, default=None, help="Output as JSON")
|
|
180
|
+
def members(group_cn, json_output, all):
|
|
181
|
+
"""
|
|
182
|
+
Show members of a group specified by [GROUP].
|
|
183
|
+
|
|
184
|
+
This command allows you to retrieve the members of a group in the LDAP
|
|
185
|
+
directory. You can specify the group using its CN (Common Name).
|
|
186
|
+
|
|
187
|
+
\b
|
|
188
|
+
Examples:
|
|
189
|
+
$ pyadm ldap members "Developers" # Retrieve members of the group with CN 'Developers'
|
|
190
|
+
$ pyadm ldap members "Admins" # Retrieve members of the group with CN 'Admins'
|
|
191
|
+
"""
|
|
192
|
+
search_filter = f"(cn={group_cn})"
|
|
193
|
+
try:
|
|
194
|
+
if all:
|
|
195
|
+
attributes = ["*"]
|
|
196
|
+
result = ldap_search(click_options, search_filter, attributes)
|
|
197
|
+
else:
|
|
198
|
+
attributes = ["cn", "description", "member"]
|
|
199
|
+
result = ldap_search(click_options, search_filter, attributes)
|
|
200
|
+
|
|
201
|
+
if result:
|
|
202
|
+
if json_output:
|
|
203
|
+
group_info = result[0].entry_to_json()
|
|
204
|
+
print(group_info)
|
|
205
|
+
else:
|
|
206
|
+
group_info = result[0].entry_attributes_as_dict
|
|
207
|
+
group_info = {str(attr): [str(value) for value in values] for attr, values in group_info.items()}
|
|
208
|
+
for attr, values in sorted(group_info.items()):
|
|
209
|
+
if attr == "member" or attr == "objectClass":
|
|
210
|
+
print(f"{attr}:")
|
|
211
|
+
for group in values:
|
|
212
|
+
print(f" - {group}")
|
|
213
|
+
else:
|
|
214
|
+
print(f"{attr}: {', '.join(values)}")
|
|
215
|
+
else:
|
|
216
|
+
raise click.ClickException(f"No user found with UID '{group_cn}'.")
|
|
217
|
+
except click.ClickException as e:
|
|
218
|
+
raise e
|
|
219
|
+
except Exception as e:
|
|
220
|
+
raise click.ClickException(f"An error occurred: {e}")
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import click
|
|
2
|
+
|
|
3
|
+
from pyadm.ldapcli.click_commands import ldapcli
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@click.group()
|
|
7
|
+
def cli():
|
|
8
|
+
"""
|
|
9
|
+
pyadm - Swiss Army Knife for Engineers and Administrators
|
|
10
|
+
|
|
11
|
+
It provides modular functionality to perform various tasks efficiently.
|
|
12
|
+
Currently, the only available module is 'ldap', which enables LDAP-related operations.
|
|
13
|
+
|
|
14
|
+
Usage: pyadm ldap SUBCOMMAND [OPTIONS]
|
|
15
|
+
|
|
16
|
+
To use the LDAP module, execute the 'pyadm ldap' command followed by the desired subcommand to perform specific LDAP operations.
|
|
17
|
+
|
|
18
|
+
For more information on a particular subcommand, run 'pyadm ldap SUBCOMMAND --help'.
|
|
19
|
+
|
|
20
|
+
\b
|
|
21
|
+
Example:
|
|
22
|
+
$ pyadm ldap user jdoe # Retrieve information for a user in the LDAP directory
|
|
23
|
+
$ pyadm ldap groups jdoe # Show groups associated with a user in the LDAP directory
|
|
24
|
+
$ pyadm ldap members "Developers" # Show members of a group in the LDAP directory
|
|
25
|
+
"""
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
@click.command()
|
|
29
|
+
def version():
|
|
30
|
+
"""Display version information"""
|
|
31
|
+
with open("VERSION", "r") as version_file:
|
|
32
|
+
version = version_file.read().strip()
|
|
33
|
+
click.echo(f"Version: {version}")
|
|
34
|
+
|
|
35
|
+
cli.add_command(ldapcli)
|
|
36
|
+
cli.add_command(version)
|
|
37
|
+
|
|
38
|
+
if __name__ == "__main__":
|
|
39
|
+
cli()
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyadm-toolkit
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: PyADM - Python Admin Toolkit. The swiss army knife for every engineer.
|
|
5
|
+
Home-page: https://github.com/okleinschmidt/pyadm
|
|
6
|
+
Author: Ole Kleinschmidt
|
|
7
|
+
Author-email: Ole Kleinschmidt <ok@datenreisende.org>
|
|
8
|
+
License: MIT License
|
|
9
|
+
Project-URL: Homepage, https://github.com/okleinschmidt/pyadm
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/okleinschmidt/pyadm/issues
|
|
11
|
+
Keywords: python admin toolkit pyadm ldap activedirectory directoryserver
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Requires-Python: >=3.10.8
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
|
|
19
|
+
# pyadm - Swiss Army Knife for Engineers and Administrators
|
|
20
|
+
|
|
21
|
+
**pyadm** is a versatile command-line tool designed as a Swiss Army Knife for engineers and administrators.\
|
|
22
|
+
It provides modular functionality to perform various tasks efficiently.
|
|
23
|
+
|
|
24
|
+
**Currently, the only available module is `ldap`, which enables LDAP-related operations.**
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
To install `pyadm`, use the following command (soon!):
|
|
29
|
+
|
|
30
|
+
```shell
|
|
31
|
+
pip install pyadm-toolkit
|
|
32
|
+
```
|
|
33
|
+
## Usage
|
|
34
|
+
The general command structure for pyadm is as follows:
|
|
35
|
+
```shell
|
|
36
|
+
pyadm MODULE SUBCOMMAND [OPTIONS]
|
|
37
|
+
```
|
|
38
|
+
To use the LDAP module, execute the pyadm ldap command followed by the desired subcommand to perform specific LDAP operations.
|
|
39
|
+
|
|
40
|
+
## LDAP Module
|
|
41
|
+
The LDAP module within pyadm allows you to interact with LDAP servers and perform common operations, such as retrieving user information, showing group associations, and displaying group members.
|
|
42
|
+
|
|
43
|
+
### Examples
|
|
44
|
+
* Retrieve information for a user in the LDAP directory:
|
|
45
|
+
```shell
|
|
46
|
+
pyadm ldap user USERNAME
|
|
47
|
+
```
|
|
48
|
+
* Show groups associated with a user in the LDAP directory:
|
|
49
|
+
```shell
|
|
50
|
+
pyadm ldap groups USERNAME
|
|
51
|
+
```
|
|
52
|
+
* Show members of a group in the LDAP directory:
|
|
53
|
+
```shell
|
|
54
|
+
pyadm ldap members GROUP_CN
|
|
55
|
+
```
|
|
56
|
+
For more information on each subcommand, you can use the --help option, as shown in the examples below:
|
|
57
|
+
```shell
|
|
58
|
+
pyadm ldap user --help
|
|
59
|
+
pyadm ldap groups --help
|
|
60
|
+
pyadm ldap members --hel
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Configuration
|
|
64
|
+
The pyadm tool allows you to customize its behavior through a configuration file. By default, the configuration file is located at `~/.config/pyadm/pyadm.conf`.
|
|
65
|
+
|
|
66
|
+
To use a custom configuration file, create a file in the following format:
|
|
67
|
+
```ini
|
|
68
|
+
[LDAP]
|
|
69
|
+
server = ldaps://dc.example.org
|
|
70
|
+
base_dn = dc=example,dc=org
|
|
71
|
+
bind_username = administrator@example.org
|
|
72
|
+
bind_password = s3cr3t-p455w0rd!
|
|
73
|
+
```
|
|
74
|
+
Specify the desired values for the LDAP server, base DN, bind username, and bind password in the configuration file.
|
|
75
|
+
|
|
76
|
+
## Contributing
|
|
77
|
+
Contributions are welcome! If you encounter any issues, have suggestions, or would like to add new features, please submit an issue or a pull request.
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).\
|
|
81
|
+
Feel free to copy and use this markdown source as needed for your `README.md` file.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
VERSION
|
|
4
|
+
pyproject.toml
|
|
5
|
+
requirements.txt
|
|
6
|
+
setup.cfg
|
|
7
|
+
src/pyadm/__init__.py
|
|
8
|
+
src/pyadm/config.py
|
|
9
|
+
src/pyadm/main.py
|
|
10
|
+
src/pyadm/ldapcli/__init__.py
|
|
11
|
+
src/pyadm/ldapcli/click_commands.py
|
|
12
|
+
src/pyadm_toolkit.egg-info/PKG-INFO
|
|
13
|
+
src/pyadm_toolkit.egg-info/SOURCES.txt
|
|
14
|
+
src/pyadm_toolkit.egg-info/dependency_links.txt
|
|
15
|
+
src/pyadm_toolkit.egg-info/entry_points.txt
|
|
16
|
+
src/pyadm_toolkit.egg-info/requires.txt
|
|
17
|
+
src/pyadm_toolkit.egg-info/top_level.txt
|
|
18
|
+
src/pyadm_toolkit.egg-info/zip-safe
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyadm
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|