{"id":2385,"date":"2022-05-04T10:22:21","date_gmt":"2022-05-04T17:22:21","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=2385"},"modified":"2022-05-04T10:22:21","modified_gmt":"2022-05-04T17:22:21","slug":"quickly-map-your-nuget-packages-to-sources","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/quickly-map-your-nuget-packages-to-sources\/","title":{"rendered":"Quickly Map Your NuGet Packages to Sources"},"content":{"rendered":"<h2>Package Source Mapper<\/h2>\n<p>When we introduced Package Source Mapping late last year, we noticed a certain challenge to make onboarding to using the feature easier. Could there be a way for us to automatically generate a NuGet.config for you based on your project\u2019s known packages and sources?<\/p>\n<p>We started to develop a tool that does just that for you. Learn more about how you can use it below.<\/p>\n<h2>Getting the Tool<\/h2>\n<p>You can download the tool directly from <a href=\"https:\/\/www.nuget.org\/packages\/NuGet.PackageSourceMapper\/\">NuGet.org as a dotnet tool<\/a>:<\/p>\n<pre><code>dotnet tool install --global NuGet.PackageSourceMapper --version 0.1.3-preview.22218.1\n<\/code><\/pre>\n<p>Alternatively, you can <a href=\"https:\/\/github.com\/NuGet\/PackageSourceMapper\">download and compile the code locally<\/a> if you prefer to.<\/p>\n<h2>Using Package Source Mapper<\/h2>\n<p>The package source mapper tool requires you to have completed a successful package restore in which it will read each respective <code>.nupkg.metadata<\/code> file generated as part of your build to best understand how you map your respective packages and sources.<\/p>\n<p>Here\u2019s what one of those files looks like. Take a package like <code>NuGet.Protocol<\/code> that was restored successfully:<\/p>\n<pre><code>{\n  \"version\": 2,\n  \"contentHash\": \"W8SShKZ3bMvO2OzwPQj9BfdXnwgQTivs8lKtRIBcrgqKhY\/ylNG0JLmGz6BTYDqe89+USnAot6H0\/s1ZGC0eTw==\",\n  \"source\": \"https:\/\/api.nuget.org\/v3\/index.json\"\n}\n<\/code><\/pre>\n<p>This specific version was restored by only the NuGet.org endpoint as indicated by its <code>contentHash<\/code> and <code>source<\/code> properties. We can easily verify that it came directly from NuGet.org at any time with this information.<\/p>\n<h2>Get Started<\/h2>\n<p>Let\u2019s take the <a href=\"https:\/\/github.com\/NuGetPackageExplorer\/NuGetPackageExplorer\">NuGetPackageExplorer<\/a> repository for example. Once we restore the packages via an IDE or <code>dotnet restore<\/code> command, we will be ready to use the tool in full.<\/p>\n<p>With the tool already installed, we can run the following command to generate a new <code>NuGet.config<\/code> file with the appropriate source mappings to get us started with our onboarding to the feature:<\/p>\n<pre><code>packagesourcemapper generate nuget.config\n<\/code><\/pre>\n<p>When we open the newly generated <code>nugetPackageSourceMapping.config<\/code> file, we will now see a complete mapping of our packages and their respective sources.<\/p>\n<pre><code>&lt;packageSourceMapping&gt;\n  &lt;packageSource key=\"NuGet CI packages\"&gt;\n    &lt;package pattern=\"microsoft.*\" \/&gt;\n    &lt;package pattern=\"nuget.jobs.common\" \/&gt;\n    \u2026\n    &lt;package pattern=\"nugetgallery.core\" \/&gt;\n    &lt;package pattern=\"strathweb.cacheoutput.webapi2.strongname\" \/&gt;\n  &lt;\/packageSource&gt;\n  &lt;packageSource key=\"dotnet-tools\"&gt;\n    &lt;package pattern=\"microsoft.*\" \/&gt;\n  &lt;\/packageSource&gt;\n  &lt;packageSource key=\"BuildPackages\"&gt;\n    &lt;package pattern=\"nuget.common\" \/&gt;\n    &lt;package pattern=\"nuget.configuration\" \/&gt;\n    \u2026\n    &lt;package pattern=\"nuget.protocol\" \/&gt;\n    &lt;package pattern=\"nuget.versioning\" \/&gt;\n  &lt;\/packageSource&gt;\n  &lt;packageSource key=\"nuget.org\"&gt;\n    &lt;package pattern=\"antlr\" \/&gt;\n    &lt;package pattern=\"appinsights.windowsdesktop\" \/&gt;\n    \u2026\n    &lt;package pattern=\"xunit\" \/&gt;\n    &lt;package pattern=\"xunit.*\" \/&gt;\n  &lt;\/packageSource&gt;\n&lt;\/packageSourceMapping&gt;\n<\/code><\/pre>\n<p>This gives you the foundation of mapping each of your sources more appropriately to the ideal source you\u2019d like it to come from. <strong>One best practice for this feature is to map a single package to a single source in a 1:1 relationship.<\/strong> This tool will do its best to provide you the best security practices such as complete package ID patterns and using wildcard (*) globs where appropriate.<\/p>\n<p>For further instructions on using this tool or getting started with it, please visit the <a href=\"https:\/\/www.nuget.org\/packages\/NuGet.PackageSourceMapper\/\">NuGet.PackageSourceMapper<\/a> package on NuGet.org.<\/p>\n<h2>Closing<\/h2>\n<p>You\u2019ve learned how to quickly map your NuGet packages to sources to secure your software supply chain.<\/p>\n<p>For more information on using this feature, please see our <a href=\"https:\/\/docs.microsoft.com\/nuget\/consume-packages\/package-source-mapping\">documentation on package source mapping<\/a>.<\/p>\n<p>For more tips and tricks on how to further secure your software with NuGet, check out our documentation on <a href=\"https:\/\/docs.microsoft.com\/en-us\/nuget\/concepts\/security-best-practices\">best practices for a secure software supply chain<\/a>.<\/p>\n<h2>Feedback<\/h2>\n<p>Your feedback is important to us. If there are any problems with this tool, check our <a href=\"https:\/\/github.com\/NuGet\/Home\/issues\/\">GitHub Issues<\/a>. For new issues within this experience, please <a href=\"https:\/\/github.com\/NuGet\/Home\/issues\/new\/choose\">report a GitHub Issue<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Package Source Mapper When we introduced Package Source Mapping late last year, we noticed a certain challenge to make onboarding to using the feature easier. Could there be a way for us to automatically generate a NuGet.config for you based on your project\u2019s known packages and sources? We started to develop a tool that does [&hellip;]<\/p>\n","protected":false},"author":90553,"featured_media":2389,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7933,7874],"tags":[7974,7975,7976,7977],"class_list":["post-2385","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-feature-announcement","category-nuget","tag-package-source","tag-package-source-mapper","tag-package-source-mapping","tag-source-mapping"],"acf":[],"blog_post_summary":"<p>Package Source Mapper When we introduced Package Source Mapping late last year, we noticed a certain challenge to make onboarding to using the feature easier. Could there be a way for us to automatically generate a NuGet.config for you based on your project\u2019s known packages and sources? We started to develop a tool that does [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/2385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/90553"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=2385"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/2385\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/2389"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=2385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=2385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=2385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}