Gestire i secret con Secret Manager in Cloud Code per Cloud Shell
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Con l'integrazione di Secret Manager con Cloud Code, puoi creare, visualizzare, aggiornare e utilizzare i secret nell'IDE e senza archiviarli nella base di codice.
Questa pagina descrive come accedere a Secret Manager nell'IDE e come iniziare a creare e gestire i secret.
Abilita l'API Secret Manager
Quando gestisci i secret con Cloud Code, questi vengono archiviati in modo sicuro in Secret Manager e possono essere recuperati tramite programmazione quando ti servono. È sufficiente che l'API Secret Manager sia abilitata e che tu disponga delle autorizzazioni necessarie per gestire i secret:
Assicurati di lavorare nel progetto in cui risiede il codice dell'applicazione. Il segreto deve trovarsi nello stesso progetto del codice dell'applicazione.
Fai clic su Cloud Code ed espandi l'esploratore Secret Manager.
Se non hai abilitato l'API Secret Manager, fai clic su
Abilita l'API Secret Manager nell'esploratore Secret Manager.
Creare secret
Per creare un secret utilizzando l'esploratore Secret Manager, segui questi passaggi:
Nell'IDE, fai clic su Cloud Code ed espandi l'esploratore Secret Manager.
Fai clic su AggiungiCrea secret nell'esploratore Secret Manager.
Nella finestra di dialogo Crea secret, imposta il progetto, il nome, il valore e la regione del secret e specifica le etichette per organizzarli.
In alternativa, puoi creare un secret utilizzando l'editor:
Apri un file contenente il testo che vuoi memorizzare come segreto nell'editor.
Evidenzia il testo da archiviare come secret, fai clic con il tasto destro del mouse e poi su
Crea secret in Secret Manager.
Nella finestra di dialogo Crea secret, personalizza il progetto, il nome, il valore, la regione e le etichette del secret.
Creare nuove versioni dei secret
Per creare una nuova versione di un secret utilizzando Secret Manager, segui questi passaggi:
Fai clic con il tasto destro del mouse su un secret esistente e scegli Crea versione del secret.
Nella finestra di dialogo Crea versione, imposta il nuovo valore del secret esistente utilizzando il campo Valore secret o importando un file.
Per rimuovere tutte le versioni precedenti del secret e mantenere solo la nuova versione
che stai creando, scegli Disattiva tutte le versioni precedenti.
Fai clic su Crea versione. La versione viene aggiunta e puoi vedere la versione segreta più recente e le versioni precedenti elencate nei menu a discesa Versioni.
In alternativa, puoi creare una nuova versione di un secret nell'editor:
Nell'editor, apri un file ed evidenzia il testo da memorizzare come segreto.
Fai clic con il tasto destro del mouse sul testo evidenziato e scegli Aggiungi versione al secret in Gestore dei secret.
Gestire le versioni dei secret
Per attivare, disattivare o distruggere una versione di un segreto, fai clic con il tasto destro del mouse sul segreto e poi seleziona il comando per l'azione che vuoi eseguire. Per le versioni attivate dei secret, puoi anche visualizzare il valore della versione.
Visualizza i secret
Per visualizzare i secret, selezionane uno dall'elenco nell'esploratore Secret Manager. I dettagli del secret, come nome, criterio di replica, timestamp di creazione e ID risorsa, sono elencati sotto il nome del secret.
Visualizzare i secret nella Google Cloud console
In alternativa, puoi visualizzare i secret nella Google Cloud console facendo clic con il tasto destro del mouse sul secret nell'esploratore Secret Manager e poi su Apri nella console Cloud.
Visualizzare i secret in Kubernetes Explorer
Per visualizzare i secret nell'esploratore Kubernetes:
Fai clic su Cloud Code ed espandi l'esploratore Kubernetes.
Espandi il cluster, quindi espandi Secrets.
Espandi il secret per visualizzarne i dettagli.
Accedere ai secret dalla tua applicazione
Una volta creato il segreto, puoi includerlo nel codice e configurare
l'autenticazione.
Per accedere a un secret dalla tua applicazione:
Installa la libreria client di Secret Manager.
Fai clic su Cloud Code ed espandi l'esploratore API Cloud.
Espandi Sicurezza Cloud > API Secret Manager e segui le istruzioni nella sezione Installa la libreria client per il linguaggio che stai utilizzando.
Per ottenere il nome della versione del secret da utilizzare nel codice, seleziona il secret nel riquadro Secret Manager, fai clic con il tasto destro del mouse e scegli Copia ID risorsa.
Per completare la configurazione dell'autenticazione, segui la guida all'autenticazione delle librerie client:
Sviluppo locale: se esegui lo sviluppo su un cluster locale (come minikube o Docker Desktop) o su un emulatore locale, devi completare i passaggi illustrati nella sezione Sviluppo locale pertinenti al tuo flusso di lavoro.
Sviluppo remoto: se nella tua applicazione utilizzi un cluster GKE o un servizio Cloud Run, devi completare i passaggi illustrati nella sezione Sviluppo remoto pertinenti al tuo flusso di lavoro, incluse le istruzioni specifiche di Secret Manager per configurare i ruoli richiesti nel tuo account di servizio.
Aggiungere un secret come variabile di ambiente
Per aggiungere un secret Kubernetes esistente al deployment come variabile di ambiente:
Fai clic su Cloud Code ed espandi l'esploratore Kubernetes.
Espandi il cluster minikube, quindi espandi Secrets.
Fai clic con il tasto destro del mouse su un segreto che rappresenta un oggetto di deployment e poi su
Aggiungi segreto come variabile di ambiente.
Montare un secret come volume
Per montare un secret Kubernetes esistente come volume nel contenitore del deployment:
Fai clic su Cloud Code ed espandi l'esploratore Kubernetes.
Espandi il cluster minikube, quindi espandi Secrets.
Fai clic con il tasto destro del mouse su un secret che rappresenta un oggetto di deployment, quindi fai clic su
Monta secret come volume.
Elimina secret
Per eliminare un secret utilizzando Secret Manager in Cloud Code:
Fai clic su Cloud Code ed espandi l'esploratore Secret Manager.
Fai clic con il tasto destro del mouse su un segreto esistente e seleziona Apri nella console Cloud.
Nella pagina Dettagli secret, fai clic su ELIMINA e segui le istruzioni per eliminare il secret.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eCloud Code's Secret Manager integration allows you to create, view, update, and use secrets directly within your IDE without storing them in the codebase.\u003c/p\u003e\n"],["\u003cp\u003eYou can create secrets and new versions of existing secrets either through the Secret Manager explorer or directly from highlighted text in your editor.\u003c/p\u003e\n"],["\u003cp\u003eSecret versions can be managed (enabled, disabled, or destroyed) by right-clicking on the secret within the Secret Manager explorer.\u003c/p\u003e\n"],["\u003cp\u003eSecrets can be accessed from your application by installing the Secret Manager client library and including relevant code snippets, with specific authentication steps required for local and remote development.\u003c/p\u003e\n"],["\u003cp\u003eYou can add existing Kubernetes secrets to deployments as environment variables or mount them as volumes through the Kubernetes explorer.\u003c/p\u003e\n"]]],[],null,["# Manage secrets with Secret Manager in Cloud Code for Cloud Shell\n\nWith Cloud Code's [Secret Manager](/secret-manager)\nintegration, you can create, view, update, and use secrets in your IDE and\nwithout storing them in your codebase.\n\nThis page describes how to access Secret Manager in your IDE and how\nyou can get started creating and managing secrets.\n\nEnable Secret Manager API\n-------------------------\n\nWhen managing secrets with Cloud Code, secrets are securely stored\nin Secret Manager and can be programmatically fetched when you need\nthem. All you need is the Secret Manager API enabled and the right\npermissions to manage secrets:\n\n1. Make sure that you're working in the project where your application code\n resides. Your secret must be in the same project as your application code.\n\n2. Click **Cloud Code** and then expand the\n **Secret Manager** explorer.\n\n3. If you haven't enabled the Secret Manager API, click\n **Enable Secret Manager API** in the Secret Manager explorer.\n\nCreate secrets\n--------------\n\nTo create a secret using the Secret Manager explorer, follow these\nsteps:\n\n1. In your IDE, click **Cloud Code** and then expand the\n **Secret Manager** explorer.\n\n2. Click add **Create Secret** in the\n **Secret Manager** explorer.\n\n In the **Create Secret** dialog, set your secret's project, name, value, and\n region, and specify labels to organize your secrets.\n\nAlternatively, you can create a secret using the editor:\n\n1. Open a file containing text that you want to store as a secret in the editor.\n2. Highlight the text to store as a secret, right-click, and then click **Create Secret in Secret Manager**.\n3. In the **Create Secret** dialog, customize the secret's project, name, value, region, and labels.\n\nCreate new versions of secrets\n------------------------------\n\nTo create a new version of a secret using Secret Manager, follow\nthese steps:\n\n1. Right-click an existing secret and then choose **Create Secret Version**.\n\n2. In the **Create Version** dialog, set the new value of your existing secret\n using the **Secret value** field or by importing a file.\n\n3. To remove all previous versions of your secret and keep just the new version\n you're creating, choose **Disable all past versions**.\n\n4. Click **Create Version** . Your version is added and you can see the\n latest secret version and previous versions listed under the **Versions**\n dropdowns.\n\nAlternatively, you can create a new version of a secret in the editor:\n\n1. In the editor, open a file and highlight the text to store as a secret.\n\n2. Right-click the highlighted text and then choose **Add Version to Secret in\n Secret Manager**.\n\n### Manage secret versions\n\nTo enable, disable, or destroy a version of a secret, right-click the secret\nand then select the command for the action you want to perform. For enabled\nversions of secrets, you can also view the version's value.\n\nView secrets\n------------\n\nTo view secrets, in the **Secret Manager** explorer, select a secret\nfrom the list. Details of the secret such as name, replication policy, creation\ntimestamp, and resource ID are listed below the secret name.\n\n### View secrets in Google Cloud console\n\nAlternatively, you can view secrets in Google Cloud console by right-clicking\nthe secret in the **Secret Manager** explorer and clicking **Open in\nCloud console**.\n\n\n### View secrets in Kubernetes explorer\n\nTo view secrets in the **Kubernetes** explorer, follow these steps:\n\n1. Click **Cloud Code** and then expand the **Kubernetes** explorer.\n2. Expand your cluster and then expand **Secrets**.\n3. Expand the secret to view its details.\n\nAccess secrets from your application\n------------------------------------\n\nAfter your secret is created, you can include it in your code and set up\nauthentication.\n\nTo access a secret from your application:\n\n1. Install the Secret Manager client library.\n\n 1. Click **Cloud Code** and then expand the\n **Cloud APIs** explorer.\n\n 2. Expand **Cloud Security** \\\u003e **Secret Manager API** and then follow the\n instructions in the Install Client Library section for the language\n you're using.\n\n2. Customize and include the\n [relevant code snippet](/secret-manager/docs/access-secret-version)\n in your application's code.\n\n To obtain your secret's version name to use in your code, select the secret\n in the Secret Manager panel, right-click, and then choose\n **Copy Resource ID**.\n3. To complete your authentication setup, follow the Client libraries\n authentication guide:\n\n - **Local development** : If you're developing on a local cluster (like minikube or Docker Desktop) or a local emulator, you should complete the steps illustrated in the [Local development section](/code/docs/shell/client-libraries#local_development) relevant to your workflow.\n - **Remote development** : If you're using a GKE cluster or a Cloud Run service in your application, you should complete the steps illustrated in the [Remote development section](/code/docs/shell/client-libraries#remote_development) relevant to your workflow, including the Secret Manager-specific instructions for setting up the required roles on your service account.\n\nAdd a secret as an environment variable\n---------------------------------------\n\nTo add an existing [Kubernetes secret](https://kubernetes.io/docs/concepts/configuration/secret/)\nto the deployment as an environment variable, follow these steps:\n\n1. Click **Cloud Code** and then expand the **Kubernetes** explorer.\n2. Expand your minikube cluster and then expand **Secrets**.\n3. Right-click a secret that represents a deployment object and then click **Add Secret as Environment Variable**.\n\nMount a secret as a volume\n--------------------------\n\nTo mount an existing [Kubernetes secret](https://kubernetes.io/docs/concepts/configuration/secret/)\nas a volume in the deployment's container, follow these steps:\n\n1. Click **Cloud Code** and then expand the **Kubernetes** explorer.\n2. Expand your minikube cluster and then expand **Secrets**.\n3. Right-click a secret that represents a deployment object and then click **Mount Secret as Volume**.\n\nDelete secrets\n--------------\n\n| **Note:** Deleting a secret requires the Secret Admin role (`roles/secretmanager.admin`) on the secret, project, folder, or organization. IAM roles can't be granted on a secret version.\n\nTo delete a secret using the Secret Manager in\nCloud Code, follow these steps:\n\n1. Click **Cloud Code** and then expand the\n **Secret Manager** explorer.\n\n2. Right-click an existing secret and select **Open in Cloud Console**.\n\n3. On the **Secret details** page, click **DELETE** and follow the prompts to\n delete the secret."]]