mreg-cli 1.0.0__py3-none-any.whl
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.
- mreg_cli/__about__.py +7 -0
- mreg_cli/__init__.py +5 -0
- mreg_cli/__main__.py +8 -0
- mreg_cli/_version.py +16 -0
- mreg_cli/api/__init__.py +7 -0
- mreg_cli/api/abstracts.py +486 -0
- mreg_cli/api/endpoints.py +130 -0
- mreg_cli/api/fields.py +104 -0
- mreg_cli/api/history.py +152 -0
- mreg_cli/api/models.py +3416 -0
- mreg_cli/cli.py +382 -0
- mreg_cli/commands/__init__.py +1 -0
- mreg_cli/commands/base.py +62 -0
- mreg_cli/commands/dhcp.py +136 -0
- mreg_cli/commands/group.py +326 -0
- mreg_cli/commands/help.py +77 -0
- mreg_cli/commands/host.py +54 -0
- mreg_cli/commands/host_submodules/__init__.py +28 -0
- mreg_cli/commands/host_submodules/a_aaaa.py +452 -0
- mreg_cli/commands/host_submodules/bacnet.py +126 -0
- mreg_cli/commands/host_submodules/cname.py +166 -0
- mreg_cli/commands/host_submodules/core.py +507 -0
- mreg_cli/commands/host_submodules/rr.py +973 -0
- mreg_cli/commands/label.py +146 -0
- mreg_cli/commands/logging.py +112 -0
- mreg_cli/commands/network.py +516 -0
- mreg_cli/commands/permission.py +202 -0
- mreg_cli/commands/policy.py +519 -0
- mreg_cli/commands/recording.py +59 -0
- mreg_cli/commands/registry.py +56 -0
- mreg_cli/commands/root.py +58 -0
- mreg_cli/commands/zone.py +288 -0
- mreg_cli/config.py +253 -0
- mreg_cli/errorbuilder.py +193 -0
- mreg_cli/exceptions.py +237 -0
- mreg_cli/help_formatter.py +38 -0
- mreg_cli/main.py +238 -0
- mreg_cli/outputmanager.py +466 -0
- mreg_cli/py.typed +0 -0
- mreg_cli/tags.txt +55 -0
- mreg_cli/tokenfile.py +89 -0
- mreg_cli/types.py +160 -0
- mreg_cli/utilities/__init__.py +5 -0
- mreg_cli/utilities/api.py +595 -0
- mreg_cli/utilities/shared.py +65 -0
- mreg_cli/utilities/validators.py +19 -0
- mreg_cli-1.0.0.dist-info/AUTHORS +12 -0
- mreg_cli-1.0.0.dist-info/LICENSE +674 -0
- mreg_cli-1.0.0.dist-info/METADATA +1079 -0
- mreg_cli-1.0.0.dist-info/RECORD +53 -0
- mreg_cli-1.0.0.dist-info/WHEEL +5 -0
- mreg_cli-1.0.0.dist-info/entry_points.txt +2 -0
- mreg_cli-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"""Permission commands for mreg_cli."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from mreg_cli.api.models import Label, NetworkOrIP, Permission
|
|
9
|
+
from mreg_cli.commands.base import BaseCommand
|
|
10
|
+
from mreg_cli.commands.registry import CommandRegistry
|
|
11
|
+
from mreg_cli.exceptions import DeleteError, EntityNotFound
|
|
12
|
+
from mreg_cli.outputmanager import OutputManager
|
|
13
|
+
from mreg_cli.types import Flag, QueryParams
|
|
14
|
+
from mreg_cli.utilities.shared import convert_wildcard_to_regex
|
|
15
|
+
|
|
16
|
+
command_registry = CommandRegistry()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class PermissionCommands(BaseCommand):
|
|
20
|
+
"""Permission commands for the CLI."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, cli: Any) -> None:
|
|
23
|
+
"""Initialize the permission commands."""
|
|
24
|
+
super().__init__(
|
|
25
|
+
cli, command_registry, "permission", "Manage permission.", "Manage permission"
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@command_registry.register_command(
|
|
30
|
+
prog="network_list",
|
|
31
|
+
description="List permissions for networks",
|
|
32
|
+
short_desc="List permissions for networks",
|
|
33
|
+
flags=[
|
|
34
|
+
Flag(
|
|
35
|
+
"-group",
|
|
36
|
+
description="Group with access (supports wildcards)",
|
|
37
|
+
metavar="GROUP",
|
|
38
|
+
),
|
|
39
|
+
Flag("-range", description="Network range", metavar="RANGE"),
|
|
40
|
+
],
|
|
41
|
+
)
|
|
42
|
+
def network_list(args: argparse.Namespace) -> None:
|
|
43
|
+
"""List permissions for networks.
|
|
44
|
+
|
|
45
|
+
:param args: argparse.Namespace (group, range)
|
|
46
|
+
"""
|
|
47
|
+
permission_list: list[Permission] = []
|
|
48
|
+
|
|
49
|
+
params: QueryParams = {}
|
|
50
|
+
if args.group is not None:
|
|
51
|
+
param, value = convert_wildcard_to_regex("group", args.group)
|
|
52
|
+
params[param] = value
|
|
53
|
+
|
|
54
|
+
# Well, this is effin' awful. We have to fetch all permissions, but the API wants to limit
|
|
55
|
+
# the number of results. We should probably fix this in the API.
|
|
56
|
+
permissions = Permission.get_by_query(query=params, ordering="range,group", limit=None)
|
|
57
|
+
|
|
58
|
+
if args.range is not None:
|
|
59
|
+
argnetwork = NetworkOrIP(ip_or_network=args.range).as_network()
|
|
60
|
+
|
|
61
|
+
for permission in permissions:
|
|
62
|
+
permnet = permission.range
|
|
63
|
+
if permnet.version != argnetwork.version:
|
|
64
|
+
continue # no warning if the networks are not comparable
|
|
65
|
+
if argnetwork.supernet_of(permnet): # type: ignore # guaranteed to be the same version
|
|
66
|
+
permission_list.append(permission)
|
|
67
|
+
else:
|
|
68
|
+
permission_list = permissions
|
|
69
|
+
|
|
70
|
+
if not permission_list:
|
|
71
|
+
raise EntityNotFound("No permissions found")
|
|
72
|
+
|
|
73
|
+
output: list[dict[str, str]] = []
|
|
74
|
+
labelnames: dict[int, str] = {}
|
|
75
|
+
|
|
76
|
+
for label in Label.get_all():
|
|
77
|
+
labelnames[label.id] = label.name
|
|
78
|
+
|
|
79
|
+
for permission in permission_list:
|
|
80
|
+
perm_data: dict[str, str] = {}
|
|
81
|
+
row_labels: list[str] = [labelnames[label] for label in permission.labels]
|
|
82
|
+
perm_data["labels"] = ", ".join(row_labels)
|
|
83
|
+
perm_data["range"] = str(permission.range)
|
|
84
|
+
perm_data["group"] = permission.group
|
|
85
|
+
perm_data["regex"] = permission.regex
|
|
86
|
+
output.append(perm_data)
|
|
87
|
+
|
|
88
|
+
OutputManager().add_formatted_table(
|
|
89
|
+
("Range", "Group", "Regex", "Labels"),
|
|
90
|
+
("range", "group", "regex", "labels"),
|
|
91
|
+
output,
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@command_registry.register_command(
|
|
96
|
+
prog="network_add",
|
|
97
|
+
description="Add permission for network",
|
|
98
|
+
short_desc="Add permission for network",
|
|
99
|
+
flags=[
|
|
100
|
+
Flag("range", description="Network range", metavar="RANGE"),
|
|
101
|
+
Flag("group", description="Group with access", metavar="GROUP"),
|
|
102
|
+
Flag("regex", description="Regular expression", metavar="REGEX"),
|
|
103
|
+
],
|
|
104
|
+
)
|
|
105
|
+
def network_add(args: argparse.Namespace) -> None:
|
|
106
|
+
"""Add permission for network.
|
|
107
|
+
|
|
108
|
+
:param args: argparse.Namespace (range, group, regex)
|
|
109
|
+
"""
|
|
110
|
+
NetworkOrIP(ip_or_network=args.range).as_network()
|
|
111
|
+
|
|
112
|
+
query = {
|
|
113
|
+
"range": args.range,
|
|
114
|
+
"group": args.group,
|
|
115
|
+
"regex": args.regex,
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
Permission.get_by_query_unique_and_raise(query)
|
|
119
|
+
Permission.create(params=query)
|
|
120
|
+
OutputManager().add_ok(f"Added permission to {args.range}")
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@command_registry.register_command(
|
|
124
|
+
prog="network_remove",
|
|
125
|
+
description="Remove permission for network",
|
|
126
|
+
short_desc="Remove permission for network",
|
|
127
|
+
flags=[
|
|
128
|
+
Flag("range", description="Network range", metavar="RANGE"),
|
|
129
|
+
Flag("group", description="Group with access", metavar="GROUP"),
|
|
130
|
+
Flag("regex", description="Regular expression", metavar="REGEX"),
|
|
131
|
+
],
|
|
132
|
+
)
|
|
133
|
+
def network_remove(args: argparse.Namespace) -> None:
|
|
134
|
+
"""Remove permission for networks.
|
|
135
|
+
|
|
136
|
+
:param args: argparse.Namespace (range, group, regex)
|
|
137
|
+
"""
|
|
138
|
+
query = {
|
|
139
|
+
"group": args.group,
|
|
140
|
+
"range": args.range,
|
|
141
|
+
"regex": args.regex,
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
permission = Permission.get_by_query_unique_or_raise(query)
|
|
145
|
+
if permission.delete():
|
|
146
|
+
OutputManager().add_ok(f"Removed permission for {args.range}")
|
|
147
|
+
else:
|
|
148
|
+
raise DeleteError(f"Failed to remove permission for {args.range}")
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
@command_registry.register_command(
|
|
152
|
+
prog="label_add",
|
|
153
|
+
description="Add a label to a permission",
|
|
154
|
+
short_desc="Add label",
|
|
155
|
+
flags=[
|
|
156
|
+
Flag("range", description="Network range", metavar="RANGE"),
|
|
157
|
+
Flag("group", description="Group with access", metavar="GROUP"),
|
|
158
|
+
Flag("regex", description="Regular expression", metavar="REGEX"),
|
|
159
|
+
Flag("label", description="The label you want to add", metavar="LABEL"),
|
|
160
|
+
],
|
|
161
|
+
)
|
|
162
|
+
def add_label_to_permission(args: argparse.Namespace) -> None:
|
|
163
|
+
"""Add a label to a permission triplet.
|
|
164
|
+
|
|
165
|
+
:param args: argparse.Namespace (range, group, regex, label)
|
|
166
|
+
"""
|
|
167
|
+
# find the permission
|
|
168
|
+
query = {
|
|
169
|
+
"group": args.group,
|
|
170
|
+
"range": args.range,
|
|
171
|
+
"regex": args.regex,
|
|
172
|
+
}
|
|
173
|
+
permission = Permission.get_by_query_unique_or_raise(query)
|
|
174
|
+
permission.add_label(args.label)
|
|
175
|
+
OutputManager().add_ok(f"Added the label {args.label!r} to the permission.")
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
@command_registry.register_command(
|
|
179
|
+
prog="label_remove",
|
|
180
|
+
description="Remove a label from a permission",
|
|
181
|
+
short_desc="Remove label",
|
|
182
|
+
flags=[
|
|
183
|
+
Flag("range", description="Network range", metavar="RANGE"),
|
|
184
|
+
Flag("group", description="Group with access", metavar="GROUP"),
|
|
185
|
+
Flag("regex", description="Regular expression", metavar="REGEX"),
|
|
186
|
+
Flag("label", description="The label you want to remove", metavar="LABEL"),
|
|
187
|
+
],
|
|
188
|
+
)
|
|
189
|
+
def remove_label_from_permission(args: argparse.Namespace) -> None:
|
|
190
|
+
"""Remove a label from a permission.
|
|
191
|
+
|
|
192
|
+
:param args: argparse.Namespace (range, group, regex, label)
|
|
193
|
+
"""
|
|
194
|
+
# find the permission
|
|
195
|
+
query = {
|
|
196
|
+
"group": args.group,
|
|
197
|
+
"range": args.range,
|
|
198
|
+
"regex": args.regex,
|
|
199
|
+
}
|
|
200
|
+
permission = Permission.get_by_query_unique_or_raise(query)
|
|
201
|
+
permission.remove_label(args.label)
|
|
202
|
+
OutputManager().add_ok(f"Removed the label {args.label!r} from the permission.")
|