예제 #1
0
def _add_secret_to_service_account(client, sa_config, product_type, sa_email):
    """Adds new secret to service account.

  Args:
    client: An api_tools client.
    sa_config: A ServiceAccountConfig.
    product_type: events_constants.Product enum.
    sa_email: String of the targeted service account email.
  """
    control_plane_namespace = (
        events_constants.ControlPlaneNamespaceFromProductType(product_type))

    secret_ref = resources.REGISTRY.Parse(
        sa_config.secret_name,
        params={'namespacesId': control_plane_namespace},
        collection='run.api.v1.namespaces.secrets',
        api_version='v1')

    service_account_ref = resources.REGISTRY.Parse(
        sa_email,
        params={'projectsId': '-'},
        collection=core_iam_util.SERVICE_ACCOUNTS_COLLECTION)

    prompt_if_can_prompt(
        'This will create a new key for the service account [{}].'.format(
            sa_email))
    _, key_ref = client.CreateOrReplaceServiceAccountSecret(
        secret_ref, service_account_ref)
    log.status.Print('Added key [{}] to cluster for [{}].'.format(
        key_ref.Name(), sa_email))
예제 #2
0
 def IsClusterInitialized(self, product_type):
   """Returns whether the cluster has been initialized for eventing."""
   control_plane_namespace = (
       events_constants.ControlPlaneNamespaceFromProductType(product_type))
   configmap_obj = self._GetConfigMap(
       _ConfigMapRef(control_plane_namespace, _CONFIG_GCP_AUTH_NAME))
   if configmap_obj is None:
     return False
   return configmap_obj.annotations.get(
       _CLUSTER_INITIALIZED_ANNOTATION) == 'true'
예제 #3
0
    def CreateOrReplaceSourcesSecret(self, namespace_ref, product_type):
        """Create or replace the namespaces' sources secret.

    Retrieves default sources secret 'google-cloud-sources-key' from
    cloud-run-events and copies into secret 'google-cloud-key' into target
    namespace.

    Args:
      namespace_ref: googlecloudsdk.core.resources.Resource, namespace resource
      product_type: Enum, specifies which namespace to target.

    Returns:
      None
    """
        control_plane_namespace = (
            events_constants.ControlPlaneNamespaceFromProductType(product_type)
        )

        messages = self._core_client.MESSAGES_MODULE
        default_secret_full_name = 'namespaces/{}/secrets/{}'.format(
            control_plane_namespace, _DEFAULT_SOURCES_KEY)
        secret_ref = resources.REGISTRY.Parse(
            SOURCES_KEY,
            params={'namespacesId': namespace_ref.Name()},
            collection=_SECRET_COLLECTION,
            api_version='v1')

        # Retrieve default sources secret.
        try:
            request = messages.AnthoseventsApiV1NamespacesSecretsGetRequest(
                name=default_secret_full_name)
            response = self._core_client.api_v1_namespaces_secrets.Get(request)
        except api_exceptions.HttpNotFoundError:
            raise exceptions.SecretNotFound(
                'Secret [{}] not found in namespace [{}].'.format(
                    _DEFAULT_SOURCES_KEY, control_plane_namespace))

        existing_secret_obj = secret.Secret(response, messages)

        secret_obj = secret.Secret.New(self._core_client,
                                       secret_ref.Parent().Name())
        secret_obj.name = secret_ref.Name()
        secret_obj.data['key.json'] = existing_secret_obj.data['key.json']

        try:
            # Create secret or replace if already exists.
            request = messages.AnthoseventsApiV1NamespacesSecretsCreateRequest(
                secret=secret_obj.Message(),
                parent=secret_ref.Parent().RelativeName())
            self._core_client.api_v1_namespaces_secrets.Create(request)
        except api_exceptions.HttpConflictError:
            request = messages.AnthoseventsApiV1NamespacesSecretsReplaceSecretRequest(
                secret=secret_obj.Message(), name=secret_ref.RelativeName())
            response = self._core_client.api_v1_namespaces_secrets.ReplaceSecret(
                request)
예제 #4
0
  def MarkClusterInitialized(self, cluster_defaults, product_type):
    """Marks the cluster as initialized for eventing.

    This creates or updates a ConfigMap which involves adding an annotation
    and setting some default configuration for eventing to use.
    Args:
      cluster_defaults: Dictionary with secrets or workload identity options.
      product_type: An enum denoting the eventing product type.
    """
    control_plane_namespace = (
        events_constants.ControlPlaneNamespaceFromProductType(product_type))
    configmap_obj = self._GetConfigMap(
        _ConfigMapRef(control_plane_namespace, _CONFIG_GCP_AUTH_NAME))
    if configmap_obj is None:
      configmap_obj = configmap.ConfigMap.New(self._core_client,
                                              control_plane_namespace)
      configmap_obj.name = _CONFIG_GCP_AUTH_NAME
      self._PopulateDefaultAuthConfig(configmap_obj, cluster_defaults)
      self._CreateConfigMap(configmap_obj)
    else:
      self._PopulateDefaultAuthConfig(configmap_obj, cluster_defaults)
      self._ReplaceConfigMap(configmap_obj)