Obtén un módulo personalizado de Estadísticas del estado de la seguridad

Código de muestra para obtener un módulo personalizado de las estadísticas del estado de la seguridad

Muestra de código

Java

Para autenticarte en Security Command Center, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

import com.google.cloud.securitycentermanagement.v1.GetSecurityHealthAnalyticsCustomModuleRequest;
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
import com.google.cloud.securitycentermanagement.v1.SecurityHealthAnalyticsCustomModule;
import java.io.IOException;

public class GetSecurityHealthAnalyticsCustomModule {

  public static void main(String[] args) throws IOException {
    // https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.securityHealthAnalyticsCustomModules/get
    // TODO: Developer should replace project_id with a real project ID before running this code
    String projectId = "project_id";

    String customModuleId = "custom_module_id";

    getSecurityHealthAnalyticsCustomModule(projectId, customModuleId);
  }

  public static SecurityHealthAnalyticsCustomModule getSecurityHealthAnalyticsCustomModule(
      String projectId, String customModuleId) throws IOException {

    // Initialize client that will be used to send requests. This client only needs
    // to be created
    // once, and can be reused for multiple requests.
    try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {

      String name =
          String.format(
              "projects/%s/locations/global/securityHealthAnalyticsCustomModules/%s",
              projectId, customModuleId);

      GetSecurityHealthAnalyticsCustomModuleRequest request =
          GetSecurityHealthAnalyticsCustomModuleRequest.newBuilder().setName(name).build();

      SecurityHealthAnalyticsCustomModule response =
          client.getSecurityHealthAnalyticsCustomModule(request);

      return response;
    }
  }
}

Node.js

Para autenticarte en Security Command Center, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

// npm install '@google-cloud/securitycentermanagement'
const {
  SecurityCenterManagementClient,
} = require('@google-cloud/securitycentermanagement');

const client = new SecurityCenterManagementClient();

/*
 * Required. Resource name of security health analytics module.
 *     Its format is
 *    `organizations/[organization_id]/locations/[location_id]/securityHealthAnalyticsCustomModules/[custom_module]`
 *    `folders/[folder_id]/locations/[location_id]/securityHealthAnalyticsCustomModules/[custom_module]`
 *    `projects/[project_id]/locations/[location_id]/securityHealthAnalyticsCustomModules/[custom_module]`
 */
const name = `organizations/${organizationId}/locations/${locationId}/securityHealthAnalyticsCustomModules/${customModuleId}`;

async function getSecurityHealthAnalyticsCustomModule() {
  const [response] = await client.getSecurityHealthAnalyticsCustomModule({
    name: name,
  });
  console.log(
    'Security Health Analytics Custom Module get succeeded: ',
    response
  );
}

getSecurityHealthAnalyticsCustomModule();

Python

Para autenticarte en Security Command Center, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

def get_security_health_analytics_custom_module(parent: str, module_id: str):
    """
    Retrieves a Security Health Analytics custom module.
    Args:
        parent: Use any one of the following options:
                - organizations/{organization_id}/locations/{location_id}
                - folders/{folder_id}/locations/{location_id}
                - projects/{project_id}/locations/{location_id}
    Returns:
        The retrieved Security Health Analytics custom module.
    Raises:
        NotFound: If the specified custom module does not exist.
    """
    client = securitycentermanagement_v1.SecurityCenterManagementClient()

    try:
        request = securitycentermanagement_v1.GetSecurityHealthAnalyticsCustomModuleRequest(
            name=f"{parent}/securityHealthAnalyticsCustomModules/{module_id}",
        )

        response = client.get_security_health_analytics_custom_module(request=request)
        print(f"Retrieved Security Health Analytics Custom Module: {response.name}")
        return response
    except NotFound as e:
        print(f"Custom Module not found: {response.name}")
        raise e

¿Qué sigue?

Para buscar y filtrar muestras de código para otros productos de Google Cloud , consulta el navegador de muestras deGoogle Cloud .