Преглед изворни кода

HUE-5173 [fb] Correctly check READ and WRITE permissions for S3 before upload

Jenny Kim пре 9 година
родитељ
комит
9c11e66f62

+ 13 - 8
desktop/libs/aws/src/aws/s3/s3fs.py

@@ -21,6 +21,7 @@ import logging
 import os
 import posixpath
 import re
+import time
 
 from boto.exception import S3ResponseError
 from boto.s3.connection import Location
@@ -427,14 +428,18 @@ class S3FileSystem(object):
   @translate_s3_error
   def check_access(self, path, permission='READ'):
     permission = permission.upper()
-    bucket_name, key_name = s3.parse_uri(path)[:2]
-    bucket = self._get_bucket(bucket_name)
-    acp = bucket.get_acl()
-    for grant in acp.acl.grants:
-      if grant.permission == permission or grant.permission == 'FULL_CONTROL':
-        # TODO: Check grant.uri for user list too
-        return True
-    return False
+    try:
+      if permission == 'WRITE':
+        tmp_file = 'temp_%s' % str(int(time.time() * 1000))
+        tmp_path = '%s/%s' % (path, tmp_file)
+        self.create(path=tmp_path, overwrite=True)
+        self.remove(path=tmp_path)
+      else:
+        self.open(path)
+    except (S3ResponseError, IOError), e:
+      LOG.warn('S3 check_access encountered error verifying %s permission at path "%s": %s' % (permission, path, str(e)))
+      return False
+    return True
 
   def setuser(self, user):
     pass  # user-concept doesn't have sense for this implementation

+ 7 - 0
desktop/libs/aws/src/aws/s3/s3fs_test.py

@@ -308,3 +308,10 @@ class S3FSTest(S3TestBase):
       actual = f.read(file_size)
       expected = file(local_file).read()
       assert_equal(actual, expected, 'files do not match: %s != %s' % (len(actual), len(expected)))
+
+
+  def test_check_access(self):
+    dir_path = self.get_test_path('test_check_access')
+    self.fs.mkdir(dir_path)
+
+    assert_true(self.fs.check_access(dir_path, permission='WRITE'))

+ 2 - 2
desktop/libs/aws/src/aws/s3/upload.py

@@ -25,7 +25,7 @@ import logging
 import StringIO
 
 from django.core.files.uploadedfile import SimpleUploadedFile
-from django.core.files.uploadhandler import FileUploadHandler, SkipFile, StopFutureHandlers, StopUpload
+from django.core.files.uploadhandler import FileUploadHandler, SkipFile, StopFutureHandlers, StopUpload, UploadFileException
 from django.utils.translation import ugettext as _
 
 from aws import get_s3fs
@@ -37,7 +37,7 @@ DEFAULT_WRITE_SIZE = 1024 * 1024 * 50  # TODO: set in configuration (currently 5
 LOG = logging.getLogger(__name__)
 
 
-class S3FileUploadError(Exception):
+class S3FileUploadError(UploadFileException):
   pass
 
 

+ 2 - 2
desktop/libs/hadoop/src/hadoop/fs/upload.py

@@ -29,7 +29,7 @@ import errno
 import logging
 import time
 
-from django.core.files.uploadhandler import FileUploadHandler, StopFutureHandlers, StopUpload
+from django.core.files.uploadhandler import FileUploadHandler, StopFutureHandlers, StopUpload, UploadFileException
 from django.utils.translation import ugettext as _
 
 import hadoop.cluster
@@ -43,7 +43,7 @@ LOG = logging.getLogger(__name__)
 UPLOAD_SUBDIR = 'hue-uploads'
 
 
-class HDFSerror(Exception):
+class HDFSerror(UploadFileException):
   pass