Repository navigation
Expand file tree
/
Copy pathviews.py
More file actions
152 lines (131 loc) · 4.84 KB
/
Copy pathviews.py
File metadata and controls
152 lines (131 loc) · 4.84 KB
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
from boto import exception
from django.core.exceptions import ValidationError
from flask import request
from rest_framework import status as http_status
import addons.myminio.settings as settings
from addons.base import generic_views
from addons.myminio import SHORT_NAME, FULL_NAME
from addons.myminio import utils
from addons.myminio.serializer import MyMinIOSerializer
from admin.rdm_addons.decorators import must_be_rdm_addons_allowed
from framework.auth.decorators import must_be_logged_in
from framework.exceptions import HTTPError
from osf.models import ExternalAccount
from website.project.decorators import (
must_have_addon, must_have_permission,
must_be_addon_authorizer,
)
myminio_account_list = generic_views.account_list(
SHORT_NAME,
MyMinIOSerializer
)
myminio_import_auth = generic_views.import_auth(
SHORT_NAME,
MyMinIOSerializer
)
myminio_deauthorize_node = generic_views.deauthorize_node(
SHORT_NAME
)
myminio_get_config = generic_views.get_config(
SHORT_NAME,
MyMinIOSerializer
)
def _set_folder(node_addon, folder, auth):
folder_id = folder['id']
node_addon.set_folder(folder_id, auth=auth)
node_addon.save()
myminio_set_config = generic_views.set_config(
SHORT_NAME,
FULL_NAME,
MyMinIOSerializer,
_set_folder
)
@must_have_addon(SHORT_NAME, 'node')
@must_be_addon_authorizer(SHORT_NAME)
def myminio_folder_list(node_addon, **kwargs):
""" Returns all the subsequent folders under the folder id passed.
"""
return node_addon.get_folders()
@must_be_logged_in
@must_be_rdm_addons_allowed(SHORT_NAME)
def myminio_add_user_account(auth, **kwargs):
"""Verifies new external account credentials and adds to user's list"""
host = settings.HOST
try:
access_key = request.json['access_key']
secret_key = request.json['secret_key']
except KeyError:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST)
if not (access_key and secret_key):
return {
'message': 'All the fields above are required.'
}, http_status.HTTP_400_BAD_REQUEST
user_info = utils.get_user_info(host, access_key, secret_key)
if not user_info:
return {
'message': ('Unable to access account.\n'
'Check to make sure that the above credentials are valid, '
'and that they have permission to list buckets.')
}, http_status.HTTP_400_BAD_REQUEST
if not utils.can_list(host, access_key, secret_key):
return {
'message': ('Unable to list buckets.\n'
'Listing buckets is required permission that can be changed via IAM')
}, http_status.HTTP_400_BAD_REQUEST
try:
account = ExternalAccount(
provider=SHORT_NAME,
provider_name=FULL_NAME,
oauth_key=access_key,
oauth_secret=secret_key,
provider_id=user_info.id,
display_name=user_info.display_name,
)
account.save()
except ValidationError:
# ... or get the old one
account = ExternalAccount.objects.get(
provider=SHORT_NAME,
provider_id=user_info.id
)
if account.oauth_key != access_key or account.oauth_secret != secret_key:
account.oauth_key = access_key
account.oauth_secret = secret_key
account.save()
assert account is not None
if not auth.user.external_accounts.filter(id=account.id).exists():
auth.user.external_accounts.add(account)
# Ensure My MinIO is enabled.
auth.user.get_or_add_addon('myminio', auth=auth)
auth.user.save()
return {}
@must_be_addon_authorizer(SHORT_NAME)
@must_have_addon('myminio', 'node')
@must_have_permission('write')
def myminio_create_bucket(auth, node_addon, **kwargs):
bucket_name = request.json.get('bucket_name', '')
# bucket_location = request.json.get('bucket_location', '')
if not utils.validate_bucket_name(bucket_name):
return {
'message': 'That bucket name is not valid.',
'title': 'Invalid bucket name',
}, http_status.HTTP_400_BAD_REQUEST
try:
# utils.create_bucket(node_addon, bucket_name, bucket_location)
utils.create_bucket(node_addon, bucket_name)
except exception.S3ResponseError as e:
return {
'message': e.message,
'title': 'Problem connecting to My MinIO',
}, http_status.HTTP_400_BAD_REQUEST
except exception.S3CreateError as e:
return {
'message': e.message,
'title': "Problem creating bucket '{0}'".format(bucket_name),
}, http_status.HTTP_400_BAD_REQUEST
except exception.BotoClientError as e: # Base class catchall
return {
'message': e.message,
'title': 'Error connecting to My MinIO',
}, http_status.HTTP_400_BAD_REQUEST
return {}