从源代码管理中隐藏 nuget.config 中的明文密码

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

我有一个

nuget.config
文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <clear />
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
        <add key="github" value="https://nuget.pkg.github.com/mycompany/index.json" />
    </packageSources>

    <packageSourceCredentials>
        <github>
            <add key="Username" value="username" />
            <add key="ClearTextPassword" value="password" />
        </github>
    </packageSourceCredentials>

    <packageSourceMapping>
        <packageSource key="github">
            <package pattern="MyCompany.*" />
        </packageSource>

        <packageSource key="nuget.org">
            <package pattern="*" />
        </packageSource>
    </packageSourceMapping>
</configuration>

此文件已签入源代码管理并包含明文密码。我不希望密码在源代码管理中可见。我使用 GitHub Actions 来构建项目。是否可以使用 Secret/env 变量来替换密码?

.net continuous-integration nuget github-actions
2个回答
1
投票

选项 1:用户配置文件 nuget.config 中的凭据

正如 NuGet 关于其配置文件的文档所述,NuGet 读取多个 nuget.config 文件并“累积”它们。

因此,您可以让存储库 nuget.config 包含

<packageSource>
<packageSourceMapping>
部分,但将
<packageSourceCredentials>
移动到您的用户配置文件 nuget.config (或存储库父目录中的另一个 nuget.config,所以它不在源代码管理中),这样你的 repo nuget.config 中就不会保存任何凭据

选项 2:使用环境变量替换凭证值

nuget.cofig 的

packageSourceCredentials
的文档有几个示例,其中第二个示例显示使用
%name%
替换从环境变量中获取值。例如:

    <packageSourceCredentials>
        <github>
            <add key="Username" value="username" />
            <add key="ClearTextPassword" value="%EnvironmentVariableName%" />
        </github>
    </packageSourceCredentials>

0
投票

您应该使用 自动令牌身份验证,它通过替换您的密码所在的位置

${{ secrets.GITHUB_TOKEN }}
来为您处理秘密管理。

如果您在 GitHub Actions 工作流程中使用此令牌,它不会破坏本地开发,因为这是完全独立的。

请阅读GitHub Actions 的安全强化了解更多信息。

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