Skip to content

Commit 99587bb

Browse files
authored
Add read only mode (loic-sharma#236)
1 parent 5c2d4cb commit 99587bb

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/BaGet.Core.Server/Controllers/PackagePublishController.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Threading;
33
using System.Threading.Tasks;
4+
using BaGet.Core.Configuration;
45
using BaGet.Core.Services;
56
using BaGet.Extensions;
67
using Microsoft.AspNetCore.Mvc;
78
using Microsoft.Extensions.Logging;
9+
using Microsoft.Extensions.Options;
810
using NuGet.Versioning;
911

1012
namespace BaGet.Controllers
@@ -15,26 +17,30 @@ public class PackagePublishController : Controller
1517
private readonly IPackageIndexingService _indexer;
1618
private readonly IPackageService _packages;
1719
private readonly IPackageDeletionService _deleteService;
20+
private readonly IOptionsSnapshot<BaGetOptions> _options;
1821
private readonly ILogger<PackagePublishController> _logger;
1922

2023
public PackagePublishController(
2124
IAuthenticationService authentication,
2225
IPackageIndexingService indexer,
2326
IPackageService packages,
2427
IPackageDeletionService deletionService,
28+
IOptionsSnapshot<BaGetOptions> options,
2529
ILogger<PackagePublishController> logger)
2630
{
2731
_authentication = authentication ?? throw new ArgumentNullException(nameof(authentication));
2832
_indexer = indexer ?? throw new ArgumentNullException(nameof(indexer));
2933
_packages = packages ?? throw new ArgumentNullException(nameof(packages));
3034
_deleteService = deletionService ?? throw new ArgumentNullException(nameof(deletionService));
35+
_options = options ?? throw new ArgumentNullException(nameof(options));
3136
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
3237
}
3338

3439
// See: https://docs.microsoft.com/en-us/nuget/api/package-publish-resource#push-a-package
3540
public async Task Upload(CancellationToken cancellationToken)
3641
{
37-
if (!await _authentication.AuthenticateAsync(Request.GetApiKey()))
42+
if (_options.Value.IsReadOnlyMode ||
43+
!await _authentication.AuthenticateAsync(Request.GetApiKey()))
3844
{
3945
HttpContext.Response.StatusCode = 401;
4046
return;
@@ -79,6 +85,11 @@ public async Task Upload(CancellationToken cancellationToken)
7985
[HttpDelete]
8086
public async Task<IActionResult> Delete(string id, string version, CancellationToken cancellationToken)
8187
{
88+
if (_options.Value.IsReadOnlyMode)
89+
{
90+
return Unauthorized();
91+
}
92+
8293
if (!NuGetVersion.TryParse(version, out var nugetVersion))
8394
{
8495
return NotFound();
@@ -102,6 +113,11 @@ public async Task<IActionResult> Delete(string id, string version, CancellationT
102113
[HttpPost]
103114
public async Task<IActionResult> Relist(string id, string version)
104115
{
116+
if (_options.Value.IsReadOnlyMode)
117+
{
118+
return Unauthorized();
119+
}
120+
105121
if (!NuGetVersion.TryParse(version, out var nugetVersion))
106122
{
107123
return NotFound();

src/BaGet.Core.Server/Controllers/SymbolController.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using System;
1+
using System;
22
using System.Threading;
33
using System.Threading.Tasks;
4+
using BaGet.Core.Configuration;
45
using BaGet.Core.Services;
56
using BaGet.Extensions;
67
using Microsoft.AspNetCore.Mvc;
78
using Microsoft.Extensions.Logging;
9+
using Microsoft.Extensions.Options;
810

911
namespace BaGet.Controllers
1012
{
@@ -13,24 +15,27 @@ public class SymbolController : Controller
1315
private readonly IAuthenticationService _authentication;
1416
private readonly ISymbolIndexingService _indexer;
1517
private readonly ISymbolStorageService _storage;
18+
private readonly IOptionsSnapshot<BaGetOptions> _options;
1619
private readonly ILogger<SymbolController> _logger;
1720

1821
public SymbolController(
1922
IAuthenticationService authentication,
2023
ISymbolIndexingService indexer,
2124
ISymbolStorageService storage,
25+
IOptionsSnapshot<BaGetOptions> options,
2226
ILogger<SymbolController> logger)
2327
{
2428
_authentication = authentication ?? throw new ArgumentNullException(nameof(authentication));
2529
_indexer = indexer ?? throw new ArgumentNullException(nameof(indexer));
2630
_storage = storage ?? throw new ArgumentNullException(nameof(storage));
31+
_options = options ?? throw new ArgumentNullException(nameof(options));
2732
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
2833
}
2934

3035
// See: https://docs.microsoft.com/en-us/nuget/api/package-publish-resource#push-a-package
3136
public async Task Upload(CancellationToken cancellationToken)
3237
{
33-
if (!await _authentication.AuthenticateAsync(Request.GetApiKey()))
38+
if (_options.Value.IsReadOnlyMode || !await _authentication.AuthenticateAsync(Request.GetApiKey()))
3439
{
3540
HttpContext.Response.StatusCode = 401;
3641
return;

src/BaGet.Core/Configuration/BaGetOptions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public class BaGetOptions
3232
/// </summary>
3333
public bool AllowPackageOverwrites { get; set; } = false;
3434

35+
/// <summary>
36+
/// If true, disables package pushing, deleting, and relisting.
37+
/// </summary>
38+
public bool IsReadOnlyMode { get; set; } = false;
39+
3540
[Required]
3641
public DatabaseOptions Database { get; set; }
3742

0 commit comments

Comments
 (0)