我可以使用 Bicep 为容器注册表创建令牌凭证吗?

问题描述 投票:0回答:1

我正在尝试通过 Azure Bicep 创建一个容器注册表,作为其中的一部分,还在其中创建访问令牌。

我的 .bicep 文件包含此部分:

param tokenExpiry string = dateTimeAdd(utcNow(), 'P30D')

resource MyToken 'Microsoft.ContainerRegistry/registries/tokens@2022-12-01' = {
  parent: containerRegistry
  name: 'MyToken'
  properties: {
    status: 'enabled'
    scopeMapId: resourceId('Microsoft.ContainerRegistry/registries/scopeMaps', containerRegistry.name, '_repositories_pull')
    credentials: {
      passwords: [
        {
           name: 'password1'
           expiry: tokenExpiry
          }
      ]
    }
  }
}

但是,在运行部署时,失败了:

[{"code":"PasswordCannotBeAdded","message":"New passwords can be added only through 'generateCredentials'.

我尝试在密码中添加

value
属性(就像我为密钥保管库所做的那样),但这也失败了:
{"code":"PasswordPropertiesImmutable","message":"Password properties cannot be changed. To retain the password, please provide an empty value for a password. To add a new password please use 'generateCredentials'.

我没有找到从二头肌脚本中调用

generateCredentials
的方法,唯一的搜索结果是提及
New-AzContainerRegistryToken
PowerShell cmdlet。

我可能在这里滥用了二头肌吗?我想确保注册表中只有一组特定的访问令牌,如果我想刷新它们,我只需再次运行 bicep 即可。

我可以这样做吗?还是我必须在没有任何凭据的情况下创建令牌,然后必须通过 REST API 单独创建密码?

azure azure-bicep azure-container-registry
1个回答
0
投票

您可以仅添加

tokens
和名称。这个令牌名称可以比作用户名。

您无法直接修改令牌的凭据。您只能使用generateCredentials API或操作。

API 接受 tokenId、到期日和可选的名称。此处的名称指示要生成两个密码中的哪一个,并且必须是“password1”或“password2”。如果未指定名称,则(重新)生成密码 1 和密码 2。

无法指定您自己的密码。当generateCredentials被成功调用时,响应将包含内部生成的两个随机密码。

我不能保证这会起作用,但你也许可以尝试这样的事情:

param tokenExpiry string = dateTimeAdd(utcNow(), 'P30D')

resource MyToken 'Microsoft.ContainerRegistry/registries/tokens@2022-12-01' = {
  name: 'MyToken'
}

resource MyCreds 'Microsoft.ContainerRegistry/registries/generateCredentials/action@2023-01-01-preview' = {
  name: 'MyCreds'
  tokenId: MyToken.id
  expiry: tokenExpiry
}

如果这不起作用,您需要直接调用API。如果您进行了自动化部署,则可以在脚本中使用

Invoke-RestMethod
curl
。不过,您需要将访问令牌包含在您的请求中。

API参考:

https://learn.microsoft.com/en-us/rest/api/containerregistry/registries/generate-credentials?view=rest-containerregistry-2023-01-01-preview&tabs=HTTP#security

© www.soinside.com 2019 - 2024. All rights reserved.