示例#1
0
 def _ConfigureNoOpAuthIfNeeded(self):
     """Sets up no-op auth handler if no boto credentials are configured."""
     config = boto.config
     if not util.HasConfiguredCredentials():
         if self.config_file_list:
             if (config.has_option('Credentials', 'gs_service_client_id')
                     and not HAS_CRYPTO):
                 raise CommandException(
                     'Your gsutil is configured with an OAuth2 service account,\nbut '
                     'you do not have PyOpenSSL or PyCrypto 2.6 or later installed.\n'
                     'Service account authentication requires one of these '
                     'libraries;\nplease install either of them to proceed, or '
                     'configure \na different type of credentials with '
                     '"gsutil config".')
             raise CommandException(
                 'You have no storage service credentials in any '
                 'of the following boto config\nfiles. Please '
                 'add your credentials as described in the '
                 'gsutil README.md file, or else\nre-run '
                 '"gsutil config" to re-create a config '
                 'file:\n%s' % self.config_file_list)
         else:
             # With no boto config file the user can still access publicly readable
             # buckets and objects.
             from gslib import no_op_auth_plugin
示例#2
0
 def _ConfigureNoOpAuthIfNeeded(self):
   """Sets up no-op auth handler if no boto credentials are configured."""
   config = boto.config
   if not util.HasConfiguredCredentials(self.bypass_prodaccess):
     if self.config_file_list:
       if (config.has_option('Credentials', 'gs_oauth2_refresh_token')
           and not HAVE_OAUTH2):
         raise CommandException(
             'Your gsutil is configured with OAuth2 authentication '
             'credentials.\nHowever, OAuth2 is only supported when running '
             'under Python 2.6 or later\n(unless additional dependencies are '
             'installed, see README for details); you are running Python %s.' %
             sys.version)
       raise CommandException('You have no storage service credentials in any '
                              'of the following boto config\nfiles. Please '
                              'add your credentials as described in the '
                              'gsutil README file, or else\nre-run '
                              '"gsutil config" to re-create a config '
                              'file:\n%s' % self.config_file_list)
     else:
       # With no boto config file the user can still access publicly readable
       # buckets and objects.
       from gslib import no_op_auth_plugin
示例#3
0
def _RunNamedCommandAndHandleExceptions(command_runner,
                                        command_name,
                                        args=None,
                                        headers=None,
                                        debug=0,
                                        parallel_operations=False):
    try:
        # Catch ^C so we can print a brief message instead of the normal Python
        # stack trace.
        signal.signal(signal.SIGINT, _HandleControlC)
        # Catch ^\ so we can force a breakpoint in a running gsutil.
        if not util.IS_WINDOWS:
            signal.signal(signal.SIGQUIT, _HandleSigQuit)
        return command_runner.RunNamedCommand(command_name, args, headers,
                                              debug, parallel_operations)
    except AttributeError as e:
        if str(e).find('secret_access_key') != -1:
            _OutputAndExit(
                'Missing credentials for the given URI(s). Does your '
                'boto config file contain all needed credentials?')
        else:
            _OutputAndExit(str(e))
    except boto.exception.StorageDataError as e:
        _OutputAndExit('StorageDataError: %s.' % e.reason)
    except boto.exception.BotoClientError as e:
        _OutputAndExit('BotoClientError: %s.' % e.reason)
    except gslib.exception.CommandException as e:
        _HandleCommandException(e)
    except getopt.GetoptError as e:
        _HandleCommandException(gslib.exception.CommandException(e.msg))
    except boto.exception.InvalidAclError as e:
        _OutputAndExit('InvalidAclError: %s.' % str(e))
    except boto.exception.InvalidUriError as e:
        _OutputAndExit('InvalidUriError: %s.' % e.message)
    except gslib.exception.ProjectIdException as e:
        _OutputAndExit('ProjectIdException: %s.' % e.reason)
    except boto.auth_handler.NotReadyToAuthenticate:
        _OutputAndExit('NotReadyToAuthenticate')
    except OSError as e:
        _OutputAndExit('OSError: %s.' % e.strerror)
    except wildcard_iterator.WildcardException as e:
        _OutputAndExit(e.reason)
    except boto.exception.StorageResponseError as e:
        # Check for access denied, and provide detail to users who have no boto
        # config file (who might previously have been using gsutil only for
        # accessing publicly readable buckets and objects).
        if (e.status == 403
                or (e.status == 400 and e.code == 'MissingSecurityHeader')):
            if not util.HasConfiguredCredentials():
                _OutputAndExit(
                    'You are attempting to access protected data with no configured '
                    'credentials.\nPlease see '
                    'http://code.google.com/apis/storage/docs/signup.html for\ndetails '
                    'about activating the Google Cloud Storage service and then run '
                    'the\n"gsutil config" command to configure gsutil to use these '
                    'credentials.')
            elif (e.error_code == 'AccountProblem'
                  and ','.join(args).find('gs://') != -1):
                default_project_id = boto.config.get_value(
                    'GSUtil', 'default_project_id')
                acct_help_part_1 = (
                    """Your request resulted in an AccountProblem (403) error. Usually this happens
if you attempt to create a bucket or upload an object without having first
enabled billing for the project you are using. To remedy this problem, please do
the following:

1. Navigate to the Google APIs console (https://code.google.com/apis/console),
   and ensure the drop-down selector beneath "Google APIs" shows the project
   you're attempting to use.

""")
                acct_help_part_2 = '\n'
                if default_project_id:
                    acct_help_part_2 = (
                        """2. Click "Google Cloud Storage" on the left hand pane, and then check that
   the value listed for "x-goog-project-id" on this page matches the project ID
   (%s) from your boto config file.

""" % default_project_id)
                acct_help_part_3 = (
                    """Check whether there's an "!" next to Billing. If so, click Billing and then
   enable billing for this project. Note that it can take up to one hour after
   enabling billing for the project to become activated for creating buckets and
   uploading objects.

If the above doesn't resolve your AccountProblem, please send mail to
[email protected] requesting assistance, noting the exact command you ran, the
fact that you received a 403 AccountProblem error, and your project ID. Please
do not post your project ID on StackOverflow.

Note: It's possible to use Google Cloud Storage without enabling billing if
you're only listing or reading objects for which you're authorized, or if
you're uploading objects to a bucket billed to a project that has billing
enabled. But if you're attempting to create buckets or upload objects to a
bucket owned by your own project, you must first enable billing for that
project.""")
                if default_project_id:
                    _OutputAndExit(acct_help_part_1 + acct_help_part_2 +
                                   '3. ' + acct_help_part_3)
                else:
                    _OutputAndExit(acct_help_part_1 + '2. ' + acct_help_part_3)

        if not e.body:
            e.body = ''
        exc_name, error_detail = util.ExtractErrorDetail(e)
        if error_detail:
            _OutputAndExit(
                '%s: status=%d, code=%s, reason=%s, detail=%s.' %
                (exc_name, e.status, e.code, e.reason, error_detail))
        else:
            _OutputAndExit('%s: status=%d, code=%s, reason=%s.' %
                           (exc_name, e.status, e.code, e.reason))
    except boto.exception.ResumableUploadException as e:
        _OutputAndExit('ResumableUploadException: %s.' % e.message)
    except socket.error as e:
        if e.args[0] == errno.EPIPE:
            # Retrying with a smaller file (per suggestion below) works because
            # the library code send loop (in boto/s3/key.py) can get through the
            # entire file and then request the HTTP response before the socket
            # gets closed and the response lost.
            message = ("""
Got a "Broken pipe" error. This can happen to clients using Python 2.x,
when the server sends an error response and then closes the socket (see
http://bugs.python.org/issue5542). If you are trying to upload a large
object you might retry with a small (say 200k) object, and see if you get
a more specific error code.
""")
            _OutputAndExit(message)
        else:
            _HandleUnknownFailure(e)
    except Exception as e:
        # Check for two types of errors related to service accounts. These errors
        # appear to be the same except for their messages, but they are caused by
        # different problems and both have unhelpful error messages. Moreover,
        # the error type belongs to PyOpenSSL, which is not necessarily installed.
        if 'mac verify failure' in str(e):
            _OutputAndExit(
                "Encountered an error while refreshing access token." +
                " If you are using a service account,\nplease verify that the "
                + "gs_service_key_file_password field in your config file," +
                "\n%s, is correct." % GetConfigFilePath())
        elif 'asn1 encoding routines' in str(e):
            _OutputAndExit(
                "Encountered an error while refreshing access token." +
                " If you are using a service account,\nplease verify that the "
                +
                "gs_service_key_file field in your config file,\n%s, is correct."
                % GetConfigFilePath())
        _HandleUnknownFailure(e)