Gérer les secrets dans la configuration des métriques de l'agent Ops
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Pour configurer certaines intégrations tierces, vous devez fournir des secrets, tels que des mots de passe, pour les récepteurs de métriques de l'agent Ops. Par défaut, ces secrets sont stockés en texte brut dans le fichier config.yaml de l'agent. Ces secrets sont inclus dans les journaux système écrits par l'agent et transmis à Cloud Logging, ce qui les expose au-delà de la machine virtuelle (VM) sur laquelle l'agent Ops est exécuté.
À partir de la version 2.57.0 de l'agent Ops, vous pouvez utiliser un fournisseur OpenTelemetry intégré à Secret Manager pour éliminer les secrets en texte brut dans vos fichiers de configuration.
Un fournisseur est un composant de configuration OpenTelemetry, analogue aux composants de récepteur et de processeur. Chaque fournisseur a un type, et chaque type de fournisseur mappe un identifiant spécifique dans la configuration à une valeur.
Le fournisseur googlesecretmanager mappe les identifiants Secret Manager aux secrets, tels que les mots de passe, les jetons et les clés API, que vous avez stockés dans Secret Manager. L'utilisation du fournisseur googlesecretmanager offre les avantages suivants :
Sécurité renforcée : vos fichiers de configuration ne contiennent pas d'informations sensibles, comme des mots de passe. Les secrets réels sont stockés dans Secret Manager, un service conçu spécifiquement pour stocker, accéder et gérer les données sensibles de manière sécurisée.
Réduction du risque d'exposition : Secret Manager récupère les secrets lors de l'initialisation de l'Agent Ops, ce qui empêche l'enregistrement accidentel de secrets en texte brut dans les journaux.
Vous ne pouvez utiliser googlesecretmanager que dans la configuration de la collecte de métriques dans les configurations personnalisées de l'agent Ops. N'utilisez pas le fournisseur pour remplacer les secrets dans la configuration de la collecte des journaux.
Avant de commencer
Pour utiliser le fournisseur googlesecretmanager, vous devez activer l'API Secret Manager et autoriser l'accès à l'API, comme décrit dans les étapes suivantes :
Installez la Google Cloud CLI.
Une fois que la Google Cloud CLI est installée, initialisez-la en exécutant la commande suivante :
Définissez le projet par défaut pour Google Cloud CLI :
gcloud config set project PROJECT_ID
Avant d'exécuter la commande précédente, remplacez la variable PROJECT_ID par l'identifiant de votre projet Google Cloud .
Enable the Secret Manager API:
gcloudservicesenablesecretmanager.googleapis.com
Mettez à jour les champs d'application de l'accès OAuth pour votre instance afin d'inclure le champ d'application requis pour Secret Manager, https://www.googleapis.com/auth/cloud-platform :
Accordez à l'utilisateur qui gère les configurations de l'agent Ops les autorisations nécessaires pour créer et gérer des secrets. Le rôle Identity and Access Management roles/secretManager.secretAdmin inclut les autorisations nécessaires :
Avant d'exécuter la commande précédente, remplacez les variables suivantes :
PROJECT_ID : identifiant de votre projet Google Cloud .
USER_EMAIL : adresse de l'utilisateur auquel le rôle est attribué.
Accordez au compte de service associé à la VM les autorisations dont il a besoin pour accéder aux secrets. Le rôle Identity and Access Management roles/secretManager.secretAccessor inclut les autorisations nécessaires :
Avant d'exécuter la commande précédente, remplacez les variables suivantes :
PROJECT_ID : identifiant de votre projet Google Cloud .
SERVICE_ACCT_EMAIL : adresse du compte de service associé à la VM.
Remplacer les secrets en texte brut par des secrets gérés
Pour éviter d'utiliser des secrets en texte brut dans vos fichiers de configuration en utilisant Secret Manager et le fournisseur googlesecretmanager, procédez comme suit :
Créez un secret dans Secret Manager pour chaque secret en texte brut dans vos fichiers de configuration.
Remplacez chaque secret en texte brut dans vos fichiers de configuration par une référence au secret correspondant dans Secret Manager.
Par exemple, si vous utilisez un récepteur de métriques mysql, votre fichier de configuration peut inclure une entrée semblable à la suivante :
receivers:
mysql:
type: mysql
username: root
password: plaintext-secret
Dans cet exemple, vous souhaitez placer la chaîne plaintext-secret dans Secret Manager, puis remplacer le secret en texte brut par une référence au secret géré.
Créer des secrets Secret Manager pour les secrets en texte brut
Pour créer un secret Secret Manager contenant le secret en texte brut plaintext-secret, exécutez la commande suivante :
Pour en savoir plus sur le stockage, le versionnage et l'accès aux secrets dans Secret Manager, consultez Créer un secret.
Remplacer les secrets en texte brut
Pour mettre à jour vos fichiers de configuration, remplacez chaque secret en texte brut par une référence au fournisseur googlesecretmanager et au nom de ressource du secret géré, comme indiqué dans l'exemple suivant :
receivers:
mysql:
type: mysql
username: root
password: ${googlesecretmanager:projects/PROJECT_ID/secrets/SECRET_NAME/versions/VERSION}
Redémarrer l'agent Ops
Linux
Pour redémarrer l'agent, exécutez la commande suivante sur votre instance :
sudo systemctl restart google-cloud-ops-agent
Pour vérifier que l'agent a redémarré, exécutez la commande suivante et vérifiez que les composants "Agent de métriques" et "Agent de journalisation" ont démarré :
sudo systemctl status "google-cloud-ops-agent*"
Windows
Connectez-vous à votre instance via RDP ou un outil similaire, et connectez-vous à Windows.
Ouvrez un terminal PowerShell avec des droits d'administrateur en effectuant un clic droit sur l'icône PowerShell, puis en sélectionnant Exécuter en tant qu'administrateur.
Pour redémarrer l'agent, exécutez la commande PowerShell suivante :
Restart-Service google-cloud-ops-agent -Force
Pour vérifier que l'agent a redémarré, exécutez la commande suivante et vérifiez que les composants "Agent de métriques" et "Agent de journalisation" ont démarré :
Get-Service google-cloud-ops-agent*
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[],[],null,["# Manage secrets in Ops Agent metric configuration\n\nConfiguring some of the third-party integrations requires you to\nprovide secrets, such as passwords, for the Ops Agent metric\nreceivers. By default, these secrets are stored as plaintext in the agent's\n`config.yaml` file. These secrets are included in system logs\nwritten by the agent and transmitted to Cloud Logging, exposing\nthe secrets beyond the virtual machine (VM) where the Ops Agent is\nrunning.\n\nStarting with Ops Agent version 2.57.0,\nyou can use an OpenTelemetry provider integrated [Secret Manager](/secret-manager/docs/overview) to eliminate\nplaintext secrets in your configuration files.\n\nA *provider* is an OpenTelemetry configuration component,\nanalogous to the receiver and processor components. Each provider has a\ntype, and each type of provider maps a specific identifier in the\nconfiguration to a value.\n\nThe `googlesecretmanager` provider maps Secret Manager\nidentifiers to the secrets, like passwords, tokens, and API keys, that you've\nstored in Secret Manager. Using the\n`googlesecretmanager` provider offers the following benefits:\n\n- **Enhanced security** : Your configuration files don't contain sensitive information like passwords. The actual secrets are stored in [Secret Manager](/secret-manager/docs/overview), a service designed specifically for securely storing, accessing, and managing sensitive data.\n- **Reduced risk of exposure**: Secret Manager fetches secrets during initialization of the Ops Agent, which prevents plaintext secrets from accidentally being recorded in logs.\n\nYou can use the `googlesecretmanager` only in the configuration of\nmetric collection in custom Ops Agent configurations. Don't use the provider to\nreplace secrets in the configuration of log collection.\n\nBefore you begin\n----------------\n\nTo use the `googlesecretmanager` provider, you must enable the\nSecret Manager API and permit access to the API,\nas described in the following steps:\n\n1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n After installation,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\n\n If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n2. Set the default project for Google Cloud CLI:\n\n ```\n gcloud config set project PROJECT_ID\n ```\n\n Before you run the previous command, replace the \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\n variable with the identifier of your Google Cloud project.\n3.\n\n\n Enable the Secret Manager API:\n\n\n ```bash\n gcloud services enable secretmanager.googleapis.com\n ```\n4. Update the OAuth access scopes for your instance to include the required scope for Secret Manager, `https://www.googleapis.com/auth/cloud-platform`: \n\n ```\n gcloud compute instances set-service-account \"INSTANCE_ID\" \\\n --service-account \"SERVICE_ACCT_EMAIL\" \\\n --scopes \"https://www.googleapis.com/auth/cloud-platform\"\n ```\n\n Before you run the previous command, replace the following variables:\n - \u003cvar translate=\"no\"\u003eINSTANCE_ID\u003c/var\u003e: the identifier of your VM.\n - \u003cvar translate=\"no\"\u003eSERVICE_ACCT_EMAIL\u003c/var\u003e: the address of the service account associated with the VM.\n\n For more information, see\n [Access the Secret Manager API](/stackdriver/docs/solutions/agents/ops-agent/third-party/secret-manager/docs/accessing-the-api).\n5. Grant the user who manages the Ops Agent configurations the permissions needed to create and manage secrets. The Identity and Access Management role `roles/secretManager.secretAdmin` includes the necessary permissions: \n\n ```\n gcloud projects add-iam-policy-binding PROJECT_ID \\\n --member=\"user:USER_EMAIL\" \\\n --role=roles/secretManager.secretAdmin\n ```\n\n Before you run the previous command, replace the following variables:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the identifier of your Google Cloud project.\n - \u003cvar translate=\"no\"\u003eUSER_EMAIL\u003c/var\u003e: the address of the user being granted the role.\n6. Grant the service account associated with the VM the permissions it needs to access the secrets. The Identity and Access Management role `roles/secretManager.secretAccessor` includes the necessary permissions: \n\n ```\n gcloud projects add-iam-policy-binding PROJECT_ID \\\n --member=\"serviceAccount:SERVICE_ACCT_EMAIL\" \\\n --role=roles/secretManager.secretAccessor\n ```\n\n Before you run the previous command, replace the following variables:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the identifier of your Google Cloud project.\n - \u003cvar translate=\"no\"\u003eSERVICE_ACCT_EMAIL\u003c/var\u003e: the address of the service account associated with the VM.\n\nReplace plaintext secrets with managed secrets\n----------------------------------------------\n\nTo eliminate the use of plaintext secrets in your configuration files by\nusing Secret Manager and the `googlesecretmanager`\nprovider, do the following:\n\n1. Create a secret in Secret Manager for each plaintext secret in your configuration files.\n2. Replace each plaintext secret in your configuration files with a reference to the corresponding secret in Secret Manager.\n\nFor example, if you are using a `mysql` metric receiver, then\nyour configuration file might include an entry like the following: \n\n```\nreceivers:\n mysql:\n type: mysql\n username: root\n password: plaintext-secret\n```\n\nIn this example, you want to place the \u003cvar translate=\"no\"\u003eplaintext-secret\u003c/var\u003e\nstring into Secret Manager and then replace\nthe plaintext secret with a reference to the managed secret.\n\n### Create Secret Manager secrets\nfor plaintext secrets\n\nTo create a Secret Manager secret containing the plaintext secret `plaintext-secret`, run the following command: \n\n```\necho -n \"plaintext-secret\" | gcloud secrets create SECRET_NAME \\\n --replication-policy=\"automatic\" \\\n --data-file=-\n```\n\nBefore you run the previous command, replace the following variables:\n\n- \u003cvar translate=\"no\"\u003eplaintext-secret\u003c/var\u003e: Replace with your plaintext secret.\n- \u003cvar translate=\"no\"\u003eSECRET_NAME\u003c/var\u003e: Replace with a meaningful name for your secret.\n\nThe fully qualified resource name of your new secret has the following\nformat, with a \u003cvar translate=\"no\"\u003eVERSION\u003c/var\u003e of `1`: \n\n```\nprojects/PROJECT_ID/secrets/SECRET_NAME/versions/VERSION\n```\n\nFor more information about storing, versioning, and accessing secrets in\nSecret Manager, see\n[Create a secret](/secret-manager/docs/creating-and-accessing-secrets).\n\n### Replace plaintext secrets\n\nTo update your configuration files, replace each plaintext secret\nwith a reference to the `googlesecretmanager` provider and the resource\nname of the managed secret, as shown in the following example: \n\n```\nreceivers:\n mysql:\n type: mysql\n username: root\n password: ${googlesecretmanager:projects/PROJECT_ID/secrets/SECRET_NAME/versions/VERSION}\n```\n\n### Restart the Ops Agent\n\n### Linux\n\n1. To restart the agent, run the following command on your instance: \n\n ```\n sudo systemctl restart google-cloud-ops-agent\n ```\n2. To confirm that the agent restarted, run the following command and verify that the components \"Metrics Agent\" and \"Logging Agent\" started: \n\n ```\n sudo systemctl status \"google-cloud-ops-agent*\"\n ```\n\n### Windows\n\n1. Connect to your instance using RDP or a similar tool and login to Windows.\n2. Open a PowerShell terminal with administrator privileges by right-clicking the PowerShell icon and selecting **Run as Administrator**\n3. To restart the agent, run the following PowerShell command: \n\n ```\n Restart-Service google-cloud-ops-agent -Force\n ```\n4. To confirm that the agent restarted, run the following command and verify that the components \"Metrics Agent\" and \"Logging Agent\" started: \n\n ```\n Get-Service google-cloud-ops-agent*\n ```"]]