Verified Commit 422a5f89 authored by Martin Abbrent's avatar Martin Abbrent 💬
Browse files

Initial commit

parents
.idea
venv
\ No newline at end of file
# MinIO CLI Wrapper
A small package to wrap the
[MinIO Client](https://docs.min.io/docs/minio-client-complete-guide.html)
(`mc`) commands that are not provided by the [MinIO Python
SDK](https://docs.min.io/docs/python-client-api-reference.html) like
most of the `admin` functions. As it uses an individual and temporary
configuration folder with each instance it is concurrency save and does
not interfere with your personal settings.
# Setup
Install
[MinIO Client](https://docs.min.io/docs/minio-client-complete-guide.html)
from
https://docs.min.io/docs/minio-client-quickstart-guide.html#GNU/Linux
like described in the
[guide](https://docs.min.io/docs/minio-client-quickstart-guide.html#GNU/Linux).
Install the MinIO CLI Wrapper package or add the to `requirements.txt`:
```bash
pip install git+https://git.ufz.de/<package url>
```
@todo add package url
# Usage
Create a client instance:
```python
import Mc
mc = Mc(
"localhost:9000",
secure=False,
access_key="minio",
secret_key="minio123"
)
```
Create a MinIO user:
```python
mc.user_add('my-user', 'my_secret')
```
Create a IAM policy:
```python
mc.policy_add('my-policy-name', {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::{bucket_name}".format(bucket_name='my-bucket-name')
]
}
]
})
```
Assign a IAM policy to a user:
```python
mc.policy_set_user('my-user', 'my-policy-name')
```
Create a new service account for a user (this doesn't work until the
user has an IAM policy assigned):
```python
svc_creds = mc.create_service_account('my-user')
```
import json
import logging
import subprocess
import tempfile
class MinIoClientError(ChildProcessError):
pass
class Mc:
ALIAS = 'my_minio'
CONFIG_TEMPLATE = {
"version": "10",
"aliases": {
ALIAS: {
"url": "",
"accessKey": "",
"secretKey": "",
"api": "s3v4",
"path": "auto"
}
}
}
def __init__(self, url: str, access_key: str, secret_key: str, secure=True):
self.url = url
self.access_key = access_key
self.secret_key = secret_key
self.secure = secure
self.alias = Mc.ALIAS
self.config = Mc.CONFIG_TEMPLATE.copy()
self.proto = 'https://'
self.config_dir = tempfile.TemporaryDirectory()
self.config_file = open('{}/config.json'.format(self.config_dir.name), mode='w+')
self._init_config()
def _init_config(self):
self.proto = 'https://' if self.secure else 'http://'
self.config['aliases'][self.alias]['url'] = '{proto}{url}'.format(proto=self.proto,
url=self.url)
self.config['aliases'][self.alias]['accessKey'] = self.access_key
self.config['aliases'][self.alias]['secretKey'] = self.secret_key
json.dump(self.config, self.config_file, indent=4)
self.config_file.flush()
def _command(self, command: list, confidential=False):
ret = subprocess.run(
['mc', '-C', self.config_dir.name] + command + ['--json'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
)
if ret.returncode != 0:
raise MinIoClientError(ret.stderr)
if not confidential:
logging.info(ret.stdout)
return json.loads(ret.stdout)
def user_add(self, access_key: str, secret_key: str):
self._command(['admin', 'user', 'add', self.alias, access_key, secret_key])
def policy_add(self, policy_name: str, policy: dict):
policy_file = tempfile.NamedTemporaryFile(mode='w+')
json.dump(policy, policy_file, indent=4)
policy_file.flush()
self._command(['admin', 'policy', 'add', self.alias, policy_name, policy_file.name])
def policy_set_user(self, policy_name: str, username: str):
self._command(
['admin', 'policy', 'set', self.alias, policy_name, 'user={}'.format(username)]
)
def create_service_account(self, username: str):
ret = self._command(
['admin', 'user', 'svcacct', 'add', self.alias, username],
confidential=True
)
# filter the relevant keys from the returned output
return {'accessKey': ret['accessKey'], 'secretKey': ret['secretKey']}
import setuptools
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setuptools.setup(
name='MinIO CLI Wrapper',
version='0.0.1',
author='Martin Abbrent',
author_email='martin.abbrent@ufz.de',
description='Wrapper to control MinIO commands that are not covered by the MinIO Python SDK',
long_description=long_description,
long_description_content_type="text/markdown",
url='',
project_urls = {
"Bug Tracker": ""
},
license='HEESIL',
packages=['miniocliwrapper'],
install_requires=['json', 'logging', 'subprocess', 'tempfile'],
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment