Esempio n. 1
0
def ConfigureNoOpAuthIfNeeded():
  """Sets up no-op auth handler if no boto credentials are configured."""
  if not HasConfiguredCredentials():
    if (config.has_option('Credentials', 'gs_service_client_id') and
        not HAS_CRYPTO):
      if system_util.InvokedViaCloudSdk():
        raise CommandException('\n'.join(
            textwrap.wrap(
                'Your gsutil is configured with an OAuth2 service account, but '
                'you do not have PyOpenSSL or PyCrypto 2.6 or later installed. '
                'Service account authentication requires one of these libraries; '
                'please reactivate your service account via the gcloud auth '
                'command and ensure any gcloud packages necessary for '
                'service accounts are present.')))
      else:
        raise CommandException('\n'.join(
            textwrap.wrap(
                'Your gsutil is configured with an OAuth2 service account, but '
                'you do not have PyOpenSSL or PyCrypto 2.6 or later installed. '
                'Service account authentication requires one of these libraries; '
                'please install either of them to proceed, or configure a '
                'different type of credentials with "gsutil config".')))
    else:
      # With no boto config file the user can still access publicly readable
      # buckets and objects.
      from gslib import no_op_auth_plugin  # pylint: disable=unused-variable
Esempio n. 2
0
def GetUserAgent(args, metrics_off=True):
    """Using the command arguments return a suffix for the UserAgent string.

  Args:
    args: str[], parsed set of arguments entered in the CLI.
    metrics_off: boolean, whether the MetricsCollector is disabled.

  Returns:
    str, A string value that can be appended to an existing UserAgent.
  """
    user_agent = ' gsutil/%s' % gslib.VERSION
    user_agent += ' (%s)' % sys.platform
    user_agent += ' analytics/%s ' % ('disabled' if metrics_off else 'enabled')
    user_agent += ' interactive/%s' % system_util.IsRunningInteractively()

    if len(args) > 0:
        user_agent += ' command/%s' % args[0]

        if args[0] in ['cp', 'mv', 'rsync']:
            # Any cp, mv or rsync commands that have both a source and destination in
            # the cloud should be noted as that represents a unique use case that may
            # be better served by the transfer service.
            cloud_uri_pattern = re.compile(r'^(gs|s3)\://')
            cloud_uris = [arg for arg in args if cloud_uri_pattern.search(arg)]
            cloud_uri_dst = cloud_uri_pattern.search(args[-1])

            if len(cloud_uris) > 1 and cloud_uri_dst:
                user_agent += '-CloudToCloud'

    if system_util.InvokedViaCloudSdk():
        user_agent += ' google-cloud-sdk'
        if system_util.CloudSdkVersion():
            user_agent += '/%s' % system_util.CloudSdkVersion()

    return user_agent
Esempio n. 3
0
def _CheckAndHandleCredentialException(e, args):
  # 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 (not boto_util.HasConfiguredCredentials() and not boto.config.get_value(
      'Tests', 'bypass_anonymous_access_warning', False)):
    # The check above allows tests to assert that we get a particular,
    # expected failure, rather than always encountering this error message
    # when there are no configured credentials. This allows tests to
    # simulate a second user without permissions, without actually requiring
    # two separate configured users.
    if system_util.InvokedViaCloudSdk():
      message = '\n'.join(
          textwrap.wrap(
              'You are attempting to access protected data with no configured '
              'credentials. Please visit '
              'https://cloud.google.com/console#/project and sign up for an '
              'account, and then run the "gcloud auth login" command to '
              'configure gsutil to use these credentials.'))
    else:
      message = '\n'.join(
          textwrap.wrap(
              'You are attempting to access protected data with no configured '
              'credentials. Please visit '
              'https://cloud.google.com/console#/project and sign up for an '
              'account, and then run the "gsutil config" command to configure '
              'gsutil to use these credentials.'))
    _OutputAndExit(message=message, exception=e)
  elif (e.reason and
        (e.reason == 'AccountProblem' or e.reason == 'Account disabled.' or
         'account for the specified project has been disabled' in e.reason) and
        ','.join(args).find('gs://') != -1):
    _OutputAndExit('\n'.join(
        textwrap.wrap(_ConstructAccountProblemHelp(e.reason))),
                   exception=e)
Esempio n. 4
0
    def RunCommand(self):
        """Command entry point for the version command."""
        long_form = False
        if self.sub_opts:
            for o, _ in self.sub_opts:
                if o == '-l':
                    long_form = True

        config_paths = ', '.join(GetFriendlyConfigFilePaths())

        shipped_checksum = gslib.CHECKSUM
        try:
            cur_checksum = self._ComputeCodeChecksum()
        except IOError:
            cur_checksum = 'MISSING FILES'
        if shipped_checksum == cur_checksum:
            checksum_ok_str = 'OK'
        else:
            checksum_ok_str = '!= %s' % shipped_checksum

        sys.stdout.write('gsutil version: %s\n' % gslib.VERSION)

        if long_form:

            long_form_output = (
                'checksum: {checksum} ({checksum_ok})\n'
                'boto version: {boto_version}\n'
                'python version: {python_version}\n'
                'OS: {os_version}\n'
                'multiprocessing available: {multiprocessing_available}\n'
                'using cloud sdk: {cloud_sdk}\n'
                'pass cloud sdk credentials to gsutil: {cloud_sdk_credentials}\n'
                'config path(s): {config_paths}\n'
                'gsutil path: {gsutil_path}\n'
                'compiled crcmod: {compiled_crcmod}\n'
                'installed via package manager: {is_package_install}\n'
                'editable install: {is_editable_install}\n')

            sys.stdout.write(
                long_form_output.format(
                    checksum=cur_checksum,
                    checksum_ok=checksum_ok_str,
                    boto_version=boto.__version__,
                    python_version=sys.version.replace('\n', ''),
                    os_version='%s %s' %
                    (platform.system(), platform.release()),
                    multiprocessing_available=(
                        CheckMultiprocessingAvailableAndInit().is_available),
                    cloud_sdk=system_util.InvokedViaCloudSdk(),
                    cloud_sdk_credentials=system_util.
                    CloudSdkCredPassingEnabled(),
                    config_paths=config_paths,
                    gsutil_path=GetCloudSdkGsutilWrapperScriptPath()
                    or gslib.GSUTIL_PATH,
                    compiled_crcmod=UsingCrcmodExtension(crcmod),
                    is_package_install=gslib.IS_PACKAGE_INSTALL,
                    is_editable_install=gslib.IS_EDITABLE_INSTALL,
                ))

        return 0
Esempio n. 5
0
def GetCloudSdkGsutilWrapperScriptPath():
  """If gsutil was invoked via the Cloud SDK, find its gsutil wrapper script.

  Returns:
    (str) The path to the Cloud SDK's gsutil wrapper script, or an empty string
    if gsutil was not invoked via the Cloud SDK or the wrapper script could not
    be found at its expected path.
  """
  # If running via the Cloud SDK, the "real" gsutil script was probably invoked
  # indirectly through gcloud's gsutil wrapper script (the former is at
  # <sdk-root>/platform/gsutil/gsutil, while the latter is located at
  # <sdk-root>/bin/gsutil). We should print the wrapper script's path if it
  # exists and we were invoked via the Cloud SDK.
  gsutil_path = gslib.GSUTIL_PATH
  if system_util.InvokedViaCloudSdk():
    platform_path_suffix = os.path.join('platform', 'gsutil', 'gsutil')
    if gsutil_path.endswith(platform_path_suffix):
      bin_path = os.path.join(
          gsutil_path[0:gsutil_path.rfind(platform_path_suffix)],
          'bin',
          'gsutil',
      )
      if os.path.exists(bin_path):
        return bin_path

  return ''
Esempio n. 6
0
def GetUserAgent(args, metrics_off=True):
    """Using the command arguments return a suffix for the UserAgent string.

  Args:
    args: str[], parsed set of arguments entered in the CLI.
    metrics_off: boolean, whether the MetricsCollector is disabled.

  Returns:
    str, A string value that can be appended to an existing UserAgent.
  """
    user_agent = ' gsutil/%s' % gslib.VERSION
    user_agent += ' (%s)' % sys.platform
    user_agent += ' analytics/%s' % ('disabled' if metrics_off else 'enabled')
    user_agent += ' interactive/%s' % system_util.IsRunningInteractively()

    if len(args) > 0:
        user_agent += ' command/%s' % args[0]

        if len(args) > 2:
            if args[0] in ['cp', 'mv', 'rsync']:
                # Any cp, mv or rsync commands that use daisy chain mode should be noted
                # as that represents a unique use case that may be better served by the
                # storage transfer service.
                try:
                    src = StorageUrlFromString(six.ensure_text(args[-2]))
                    dst = StorageUrlFromString(six.ensure_text(args[-1]))
                    if src.IsCloudUrl() and dst.IsCloudUrl(
                    ) and src.scheme != dst.scheme:
                        user_agent += '-DaisyChain'
                except InvalidUrlError:
                    pass
            elif args[0] == 'rewrite':
                if '-k' in args:
                    # Rewrite encryption key.
                    user_agent += '-k'
                if '-s' in args:
                    # Rewrite storage class.
                    user_agent += '-s'

    if system_util.InvokedViaCloudSdk():
        user_agent += ' google-cloud-sdk'
        if system_util.CloudSdkVersion():
            user_agent += '/%s' % system_util.CloudSdkVersion()

    return user_agent
Esempio n. 7
0
def CheckAndMaybePromptForAnalyticsEnabling():
  """Asks a user to opt-in to data collection if a UUID file does not exist.

  If the user agrees, generates a UUID file. Will not prompt if part of SDK.
  """
  disable_prompt = boto.config.get_value('GSUtil', 'disable_analytics_prompt')
  if (not os.path.exists(_UUID_FILE_PATH) and not disable_prompt and
      not system_util.InvokedViaCloudSdk()):
    enable_analytics = input('\n' + textwrap.fill(
        'gsutil developers rely on user feedback to make improvements to the '
        'tool. Would you like to send anonymous usage statistics to help '
        'improve gsutil? [y/N]') + ' ')

    text_to_write = _DISABLED_TEXT
    if enable_analytics.lower()[0] == 'y':
      text_to_write = uuid.uuid4().hex
    system_util.CreateDirIfNeeded(os.path.dirname(_UUID_FILE_PATH))
    with open(_UUID_FILE_PATH, 'w') as f:
      f.write(text_to_write)
Esempio n. 8
0
  def _CheckAndSetDisabledCache(cls):
    """Sets _disabled_cache based on user opt-in or out."""
    # Disable collection for a test case where no metrics should be collected.
    if os.environ.get('GSUTIL_TEST_ANALYTICS') == '1':
      cls._disabled_cache = True
    # Enable test collector for a subprocess integration test case where we
    # check the log output, which requires a test collector.
    elif os.environ.get('GSUTIL_TEST_ANALYTICS') == '2':
      cls._disabled_cache = False
      cls.StartTestCollector()

    # Non-testing cases involve checking the cloud SDK wrapper and the analytics
    # uuid file.
    elif system_util.InvokedViaCloudSdk():
      cls._disabled_cache = not os.environ.get('GA_CID')
    elif os.path.exists(_UUID_FILE_PATH):
      with open(_UUID_FILE_PATH) as f:
        cls._disabled_cache = (f.read() == _DISABLED_TEXT)
    else:
      cls._disabled_cache = True
Esempio n. 9
0
def GetUserAgent(args, metrics_off=True):
    """Using the command arguments return a suffix for the UserAgent string.

  Args:
    args: str[], parsed set of arguments entered in the CLI.
    metrics_off: boolean, whether the MetricsCollector is disabled.

  Returns:
    str, A string value that can be appended to an existing UserAgent.
  """
    user_agent = ' gsutil/%s' % gslib.VERSION
    user_agent += ' (%s)' % sys.platform
    user_agent += ' analytics/%s ' % ('disabled' if metrics_off else 'enabled')

    if system_util.InvokedViaCloudSdk():
        user_agent += ' google-cloud-sdk'
        if system_util.CloudSdkVersion():
            user_agent += '/%s' % system_util.CloudSdkVersion()

    return user_agent
Esempio n. 10
0
def _RunNamedCommandAndHandleExceptions(command_runner,
                                        command_name,
                                        args=None,
                                        headers=None,
                                        debug_level=0,
                                        trace_token=None,
                                        parallel_operations=False,
                                        perf_trace_token=None,
                                        user_project=None):
  """Runs the command and handles common exceptions."""
  # Note that this method is run at the end of main() and thus has access to
  # all of the modules imported there.
  # pylint: disable=g-import-not-at-top
  try:
    # Catch ^C so we can print a brief message instead of the normal Python
    # stack trace. Register as a final signal handler because this handler kills
    # the main gsutil process (so it must run last).
    RegisterSignalHandler(signal.SIGINT, _HandleControlC, is_final_handler=True)
    # Catch ^\ so we can force a breakpoint in a running gsutil.
    if not system_util.IS_WINDOWS:
      RegisterSignalHandler(signal.SIGQUIT, _HandleSigQuit)

    return command_runner.RunNamedCommand(command_name,
                                          args,
                                          headers,
                                          debug_level,
                                          trace_token,
                                          parallel_operations,
                                          perf_trace_token=perf_trace_token,
                                          collect_analytics=True,
                                          user_project=user_project)
  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?',
          exception=e)
    else:
      _OutputAndExit(message=str(e), exception=e)
  except CommandException as e:
    _HandleCommandException(e)
  except getopt.GetoptError as e:
    _HandleCommandException(CommandException(e.msg))
  except boto.exception.InvalidUriError as e:
    _OutputAndExit(message='InvalidUriError: %s.' % e.message, exception=e)
  except gslib.exception.InvalidUrlError as e:
    _OutputAndExit(message='InvalidUrlError: %s.' % e.message, exception=e)
  except boto.auth_handler.NotReadyToAuthenticate:
    _OutputAndExit(message='NotReadyToAuthenticate', exception=e)
  except OSError as e:
    # In Python 3, IOError (next except) is an alias for OSError
    # Sooo... we need the same logic here
    if (e.errno == errno.EPIPE or
        (system_util.IS_WINDOWS and e.errno == errno.EINVAL) and
        not system_util.IsRunningInteractively()):
      # If we get a pipe error, this just means that the pipe to stdout or
      # stderr is broken. This can happen if the user pipes gsutil to a command
      # that doesn't use the entire output stream. Instead of raising an error,
      # just swallow it up and exit cleanly.
      sys.exit(0)
    else:
      _OutputAndExit(message='OSError: %s.' % e.strerror, exception=e)
  except IOError as e:
    if (e.errno == errno.EPIPE or
        (system_util.IS_WINDOWS and e.errno == errno.EINVAL) and
        not system_util.IsRunningInteractively()):
      # If we get a pipe error, this just means that the pipe to stdout or
      # stderr is broken. This can happen if the user pipes gsutil to a command
      # that doesn't use the entire output stream. Instead of raising an error,
      # just swallow it up and exit cleanly.
      sys.exit(0)
    else:
      raise
  except wildcard_iterator.WildcardException as e:
    _OutputAndExit(message=e.reason, exception=e)
  except ProjectIdException as e:
    _OutputAndExit(
        'You are attempting to perform an operation that requires a '
        'project id, with none configured. Please re-run '
        'gsutil config and make sure to follow the instructions for '
        'finding and entering your default project id.',
        exception=e)
  except BadRequestException as e:
    if e.reason == 'MissingSecurityHeader':
      _CheckAndHandleCredentialException(e, args)
    _OutputAndExit(message=e, exception=e)
  except AccessDeniedException as e:
    _CheckAndHandleCredentialException(e, args)
    _OutputAndExit(message=e, exception=e)
  except ArgumentException as e:
    _OutputAndExit(message=e, exception=e)
  except ServiceException as e:
    _OutputAndExit(message=e, exception=e)
  except oauth2client.client.HttpAccessTokenRefreshError as e:
    if system_util.InvokedViaCloudSdk():
      _OutputAndExit(
          'Your credentials are invalid. '
          'Please run\n$ gcloud auth login',
          exception=e)
    else:
      _OutputAndExit(
          'Your credentials are invalid. For more help, see '
          '"gsutil help creds", or re-run the gsutil config command (see '
          '"gsutil help config").',
          exception=e)
  except apitools_exceptions.HttpError as e:
    # These should usually be retried by the underlying implementation or
    # wrapped by CloudApi ServiceExceptions, but if we do get them,
    # print something useful.
    _OutputAndExit('HttpError: %s, %s' %
                   (getattr(e.response, 'status', ''), e.content or ''),
                   exception=e)
  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.
      _OutputAndExit(
          '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.',
          exception=e)
    elif e.args[0] == errno.ECONNRESET and ' '.join(args).contains('s3://'):
      _OutputAndExit('\n'.join(
          textwrap.wrap(
              'Got a "Connection reset by peer" error. One way this can happen is '
              'when copying data to/from an S3 regional bucket. If you are using a '
              'regional S3 bucket you could try re-running this command using the '
              'regional S3 endpoint, for example '
              's3://s3-<region>.amazonaws.com/your-bucket. For details about this '
              'problem see https://github.com/boto/boto/issues/2207')),
                     exception=e)
    else:
      _HandleUnknownFailure(e)
  except oauth2client.client.FlowExchangeError as e:
    _OutputAndExit('\n%s\n\n' % '\n'.join(
        textwrap.wrap(
            'Failed to retrieve valid credentials (%s). Make sure you selected and '
            'pasted the ENTIRE authorization code (including any numeric prefix '
            "e.g. '4/')." % e)),
                   exception=e)
  except reauth_errors.ReauthSamlLoginRequiredError:
    if system_util.InvokedViaCloudSdk():
      _OutputAndExit('You must re-authenticate with your SAML IdP. '
                     'Please run\n$ gcloud auth login')
    else:
      _OutputAndExit('You must re-authenticate with your SAML IdP. '
                     'Please run\n$ gsutil config')
  except Exception as e:  # pylint: disable=broad-except
    config_paths = ', '.join(boto_util.GetFriendlyConfigFilePaths())
    # 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(s),'
          '\n%s, is correct.' % config_paths,
          exception=e)
    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(s),\n%s, is correct.' %
          config_paths,
          exception=e)
    _HandleUnknownFailure(e)
Esempio n. 11
0
from six.moves import range

from gslib.utils.version_check import check_python_version_support

# Load the gsutil version number and append it to boto.UserAgent so the value is
# set before anything instantiates boto. This has to run after THIRD_PARTY_DIR
# is modified (done in gsutil.py) but before any calls are made that would cause
# boto.s3.Connection to be loaded - otherwise the Connection class would end up
# with a static reference to the pre-modified version of the UserAgent field,
# so boto requests would not include gsutil/version# in the UserAgent string.
import boto
import gslib
from gslib.utils import system_util

boto.UserAgent += ' gsutil/%s (%s)' % (gslib.VERSION, sys.platform)
if system_util.InvokedViaCloudSdk():
    boto.UserAgent += ' google-cloud-sdk'
    if system_util.CloudSdkVersion():
        boto.UserAgent += '/%s' % system_util.CloudSdkVersion()
# pylint: disable=g-import-not-at-top
# This module also imports boto, and will override the UserAgent global variable
# if imported above.
from gslib import metrics
if metrics.MetricsCollector.IsDisabled():
    boto.UserAgent += ' analytics/disabled'
else:
    boto.UserAgent += ' analytics/enabled'

# pylint: disable=g-bad-import-order
import httplib2
import oauth2client
Esempio n. 12
0
    def MaybeCheckForAndOfferSoftwareUpdate(self, command_name, debug):
        """Checks the last time we checked for an update and offers one if needed.

    Offer is made if the time since the last update check is longer
    than the configured threshold offers the user to update gsutil.

    Args:
      command_name: The name of the command being run.
      debug: Debug level to pass in to boto connection (range 0..3).

    Returns:
      True if the user decides to update.
    """
        # Don't try to interact with user if:
        # - gsutil is not connected to a tty (e.g., if being run from cron);
        # - user is running gsutil -q
        # - user is running the config command (which could otherwise attempt to
        #   check for an update for a user running behind a proxy, who has not yet
        #   configured gsutil to go through the proxy; for such users we need the
        #   first connection attempt to be made by the gsutil config command).
        # - user is running the version command (which gets run when using
        #   gsutil -D, which would prevent users with proxy config problems from
        #   sending us gsutil -D output).
        # - user is running the update command (which could otherwise cause an
        #   additional note that an update is available when user is already trying
        #   to perform an update);
        # - user specified gs_host (which could be a non-production different
        #   service instance, in which case credentials won't work for checking
        #   gsutil tarball).
        # - user is using a Cloud SDK install (which should only be updated via
        #   gcloud components update)
        logger = logging.getLogger()
        gs_host = boto.config.get('Credentials', 'gs_host', None)
        gs_host_is_not_default = (gs_host !=
                                  boto.gs.connection.GSConnection.DefaultHost)
        if (not system_util.IsRunningInteractively()
                or command_name in ('config', 'update', 'ver', 'version')
                or not logger.isEnabledFor(logging.INFO)
                or gs_host_is_not_default or system_util.InvokedViaCloudSdk()):
            return False

        software_update_check_period = boto.config.getint(
            'GSUtil', 'software_update_check_period', 30)
        # Setting software_update_check_period to 0 means periodic software
        # update checking is disabled.
        if software_update_check_period == 0:
            return False

        last_checked_for_gsutil_update_timestamp_file = (
            boto_util.GetLastCheckedForGsutilUpdateTimestampFile())

        cur_ts = int(time.time())
        if not os.path.isfile(last_checked_for_gsutil_update_timestamp_file):
            # Set last_checked_ts from date of VERSION file, so if the user installed
            # an old copy of gsutil it will get noticed (and an update offered) the
            # first time they try to run it.
            last_checked_ts = gslib.GetGsutilVersionModifiedTime()
            with open(last_checked_for_gsutil_update_timestamp_file, 'w') as f:
                f.write(str(last_checked_ts))
        else:
            try:
                with open(last_checked_for_gsutil_update_timestamp_file,
                          'r') as f:
                    last_checked_ts = int(f.readline())
            except (TypeError, ValueError):
                return False

        if (cur_ts - last_checked_ts >
                software_update_check_period * SECONDS_PER_DAY):
            # Create a credential-less gsutil API to check for the public
            # update tarball.
            gsutil_api = GcsJsonApi(self.bucket_storage_uri_class,
                                    logger,
                                    DiscardMessagesQueue(),
                                    credentials=NoOpCredentials(),
                                    debug=debug)

            cur_ver = LookUpGsutilVersion(gsutil_api, GSUTIL_PUB_TARBALL)
            with open(last_checked_for_gsutil_update_timestamp_file, 'w') as f:
                f.write(str(cur_ts))
            (g, m) = CompareVersions(cur_ver, gslib.VERSION)
            if m:
                print '\n'.join(
                    textwrap.wrap(
                        'A newer version of gsutil (%s) is available than the version you '
                        'are running (%s). NOTE: This is a major new version, so it is '
                        'strongly recommended that you review the release note details at '
                        '%s before updating to this version, especially if you use gsutil '
                        'in scripts.' %
                        (cur_ver, gslib.VERSION, RELEASE_NOTES_URL)))
                if gslib.IS_PACKAGE_INSTALL:
                    return False
                print
                answer = raw_input('Would you like to update [y/N]? ')
                return answer and answer.lower()[0] == 'y'
            elif g:
                print '\n'.join(
                    textwrap.wrap(
                        'A newer version of gsutil (%s) is available than the version you '
                        'are running (%s). A detailed log of gsutil release changes is '
                        'available at %s if you would like to read them before updating.'
                        % (cur_ver, gslib.VERSION, RELEASE_NOTES_URL)))
                if gslib.IS_PACKAGE_INSTALL:
                    return False
                print
                answer = raw_input('Would you like to update [Y/n]? ')
                return not answer or answer.lower()[0] != 'n'
        return False
Esempio n. 13
0
  def RunCommand(self):
    """Command entry point for the update command."""

    if gslib.IS_PACKAGE_INSTALL:
      raise CommandException(
          'The update command is only available for gsutil installed from a '
          'tarball. If you installed gsutil via another method, use the same '
          'method to update it.')

    if system_util.InvokedViaCloudSdk():
      raise CommandException(
          'The update command is disabled for Cloud SDK installs. Please run '
          '"gcloud components update" to update it. Note: the Cloud SDK '
          'incorporates updates to the underlying tools approximately every 2 '
          'weeks, so if you are attempting to update to a recently created '
          'release / pre-release of gsutil it may not yet be available via '
          'the Cloud SDK.')

    https_validate_certificates = CERTIFICATE_VALIDATION_ENABLED
    if not https_validate_certificates:
      raise CommandException(
          'Your boto configuration has https_validate_certificates = False.\n'
          'The update command cannot be run this way, for security reasons.')

    DisallowUpdateIfDataInGsutilDir()

    force_update = False
    no_prompt = False
    if self.sub_opts:
      for o, unused_a in self.sub_opts:
        if o == '-f':
          force_update = True
        if o == '-n':
          no_prompt = True

    dirs_to_remove = []
    tmp_dir = tempfile.mkdtemp()
    dirs_to_remove.append(tmp_dir)
    old_cwd = os.getcwd()
    os.chdir(tmp_dir)

    if not no_prompt:
      self.logger.info('Checking for software update...')
    if self.args:
      update_from_url_str = self.args[0]
      if not update_from_url_str.endswith('.tar.gz'):
        raise CommandException(
            'The update command only works with tar.gz files.')
      for i, result in enumerate(self.WildcardIterator(update_from_url_str)):
        if i > 0:
          raise CommandException(
              'Invalid update URL. Must name a single .tar.gz file.')
        storage_url = result.storage_url
        if storage_url.IsFileUrl() and not storage_url.IsDirectory():
          if not force_update:
            raise CommandException(
                ('"update" command does not support "file://" URLs without the '
                 '-f option.'))
        elif not (storage_url.IsCloudUrl() and storage_url.IsObject()):
          raise CommandException(
              'Invalid update object URL. Must name a single .tar.gz file.')
    else:
      update_from_url_str = GSUTIL_PUB_TARBALL

    # Try to retrieve version info from tarball metadata; failing that; download
    # the tarball and extract the VERSION file. The version lookup will fail
    # when running the update system test, because it retrieves the tarball from
    # a temp file rather than a cloud URL (files lack the version metadata).
    tarball_version = LookUpGsutilVersion(self.gsutil_api, update_from_url_str)
    if tarball_version:
      tf = None
    else:
      tf = self._FetchAndOpenGsutilTarball(update_from_url_str)
      tf.extractall()
      with open(os.path.join('gsutil', 'VERSION'), 'r') as ver_file:
        tarball_version = ver_file.read().strip()

    if not force_update and gslib.VERSION == tarball_version:
      self._CleanUpUpdateCommand(tf, dirs_to_remove, old_cwd)
      if self.args:
        raise CommandException('You already have %s installed.' %
                               update_from_url_str,
                               informational=True)
      else:
        raise CommandException(
            'You already have the latest gsutil release '
            'installed.',
            informational=True)

    if not no_prompt:
      CheckAndMaybePromptForAnalyticsEnabling()
      (_, major) = CompareVersions(tarball_version, gslib.VERSION)
      if major:
        print(('\n'.join(
            textwrap.wrap(
                'This command will update to the "%s" version of gsutil at %s. '
                'NOTE: This a major new version, so it is strongly recommended '
                'that you review the release note details at %s before updating to '
                'this version, especially if you use gsutil in scripts.' %
                (tarball_version, gslib.GSUTIL_DIR, RELEASE_NOTES_URL)))))
      else:
        print(('This command will update to the "%s" version of\ngsutil at %s' %
               (tarball_version, gslib.GSUTIL_DIR)))
    self._ExplainIfSudoNeeded(tf, dirs_to_remove, old_cwd)

    if no_prompt:
      answer = 'y'
    else:
      answer = input('Proceed? [y/N] ')
    if not answer or answer.lower()[0] != 'y':
      self._CleanUpUpdateCommand(tf, dirs_to_remove, old_cwd)
      raise CommandException('Not running update.', informational=True)

    if not tf:
      tf = self._FetchAndOpenGsutilTarball(update_from_url_str)

    # Ignore keyboard interrupts during the update to reduce the chance someone
    # hitting ^C leaves gsutil in a broken state.
    RegisterSignalHandler(signal.SIGINT, signal.SIG_IGN)

    # gslib.GSUTIL_DIR lists the path where the code should end up (like
    # /usr/local/gsutil), which is one level down from the relative path in the
    # tarball (since the latter creates files in ./gsutil). So, we need to
    # extract at the parent directory level.
    gsutil_bin_parent_dir = os.path.normpath(
        os.path.join(gslib.GSUTIL_DIR, '..'))

    # Extract tarball to a temporary directory in a sibling to GSUTIL_DIR.
    old_dir = tempfile.mkdtemp(dir=gsutil_bin_parent_dir)
    new_dir = tempfile.mkdtemp(dir=gsutil_bin_parent_dir)
    dirs_to_remove.append(old_dir)
    dirs_to_remove.append(new_dir)
    self._EnsureDirsSafeForUpdate(dirs_to_remove)
    try:
      tf.extractall(path=new_dir)
    except Exception as e:
      self._CleanUpUpdateCommand(tf, dirs_to_remove, old_cwd)
      raise CommandException('Update failed: %s.' % e)

    # For enterprise mode (shared/central) installation, users with
    # different user/group than the installation user/group must be
    # able to run gsutil so we need to do some permissions adjustments
    # here. Since enterprise mode is not not supported for Windows
    # users, we can skip this step when running on Windows, which
    # avoids the problem that Windows has no find or xargs command.
    if not system_util.IS_WINDOWS:
      # Make all files and dirs in updated area owner-RW and world-R, and make
      # all directories owner-RWX and world-RX.
      for dirname, subdirs, filenames in os.walk(new_dir):
        for filename in filenames:
          fd = os.open(os.path.join(dirname, filename), os.O_RDONLY)
          os.fchmod(fd,
                    stat.S_IWRITE | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
          os.close(fd)
        for subdir in subdirs:
          fd = os.open(os.path.join(dirname, subdir), os.O_RDONLY)
          os.fchmod(
              fd, stat.S_IRWXU | stat.S_IXGRP | stat.S_IXOTH | stat.S_IRGRP |
              stat.S_IROTH)
          os.close(fd)

      # Make main gsutil script owner-RWX and world-RX.
      fd = os.open(os.path.join(new_dir, 'gsutil', 'gsutil'), os.O_RDONLY)
      os.fchmod(
          fd, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH |
          stat.S_IXOTH)
      os.close(fd)

    # Move old installation aside and new into place.
    os.rename(gslib.GSUTIL_DIR, os.path.join(old_dir, 'old'))
    os.rename(os.path.join(new_dir, 'gsutil'), gslib.GSUTIL_DIR)
    self._CleanUpUpdateCommand(tf, dirs_to_remove, old_cwd)
    RegisterSignalHandler(signal.SIGINT, signal.SIG_DFL)
    self.logger.info('Update complete.')
    return 0
class PubsubV1(base_api.BaseApiClient):
  """Generated client library for service pubsub version v1."""

  MESSAGES_MODULE = messages
  BASE_URL = u'https://pubsub.googleapis.com/'

  _PACKAGE = u'pubsub'
  _SCOPES = [u'https://www.googleapis.com/auth/cloud-platform', u'https://www.googleapis.com/auth/pubsub']
  _VERSION = u'v1'
  _CLIENT_ID = 'nomatter'
  _CLIENT_SECRET = 'nomatter'
  _USER_AGENT = 'apitools gsutil/%s Python/%s (%s)' % (
      gslib.VERSION, platform.python_version(), sys.platform)
  if system_util.InvokedViaCloudSdk():
    _USER_AGENT += ' google-cloud-sdk'
    if system_util.CloudSdkVersion():
      _USER_AGENT += '/%s' % system_util.CloudSdkVersion()
  if MetricsCollector.IsDisabled():
    _USER_AGENT += ' analytics/disabled'
  else:
    _USER_AGENT += ' analytics/enabled'
  _CLIENT_CLASS_NAME = u'PubsubV1'
  _URL_VERSION = u'v1'
  _API_KEY = None

  def __init__(self, url='', credentials=None,
               get_credentials=True, http=None, model=None,
               log_request=False, log_response=False,
               credentials_args=None, default_global_params=None,
               additional_http_headers=None):
    """Create a new pubsub handle."""
    url = url or self.BASE_URL
    super(PubsubV1, self).__init__(
        url, credentials=credentials,
        get_credentials=get_credentials, http=http, model=model,
        log_request=log_request, log_response=log_response,
        credentials_args=credentials_args,
        default_global_params=default_global_params,
        additional_http_headers=additional_http_headers)
    self.projects_snapshots = self.ProjectsSnapshotsService(self)
    self.projects_subscriptions = self.ProjectsSubscriptionsService(self)
    self.projects_topics_subscriptions = self.ProjectsTopicsSubscriptionsService(self)
    self.projects_topics = self.ProjectsTopicsService(self)
    self.projects = self.ProjectsService(self)

  class ProjectsSnapshotsService(base_api.BaseApiService):
    """Service class for the projects_snapshots resource."""

    _NAME = u'projects_snapshots'

    def __init__(self, client):
      super(PubsubV1.ProjectsSnapshotsService, self).__init__(client)
      self._upload_configs = {
          }

    def GetIamPolicy(self, request, global_params=None):
      """Gets the access control policy for a resource.
Returns an empty policy if the resource exists and does not have a policy
set.

      Args:
        request: (PubsubProjectsSnapshotsGetIamPolicyRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Policy) The response message.
      """
      config = self.GetMethodConfig('GetIamPolicy')
      return self._RunMethod(
          config, request, global_params=global_params)

    GetIamPolicy.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/snapshots/{snapshotsId}:getIamPolicy',
        http_method=u'GET',
        method_id=u'pubsub.projects.snapshots.getIamPolicy',
        ordered_params=[u'resource'],
        path_params=[u'resource'],
        query_params=[],
        relative_path=u'v1/{+resource}:getIamPolicy',
        request_field='',
        request_type_name=u'PubsubProjectsSnapshotsGetIamPolicyRequest',
        response_type_name=u'Policy',
        supports_download=False,
    )

    def SetIamPolicy(self, request, global_params=None):
      """Sets the access control policy on the specified resource. Replaces any.
existing policy.

      Args:
        request: (PubsubProjectsSnapshotsSetIamPolicyRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Policy) The response message.
      """
      config = self.GetMethodConfig('SetIamPolicy')
      return self._RunMethod(
          config, request, global_params=global_params)

    SetIamPolicy.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/snapshots/{snapshotsId}:setIamPolicy',
        http_method=u'POST',
        method_id=u'pubsub.projects.snapshots.setIamPolicy',
        ordered_params=[u'resource'],
        path_params=[u'resource'],
        query_params=[],
        relative_path=u'v1/{+resource}:setIamPolicy',
        request_field=u'setIamPolicyRequest',
        request_type_name=u'PubsubProjectsSnapshotsSetIamPolicyRequest',
        response_type_name=u'Policy',
        supports_download=False,
    )

    def TestIamPermissions(self, request, global_params=None):
      """Returns permissions that a caller has on the specified resource.
If the resource does not exist, this will return an empty set of
permissions, not a NOT_FOUND error.

Note: This operation is designed to be used for building permission-aware
UIs and command-line tools, not for authorization checking. This operation
may "fail open" without warning.

      Args:
        request: (PubsubProjectsSnapshotsTestIamPermissionsRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (TestIamPermissionsResponse) The response message.
      """
      config = self.GetMethodConfig('TestIamPermissions')
      return self._RunMethod(
          config, request, global_params=global_params)

    TestIamPermissions.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/snapshots/{snapshotsId}:testIamPermissions',
        http_method=u'POST',
        method_id=u'pubsub.projects.snapshots.testIamPermissions',
        ordered_params=[u'resource'],
        path_params=[u'resource'],
        query_params=[],
        relative_path=u'v1/{+resource}:testIamPermissions',
        request_field=u'testIamPermissionsRequest',
        request_type_name=u'PubsubProjectsSnapshotsTestIamPermissionsRequest',
        response_type_name=u'TestIamPermissionsResponse',
        supports_download=False,
    )

  class ProjectsSubscriptionsService(base_api.BaseApiService):
    """Service class for the projects_subscriptions resource."""

    _NAME = u'projects_subscriptions'

    def __init__(self, client):
      super(PubsubV1.ProjectsSubscriptionsService, self).__init__(client)
      self._upload_configs = {
          }

    def Acknowledge(self, request, global_params=None):
      """Acknowledges the messages associated with the `ack_ids` in the.
`AcknowledgeRequest`. The Pub/Sub system can remove the relevant messages
from the subscription.

Acknowledging a message whose ack deadline has expired may succeed,
but such a message may be redelivered later. Acknowledging a message more
than once will not result in an error.

      Args:
        request: (PubsubProjectsSubscriptionsAcknowledgeRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Empty) The response message.
      """
      config = self.GetMethodConfig('Acknowledge')
      return self._RunMethod(
          config, request, global_params=global_params)

    Acknowledge.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/subscriptions/{subscriptionsId}:acknowledge',
        http_method=u'POST',
        method_id=u'pubsub.projects.subscriptions.acknowledge',
        ordered_params=[u'subscription'],
        path_params=[u'subscription'],
        query_params=[],
        relative_path=u'v1/{+subscription}:acknowledge',
        request_field=u'acknowledgeRequest',
        request_type_name=u'PubsubProjectsSubscriptionsAcknowledgeRequest',
        response_type_name=u'Empty',
        supports_download=False,
    )

    def Create(self, request, global_params=None):
      """Creates a subscription to a given topic.
If the subscription already exists, returns `ALREADY_EXISTS`.
If the corresponding topic doesn't exist, returns `NOT_FOUND`.

If the name is not provided in the request, the server will assign a random
name for this subscription on the same project as the topic, conforming
to the
[resource name format](https://cloud.google.com/pubsub/docs/overview#names).
The generated name is populated in the returned Subscription object.
Note that for REST API requests, you must specify a name in the request.

      Args:
        request: (Subscription) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Subscription) The response message.
      """
      config = self.GetMethodConfig('Create')
      return self._RunMethod(
          config, request, global_params=global_params)

    Create.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/subscriptions/{subscriptionsId}',
        http_method=u'PUT',
        method_id=u'pubsub.projects.subscriptions.create',
        ordered_params=[u'name'],
        path_params=[u'name'],
        query_params=[],
        relative_path=u'v1/{+name}',
        request_field='<request>',
        request_type_name=u'Subscription',
        response_type_name=u'Subscription',
        supports_download=False,
    )

    def Delete(self, request, global_params=None):
      """Deletes an existing subscription. All messages retained in the subscription.
are immediately dropped. Calls to `Pull` after deletion will return
`NOT_FOUND`. After a subscription is deleted, a new one may be created with
the same name, but the new one has no association with the old
subscription or its topic unless the same topic is specified.

      Args:
        request: (PubsubProjectsSubscriptionsDeleteRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Empty) The response message.
      """
      config = self.GetMethodConfig('Delete')
      return self._RunMethod(
          config, request, global_params=global_params)

    Delete.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/subscriptions/{subscriptionsId}',
        http_method=u'DELETE',
        method_id=u'pubsub.projects.subscriptions.delete',
        ordered_params=[u'subscription'],
        path_params=[u'subscription'],
        query_params=[],
        relative_path=u'v1/{+subscription}',
        request_field='',
        request_type_name=u'PubsubProjectsSubscriptionsDeleteRequest',
        response_type_name=u'Empty',
        supports_download=False,
    )

    def Get(self, request, global_params=None):
      """Gets the configuration details of a subscription.

      Args:
        request: (PubsubProjectsSubscriptionsGetRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Subscription) The response message.
      """
      config = self.GetMethodConfig('Get')
      return self._RunMethod(
          config, request, global_params=global_params)

    Get.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/subscriptions/{subscriptionsId}',
        http_method=u'GET',
        method_id=u'pubsub.projects.subscriptions.get',
        ordered_params=[u'subscription'],
        path_params=[u'subscription'],
        query_params=[],
        relative_path=u'v1/{+subscription}',
        request_field='',
        request_type_name=u'PubsubProjectsSubscriptionsGetRequest',
        response_type_name=u'Subscription',
        supports_download=False,
    )

    def GetIamPolicy(self, request, global_params=None):
      """Gets the access control policy for a resource.
Returns an empty policy if the resource exists and does not have a policy
set.

      Args:
        request: (PubsubProjectsSubscriptionsGetIamPolicyRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Policy) The response message.
      """
      config = self.GetMethodConfig('GetIamPolicy')
      return self._RunMethod(
          config, request, global_params=global_params)

    GetIamPolicy.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/subscriptions/{subscriptionsId}:getIamPolicy',
        http_method=u'GET',
        method_id=u'pubsub.projects.subscriptions.getIamPolicy',
        ordered_params=[u'resource'],
        path_params=[u'resource'],
        query_params=[],
        relative_path=u'v1/{+resource}:getIamPolicy',
        request_field='',
        request_type_name=u'PubsubProjectsSubscriptionsGetIamPolicyRequest',
        response_type_name=u'Policy',
        supports_download=False,
    )

    def List(self, request, global_params=None):
      """Lists matching subscriptions.

      Args:
        request: (PubsubProjectsSubscriptionsListRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (ListSubscriptionsResponse) The response message.
      """
      config = self.GetMethodConfig('List')
      return self._RunMethod(
          config, request, global_params=global_params)

    List.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/subscriptions',
        http_method=u'GET',
        method_id=u'pubsub.projects.subscriptions.list',
        ordered_params=[u'project'],
        path_params=[u'project'],
        query_params=[u'pageSize', u'pageToken'],
        relative_path=u'v1/{+project}/subscriptions',
        request_field='',
        request_type_name=u'PubsubProjectsSubscriptionsListRequest',
        response_type_name=u'ListSubscriptionsResponse',
        supports_download=False,
    )

    def ModifyAckDeadline(self, request, global_params=None):
      """Modifies the ack deadline for a specific message. This method is useful.
to indicate that more time is needed to process a message by the
subscriber, or to make the message available for redelivery if the
processing was interrupted. Note that this does not modify the
subscription-level `ackDeadlineSeconds` used for subsequent messages.

      Args:
        request: (PubsubProjectsSubscriptionsModifyAckDeadlineRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Empty) The response message.
      """
      config = self.GetMethodConfig('ModifyAckDeadline')
      return self._RunMethod(
          config, request, global_params=global_params)

    ModifyAckDeadline.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/subscriptions/{subscriptionsId}:modifyAckDeadline',
        http_method=u'POST',
        method_id=u'pubsub.projects.subscriptions.modifyAckDeadline',
        ordered_params=[u'subscription'],
        path_params=[u'subscription'],
        query_params=[],
        relative_path=u'v1/{+subscription}:modifyAckDeadline',
        request_field=u'modifyAckDeadlineRequest',
        request_type_name=u'PubsubProjectsSubscriptionsModifyAckDeadlineRequest',
        response_type_name=u'Empty',
        supports_download=False,
    )

    def ModifyPushConfig(self, request, global_params=None):
      """Modifies the `PushConfig` for a specified subscription.

This may be used to change a push subscription to a pull one (signified by
an empty `PushConfig`) or vice versa, or change the endpoint URL and other
attributes of a push subscription. Messages will accumulate for delivery
continuously through the call regardless of changes to the `PushConfig`.

      Args:
        request: (PubsubProjectsSubscriptionsModifyPushConfigRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Empty) The response message.
      """
      config = self.GetMethodConfig('ModifyPushConfig')
      return self._RunMethod(
          config, request, global_params=global_params)

    ModifyPushConfig.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/subscriptions/{subscriptionsId}:modifyPushConfig',
        http_method=u'POST',
        method_id=u'pubsub.projects.subscriptions.modifyPushConfig',
        ordered_params=[u'subscription'],
        path_params=[u'subscription'],
        query_params=[],
        relative_path=u'v1/{+subscription}:modifyPushConfig',
        request_field=u'modifyPushConfigRequest',
        request_type_name=u'PubsubProjectsSubscriptionsModifyPushConfigRequest',
        response_type_name=u'Empty',
        supports_download=False,
    )

    def Pull(self, request, global_params=None):
      """Pulls messages from the server. Returns an empty list if there are no.
messages available in the backlog. The server may return `UNAVAILABLE` if
there are too many concurrent pull requests pending for the given
subscription.

      Args:
        request: (PubsubProjectsSubscriptionsPullRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (PullResponse) The response message.
      """
      config = self.GetMethodConfig('Pull')
      return self._RunMethod(
          config, request, global_params=global_params)

    Pull.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/subscriptions/{subscriptionsId}:pull',
        http_method=u'POST',
        method_id=u'pubsub.projects.subscriptions.pull',
        ordered_params=[u'subscription'],
        path_params=[u'subscription'],
        query_params=[],
        relative_path=u'v1/{+subscription}:pull',
        request_field=u'pullRequest',
        request_type_name=u'PubsubProjectsSubscriptionsPullRequest',
        response_type_name=u'PullResponse',
        supports_download=False,
    )

    def SetIamPolicy(self, request, global_params=None):
      """Sets the access control policy on the specified resource. Replaces any.
existing policy.

      Args:
        request: (PubsubProjectsSubscriptionsSetIamPolicyRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Policy) The response message.
      """
      config = self.GetMethodConfig('SetIamPolicy')
      return self._RunMethod(
          config, request, global_params=global_params)

    SetIamPolicy.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/subscriptions/{subscriptionsId}:setIamPolicy',
        http_method=u'POST',
        method_id=u'pubsub.projects.subscriptions.setIamPolicy',
        ordered_params=[u'resource'],
        path_params=[u'resource'],
        query_params=[],
        relative_path=u'v1/{+resource}:setIamPolicy',
        request_field=u'setIamPolicyRequest',
        request_type_name=u'PubsubProjectsSubscriptionsSetIamPolicyRequest',
        response_type_name=u'Policy',
        supports_download=False,
    )

    def TestIamPermissions(self, request, global_params=None):
      """Returns permissions that a caller has on the specified resource.
If the resource does not exist, this will return an empty set of
permissions, not a NOT_FOUND error.

Note: This operation is designed to be used for building permission-aware
UIs and command-line tools, not for authorization checking. This operation
may "fail open" without warning.

      Args:
        request: (PubsubProjectsSubscriptionsTestIamPermissionsRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (TestIamPermissionsResponse) The response message.
      """
      config = self.GetMethodConfig('TestIamPermissions')
      return self._RunMethod(
          config, request, global_params=global_params)

    TestIamPermissions.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/subscriptions/{subscriptionsId}:testIamPermissions',
        http_method=u'POST',
        method_id=u'pubsub.projects.subscriptions.testIamPermissions',
        ordered_params=[u'resource'],
        path_params=[u'resource'],
        query_params=[],
        relative_path=u'v1/{+resource}:testIamPermissions',
        request_field=u'testIamPermissionsRequest',
        request_type_name=u'PubsubProjectsSubscriptionsTestIamPermissionsRequest',
        response_type_name=u'TestIamPermissionsResponse',
        supports_download=False,
    )

  class ProjectsTopicsSubscriptionsService(base_api.BaseApiService):
    """Service class for the projects_topics_subscriptions resource."""

    _NAME = u'projects_topics_subscriptions'

    def __init__(self, client):
      super(PubsubV1.ProjectsTopicsSubscriptionsService, self).__init__(client)
      self._upload_configs = {
          }

    def List(self, request, global_params=None):
      """Lists the name of the subscriptions for this topic.

      Args:
        request: (PubsubProjectsTopicsSubscriptionsListRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (ListTopicSubscriptionsResponse) The response message.
      """
      config = self.GetMethodConfig('List')
      return self._RunMethod(
          config, request, global_params=global_params)

    List.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/topics/{topicsId}/subscriptions',
        http_method=u'GET',
        method_id=u'pubsub.projects.topics.subscriptions.list',
        ordered_params=[u'topic'],
        path_params=[u'topic'],
        query_params=[u'pageSize', u'pageToken'],
        relative_path=u'v1/{+topic}/subscriptions',
        request_field='',
        request_type_name=u'PubsubProjectsTopicsSubscriptionsListRequest',
        response_type_name=u'ListTopicSubscriptionsResponse',
        supports_download=False,
    )

  class ProjectsTopicsService(base_api.BaseApiService):
    """Service class for the projects_topics resource."""

    _NAME = u'projects_topics'

    def __init__(self, client):
      super(PubsubV1.ProjectsTopicsService, self).__init__(client)
      self._upload_configs = {
          }

    def Create(self, request, global_params=None):
      """Creates the given topic with the given name.

      Args:
        request: (Topic) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Topic) The response message.
      """
      config = self.GetMethodConfig('Create')
      return self._RunMethod(
          config, request, global_params=global_params)

    Create.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/topics/{topicsId}',
        http_method=u'PUT',
        method_id=u'pubsub.projects.topics.create',
        ordered_params=[u'name'],
        path_params=[u'name'],
        query_params=[],
        relative_path=u'v1/{+name}',
        request_field='<request>',
        request_type_name=u'Topic',
        response_type_name=u'Topic',
        supports_download=False,
    )

    def Delete(self, request, global_params=None):
      """Deletes the topic with the given name. Returns `NOT_FOUND` if the topic.
does not exist. After a topic is deleted, a new topic may be created with
the same name; this is an entirely new topic with none of the old
configuration or subscriptions. Existing subscriptions to this topic are
not deleted, but their `topic` field is set to `_deleted-topic_`.

      Args:
        request: (PubsubProjectsTopicsDeleteRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Empty) The response message.
      """
      config = self.GetMethodConfig('Delete')
      return self._RunMethod(
          config, request, global_params=global_params)

    Delete.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/topics/{topicsId}',
        http_method=u'DELETE',
        method_id=u'pubsub.projects.topics.delete',
        ordered_params=[u'topic'],
        path_params=[u'topic'],
        query_params=[],
        relative_path=u'v1/{+topic}',
        request_field='',
        request_type_name=u'PubsubProjectsTopicsDeleteRequest',
        response_type_name=u'Empty',
        supports_download=False,
    )

    def Get(self, request, global_params=None):
      """Gets the configuration of a topic.

      Args:
        request: (PubsubProjectsTopicsGetRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Topic) The response message.
      """
      config = self.GetMethodConfig('Get')
      return self._RunMethod(
          config, request, global_params=global_params)

    Get.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/topics/{topicsId}',
        http_method=u'GET',
        method_id=u'pubsub.projects.topics.get',
        ordered_params=[u'topic'],
        path_params=[u'topic'],
        query_params=[],
        relative_path=u'v1/{+topic}',
        request_field='',
        request_type_name=u'PubsubProjectsTopicsGetRequest',
        response_type_name=u'Topic',
        supports_download=False,
    )

    def GetIamPolicy(self, request, global_params=None):
      """Gets the access control policy for a resource.
Returns an empty policy if the resource exists and does not have a policy
set.

      Args:
        request: (PubsubProjectsTopicsGetIamPolicyRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Policy) The response message.
      """
      config = self.GetMethodConfig('GetIamPolicy')
      return self._RunMethod(
          config, request, global_params=global_params)

    GetIamPolicy.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/topics/{topicsId}:getIamPolicy',
        http_method=u'GET',
        method_id=u'pubsub.projects.topics.getIamPolicy',
        ordered_params=[u'resource'],
        path_params=[u'resource'],
        query_params=[],
        relative_path=u'v1/{+resource}:getIamPolicy',
        request_field='',
        request_type_name=u'PubsubProjectsTopicsGetIamPolicyRequest',
        response_type_name=u'Policy',
        supports_download=False,
    )

    def List(self, request, global_params=None):
      """Lists matching topics.

      Args:
        request: (PubsubProjectsTopicsListRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (ListTopicsResponse) The response message.
      """
      config = self.GetMethodConfig('List')
      return self._RunMethod(
          config, request, global_params=global_params)

    List.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/topics',
        http_method=u'GET',
        method_id=u'pubsub.projects.topics.list',
        ordered_params=[u'project'],
        path_params=[u'project'],
        query_params=[u'pageSize', u'pageToken'],
        relative_path=u'v1/{+project}/topics',
        request_field='',
        request_type_name=u'PubsubProjectsTopicsListRequest',
        response_type_name=u'ListTopicsResponse',
        supports_download=False,
    )

    def Publish(self, request, global_params=None):
      """Adds one or more messages to the topic. Returns `NOT_FOUND` if the topic.
does not exist. The message payload must not be empty; it must contain
 either a non-empty data field, or at least one attribute.

      Args:
        request: (PubsubProjectsTopicsPublishRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (PublishResponse) The response message.
      """
      config = self.GetMethodConfig('Publish')
      return self._RunMethod(
          config, request, global_params=global_params)

    Publish.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/topics/{topicsId}:publish',
        http_method=u'POST',
        method_id=u'pubsub.projects.topics.publish',
        ordered_params=[u'topic'],
        path_params=[u'topic'],
        query_params=[],
        relative_path=u'v1/{+topic}:publish',
        request_field=u'publishRequest',
        request_type_name=u'PubsubProjectsTopicsPublishRequest',
        response_type_name=u'PublishResponse',
        supports_download=False,
    )

    def SetIamPolicy(self, request, global_params=None):
      """Sets the access control policy on the specified resource. Replaces any.
existing policy.

      Args:
        request: (PubsubProjectsTopicsSetIamPolicyRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Policy) The response message.
      """
      config = self.GetMethodConfig('SetIamPolicy')
      return self._RunMethod(
          config, request, global_params=global_params)

    SetIamPolicy.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/topics/{topicsId}:setIamPolicy',
        http_method=u'POST',
        method_id=u'pubsub.projects.topics.setIamPolicy',
        ordered_params=[u'resource'],
        path_params=[u'resource'],
        query_params=[],
        relative_path=u'v1/{+resource}:setIamPolicy',
        request_field=u'setIamPolicyRequest',
        request_type_name=u'PubsubProjectsTopicsSetIamPolicyRequest',
        response_type_name=u'Policy',
        supports_download=False,
    )

    def TestIamPermissions(self, request, global_params=None):
      """Returns permissions that a caller has on the specified resource.
If the resource does not exist, this will return an empty set of
permissions, not a NOT_FOUND error.

Note: This operation is designed to be used for building permission-aware
UIs and command-line tools, not for authorization checking. This operation
may "fail open" without warning.

      Args:
        request: (PubsubProjectsTopicsTestIamPermissionsRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (TestIamPermissionsResponse) The response message.
      """
      config = self.GetMethodConfig('TestIamPermissions')
      return self._RunMethod(
          config, request, global_params=global_params)

    TestIamPermissions.method_config = lambda: base_api.ApiMethodInfo(
        flat_path=u'v1/projects/{projectsId}/topics/{topicsId}:testIamPermissions',
        http_method=u'POST',
        method_id=u'pubsub.projects.topics.testIamPermissions',
        ordered_params=[u'resource'],
        path_params=[u'resource'],
        query_params=[],
        relative_path=u'v1/{+resource}:testIamPermissions',
        request_field=u'testIamPermissionsRequest',
        request_type_name=u'PubsubProjectsTopicsTestIamPermissionsRequest',
        response_type_name=u'TestIamPermissionsResponse',
        supports_download=False,
    )

  class ProjectsService(base_api.BaseApiService):
    """Service class for the projects resource."""

    _NAME = u'projects'

    def __init__(self, client):
      super(PubsubV1.ProjectsService, self).__init__(client)
      self._upload_configs = {
          }
Esempio n. 15
0
class IamcredentialsV1(base_api.BaseApiClient):
    """Generated client library for service iamcredentials version v1."""

    MESSAGES_MODULE = messages
    BASE_URL = u'https://iamcredentials.googleapis.com/'

    _PACKAGE = u'iamcredentials'
    _SCOPES = [u'https://www.googleapis.com/auth/cloud-platform']
    _VERSION = u'v1'
    _CLIENT_ID = 'nomatter'
    _CLIENT_SECRET = 'nomatter'
    _USER_AGENT = 'apitools gsutil/%s Python/%s (%s)' % (
        gslib.VERSION, platform.python_version(), sys.platform)
    if system_util.InvokedViaCloudSdk():
        _USER_AGENT += ' google-cloud-sdk'
        if system_util.CloudSdkVersion():
            _USER_AGENT += '/%s' % system_util.CloudSdkVersion()
    if MetricsCollector.IsDisabled():
        _USER_AGENT += ' analytics/disabled'
    else:
        _USER_AGENT += ' analytics/enabled'
    _CLIENT_CLASS_NAME = u'IamcredentialsV1'
    _URL_VERSION = u'v1'
    _API_KEY = None

    def __init__(self,
                 url='',
                 credentials=None,
                 get_credentials=True,
                 http=None,
                 model=None,
                 log_request=False,
                 log_response=False,
                 credentials_args=None,
                 default_global_params=None,
                 additional_http_headers=None,
                 response_encoding=None):
        """Create a new iamcredentials handle."""
        url = url or self.BASE_URL
        super(IamcredentialsV1,
              self).__init__(url,
                             credentials=credentials,
                             get_credentials=get_credentials,
                             http=http,
                             model=model,
                             log_request=log_request,
                             log_response=log_response,
                             credentials_args=credentials_args,
                             default_global_params=default_global_params,
                             additional_http_headers=additional_http_headers,
                             response_encoding=response_encoding)
        self.projects_serviceAccounts = self.ProjectsServiceAccountsService(
            self)
        self.projects = self.ProjectsService(self)

    class ProjectsServiceAccountsService(base_api.BaseApiService):
        """Service class for the projects_serviceAccounts resource."""

        _NAME = u'projects_serviceAccounts'

        def __init__(self, client):
            super(IamcredentialsV1.ProjectsServiceAccountsService,
                  self).__init__(client)
            self._upload_configs = {}

        def GenerateAccessToken(self, request, global_params=None):
            """Generates an OAuth 2.0 access token for a service account.

      Args:
        request: (IamcredentialsProjectsServiceAccountsGenerateAccessTokenRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (GenerateAccessTokenResponse) The response message.
      """
            config = self.GetMethodConfig('GenerateAccessToken')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        GenerateAccessToken.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/serviceAccounts/{serviceAccountsId}:generateAccessToken',
            http_method=u'POST',
            method_id=
            u'iamcredentials.projects.serviceAccounts.generateAccessToken',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}:generateAccessToken',
            request_field=u'generateAccessTokenRequest',
            request_type_name=
            u'IamcredentialsProjectsServiceAccountsGenerateAccessTokenRequest',
            response_type_name=u'GenerateAccessTokenResponse',
            supports_download=False,
        )

        def GenerateIdToken(self, request, global_params=None):
            r"""Generates an OpenID Connect ID token for a service account.

      Args:
        request: (IamcredentialsProjectsServiceAccountsGenerateIdTokenRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (GenerateIdTokenResponse) The response message.
      """
            config = self.GetMethodConfig('GenerateIdToken')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        GenerateIdToken.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/serviceAccounts/{serviceAccountsId}:generateIdToken',
            http_method=u'POST',
            method_id=
            u'iamcredentials.projects.serviceAccounts.generateIdToken',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}:generateIdToken',
            request_field=u'generateIdTokenRequest',
            request_type_name=
            u'IamcredentialsProjectsServiceAccountsGenerateIdTokenRequest',
            response_type_name=u'GenerateIdTokenResponse',
            supports_download=False,
        )

        def SignBlob(self, request, global_params=None):
            r"""Signs a blob using a service account's system-managed private key.

      Args:
        request: (IamcredentialsProjectsServiceAccountsSignBlobRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (SignBlobResponse) The response message.
      """
            config = self.GetMethodConfig('SignBlob')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        SignBlob.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/serviceAccounts/{serviceAccountsId}:signBlob',
            http_method=u'POST',
            method_id=u'iamcredentials.projects.serviceAccounts.signBlob',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}:signBlob',
            request_field=u'signBlobRequest',
            request_type_name=
            u'IamcredentialsProjectsServiceAccountsSignBlobRequest',
            response_type_name=u'SignBlobResponse',
            supports_download=False,
        )

        def SignJwt(self, request, global_params=None):
            r"""Signs a JWT using a service account's system-managed private key.

      Args:
        request: (IamcredentialsProjectsServiceAccountsSignJwtRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (SignJwtResponse) The response message.
      """
            config = self.GetMethodConfig('SignJwt')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        SignJwt.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/serviceAccounts/{serviceAccountsId}:signJwt',
            http_method=u'POST',
            method_id=u'iamcredentials.projects.serviceAccounts.signJwt',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}:signJwt',
            request_field=u'signJwtRequest',
            request_type_name=
            u'IamcredentialsProjectsServiceAccountsSignJwtRequest',
            response_type_name=u'SignJwtResponse',
            supports_download=False,
        )

    class ProjectsService(base_api.BaseApiService):
        """Service class for the projects resource."""

        _NAME = u'projects'

        def __init__(self, client):
            super(IamcredentialsV1.ProjectsService, self).__init__(client)
            self._upload_configs = {}
Esempio n. 16
0
 def _IsPackageOrCloudSDKInstall(self):
     # Update should not trigger for package installs or Cloud SDK installs.
     return gslib.IS_PACKAGE_INSTALL or system_util.InvokedViaCloudSdk()
Esempio n. 17
0
def _CheckAndGetCredentials(logger):
    """Returns credentials from the configuration file, if any are present.

  Args:
    logger: logging.Logger instance for outputting messages.

  Returns:
    OAuth2Credentials object if any valid ones are found, otherwise None.
  """
    configured_cred_types = []
    failed_cred_type = None
    try:
        if _HasOauth2UserAccountCreds():
            configured_cred_types.append(CredTypes.OAUTH2_USER_ACCOUNT)
        if _HasOauth2ServiceAccountCreds():
            configured_cred_types.append(CredTypes.OAUTH2_SERVICE_ACCOUNT)
        if len(configured_cred_types) > 1:
            # We only allow one set of configured credentials. Otherwise, we're
            # choosing one arbitrarily, which can be very confusing to the user
            # (e.g., if only one is authorized to perform some action) and can
            # also mask errors.
            # Because boto merges config files, GCE credentials show up by default
            # for GCE VMs. We don't want to fail when a user creates a boto file
            # with their own credentials, so in this case we'll use the OAuth2
            # user credentials.
            raise CommandException((
                'You have multiple types of configured credentials (%s), which is '
                'not supported. One common way this happens is if you run gsutil '
                'config to create credentials and later run gcloud auth, and '
                'create a second set of credentials. Your boto config path is: '
                '%s. For more help, see "gsutil help creds".') %
                                   (configured_cred_types,
                                    GetFriendlyConfigFilePaths()))

        failed_cred_type = CredTypes.OAUTH2_USER_ACCOUNT
        user_creds = _GetOauth2UserAccountCredentials()
        failed_cred_type = CredTypes.OAUTH2_SERVICE_ACCOUNT
        service_account_creds = _GetOauth2ServiceAccountCredentials()
        failed_cred_type = CredTypes.EXTERNAL_ACCOUNT
        external_account_creds = _GetExternalAccountCredentials()
        failed_cred_type = CredTypes.GCE
        gce_creds = _GetGceCreds()
        failed_cred_type = CredTypes.DEVSHELL
        devshell_creds = _GetDevshellCreds()

        creds = user_creds or service_account_creds or gce_creds or external_account_creds or devshell_creds

        # Use one of the above credential types to impersonate, if configured.
        if _HasImpersonateServiceAccount() and creds:
            failed_cred_type = CredTypes.IMPERSONATION
            return _GetImpersonationCredentials(creds, logger)
        else:
            return creds

    except Exception as e:
        # If we didn't actually try to authenticate because there were multiple
        # types of configured credentials, don't emit this warning.
        if failed_cred_type:
            if logger.isEnabledFor(logging.DEBUG):
                logger.debug(traceback.format_exc())
            # If impersonation fails, show the user the actual error, since we handle
            # errors in iamcredentials_api.
            if failed_cred_type == CredTypes.IMPERSONATION:
                raise e
            elif system_util.InvokedViaCloudSdk():
                logger.warn(
                    'Your "%s" credentials are invalid. Please run\n'
                    '  $ gcloud auth login', failed_cred_type)
            else:
                logger.warn(
                    'Your "%s" credentials are invalid. For more help, see '
                    '"gsutil help creds", or re-run the gsutil config command (see '
                    '"gsutil help config").', failed_cred_type)

        # If there's any set of configured credentials, we'll fail if they're
        # invalid, rather than silently falling back to anonymous config (as
        # boto does). That approach leads to much confusion if users don't
        # realize their credentials are invalid.
        raise
Esempio n. 18
0
  def test_update(self):
    """Tests that the update command works or raises proper exceptions."""
    if system_util.InvokedViaCloudSdk():
      stderr = self.RunGsUtil(['update'],
                              stdin='n',
                              return_stderr=True,
                              expected_status=1)
      self.assertIn('update command is disabled for Cloud SDK', stderr)
      return

    if gslib.IS_PACKAGE_INSTALL:
      # The update command is not present when installed via package manager.
      stderr = self.RunGsUtil(['update'], return_stderr=True, expected_status=1)
      self.assertIn('Invalid command', stderr)
      return

    # Create two temp directories, one of which we will run 'gsutil update' in
    # to pull the changes from the other.
    tmpdir_src = self.CreateTempDir()
    tmpdir_dst = self.CreateTempDir()

    # Copy gsutil to both source and destination directories.
    gsutil_src = os.path.join(tmpdir_src, 'gsutil')
    gsutil_dst = os.path.join(tmpdir_dst, 'gsutil')
    # Path when executing from tmpdir (Windows doesn't support in-place rename)
    gsutil_relative_dst = os.path.join('gsutil', 'gsutil')

    ignore_callable = shutil.ignore_patterns(
        '.git*',
        '*.pyc',
        '*.pyo',
        '__pycache__',
    )
    shutil.copytree(GSUTIL_DIR, gsutil_src, ignore=ignore_callable)
    # Copy specific files rather than all of GSUTIL_DIR so we don't pick up temp
    # working files left in top-level directory by gsutil developers (like tags,
    # .git*, .pyc files, etc.)
    os.makedirs(gsutil_dst)
    for comp in os.listdir(GSUTIL_DIR):
      if ('.git' not in comp and
          '__pycache__' not in comp and
           not comp.endswith('.pyc') and
           not comp.endswith('.pyo')):  # yapf: disable
        cp_src_path = os.path.join(GSUTIL_DIR, comp)
        cp_dst_path = os.path.join(gsutil_dst, comp)
        if os.path.isdir(cp_src_path):
          shutil.copytree(cp_src_path, cp_dst_path, ignore=ignore_callable)
        else:
          shutil.copyfile(cp_src_path, cp_dst_path)

    # Create a fake version number in the source so we can verify it in the
    # destination.
    expected_version = '17.25'
    src_version_file = os.path.join(gsutil_src, 'VERSION')
    self.assertTrue(os.path.exists(src_version_file))
    with open(src_version_file, 'w') as f:
      f.write(expected_version)

    # Create a tarball out of the source directory and copy it to a bucket.
    src_tarball = os.path.join(tmpdir_src, 'gsutil.test.tar.gz')

    normpath = os.path.normpath
    try:
      # We monkey patch os.path.normpath here because the tarfile module
      # normalizes the ./gsutil path, but the update command expects the tar
      # file to be prefixed with . This preserves the ./gsutil path.
      os.path.normpath = lambda fname: fname
      tar = tarfile.open(src_tarball, 'w:gz')
      tar.add(gsutil_src, arcname='./gsutil')
      tar.close()
    finally:
      os.path.normpath = normpath

    prefix = [sys.executable] if sys.executable else []

    # Run with an invalid gs:// URI.
    p = subprocess.Popen(prefix + ['gsutil', 'update', 'gs://pub'],
                         cwd=gsutil_dst,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    (_, stderr) = p.communicate()
    p.stdout.close()
    p.stderr.close()
    self.assertEqual(p.returncode, 1)
    self.assertIn(b'update command only works with tar.gz', stderr)

    # Run with non-existent gs:// URI.
    p = subprocess.Popen(prefix +
                         ['gsutil', 'update', 'gs://pub/Jdjh38)(;.tar.gz'],
                         cwd=gsutil_dst,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    (_, stderr) = p.communicate()
    p.stdout.close()
    p.stderr.close()
    self.assertEqual(p.returncode, 1)
    self.assertIn(b'NotFoundException', stderr)

    # Run with file:// URI wihout -f option.
    p = subprocess.Popen(
        prefix + ['gsutil', 'update', suri(src_tarball)],
        cwd=gsutil_dst,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
    (_, stderr) = p.communicate()
    p.stdout.close()
    p.stderr.close()
    self.assertEqual(p.returncode, 1)
    self.assertIn(b'command does not support', stderr)

    # Run with a file present that was not distributed with gsutil.
    with open(os.path.join(gsutil_dst, 'userdata.txt'), 'w') as fp:
      fp.write('important data\n')
    p = subprocess.Popen(
        prefix +
        ['gsutil', 'update', '-f', suri(src_tarball)],
        cwd=gsutil_dst,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        stdin=subprocess.PIPE)
    (_, stderr) = p.communicate()
    p.stdout.close()
    p.stderr.close()
    # Clean up before next test, and before assertions so failure doesn't leave
    # this file around.
    os.unlink(os.path.join(gsutil_dst, 'userdata.txt'))
    self.assertEqual(p.returncode, 1)
    # Additional check for Windows since it has \r\n and string may have just \n
    os_ls = os.linesep.encode(UTF8)
    if os_ls in stderr:
      stderr = stderr.replace(os_ls, b' ')
    elif b'\n' in stderr:
      stderr = stderr.replace(b'\n', b' ')
    self.assertIn(
        b'The update command cannot run with user data in the gsutil directory',
        stderr)

    # Determine whether we'll need to decline the analytics prompt.
    analytics_prompt = not (os.path.exists(_UUID_FILE_PATH) or
                            boto.config.get_value('GSUtil',
                                                  'disable_analytics_prompt'))

    update_input = b'n\r\ny\r\n' if analytics_prompt else b'y\r\n'

    # Now do the real update, which should succeed.
    p = subprocess.Popen(
        prefix + [gsutil_relative_dst, 'update', '-f',
                  suri(src_tarball)],
        cwd=tmpdir_dst,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        stdin=subprocess.PIPE)
    (_, stderr) = p.communicate(input=update_input)
    p.stdout.close()
    p.stderr.close()
    self.assertEqual(
        p.returncode,
        0,
        msg=('Non-zero return code (%d) from gsutil update. stderr = \n%s' %
             (p.returncode, stderr.decode(UTF8))))

    # Verify that version file was updated.
    dst_version_file = os.path.join(tmpdir_dst, 'gsutil', 'VERSION')
    with open(dst_version_file, 'r') as f:
      self.assertEqual(f.read(), expected_version)

    # If the analytics prompt was given, that means we disabled analytics. We
    # should reset to the default by deleting the UUID file.
    if analytics_prompt:
      os.unlink(_UUID_FILE_PATH)
Esempio n. 19
0
class CloudkmsV1(base_api.BaseApiClient):
    """Generated client library for service cloudkms version v1."""

    MESSAGES_MODULE = messages
    BASE_URL = u'https://cloudkms.googleapis.com/'

    _PACKAGE = u'cloudkms'
    _SCOPES = [u'https://www.googleapis.com/auth/cloud-platform']
    _VERSION = u'v1'
    _CLIENT_ID = 'nomatter'
    _CLIENT_SECRET = 'nomatter'
    _USER_AGENT = 'apitools gsutil/%s Python/%s (%s)' % (
        gslib.VERSION, platform.python_version(), sys.platform)
    if system_util.InvokedViaCloudSdk():
        _USER_AGENT += ' google-cloud-sdk'
        if system_util.CloudSdkVersion():
            _USER_AGENT += '/%s' % system_util.CloudSdkVersion()
    if MetricsCollector.IsDisabled():
        _USER_AGENT += ' analytics/disabled'
    else:
        _USER_AGENT += ' analytics/enabled'
    _CLIENT_CLASS_NAME = u'CloudkmsV1'
    _URL_VERSION = u'v1'
    _API_KEY = None

    def __init__(self,
                 url='',
                 credentials=None,
                 get_credentials=True,
                 http=None,
                 model=None,
                 log_request=False,
                 log_response=False,
                 credentials_args=None,
                 default_global_params=None,
                 additional_http_headers=None):
        """Create a new cloudkms handle."""
        url = url or self.BASE_URL
        super(CloudkmsV1,
              self).__init__(url,
                             credentials=credentials,
                             get_credentials=get_credentials,
                             http=http,
                             model=model,
                             log_request=log_request,
                             log_response=log_response,
                             credentials_args=credentials_args,
                             default_global_params=default_global_params,
                             additional_http_headers=additional_http_headers)
        self.projects_locations_keyRings_cryptoKeys_cryptoKeyVersions = self.ProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsService(
            self)
        self.projects_locations_keyRings_cryptoKeys = self.ProjectsLocationsKeyRingsCryptoKeysService(
            self)
        self.projects_locations_keyRings = self.ProjectsLocationsKeyRingsService(
            self)
        self.projects_locations = self.ProjectsLocationsService(self)
        self.projects = self.ProjectsService(self)

    class ProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsService(
            base_api.BaseApiService):
        """Service class for the projects_locations_keyRings_cryptoKeys_cryptoKeyVersions resource."""

        _NAME = u'projects_locations_keyRings_cryptoKeys_cryptoKeyVersions'

        def __init__(self, client):
            super(
                CloudkmsV1.
                ProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsService,
                self).__init__(client)
            self._upload_configs = {}

        def Create(self, request, global_params=None):
            """Create a new CryptoKeyVersion in a CryptoKey.

The server will assign the next sequential id. If unset,
state will be set to
ENABLED.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsCreateRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (CryptoKeyVersion) The response message.
      """
            config = self.GetMethodConfig('Create')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Create.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}/cryptoKeyVersions',
            http_method=u'POST',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.cryptoKeyVersions.create',
            ordered_params=[u'parent'],
            path_params=[u'parent'],
            query_params=[],
            relative_path=u'v1/{+parent}/cryptoKeyVersions',
            request_field=u'cryptoKeyVersion',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsCreateRequest',
            response_type_name=u'CryptoKeyVersion',
            supports_download=False,
        )

        def Destroy(self, request, global_params=None):
            """Schedule a CryptoKeyVersion for destruction.

Upon calling this method, CryptoKeyVersion.state will be set to
DESTROY_SCHEDULED
and destroy_time will be set to a time 24
hours in the future, at which point the state
will be changed to
DESTROYED, and the key
material will be irrevocably destroyed.

Before the destroy_time is reached,
RestoreCryptoKeyVersion may be called to reverse the process.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsDestroyRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (CryptoKeyVersion) The response message.
      """
            config = self.GetMethodConfig('Destroy')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Destroy.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}/cryptoKeyVersions/{cryptoKeyVersionsId}:destroy',
            http_method=u'POST',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.cryptoKeyVersions.destroy',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}:destroy',
            request_field=u'destroyCryptoKeyVersionRequest',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsDestroyRequest',
            response_type_name=u'CryptoKeyVersion',
            supports_download=False,
        )

        def Get(self, request, global_params=None):
            """Returns metadata for a given CryptoKeyVersion.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsGetRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (CryptoKeyVersion) The response message.
      """
            config = self.GetMethodConfig('Get')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Get.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}/cryptoKeyVersions/{cryptoKeyVersionsId}',
            http_method=u'GET',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.cryptoKeyVersions.get',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}',
            request_field='',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsGetRequest',
            response_type_name=u'CryptoKeyVersion',
            supports_download=False,
        )

        def List(self, request, global_params=None):
            """Lists CryptoKeyVersions.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsListRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (ListCryptoKeyVersionsResponse) The response message.
      """
            config = self.GetMethodConfig('List')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        List.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}/cryptoKeyVersions',
            http_method=u'GET',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.cryptoKeyVersions.list',
            ordered_params=[u'parent'],
            path_params=[u'parent'],
            query_params=[u'pageSize', u'pageToken'],
            relative_path=u'v1/{+parent}/cryptoKeyVersions',
            request_field='',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsListRequest',
            response_type_name=u'ListCryptoKeyVersionsResponse',
            supports_download=False,
        )

        def Patch(self, request, global_params=None):
            """Update a CryptoKeyVersion's metadata.

state may be changed between
ENABLED and
DISABLED using this
method. See DestroyCryptoKeyVersion and RestoreCryptoKeyVersion to
move between other states.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsPatchRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (CryptoKeyVersion) The response message.
      """
            config = self.GetMethodConfig('Patch')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Patch.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}/cryptoKeyVersions/{cryptoKeyVersionsId}',
            http_method=u'PATCH',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.cryptoKeyVersions.patch',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[u'updateMask'],
            relative_path=u'v1/{+name}',
            request_field=u'cryptoKeyVersion',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsPatchRequest',
            response_type_name=u'CryptoKeyVersion',
            supports_download=False,
        )

        def Restore(self, request, global_params=None):
            """Restore a CryptoKeyVersion in the.
DESTROY_SCHEDULED,
state.

Upon restoration of the CryptoKeyVersion, state
will be set to DISABLED,
and destroy_time will be cleared.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsRestoreRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (CryptoKeyVersion) The response message.
      """
            config = self.GetMethodConfig('Restore')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Restore.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}/cryptoKeyVersions/{cryptoKeyVersionsId}:restore',
            http_method=u'POST',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.cryptoKeyVersions.restore',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}:restore',
            request_field=u'restoreCryptoKeyVersionRequest',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsRestoreRequest',
            response_type_name=u'CryptoKeyVersion',
            supports_download=False,
        )

    class ProjectsLocationsKeyRingsCryptoKeysService(base_api.BaseApiService):
        """Service class for the projects_locations_keyRings_cryptoKeys resource."""

        _NAME = u'projects_locations_keyRings_cryptoKeys'

        def __init__(self, client):
            super(CloudkmsV1.ProjectsLocationsKeyRingsCryptoKeysService,
                  self).__init__(client)
            self._upload_configs = {}

        def Create(self, request, global_params=None):
            """Create a new CryptoKey within a KeyRing.

CryptoKey.purpose is required.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysCreateRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (CryptoKey) The response message.
      """
            config = self.GetMethodConfig('Create')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Create.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys',
            http_method=u'POST',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.create',
            ordered_params=[u'parent'],
            path_params=[u'parent'],
            query_params=[u'cryptoKeyId'],
            relative_path=u'v1/{+parent}/cryptoKeys',
            request_field=u'cryptoKey',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysCreateRequest',
            response_type_name=u'CryptoKey',
            supports_download=False,
        )

        def Decrypt(self, request, global_params=None):
            """Decrypts data that was protected by Encrypt.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysDecryptRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (DecryptResponse) The response message.
      """
            config = self.GetMethodConfig('Decrypt')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Decrypt.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}:decrypt',
            http_method=u'POST',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.decrypt',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}:decrypt',
            request_field=u'decryptRequest',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysDecryptRequest',
            response_type_name=u'DecryptResponse',
            supports_download=False,
        )

        def Encrypt(self, request, global_params=None):
            """Encrypts data, so that it can only be recovered by a call to Decrypt.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysEncryptRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (EncryptResponse) The response message.
      """
            config = self.GetMethodConfig('Encrypt')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Encrypt.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}:encrypt',
            http_method=u'POST',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.encrypt',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}:encrypt',
            request_field=u'encryptRequest',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysEncryptRequest',
            response_type_name=u'EncryptResponse',
            supports_download=False,
        )

        def Get(self, request, global_params=None):
            """Returns metadata for a given CryptoKey, as well as its.
primary CryptoKeyVersion.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysGetRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (CryptoKey) The response message.
      """
            config = self.GetMethodConfig('Get')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Get.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}',
            http_method=u'GET',
            method_id=u'cloudkms.projects.locations.keyRings.cryptoKeys.get',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}',
            request_field='',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysGetRequest',
            response_type_name=u'CryptoKey',
            supports_download=False,
        )

        def GetIamPolicy(self, request, global_params=None):
            """Gets the access control policy for a resource.
Returns an empty policy if the resource exists and does not have a policy
set.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysGetIamPolicyRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Policy) The response message.
      """
            config = self.GetMethodConfig('GetIamPolicy')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        GetIamPolicy.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}:getIamPolicy',
            http_method=u'GET',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.getIamPolicy',
            ordered_params=[u'resource'],
            path_params=[u'resource'],
            query_params=[],
            relative_path=u'v1/{+resource}:getIamPolicy',
            request_field='',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysGetIamPolicyRequest',
            response_type_name=u'Policy',
            supports_download=False,
        )

        def List(self, request, global_params=None):
            """Lists CryptoKeys.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysListRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (ListCryptoKeysResponse) The response message.
      """
            config = self.GetMethodConfig('List')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        List.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys',
            http_method=u'GET',
            method_id=u'cloudkms.projects.locations.keyRings.cryptoKeys.list',
            ordered_params=[u'parent'],
            path_params=[u'parent'],
            query_params=[u'pageSize', u'pageToken'],
            relative_path=u'v1/{+parent}/cryptoKeys',
            request_field='',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysListRequest',
            response_type_name=u'ListCryptoKeysResponse',
            supports_download=False,
        )

        def Patch(self, request, global_params=None):
            """Update a CryptoKey.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysPatchRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (CryptoKey) The response message.
      """
            config = self.GetMethodConfig('Patch')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Patch.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}',
            http_method=u'PATCH',
            method_id=u'cloudkms.projects.locations.keyRings.cryptoKeys.patch',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[u'updateMask'],
            relative_path=u'v1/{+name}',
            request_field=u'cryptoKey',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysPatchRequest',
            response_type_name=u'CryptoKey',
            supports_download=False,
        )

        def SetIamPolicy(self, request, global_params=None):
            """Sets the access control policy on the specified resource. Replaces any.
existing policy.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysSetIamPolicyRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Policy) The response message.
      """
            config = self.GetMethodConfig('SetIamPolicy')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        SetIamPolicy.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}:setIamPolicy',
            http_method=u'POST',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.setIamPolicy',
            ordered_params=[u'resource'],
            path_params=[u'resource'],
            query_params=[],
            relative_path=u'v1/{+resource}:setIamPolicy',
            request_field=u'setIamPolicyRequest',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysSetIamPolicyRequest',
            response_type_name=u'Policy',
            supports_download=False,
        )

        def TestIamPermissions(self, request, global_params=None):
            """Returns permissions that a caller has on the specified resource.
If the resource does not exist, this will return an empty set of
permissions, not a NOT_FOUND error.

Note: This operation is designed to be used for building permission-aware
UIs and command-line tools, not for authorization checking. This operation
may "fail open" without warning.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysTestIamPermissionsRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (TestIamPermissionsResponse) The response message.
      """
            config = self.GetMethodConfig('TestIamPermissions')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        TestIamPermissions.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}:testIamPermissions',
            http_method=u'POST',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.testIamPermissions',
            ordered_params=[u'resource'],
            path_params=[u'resource'],
            query_params=[],
            relative_path=u'v1/{+resource}:testIamPermissions',
            request_field=u'testIamPermissionsRequest',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysTestIamPermissionsRequest',
            response_type_name=u'TestIamPermissionsResponse',
            supports_download=False,
        )

        def UpdatePrimaryVersion(self, request, global_params=None):
            """Update the version of a CryptoKey that will be used in Encrypt.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCryptoKeysUpdatePrimaryVersionRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (CryptoKey) The response message.
      """
            config = self.GetMethodConfig('UpdatePrimaryVersion')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        UpdatePrimaryVersion.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}/cryptoKeys/{cryptoKeysId}:updatePrimaryVersion',
            http_method=u'POST',
            method_id=
            u'cloudkms.projects.locations.keyRings.cryptoKeys.updatePrimaryVersion',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}:updatePrimaryVersion',
            request_field=u'updateCryptoKeyPrimaryVersionRequest',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCryptoKeysUpdatePrimaryVersionRequest',
            response_type_name=u'CryptoKey',
            supports_download=False,
        )

    class ProjectsLocationsKeyRingsService(base_api.BaseApiService):
        """Service class for the projects_locations_keyRings resource."""

        _NAME = u'projects_locations_keyRings'

        def __init__(self, client):
            super(CloudkmsV1.ProjectsLocationsKeyRingsService,
                  self).__init__(client)
            self._upload_configs = {}

        def Create(self, request, global_params=None):
            """Create a new KeyRing in a given Project and Location.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsCreateRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (KeyRing) The response message.
      """
            config = self.GetMethodConfig('Create')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Create.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings',
            http_method=u'POST',
            method_id=u'cloudkms.projects.locations.keyRings.create',
            ordered_params=[u'parent'],
            path_params=[u'parent'],
            query_params=[u'keyRingId'],
            relative_path=u'v1/{+parent}/keyRings',
            request_field=u'keyRing',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsCreateRequest',
            response_type_name=u'KeyRing',
            supports_download=False,
        )

        def Get(self, request, global_params=None):
            """Returns metadata for a given KeyRing.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsGetRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (KeyRing) The response message.
      """
            config = self.GetMethodConfig('Get')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Get.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}',
            http_method=u'GET',
            method_id=u'cloudkms.projects.locations.keyRings.get',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}',
            request_field='',
            request_type_name=u'CloudkmsProjectsLocationsKeyRingsGetRequest',
            response_type_name=u'KeyRing',
            supports_download=False,
        )

        def GetIamPolicy(self, request, global_params=None):
            """Gets the access control policy for a resource.
Returns an empty policy if the resource exists and does not have a policy
set.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsGetIamPolicyRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Policy) The response message.
      """
            config = self.GetMethodConfig('GetIamPolicy')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        GetIamPolicy.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}:getIamPolicy',
            http_method=u'GET',
            method_id=u'cloudkms.projects.locations.keyRings.getIamPolicy',
            ordered_params=[u'resource'],
            path_params=[u'resource'],
            query_params=[],
            relative_path=u'v1/{+resource}:getIamPolicy',
            request_field='',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsGetIamPolicyRequest',
            response_type_name=u'Policy',
            supports_download=False,
        )

        def List(self, request, global_params=None):
            """Lists KeyRings.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsListRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (ListKeyRingsResponse) The response message.
      """
            config = self.GetMethodConfig('List')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        List.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings',
            http_method=u'GET',
            method_id=u'cloudkms.projects.locations.keyRings.list',
            ordered_params=[u'parent'],
            path_params=[u'parent'],
            query_params=[u'pageSize', u'pageToken'],
            relative_path=u'v1/{+parent}/keyRings',
            request_field='',
            request_type_name=u'CloudkmsProjectsLocationsKeyRingsListRequest',
            response_type_name=u'ListKeyRingsResponse',
            supports_download=False,
        )

        def SetIamPolicy(self, request, global_params=None):
            """Sets the access control policy on the specified resource. Replaces any.
existing policy.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsSetIamPolicyRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Policy) The response message.
      """
            config = self.GetMethodConfig('SetIamPolicy')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        SetIamPolicy.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}:setIamPolicy',
            http_method=u'POST',
            method_id=u'cloudkms.projects.locations.keyRings.setIamPolicy',
            ordered_params=[u'resource'],
            path_params=[u'resource'],
            query_params=[],
            relative_path=u'v1/{+resource}:setIamPolicy',
            request_field=u'setIamPolicyRequest',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsSetIamPolicyRequest',
            response_type_name=u'Policy',
            supports_download=False,
        )

        def TestIamPermissions(self, request, global_params=None):
            """Returns permissions that a caller has on the specified resource.
If the resource does not exist, this will return an empty set of
permissions, not a NOT_FOUND error.

Note: This operation is designed to be used for building permission-aware
UIs and command-line tools, not for authorization checking. This operation
may "fail open" without warning.

      Args:
        request: (CloudkmsProjectsLocationsKeyRingsTestIamPermissionsRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (TestIamPermissionsResponse) The response message.
      """
            config = self.GetMethodConfig('TestIamPermissions')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        TestIamPermissions.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=
            u'v1/projects/{projectsId}/locations/{locationsId}/keyRings/{keyRingsId}:testIamPermissions',
            http_method=u'POST',
            method_id=
            u'cloudkms.projects.locations.keyRings.testIamPermissions',
            ordered_params=[u'resource'],
            path_params=[u'resource'],
            query_params=[],
            relative_path=u'v1/{+resource}:testIamPermissions',
            request_field=u'testIamPermissionsRequest',
            request_type_name=
            u'CloudkmsProjectsLocationsKeyRingsTestIamPermissionsRequest',
            response_type_name=u'TestIamPermissionsResponse',
            supports_download=False,
        )

    class ProjectsLocationsService(base_api.BaseApiService):
        """Service class for the projects_locations resource."""

        _NAME = u'projects_locations'

        def __init__(self, client):
            super(CloudkmsV1.ProjectsLocationsService, self).__init__(client)
            self._upload_configs = {}

        def Get(self, request, global_params=None):
            """Get information about a location.

      Args:
        request: (CloudkmsProjectsLocationsGetRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (Location) The response message.
      """
            config = self.GetMethodConfig('Get')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        Get.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=u'v1/projects/{projectsId}/locations/{locationsId}',
            http_method=u'GET',
            method_id=u'cloudkms.projects.locations.get',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[],
            relative_path=u'v1/{+name}',
            request_field='',
            request_type_name=u'CloudkmsProjectsLocationsGetRequest',
            response_type_name=u'Location',
            supports_download=False,
        )

        def List(self, request, global_params=None):
            """Lists information about the supported locations for this service.

      Args:
        request: (CloudkmsProjectsLocationsListRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments
      Returns:
        (ListLocationsResponse) The response message.
      """
            config = self.GetMethodConfig('List')
            return self._RunMethod(config,
                                   request,
                                   global_params=global_params)

        List.method_config = lambda: base_api.ApiMethodInfo(
            flat_path=u'v1/projects/{projectsId}/locations',
            http_method=u'GET',
            method_id=u'cloudkms.projects.locations.list',
            ordered_params=[u'name'],
            path_params=[u'name'],
            query_params=[u'filter', u'pageSize', u'pageToken'],
            relative_path=u'v1/{+name}/locations',
            request_field='',
            request_type_name=u'CloudkmsProjectsLocationsListRequest',
            response_type_name=u'ListLocationsResponse',
            supports_download=False,
        )

    class ProjectsService(base_api.BaseApiService):
        """Service class for the projects resource."""

        _NAME = u'projects'

        def __init__(self, client):
            super(CloudkmsV1.ProjectsService, self).__init__(client)
            self._upload_configs = {}