Setup

We begin the installation of our repository by gathering all of the necessary resources. The example on the NuGet Gallery main page shows you how to do it interactively with Visual Studio installed. In an enterprise environment, however, this is not a viable option.

The following prerequisites should be considered or downloaded:

  • Domain environment
  • PKI or at least an SSL certificate
  • .NET 4.5+ and the .NET 4.5+ software development kit (both included in the .NET Developer Pack)
  • SQL Server
  • IIS server with url_rewrite enabled

We will start building our gallery from the designated web server by cloning the NuGet Gallery repository and executing the build script. The build script will download additional sources and begin compiling the necessary bits and pieces for your web app. After the build is completed, we will need to modify web.config to suit our needs:

For different authentication methods, multi-factor authentication, Facebook integration, and many other settings, please go over the web.config. Everything is configurable!
# All steps are executed from the IIS host. Building the solution
# can be done on a development client instead if installing
# VisualStudio is not an option for the web server

# Gather sources
git clone https://github.com/NuGet/NuGetGallery.git C:NugetGallery 2>$null

# Build the necessary libraries
& 'C:NugetGalleryuild.ps1' -Configuration release

# Modify web.config and copy files
Copy-Item -Path C:NugetGallerysrcNugetGallery* -Destination C:NugetWebApp -Force -Recurse
$webConfig = [xml](Get-Content C:NugetWebAppweb.config)
$dbNode = $webConfig.SelectSingleNode('/configuration/connectionStrings/add[@name="Gallery.SqlServer"]')
$dbNode.connectionString = 'Server=NUGDB01;Database=NuGetGallery;Trusted_Connection=true'
$dbNode = $webConfig.SelectSingleNode('/configuration/connectionStrings/add[@name="Gallery.SupportRequestSqlServer"]')
$dbNode.connectionString = 'Server=NUGDB01;Database=SupportRequest;Trusted_Connection=true'
$dbNode = $webConfig.SelectSingleNode('/configuration/connectionStrings/add[@name="Gallery.ValidationSqlServer"]')
$dbNode.connectionString = 'Server=NUGDB01;Database=Validation;Trusted_Connection=true'
$webServer = $webConfig.SelectSingleNode('/configuration/system.webServer')
$rewrite = $webConfig.SelectSingleNode('/configuration/system.webServer/rewrite')
[void] ($webServer.RemoveChild($rewrite))
$webConfig.Save('C:NugetWebAppweb.config')

After the build is finished and the files are copied, we can create the database according to company policies. The account running the gallery, which in our case will be the identity of the computer account, will get a SQL login, so in order for the connection to succeed:

# Create the database and set database permissions
$batches = @(
'USE [master]
CREATE LOGIN [CONTOSONUGSV01$] FROM WINDOWS'
'USE [master]
CREATE DATABASE [NuGetGallery]
CREATE DATABASE [SupportRequest]
CREATE DATABASE [Validation]'
'USE [NuGetGallery]
CREATE USER [nuget-site] FOR LOGIN [CONTOSONUGSV01$]'
'USE [NuGetGallery]
EXEC sp_addrolemember "db_owner", "nuget-site"'
'USE [SupportRequest]
CREATE USER [nuget-site] FOR LOGIN [CONTOSONUGSV01$]'
'USE [SupportRequest]
EXEC sp_addrolemember "db_owner", "nuget-site"'
'USE [Validation]
CREATE USER [nuget-site] FOR LOGIN [CONTOSONUGSV01$]'
'USE [Validation]
EXEC sp_addrolemember "db_owner", "nuget-site"'
)

# Connect to SQL server without SQLPS
$sqlConnection = New-Object -TypeName System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = 'Server=NUGDB01;Trusted_Connection=true'
$sqlConnection.Open()
foreach ($batch in $batches)
{
$sqlCommand = new-Object -TypeName System.Data.SqlClient.SqlCommand
$sqlCommand.CommandText = $batch
$sqlCommand.CommandType = [System.Data.CommandType]::Text
$sqlCommand.Connection = $sqlConnection
[void] ($sqlCommand.ExecuteNonQuery())
}
$sqlConnection.Close()

# Create app pool and web site
Get-WebSite -Name 'Default Web Site' | Remove-Website

New-WebAppPool -Name 'NuGetPool'
New-WebSite -Name NuGetGallery -Port 80 -PhysicalPath C:NugetWebApp

After all of the databases and logins have been created, it is time to kick off the database setup. NuGet uses Entity Framework and provides a migration table to initialize everything and update the existing database tables:

# Run migrations for Entity Framework
& 'C:NugetGallery oolsUpdate-Databases.ps1' -MigrationTargets NugetGallery,NugetGallerySupportRequest,NugetGalleryValidation -NugetGallerySitePath C:NugetWebApp

With the gallery set up, you can start to create your first user so that we can upload packages later on. Depending on your configuration of the gallery, there are multiple authentication options. If your organization allows the use of third-party accounts such as Microsoft, Google, or Apple ID, you can enable these as logins as well. Our sample implementation just uses the good old username and password combination:

In order to fully make use of the gallery as an interactive repository that users can also push to, you will need to create an API key for personal use. Within the NuGet gallery, you can create multiple keys for multiple purposes; for example, if you want to integrate the publishing of a module within your module release pipeline, you can add an API key that can only modify your module's entry in the gallery and nothing else:

If any malicious actions are performed on your API key, you can use the same page to regenerate or delete the API key altogether. It is always a good idea to limit the availability of a key by setting up expiration. If possible, you can also limit your API keys with a pattern, or by selecting existing packages inside your repository.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset