如果您与多家公司合作,并希望与他们就源代码进行协作,我们建议您为每家公司创建一个单独的实例。
如需创建启用了 VPC Service Controls 的实例,请参阅在 VPC Service Controls 边界中配置 Secure Source Manager。
准备工作
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
Enable the Secure Source Manager API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
所需的角色
如需获得创建 Secure Source Manager 实例所需的权限,请让管理员向您授予 Google Cloud 项目的 Secure Source Manager Instance Owner (roles/securesourcemanager.instanceOwner) IAM 角色。
Secure Source Manager 角色只有在授予主账号后才会显示在 Google Cloud 控制台中。如需首次授予 Secure Source Manager 角色,请参阅授予和撤消 IAM 角色。
数据加密
默认情况下, Google Cloud 会使用 Google 管理的加密密钥自动加密静态数据。如果您对保护数据的密钥有特定的合规性或监管要求,则可以创建使用客户管理的加密密钥 (CMEK) 加密的 Secure Source Manager 实例。
请勿在实例 ID 或标签键值对中存储敏感数据,因为它们未使用 CMEK 进行加密。
如果您要在项目中创建第一个 Secure Source Manager 实例,则必须运行以下命令手动创建 Secure Source Manager 服务代理:
gcloud beta services identity create \
--service=securesourcemanager.googleapis.com \
--project=PROJECT_ID
其中,PROJECT_ID 是您将在其中创建 Secure Source Manager 实例的项目的项目 ID。
创建 Secure Source Manager 服务代理后,您必须向主账号 service-PROJECT-NUMBER@gcp-sa-sourcemanager.授予 Secure Source Manager Service Agent 角色 (roles/securesourcemanager.serviceAgent),否则实例创建将失败。
创建实例
要创建实例,请执行以下操作:
gcloud CLI
运行以下命令以创建实例。系统可能会要求您向 gcloud CLI 进行身份验证。
gcloud source-manager instances create INSTANCE_ID \ --region=LOCATION \ --project=PROJECT_ID \ --kms-key=projects/KEY_PROJECT/locations/KEY_LOCATION/keyRings/KEYRING_NAME/cryptoKeys/KEY其中:
INSTANCE_ID是实例的永久 ID。 该 ID 只能使用小写字母、数字和连字符,必须以字母开头,并且在创建后无法更改。LOCATION是您要在其中创建实例的区域。如需了解支持的位置,请参阅位置。PROJECT_ID是您要在其中创建实例的项目的项目 ID。-kms-key是可选标志。如果您想使用自己的客户管理的加密密钥 (CMEK) 创建实例,请添加此参数。CMEK 密钥必须位于您创建实例的同一位置,但可以位于不同的项目中。如果您想使用 Google 默认加密,请省略此标志。如果您想使用 CMEK 加密数据,请替换以下内容:- 将
KEY_PROJECT替换为您在其中创建密钥的项目。 - 将
KEY_LOCATION替换为密钥的位置。 - 将
KEYRING_NAME替换为您的密钥环名称。 KEY替换为您的密钥名称。
- 将
系统会启动一个长时间运行的实例创建操作。输出如下所示:
Create request issued for [my-instance]. done: false metadata: '@type': type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata apiVersion: v1 createTime: '2023-02-27T20:57:52.315609549Z' requestedCancellation: false target: projects/my-project/locations/us-central1/instances/my-instance verb: create name: projects/my-project/locations/us-central1/operations/operation-1234567894561-5ec69948c0f2b-60dd727f-a9b97a2e其中,
projects/my-project/locations/us-central1/operations/operation-1234567894561-5ec69948c0f2b-60dd727f-a9b97a2e是OPERATION_NAME。创建实例最多需要 60 分钟。
请记下
OPERATION_NAME,因为您需要使用它来检查操作的状态。通过运行以下命令来检查
create操作的状态:gcloud source-manager operations describe OPERATION_NAME \ --region=LOCATION替换以下内容:
- 将
OPERATION_NAME替换为创建命令响应中的操作名称。 - 将
LOCATION替换为您要在其中创建实例的区域。如需了解支持的位置,请参阅位置。
实例准备就绪后,响应将如下所示:
{ "name": "projects/my-project/locations/us-central1/operations/operation-123456789012-5ec69948c0f2b-60dd727f-a9b97a2e", "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "createTime": "2022-11-01T14:31:32.420469714Z", "endTime": "2022-11-01T14:48:34.140378114Z", "target": "projects/my-project/locations/us-central1/instances/test", "verb": "create", "requestedCancellation": false, "apiVersion": "v1" }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Instance", "name": "projects/my-project/locations/us-central1/instances/test", "createTime": "2022-11-01T14:31:32.416413630Z", "updateTime": "2022-11-01T14:31:32.416413630Z", "labels": { "ldap": "user", "source": "manual" }, "state": "ACTIVE", "hostConfig": { "html": "test-098765432109.us-central1.sourcemanager.dev", "api": "test-098765432109-api.us-central1.sourcemanager.dev", "gitHttp": "test-098765432109-git.us-central1.sourcemanager.dev" } } }其中
test-098765432109.us-central1.sourcemanager.dev是实例 HTML 网址。- 将
从成功创建的检查状态命令中复制 HTML 网址。您需要使用此网址通过实例的网页界面访问实例。
API
运行以下命令以创建实例。系统可能会要求您向 gcloud CLI 进行身份验证。
curl \ -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \ https://securesourcemanager.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances?instance_id=INSTANCE_ID \ -H "Content-Type: application/json" \ -d '{ "kms_key":"projects/KEY_PROJECT/locations/KEY_LOCATION/keyRings/KEYRING_NAME/cryptoKeys/KEY"}'其中:
INSTANCE_ID是实例的永久 ID。 ID 只能使用小写字母、数字和连字符,并且必须以字母开头。LOCATION是您要在其中创建实例的区域。如需了解支持的位置,请参阅位置。PROJECT_ID是您要在其中创建实例的项目的项目 ID。-d是一个可选的 HTTP POST 数据标志,可用于指定 CMEK 密钥来加密实例。如果要使用默认 Google 加密,请省略此标志。如果您想使用 CMEK 加密数据,请替换以下内容:- 将
KEY_PROJECT替换为您在其中创建密钥的项目。 - 将
KEY_LOCATION替换为密钥的位置。 - 将
KEYRING_NAME替换为您的密钥环名称。 KEY替换为您的密钥名称。
- 将
系统会启动一个长时间运行的实例创建操作。输出如下所示:
{ "name": "projects/my-project/locations/us-central1/operations/operation-1234567894561-5ec69948c0f2b-60dd727f-a9b97a2e", "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "createTime": "2022-11-01T14:31:32.420469714Z", "target": "projects/my-project/locations/us-central1/instances/test", "verb": "create", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }其中,
projects/my-project/locations/us-central1/operations/operation-1234567894561-5ec69948c0f2b-60dd727f-a9b97a2e是OPERATION_NAME。创建实例最多需要 60 分钟。
复制
OPERATION_NAME。运行以下命令,检查创建操作的状态:
curl \ -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ https://securesourcemanager.googleapis.com/v1/OPERATION_NAME其中,OPERATION_NAME 是创建命令响应中的操作名称。
实例准备就绪后,响应将如下所示:
{ "name": "projects/my-project/locations/us-central1/operations/operation-123456789012-5ec69948c0f2b-60dd727f-a9b97a2e", "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "createTime": "2022-11-01T14:31:32.420469714Z", "endTime": "2022-11-01T14:48:34.140378114Z", "target": "projects/my-project/locations/us-central1/instances/test", "verb": "create", "requestedCancellation": false, "apiVersion": "v1" }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Instance", "name": "projects/my-project/locations/us-central1/instances/test", "createTime": "2022-11-01T14:31:32.416413630Z", "updateTime": "2022-11-01T14:31:32.416413630Z", "labels": { "ldap": "user", "source": "manual" }, "state": "ACTIVE", "hostConfig": { "html": "test-098765432109.us-central1.sourcemanager.dev", "api": "test-098765432109-api.us-central1.sourcemanager.dev", "gitHttp": "test-098765432109-git.us-central1.sourcemanager.dev" } } }其中
test-098765432109.us-central1.sourcemanager.dev是实例 HTML 网址。从成功创建的检查状态命令中复制 HTML 网址。您需要使用此网址通过实例的网页界面访问实例。
Terraform
如需使用 Terraform 创建 Secure Source Manager 实例,请使用 google_secure_source_manager_instance 资源。
访问实例
创建实例后,您可以使用浏览器通过其网页界面访问该实例。
如需在实例中创建代码库,您需要对实例项目拥有 Repo Creator 角色 (roles/securesourcemanager.repoCreator),并对 Secure Source Manager 实例拥有 Instance Repository Creator 角色 (roles/securesourcemanager.instanceRepositoryCreator)。
-
如需通过网页界面访问 Secure Source Manager 实例,请将以下网址复制到浏览器地址栏中。
INSTANCE_ID-PROJECT_NUMBER.LOCATION.sourcemanager.dev请替换以下内容:
- 将 INSTANCE_ID 替换为实例名称。
- 将 PROJECT_NUMBER 替换为实例的 Google Cloud项目编号。如需了解如何识别项目,请参阅识别项目。
- 将 LOCATION 替换为实例的区域。
系统会显示一个 OAuth 页面,询问您是否要向
sourcemanager.dev授予对您 Google 账号的访问权限。点击允许按钮。
Secure Source Manager 网页界面随即打开。您可以通过网页界面创建和查看代码库以及所有关联的问题和拉取请求。
后续步骤
- 向用户授予实例访问权限。
- 创建并克隆代码库。
- 向用户授予代码库访问权限。
- 使用 IAM 控制访问权限。