为什么 TFS 机密不是 SecureString?
Why is a TFS secret not a SecureString?
根据 documentation,您必须像这样将 TFS 2018 中的秘密传递给 powershell:
Param(
[string]$sauceArgument,
[string]$secretSauceArgument
)
Write-Host No problem reading $env:SAUCE or $sauceArgument
Write-Host But I cannot read $env:SECRET_SAUCE
Write-Host But I can read $secretSauceArgument "(but the log is redacted so I do not
spoil the secret)"
将其作为字符串传递不允许在凭据中使用机密。我只能在通过 ConvertTo-SecureString -AsPlainText 转换它时使用它,根据 documentation:
这应该是不好的做法
$Secure_String_Pwd = ConvertTo-SecureString $secretSauceArgument -AsPlainText -Force
如果我将输入类型从 [String] 更改为 [SecureString],我会收到此转换错误:
Cannot process argument transformation on parameter 'secretSauceArgument'. Cannot convert the "***" value of type "System.String" to type "System.Security.SecureString".
真的需要转换吗?这是否意味着使用 Secrets 是一种不好的做法?
更新以澄清我的问题:我能够将秘密传递到 powershell。我无法使用机密作为凭据(只能通过 ConvertTo-SecureString -AsPlainText -Force,这是不好的做法)。
Azure DevOps 管道中的秘密变量使用 2048 位 RSA 密钥进行静态加密。密文在代理上可用,供任务和脚本使用。
您使用了系统保留的变量前缀secret
,因此无法读取。尽量修改变量名不要使用系统保留的变量前缀
User-defined variables can consist of letters, numbers, .
, and _
characters. Don't use variable prefixes that are reserved by the
system. These are: endpoint
, input
, secret
, and securefile
.
Any variable that begins with one of these strings (regardless of
capitalization) will not be available to your tasks and scripts.
更新:
现在我更了解你的问题了。根据我的测试,即使您在脚本中定义了 $password = '123'
,您仍然需要 -AsPlainText -Force
。脚本中定义的所有变量都被视为字符串,管道中没有 SecureString
。您可以尝试从文件中的加密字符串创建安全字符串:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/convertto-securestring?view=powershell-7.1#example-2--create-a-secure-string-from-an-encrypted-string-in-a-file.
我个人的看法是:TFS 机密对于 powershell 中的凭据而言并不是最佳选择 - 这令人惊讶,因为我希望机密的主要用途是用于凭据。
Cece Dong 的回答 - MSFT 使用文件代替是一个不错的选择。
根据 documentation,您必须像这样将 TFS 2018 中的秘密传递给 powershell:
Param(
[string]$sauceArgument,
[string]$secretSauceArgument
)
Write-Host No problem reading $env:SAUCE or $sauceArgument
Write-Host But I cannot read $env:SECRET_SAUCE
Write-Host But I can read $secretSauceArgument "(but the log is redacted so I do not
spoil the secret)"
将其作为字符串传递不允许在凭据中使用机密。我只能在通过 ConvertTo-SecureString -AsPlainText 转换它时使用它,根据 documentation:
这应该是不好的做法$Secure_String_Pwd = ConvertTo-SecureString $secretSauceArgument -AsPlainText -Force
如果我将输入类型从 [String] 更改为 [SecureString],我会收到此转换错误:
Cannot process argument transformation on parameter 'secretSauceArgument'. Cannot convert the "***" value of type "System.String" to type "System.Security.SecureString".
真的需要转换吗?这是否意味着使用 Secrets 是一种不好的做法?
更新以澄清我的问题:我能够将秘密传递到 powershell。我无法使用机密作为凭据(只能通过 ConvertTo-SecureString -AsPlainText -Force,这是不好的做法)。
Azure DevOps 管道中的秘密变量使用 2048 位 RSA 密钥进行静态加密。密文在代理上可用,供任务和脚本使用。
您使用了系统保留的变量前缀secret
,因此无法读取。尽量修改变量名不要使用系统保留的变量前缀
User-defined variables can consist of letters, numbers,
.
, and_
characters. Don't use variable prefixes that are reserved by the system. These are:endpoint
,input
,secret
, andsecurefile
. Any variable that begins with one of these strings (regardless of capitalization) will not be available to your tasks and scripts.
更新:
现在我更了解你的问题了。根据我的测试,即使您在脚本中定义了 $password = '123'
,您仍然需要 -AsPlainText -Force
。脚本中定义的所有变量都被视为字符串,管道中没有 SecureString
。您可以尝试从文件中的加密字符串创建安全字符串:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/convertto-securestring?view=powershell-7.1#example-2--create-a-secure-string-from-an-encrypted-string-in-a-file.
我个人的看法是:TFS 机密对于 powershell 中的凭据而言并不是最佳选择 - 这令人惊讶,因为我希望机密的主要用途是用于凭据。
Cece Dong 的回答 - MSFT 使用文件代替是一个不错的选择。