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

MinioAdmin: allow specifying policies as dict besides file #1480

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
58 changes: 40 additions & 18 deletions minio/minioadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import os
from datetime import timedelta
from enum import Enum, unique
from pathlib import Path
from typing import Any, TextIO, Tuple, cast
from urllib.parse import urlunsplit

Expand Down Expand Up @@ -452,16 +453,26 @@ def group_list(self) -> str:
response = self._url_open("GET", _COMMAND.LIST_GROUPS)
return response.data.decode()

def policy_add(self, policy_name: str, policy_file: str) -> str:
def policy_add(self,
policy_name: str,
policy_file: str | os.PathLike | None = None,
policy: dict | None = None) -> str:
"""Add new policy."""
with open(policy_file, encoding='utf-8') as file:
response = self._url_open(
"PUT",
_COMMAND.ADD_CANNED_POLICY,
query_params={"name": policy_name},
body=file.read().encode(),
)
return response.data.decode()
if policy_file:
with Path(policy_file).open(encoding='utf-8') as file:
body = file.read().encode()
elif policy:
body = json.dumps(policy).encode()
else:
raise ValueError("either policy or policy_file must be specified")

response = self._url_open(
"PUT",
_COMMAND.ADD_CANNED_POLICY,
query_params={"name": policy_name},
body=body,
)
return response.data.decode()

def policy_remove(self, policy_name: str) -> str:
"""Remove policy."""
Expand Down Expand Up @@ -753,7 +764,8 @@ def add_service_account(self,
secret_key: str | None = None,
name: str | None = None,
description: str | None = None,
policy_file: str | None = None,
policy: dict | None = None,
policy_file: str | os.PathLike | None = None,
expiration: str | None = None,
status: str | None = None) -> str:
"""
Expand All @@ -763,7 +775,9 @@ def add_service_account(self,
raise ValueError("both access key and secret key must be provided")
if access_key == "" or secret_key == "":
raise ValueError("access key or secret key must not be empty")
data = {
if policy_file and policy:
raise ValueError("specify either policy_file or policy, not both")
data: dict[str, Any] = {
"status": "enabled",
"accessKey": access_key,
"secretKey": secret_key,
Expand All @@ -773,8 +787,10 @@ def add_service_account(self,
if description:
data["description"] = description
if policy_file:
with open(policy_file, encoding="utf-8") as file:
with Path(policy_file).open(encoding="utf-8") as file:
data["policy"] = json.load(file)
if policy:
data["policy"] = policy
if expiration:
data["expiration"] = expiration
if status:
Expand All @@ -797,25 +813,31 @@ def update_service_account(self,
secret_key: str | None = None,
name: str | None = None,
description: str | None = None,
policy_file: str | None = None,
policy_file: str | os.PathLike | None = None,
policy: dict | None = None,
expiration: str | None = None,
status: str | None = None) -> str:
"""Update an existing service account"""
args = [secret_key, name, description, policy_file, expiration, status]
args = [secret_key, name, description,
policy_file, policy, expiration, status]
if not any(arg for arg in args):
raise ValueError("at least one of secret_key, name, description, "
"policy_file, expiration or status must be "
"specified")
data = {}
"policy_file, policy, expiration or status must "
"be specified")
if policy_file and policy:
raise ValueError("specify either policy_file or policy, not both")
data: dict[str, Any] = {}
if secret_key:
data["newSecretKey"] = secret_key
if name:
data["newName"] = name
if description:
data["newDescription"] = description
if policy_file:
with open(policy_file, encoding="utf-8") as file:
with Path(policy_file).open(encoding="utf-8") as file:
data["newPolicy"] = json.load(file)
if policy:
data["newPolicy"] = policy
if expiration:
data["newExpiration"] = expiration
if status:
Expand Down