/
users_command.py
176 lines (145 loc) · 8.94 KB
/
users_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import click
import os
import yaml
from kfk.command import kfk
from kfk.option_extensions import NotRequiredIf, RequiredIf
from kfk.commons import print_missing_options_for_command, resource_exists, get_resource_as_file, \
delete_last_applied_configuration, add_resource_kv_config, delete_resource_config, create_temp_file, \
print_resource_found_msg
from kfk.config import *
from kfk.kubectl_command_builder import Kubectl
from kfk.utils import snake_to_camel_case
@click.option('-n', '--namespace', help='Namespace to use', required=True)
@click.option('-c', '--cluster', help='Cluster to use', required=True)
@click.option('--delete-quota', 'delete_quota_tuple', help='User quotas to be removed.', multiple=True)
@click.option('--quota', 'quota_tuple', help='User\'s network and CPU utilization quotas in the Kafka cluster.',
multiple=True)
@click.option('--resource-pattern-type',
help="The type of the resource pattern or <ANY|MATCH|LITERAL|PREFIXED> pattern filter. When adding "
"acls, this should be a specific pattern type, e.g. 'literal' or 'prefixed'. (default: literal)",
default='literal')
@click.option('--resource-name', help='', cls=RequiredIf, required_if=['add_acl', 'delete_acl'])
@click.option('--resource-type', help='', cls=RequiredIf, required_if=['add_acl', 'delete_acl'])
@click.option('--type', help='Operation type for ACL. (default: allow)',
type=click.Choice(['allow', 'deny'], case_sensitive=True), default='allow')
@click.option('--host', help='Host which User will have access. (default: *)', default='*')
@click.option('--operation', 'operation_tuple', help='Operation that is being allowed or denied. (default: All)',
default=["All"], multiple=True)
@click.option('--delete-acl', help='Delete ACL of User', is_flag=True)
@click.option('--add-acl', help='Add ACL to User', is_flag=True)
@click.option('--authorization-type', help='Authorization type for user',
type=click.Choice(['simple'], case_sensitive=True))
@click.option('--alter', 'is_alter', help='Alter authentication-type, quotas, etc. of the user.', is_flag=True)
@click.option('--delete', 'is_delete', help='Delete a user.', is_flag=True)
@click.option('-o', '--output',
help='Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath'
'|jsonpath-file.')
@click.option('--describe', 'is_describe', help='List details for the given user.', is_flag=True)
@click.option('--authentication-type', help='Authentication type for user',
type=click.Choice(['tls', 'scram-sha-512'], case_sensitive=True), cls=RequiredIf,
required_if=['is_create'])
@click.option('--create', 'is_create', help='Create a new user.', is_flag=True)
@click.option('--list', 'is_list', help='List all available users.', is_flag=True)
@click.option('--user', help='User Name', required=True, cls=NotRequiredIf, not_required_if=['is_list'])
@kfk.command()
def users(user, is_list, is_create, authentication_type, is_describe, output, is_delete, is_alter, authorization_type,
add_acl, delete_acl, operation_tuple, host, type, resource_type, resource_name, resource_pattern_type,
quota_tuple,
delete_quota_tuple, cluster, namespace):
"""The kafka user(s) to be created, altered or described."""
if is_list:
list(cluster, namespace)
elif is_create:
create(user, authentication_type, quota_tuple, cluster, namespace)
elif is_describe:
describe(user, output, cluster, namespace)
elif is_delete:
delete(cluster, namespace, user)
elif is_alter:
alter(user, authentication_type, authorization_type, add_acl, delete_acl, operation_tuple, host, type,
resource_type, resource_name, resource_pattern_type, quota_tuple, delete_quota_tuple, cluster, namespace)
else:
print_missing_options_for_command("users")
def list(cluster, namespace):
os.system(
Kubectl().get().kafkausers().label("strimzi.io/cluster={cluster}").namespace(namespace).build().format(
cluster=cluster))
def create(user, authentication_type, quota, cluster, namespace):
with open('{strimzi_path}/examples/user/kafka-user.yaml'.format(strimzi_path=STRIMZI_PATH).format(
version=STRIMZI_VERSION)) as file:
user_dict = yaml.full_load(file)
user_dict["metadata"]["name"] = user
user_dict["metadata"]["labels"]["strimzi.io/cluster"] = cluster
user_dict["spec"]["authentication"]["type"] = authentication_type
del user_dict["spec"]["authorization"]
if len(quota) > 0:
if user_dict["spec"].get("quotas") is None:
user_dict["spec"]["quotas"] = {}
add_resource_kv_config(quota, user_dict["spec"]["quotas"])
user_yaml = yaml.dump(user_dict)
user_temp_file = create_temp_file(user_yaml)
os.system(
Kubectl().create().from_file("{user_temp_file_path}").namespace(namespace).build().format(
user_temp_file_path=user_temp_file.name))
user_temp_file.close()
def describe(user, output, cluster, namespace):
if output is not None:
if resource_exists("kafkausers", user, cluster, namespace):
os.system(Kubectl().get().kafkausers(user).namespace(namespace).output(output).build())
else:
if resource_exists("kafkausers", user, cluster, namespace):
os.system(Kubectl().describe().kafkausers(user).namespace(namespace).build())
def delete(cluster, namespace, user):
if resource_exists("kafkausers", user, cluster, namespace):
os.system(Kubectl().delete().kafkausers(user).namespace(namespace).build())
def alter(user, authentication_type, authorization_type, add_acl, delete_acl, operation_tuple, host, type,
resource_type, resource_name, resource_pattern_type, quota_tuple, delete_quota_tuple, cluster, namespace):
if resource_exists("kafkausers", user, cluster, namespace):
file = get_resource_as_file("kafkausers", user, namespace)
user_dict = yaml.full_load(file)
if authentication_type is not None:
user_dict["spec"]["authentication"]["type"] = authentication_type
if authorization_type is not None:
if user_dict["spec"].get("authorization") is None:
user_dict["spec"]["authorization"] = {}
user_dict["spec"]["authorization"]["type"] = authorization_type
if add_acl:
if user_dict["spec"].get("authorization") is None:
user_dict["spec"]["authorization"] = {}
add_acl_option(user_dict, operation_tuple, host, type, resource_type, resource_name, resource_pattern_type)
if delete_acl:
if user_dict["spec"].get("authorization") is None:
user_dict["spec"]["authorization"] = {}
delete_acl_option(user_dict, operation_tuple, host, type, resource_type, resource_name, resource_pattern_type)
delete_last_applied_configuration(user_dict)
if len(quota_tuple) > 0:
if user_dict["spec"].get("quotas") is None:
user_dict["spec"]["quotas"] = {}
add_resource_kv_config(quota_tuple, user_dict["spec"]["quotas"], snake_to_camel_case)
if len(delete_quota_tuple) > 0:
if user_dict["spec"].get("quotas") is not None:
delete_resource_config(delete_quota_tuple, user_dict["spec"]["quotas"], snake_to_camel_case)
user_yaml = yaml.dump(user_dict)
user_temp_file = create_temp_file(user_yaml)
os.system(
Kubectl().apply().from_file("{user_temp_file_path}").namespace(namespace).build().format(
user_temp_file_path=user_temp_file.name))
user_temp_file.close()
else:
print_resource_found_msg(cluster, namespace)
def add_acl_option(user_dict, operation, host, type, resource_type, resource_name, resource_pattern_type):
if user_dict["spec"]["authorization"].get("acls") is None:
user_dict["spec"]["authorization"]["acls"] = []
for operation_str in operation:
acl_dict = {'operation': operation_str, 'host': host, 'type': type,
'resource': {'type': resource_type, 'name': resource_name, 'patternType': resource_pattern_type}}
user_dict["spec"]["authorization"]["acls"].append(acl_dict)
def delete_acl_option(user_dict, operation, host, type, resource_type, resource_name, resource_pattern_type):
if user_dict["spec"]["authorization"].get("acls") is None:
user_dict["spec"]["authorization"]["acls"] = []
for operation_str in operation:
for acl_dict in user_dict["spec"]["authorization"]["acls"]:
acl_dict_to_be_deleted = {'operation': operation_str, 'host': host, 'type': type,
'resource': {'type': resource_type, 'name': resource_name, 'patternType': resource_pattern_type}}
if acl_dict == acl_dict_to_be_deleted:
user_dict["spec"]["authorization"]["acls"].remove(acl_dict)