Skip to content

Commit

Permalink
Updated Security Client APIs (#450)
Browse files Browse the repository at this point in the history
Signed-off-by: saimedhi <saimedhi@amazon.com>
  • Loading branch information
saimedhi committed Jul 26, 2023
1 parent 87b402c commit caeaa13
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 120 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Added the ability to run tests matching a pattern to `.ci/run-tests` ([#454](https://github.com/opensearch-project/opensearch-py/pull/454))
### Changed
- Moved security from `plugins` to `clients` ([#442](https://github.com/opensearch-project/opensearch-py/pull/442))
- Updated Security Client APIs ([#450](https://github.com/opensearch-project/opensearch-py/pull/450))
### Deprecated
### Removed
- Removed support for Python 2.7 ([#421](https://github.com/opensearch-project/opensearch-py/pull/421))
Expand Down
4 changes: 2 additions & 2 deletions guides/plugins/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ role_content = {
],
}

response = client.security.put_role(role_name, body=role_content)
response = client.security.create_role(role_name, body=role_content)
print(response)
```

Expand All @@ -45,7 +45,7 @@ print(response)
user_name = "test-user"
user_content = {"password": "test_password", "opendistro_security_roles": []}

response = client.security.put_role(user_name, body=user_content)
response = client.security.create_user(user_name, body=user_content)
print(response)
```

Expand Down
118 changes: 74 additions & 44 deletions opensearchpy/_async/client/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ async def get_account_details(self, params=None, headers=None):
)

@query_params()
async def change_password(
self, current_password, password, params=None, headers=None
):
async def change_password(self, body, params=None, headers=None):
"""
Changes the password for the current user.
"""
if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.")

return await self.transport.perform_request(
"PUT",
_make_path("_plugins", "_security", "api", "account"),
params=params,
headers=headers,
body={
"current_password": current_password,
"password": password,
},
body=body,
)

@query_params()
Expand Down Expand Up @@ -88,14 +86,13 @@ async def delete_action_group(self, action_group, params=None, headers=None):
)

@query_params()
async def put_action_group(self, action_group, body, params=None, headers=None):
async def create_action_group(self, action_group, body, params=None, headers=None):
"""
Creates or replaces the specified action group.
"""
if action_group in SKIP_IN_PATH:
raise ValueError(
"Empty value passed for a required argument 'action-group'."
)
for param in (action_group, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")

return await self.transport.perform_request(
"PUT",
Expand All @@ -110,10 +107,9 @@ async def patch_action_group(self, action_group, body, params=None, headers=None
"""
Updates individual attributes of an action group.
"""
if action_group in SKIP_IN_PATH:
raise ValueError(
"Empty value passed for a required argument 'action-group'."
)
for param in (action_group, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")

return await self.transport.perform_request(
"PATCH",
Expand All @@ -128,6 +124,9 @@ async def patch_action_groups(self, body, params=None, headers=None):
"""
Creates, updates, or deletes multiple action groups in a single call.
"""
if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.")

return await self.transport.perform_request(
"PATCH",
_make_path("_plugins", "_security", "api", "actiongroups"),
Expand Down Expand Up @@ -179,12 +178,13 @@ async def delete_user(self, username, params=None, headers=None):
)

@query_params()
async def put_user(self, username, body, params=None, headers=None):
async def create_user(self, username, body, params=None, headers=None):
"""
Creates or replaces the specified user.
"""
if username in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'username'.")
for param in (username, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")

return await self.transport.perform_request(
"PUT",
Expand All @@ -199,8 +199,9 @@ async def patch_user(self, username, body, params=None, headers=None):
"""
Updates individual attributes of an internal user.
"""
if username in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'username'.")
for param in (username, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")

return await self.transport.perform_request(
"PATCH",
Expand All @@ -215,6 +216,9 @@ async def patch_users(self, body, params=None, headers=None):
"""
Creates, updates, or deletes multiple internal users in a single call.
"""
if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.")

return await self.transport.perform_request(
"PATCH",
_make_path("_plugins", "_security", "api", "internalusers"),
Expand Down Expand Up @@ -266,12 +270,13 @@ async def delete_role(self, role, params=None, headers=None):
)

@query_params()
async def put_role(self, role, body, params=None, headers=None):
async def create_role(self, role, body, params=None, headers=None):
"""
Creates or replaces the specified role.
"""
if role in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'role'.")
for param in (role, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")

return await self.transport.perform_request(
"PUT",
Expand All @@ -286,8 +291,9 @@ async def patch_role(self, role, body, params=None, headers=None):
"""
Updates individual attributes of a role.
"""
if role in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'role'.")
for param in (role, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")

return await self.transport.perform_request(
"PATCH",
Expand All @@ -302,6 +308,9 @@ async def patch_roles(self, body, params=None, headers=None):
"""
Creates, updates, or deletes multiple roles in a single call.
"""
if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.")

return await self.transport.perform_request(
"PATCH",
_make_path("_plugins", "_security", "api", "roles"),
Expand Down Expand Up @@ -338,7 +347,7 @@ async def get_role_mappings(self, params=None, headers=None):
)

@query_params()
async def delete_role_mappings(self, role, params=None, headers=None):
async def delete_role_mapping(self, role, params=None, headers=None):
"""
Deletes the specified role mapping.
"""
Expand All @@ -353,12 +362,13 @@ async def delete_role_mappings(self, role, params=None, headers=None):
)

@query_params()
async def put_role_mappings(self, role, body, params=None, headers=None):
async def create_role_mapping(self, role, body, params=None, headers=None):
"""
Creates or replaces the specified role mapping.
"""
if role in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'role'.")
for param in (role, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")

return await self.transport.perform_request(
"PUT",
Expand All @@ -373,8 +383,9 @@ async def patch_role_mapping(self, role, body, params=None, headers=None):
"""
Updates individual attributes of a role mapping.
"""
if role in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'role'.")
for param in (role, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")

return await self.transport.perform_request(
"PATCH",
Expand All @@ -389,6 +400,9 @@ async def patch_role_mappings(self, body, params=None, headers=None):
"""
Creates or updates multiple role mappings in a single call.
"""
if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.")

return await self.transport.perform_request(
"PATCH",
_make_path("_plugins", "_security", "api", "rolesmapping"),
Expand Down Expand Up @@ -440,12 +454,13 @@ async def delete_tenant(self, tenant, params=None, headers=None):
)

@query_params()
async def put_tenant(self, tenant, body, params=None, headers=None):
async def create_tenant(self, tenant, body, params=None, headers=None):
"""
Creates or replaces the specified tenant.
"""
if tenant in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'tenant'.")
for param in (tenant, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")

return await self.transport.perform_request(
"PUT",
Expand All @@ -460,8 +475,9 @@ async def patch_tenant(self, tenant, body, params=None, headers=None):
"""
Add, delete, or modify a single tenant.
"""
if tenant in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'tenant'.")
for param in (tenant, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")

return await self.transport.perform_request(
"PATCH",
Expand All @@ -476,6 +492,9 @@ async def patch_tenants(self, body, params=None, headers=None):
"""
Add, delete, or modify multiple tenants in a single call.
"""
if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.")

return await self.transport.perform_request(
"PATCH",
_make_path("_plugins", "_security", "api", "tenants"),
Expand All @@ -497,10 +516,13 @@ async def get_configuration(self, params=None, headers=None):
)

@query_params()
async def put_configuration(self, body, params=None, headers=None):
async def update_configuration(self, body, params=None, headers=None):
"""
Retrieves the current Security plugin configuration in JSON format.
"""
if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.")

return await self.transport.perform_request(
"PUT",
_make_path("_plugins", "_security", "api", "securityconfig", "config"),
Expand All @@ -514,6 +536,9 @@ async def patch_configuration(self, body, params=None, headers=None):
"""
Updates the existing configuration using the REST API.
"""
if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.")

return await self.transport.perform_request(
"PATCH",
_make_path("_plugins", "_security", "api", "securityconfig"),
Expand All @@ -537,16 +562,15 @@ async def get_distinguished_names(
)

@query_params()
async def put_distinguished_names(
async def update_distinguished_names(
self, cluster_name, body, params=None, headers=None
):
"""
Adds or updates the specified distinguished names in the cluster's or node's allow list.
"""
if cluster_name in SKIP_IN_PATH:
raise ValueError(
"Empty value passed for a required argument 'cluster-name'."
)
for param in (cluster_name, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")

return await self.transport.perform_request(
"PUT",
Expand Down Expand Up @@ -648,10 +672,13 @@ async def get_audit_configuration(self, params=None, headers=None):
)

@query_params()
async def put_audit_configuration(self, body, params=None, headers=None):
async def update_audit_config(self, body, params=None, headers=None):
"""
A PUT call updates the audit configuration.
"""
if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.")

return await self.transport.perform_request(
"PUT",
_make_path("_opendistro", "_security", "api", "audit", "config"),
Expand All @@ -665,6 +692,9 @@ async def patch_audit_configuration(self, body, params=None, headers=None):
"""
A PATCH call is used to update specified fields in the audit configuration.
"""
if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.")

return await self.transport.perform_request(
"PATCH",
_make_path("_opendistro", "_security", "api", "audit"),
Expand Down

0 comments on commit caeaa13

Please sign in to comment.