Permalink
Browse files

Merge pull request #2212 from NuGet/daravind/FeedOnlyMode

Added FeedOnlyMode to support NuGetV2RepositoryMirrors
  • Loading branch information...
2 parents 2c7f934 + f9ddd8e commit ddd4d588b74c3bb7d96dd3e5dea3545b44620b32 @deepakaravindr deepakaravindr committed Jun 18, 2014
@@ -49,6 +49,7 @@
<!-- **************** -->
<!-- These are only changed at runtime -->
<Setting name="Gallery.ReadOnlyMode" value="false" />
+ <Setting name="Gallery.FeedOnlyMode" value="false" />
<Setting name="Startup.BlockedIPs" value="" />
</ConfigurationSettings>
<Certificates>
@@ -49,6 +49,7 @@
<!-- **************** -->
<!-- These are only changed at runtime -->
<Setting name="Gallery.ReadOnlyMode" value="false" />
+ <Setting name="Gallery.FeedOnlyMode" value="false" />
<Setting name="Startup.BlockedIPs" value="" />
</ConfigurationSettings>
<Certificates>
@@ -12,6 +12,7 @@
<Setting name="Gallery.SqlServer" />
<Setting name="Gallery.ConfirmEmailAddress" />
<Setting name="Gallery.ReadOnlyMode" />
+ <Setting name="Gallery.FeedOnlyMode" />
<Setting name="Gallery.StorageType" />
<Setting name="Gallery.SmtpUri" />
<Setting name="Gallery.FacebookAppId" />
@@ -67,7 +67,7 @@ public static void PostStart()
// Get configuration from the kernel
var config = Container.Kernel.Get<IAppConfiguration>();
BackgroundJobsPostStart(config);
- AppPostStart();
+ AppPostStart(config);
BundlingPostStart();
}
@@ -168,9 +168,9 @@ private static void ElmahPreStart()
ServiceCenter.Current = _ => Container.Kernel;
}
- private static void AppPostStart()
+ private static void AppPostStart(IAppConfiguration configuration)
{
- Routes.RegisterRoutes(RouteTable.Routes);
+ Routes.RegisterRoutes(RouteTable.Routes, configuration.FeedOnlyMode);
Routes.RegisterServiceRoutes(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
@@ -7,7 +7,26 @@ namespace NuGetGallery
{
public static class Routes
{
- public static void RegisterRoutes(RouteCollection routes)
+ public static void RegisterRoutes(RouteCollection routes, bool feedOnlyMode = false)
+ {
+ if (!feedOnlyMode)
+ {
+ Routes.RegisterUIRoutes(routes);
+ }
+ else
+ {
+ // The home route is used as a probe path by Azure Load Balancer
+ // to determine if the node is up. So, always register the home route
+ // Just do so with an Empty Home, in the FeedOnlyMode, which simply returns a 200
+ RouteTable.Routes.MapRoute(
+ RouteName.Home,
+ "",
+ new { controller = "Pages", action = "EmptyHome" });
+ }
+ Routes.RegisterApiV2Routes(routes);
+ }
+
+ public static void RegisterUIRoutes(RouteCollection routes)
{
routes.MapRoute(
RouteName.Home,
@@ -276,15 +295,65 @@ public static void RegisterRoutes(RouteCollection routes)
"v1/PublishedPackages/Publish",
new { controller = "Api", action = "PublishPackage" });
+ // Redirected Legacy Routes
+
+ routes.Redirect(
+ r => r.MapRoute(
+ "ReportAbuse",
+ "Package/ReportAbuse/{id}/{version}",
+ new { controller = "Packages", action = "ReportAbuse" }),
+ permanent: true).To(packageVersionActionRoute);
+
+ routes.Redirect(
+ r => r.MapRoute(
+ "PackageActions",
+ "Package/{action}/{id}",
+ new { controller = "Packages", action = "ContactOwners" },
+ // This next bit looks bad, but it's not. It will never change because
+ // it's mapping the legacy routes to the new better routes.
+ new { action = "ContactOwners|ManagePackageOwners" }),
+ permanent: true).To(packageActionRoute);
+
+ routes.Redirect(
+ r => r.MapRoute(
+ RouteName.ListPackages,
+ "List/Packages",
+ new { controller = "Packages", action = "ListPackages" }),
+ permanent: true).To(packageListRoute);
+
+ routes.Redirect(
+ r => r.MapRoute(
+ RouteName.DisplayPackage,
+ "List/Packages/{id}/{version}",
+ new { controller = "Packages", action = "DisplayPackage", version = UrlParameter.Optional }),
+ permanent: true).To(packageDisplayRoute);
+
+ routes.Redirect(
+ r => r.MapRoute(
+ RouteName.NewSubmission,
+ "Contribute/NewSubmission",
+ new { controller = "Packages", action = "UploadPackage" }),
+ permanent: true).To(uploadPackageRoute);
+
+ routes.Redirect(
+ r => r.MapRoute(
+ "LegacyDownloadRoute",
+ "v1/Package/Download/{id}/{version}",
+ new { controller = "Api", action = "GetPackageApi", version = UrlParameter.Optional }),
+ permanent: true).To(downloadRoute);
+ }
+
+ public static void RegisterApiV2Routes(RouteCollection routes)
+ {
// V2 routes
routes.MapRoute(
"v2" + RouteName.VerifyPackageKey,
"api/v2/verifykey/{id}/{version}",
- new {
- controller = "Api",
+ new {
+ controller = "Api",
action = "VerifyPackageKey",
- id = UrlParameter.Optional,
- version = UrlParameter.Optional
+ id = UrlParameter.Optional,
+ version = UrlParameter.Optional
});
routes.MapRoute(
@@ -294,18 +363,6 @@ public static void RegisterRoutes(RouteCollection routes)
constraints: new { httpMethod = new HttpMethodConstraint("GET") });
routes.MapRoute(
- "v2" + RouteName.DeletePackageApi,
- "api/v2/package/{id}/{version}",
- new { controller = "Api", action = "DeletePackageApi" },
- constraints: new { httpMethod = new HttpMethodConstraint("DELETE") });
-
- routes.MapRoute(
- "v2" + RouteName.PublishPackageApi,
- "api/v2/package/{id}/{version}",
- new { controller = "Api", action = "PublishPackageApi" },
- constraints: new { httpMethod = new HttpMethodConstraint("POST") });
-
- routes.MapRoute(
"v2" + RouteName.DownloadPackage,
"api/v2/package/{id}/{version}",
defaults: new { controller = "Api", action = "GetPackageApi", version = UrlParameter.Optional },
@@ -318,6 +375,18 @@ public static void RegisterRoutes(RouteCollection routes)
constraints: new { httpMethod = new HttpMethodConstraint("PUT") });
routes.MapRoute(
+ "v2" + RouteName.DeletePackageApi,
+ "api/v2/package/{id}/{version}",
+ new { controller = "Api", action = "DeletePackage" },
+ constraints: new { httpMethod = new HttpMethodConstraint("DELETE") });
+
+ routes.MapRoute(
+ "v2" + RouteName.PublishPackageApi,
+ "api/v2/package/{id}/{version}",
+ new { controller = "Api", action = "PublishPackage" },
+ constraints: new { httpMethod = new HttpMethodConstraint("POST") });
+
+ routes.MapRoute(
"v2PackageIds",
"api/v2/package-ids",
new { controller = "Api", action = "PackageIDs" });
@@ -340,12 +409,6 @@ public static void RegisterRoutes(RouteCollection routes)
constraints: new { httpMethod = new HttpMethodConstraint("GET") });
routes.MapRoute(
- RouteName.Team,
- "api/v2/team",
- defaults: new { controller = "Api", action = "Team" },
- constraints: new { httpMethod = new HttpMethodConstraint("GET") });
-
- routes.MapRoute(
RouteName.Status,
"api/status",
new { controller = "Api", action = "StatusApi" });
@@ -354,53 +417,6 @@ public static void RegisterRoutes(RouteCollection routes)
RouteName.DownloadNuGetExe,
"nuget.exe",
new { controller = "Api", action = "GetNuGetExeApi" });
-
- // Redirected Legacy Routes
-
- routes.Redirect(
- r => r.MapRoute(
- "ReportAbuse",
- "Package/ReportAbuse/{id}/{version}",
- new { controller = "Packages", action = "ReportAbuse" }),
- permanent: true).To(packageVersionActionRoute);
-
- routes.Redirect(
- r => r.MapRoute(
- "PackageActions",
- "Package/{action}/{id}",
- new { controller = "Packages", action = "ContactOwners" },
- // This next bit looks bad, but it's not. It will never change because
- // it's mapping the legacy routes to the new better routes.
- new { action = "ContactOwners|ManagePackageOwners" }),
- permanent: true).To(packageActionRoute);
-
- routes.Redirect(
- r => r.MapRoute(
- RouteName.ListPackages,
- "List/Packages",
- new { controller = "Packages", action = "ListPackages" }),
- permanent: true).To(packageListRoute);
-
- routes.Redirect(
- r => r.MapRoute(
- RouteName.DisplayPackage,
- "List/Packages/{id}/{version}",
- new { controller = "Packages", action = "DisplayPackage", version = UrlParameter.Optional }),
- permanent: true).To(packageDisplayRoute);
-
- routes.Redirect(
- r => r.MapRoute(
- RouteName.NewSubmission,
- "Contribute/NewSubmission",
- new { controller = "Packages", action = "UploadPackage" }),
- permanent: true).To(uploadPackageRoute);
-
- routes.Redirect(
- r => r.MapRoute(
- "LegacyDownloadRoute",
- "v1/Package/Download/{id}/{version}",
- new { controller = "Api", action = "GetPackageApi", version = UrlParameter.Optional }),
- permanent: true).To(downloadRoute);
}
// note: Pulled out service route registration separately because it's not testable T.T (won't run outside IIS/WAS)
@@ -119,6 +119,11 @@ public virtual void CreateSession(IOwinContext owinContext, User user)
public virtual async Task<AuthenticatedUser> Register(string username, string emailAddress, Credential credential)
{
+ if (Config.FeedOnlyMode)
+ {
+ throw new FeedOnlyModeException(FeedOnlyModeException.FeedOnlyModeError);
+ }
+
var existingUser = Entities.Users
.FirstOrDefault(u => u.Username == username || u.EmailAddress == emailAddress);
if (existingUser != null)
@@ -0,0 +1,13 @@
+using System;
+
+namespace NuGetGallery.Authentication
+{
+ [Serializable]
+ public class FeedOnlyModeException : Exception
+ {
+ public const string FeedOnlyModeError = "Illegal request! Running on Feed Only mode. User Registration or authentication is disallowed.";
+ public FeedOnlyModeException() { }
+
+ public FeedOnlyModeException(string message) : base(message) { }
+ }
+}
@@ -52,6 +52,11 @@ public class AppConfiguration : IAppConfiguration
public bool ReadOnlyMode { get; set; }
/// <summary>
+ /// Gets if only service feeds should be registered
+ /// </summary>
+ public bool FeedOnlyMode { get; set; }
+
+ /// <summary>
/// Gets the local directory in which to store files.
/// </summary>
[DefaultValue("~/App_Data/Files")]
@@ -51,6 +51,11 @@ public interface IAppConfiguration
bool ReadOnlyMode { get; set; }
/// <summary>
+ /// Gets if only service feeds should be registered
+ /// </summary>
+ bool FeedOnlyMode { get; set; }
+
+ /// <summary>
/// Gets the local directory in which to store files.
/// </summary>
string FileStorageDirectory { get; set; }
@@ -1,5 +1,6 @@
using System;
using System.Linq;
+using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
@@ -48,6 +49,11 @@ public virtual ActionResult Contact()
return View();
}
+ public virtual ActionResult EmptyHome()
+ {
+ return new HttpStatusCodeResult(HttpStatusCode.OK, "Empty Home");
+ }
+
public virtual async Task<ActionResult> Terms()
{
if (ContentService != null)
@@ -375,6 +375,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
+ <Compile Include="Authentication\FeedOnlyModeException.cs" />
<Compile Include="Diagnostics\PerfCounters.cs" />
<Compile Include="Diagnostics\ProcessPerfEvents.cs" />
<Compile Include="App_Start\OwinStartup.cs" />

0 comments on commit ddd4d58

Please sign in to comment.