Exemple #1
0
    def _AuthorizeProject(self, project_id, kms_key):
        """Authorizes a project's service account to be used with a KMS key.

    Authorizes the Cloud Storage-owned service account for project_id to be used
    with kms_key.

    Args:
      project_id: (str) Project id string (not number).
      kms_key: (str) Fully qualified resource name for the KMS key.

    Returns:
      (str, bool) A 2-tuple consisting of:
      1) The email address for the service account associated with the project,
         which is authorized to encrypt/decrypt with the specified key.
      2) A bool value - True if we had to grant the service account permission
         to encrypt/decrypt with the given key; False if the required permission
         was already present.
    """
        # Request the Cloud Storage-owned service account for project_id, creating
        # it if it does not exist.
        service_account = self.gsutil_api.GetProjectServiceAccount(
            project_id, provider='gs').email_address

        kms_api = KmsApi(logger=self.logger)
        self.logger.debug('Getting IAM policy for %s', kms_key)
        try:
            policy = kms_api.GetKeyIamPolicy(kms_key)
            self.logger.debug('Current policy is %s', policy)

            # Check if the required binding is already present; if not, add it and
            # update the key's IAM policy.
            added_new_binding = False
            binding = Binding(
                role='roles/cloudkms.cryptoKeyEncrypterDecrypter',
                members=['serviceAccount:%s' % service_account])
            if binding not in policy.bindings:
                policy.bindings.append(binding)
                kms_api.SetKeyIamPolicy(kms_key, policy)
                added_new_binding = True
            return (service_account, added_new_binding)
        except AccessDeniedException:
            if self.warn_on_key_authorize_failure:
                text_util.print_to_fd('\n'.join(
                    textwrap.wrap(
                        'Warning: Unable to check the IAM policy for the specified '
                        'encryption key. Check that your Cloud Platform project\'s '
                        'service account has the '
                        '"cloudkms.cryptoKeyEncrypterDecrypter" role for the '
                        'specified key. Without this role, you may not be able to '
                        'encrypt or decrypt objects using the key which will '
                        'prevent you from uploading or downloading objects.')))
                return (service_account, False)
            else:
                raise
Exemple #2
0
  def _AuthorizeProject(self, project_id, kms_key):
    """Authorizes a project's service account to be used with a KMS key.

    Authorizes the GCS-owned service account for project_id to be used with
    kms_key.

    Args:
      project_id: (str) Project id string (not number).
      kms_key: (str) Fully qualified resource name for the KMS key.

    Returns:
      (str, bool) A 2-tuple consisting of:
      1) The email address for the service account associated with the project,
         which is authorized to encrypt/decrypt with the specified key.
      2) A bool value - True if we had to grant the service account permission
         to encrypt/decrypt with the given key; False if the required permission
         was already present.
    """
    # Request the GCS-owned service account for project_id, creating it
    # if it does not exist.
    service_account = self.gsutil_api.GetProjectServiceAccount(
        project_id, provider='gs').email_address

    kms_api = KmsApi(logger=self.logger)
    self.logger.debug('Getting IAM policy for %s', kms_key)
    policy = kms_api.GetKeyIamPolicy(kms_key)
    self.logger.debug('Current policy is %s', policy)

    # Check if the required binding is already present; if not, add it and
    # update the key's IAM policy.
    added_new_binding = False
    binding = Binding(
        role='roles/cloudkms.cryptoKeyEncrypterDecrypter',
        members=['serviceAccount:%s' % service_account])
    if binding not in policy.bindings:
      policy.bindings.append(binding)
      kms_api.SetKeyIamPolicy(kms_key, policy)
      added_new_binding = True
    return (service_account, added_new_binding)
Exemple #3
0
  def setUp(self):
    """Creates base configuration for integration tests."""
    super(GsUtilIntegrationTestCase, self).setUp()
    self.bucket_uris = []

    # Set up API version and project ID handler.
    self.api_version = boto.config.get_value(
        'GSUtil', 'default_api_version', '1')

    # Instantiate a JSON API for use by the current integration test.
    self.json_api = GcsJsonApi(BucketStorageUri, logging.getLogger(),
                               DiscardMessagesQueue(), 'gs')
    self.xml_api = BotoTranslation(BucketStorageUri, logging.getLogger(),
                                   DiscardMessagesQueue, self.default_provider)
    self.kms_api = KmsApi()

    self.multiregional_buckets = util.USE_MULTIREGIONAL_BUCKETS

    if util.RUN_S3_TESTS:
      self.nonexistent_bucket_name = (
          'nonexistentbucket-asf801rj3r9as90mfnnkjxpo02')