En este documento, se describe cómo configurar la autenticación en los repositorios upstream de Python Package Index (PyPI) para los repositorios remotos de Artifact Registry.
En este documento, se supone que ya creaste una versión de Python para Artifact Registry repositorio remoto y un PyPI de servicio predeterminada.
Para obtener más información sobre repositorios remotos, consulta el Descripción general de los repositorios remotos.
Roles obligatorios
Si quieres obtener los permisos que necesitas para configurar la autenticación en PyPI para repositorios remotos, solicita a tu administrador que te otorgue el los siguientes roles de IAM en el proyecto:
-
Administrador de Artifact Registry (
roles/artifactregistry.admin
) -
Administrador de Secret Manager (
roles/secretmanager.admin
)
Para obtener más información sobre cómo otorgar roles, consulta Administra el acceso a proyectos, carpetas y organizaciones.
También puedes obtener los permisos necesarios mediante roles personalizados o cualquier otro rol predefinido.
Crea un token de API de PyPI
- Accede a PyPI.
- Crea un token de API.
Guarda tu token de API en una versión del Secret
- Crea un secreto en Secret Manager.
- Guarda tu token de API de PyPI como una versión secreta.
Otorga a la cuenta de servicio de Artifact Registry acceso a tu secreto.
El agente de servicio de Artifact Registry actúa en nombre de Artifact Registry cuando interactúa con los servicios de Google Cloud. Para permitir que el agente de servicio use Secrets de Cloud Storage en Secret Manager, debes otorgarle al agente de servicio para ver tu versión del Secret.
El identificador del agente de servicio es el siguiente:
service-PROJECT-NUMBER@gcp-sa-artifactregistry.iam.gserviceaccount.com
PROJECT-NUMBER es el número del proyecto del proyecto de Google Cloud en el que se ejecuta Artifact Registry.
Para otorgar al agente de servicio de Artifact Registry el Acceso a secretos de Secret Manager rol:
Console
-
Ve a la página de Secret Manager en la consola de Google Cloud.
-
En la página Secret Manager, haz clic en la casilla de verificación junto al nombre del secreto.
-
Si aún no está abierto, haz clic en Mostrar panel de información para abrir el panel.
-
En el panel de información, haz clic en Agregar principal.
-
En el área de texto Principales nuevas, ingresa las direcciones de correo electrónico de los miembros que quieres agregar.
-
En el menú desplegable Selecciona una función, selecciona Secret Manager y, luego, Administrador y descriptor de acceso a Secretos.
gcloud
$ gcloud secrets add-iam-policy-binding secret-id \
--member="member" \
--role="roles/secretmanager.secretAccessor"
En el ejemplo anterior, member es un miembro de IAM, como un usuario, un grupo o una cuenta de servicio.
C#
Para autenticarte en Artifact Registry, 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.
Go
Para autenticarte en Artifact Registry, 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.
Java
Para autenticarte en Artifact Registry, 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.
Node.js
Para autenticarte en Artifact Registry, 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.
PHP
Para autenticarte en Artifact Registry, 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.
Python
Para autenticarte en Artifact Registry, 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.
Ruby
Para autenticarte en Artifact Registry, 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.
API
Nota: A diferencia de los otros ejemplos, esto reemplaza la política de IAM completa.
$ curl "https://secretmanager.googleapis.com/v1/projects/project-id/secrets/secret-id:setIamPolicy" \
--request "POST" \
--header "authorization: Bearer $(gcloud auth print-access-token)" \
--header "content-type: application/json" \
--data "{\"policy\": {\"bindings\": [{\"members\": [\"member\"], \"role\": \"roles/secretmanager.secretAccessor\"}]}}"
Para obtener más información sobre cómo otorgar o revocar el acceso a los Secrets, consulta Administra el acceso a los secretos.
Agrega credenciales de PyPI a tu repositorio remoto
Para actualizar el repositorio remoto con tus credenciales de PyPI, sigue estos pasos:
Console
Abre la página Repositorios en la consola de Google Cloud.
En la lista de repositorios, selecciona el repositorio y haz clic Edita el repositorio.
En la sección Remote repository authentication mode, actualiza o agrega tu nombre de usuario de PYPI
__token__
y la versión secreta que contiene tu token de API de PyPI.
gcloud CLI
Para actualizar tu repositorio remoto con tus credenciales de PyPI, ejecuta el comando siguiente comando:
gcloud artifacts repositories update REPOSITORY \
--project=PROJECT_ID \
--location=LOCATION \
--remote-username=__token__ \
--remote-password-secret-version=projects/SECRET_PROJECT_ID/secrets/SECRET_ID/versions/SECRET_VERSION
Reemplaza lo siguiente:
REPOSITORY
por el nombre del recurso remoto de Artifact Registry en un repositorio de confianza.PROJECT_ID
por el ID del proyecto de Google CloudLOCATION
con la ubicación regional o multirregional del repositorio. Puedes omitir esta marca si estableces un valor predeterminado. Cómo ver una lista de ubicaciones compatibles, ejecuta el comandogcloud artifacts locations list
.USERNAME
por tu nombre de usuario de PyPISECRET_PROJECT_ID
por el ID del proyecto en la que creaste el Secret.SECRET_ID
por el nombre que le asignaste a tu secreto.SECRET_VERSION
por la versión del secreto que guardaste tu token de la API de PyPI.
Tus credenciales se usarán la próxima vez que el repositorio remoto envíe una solicitud para un artefacto de la fuente upstream.