Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert https://github.com/NuGet/NuGetGallery/pull/4844 #4850

Merged
merged 2 commits into from
Oct 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions src/NuGetGallery.Core/Auditing/AuditActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Web;

namespace NuGetGallery.Auditing
{
Expand All @@ -17,25 +18,70 @@ public class AuditActor
public string MachineIP { get; set; }
public string UserName { get; set; }
public string AuthenticationType { get; set; }
public string CredentialKey { get; set; }
public DateTime TimestampUtc { get; set; }

public AuditActor OnBehalfOf { get; set; }

public AuditActor(string machineName, string machineIP, string userName, string authenticationType, string credentialKey, DateTime timeStampUtc)
: this(machineName, machineIP, userName, authenticationType, credentialKey, timeStampUtc, null) { }
public AuditActor(string machineName, string machineIP, string userName, string authenticationType, DateTime timeStampUtc)
: this(machineName, machineIP, userName, authenticationType, timeStampUtc, null) { }

public AuditActor(string machineName, string machineIP, string userName, string authenticationType, string credentialKey, DateTime timeStampUtc, AuditActor onBehalfOf)
public AuditActor(string machineName, string machineIP, string userName, string authenticationType, DateTime timeStampUtc, AuditActor onBehalfOf)
{
MachineName = machineName;
MachineIP = machineIP;
UserName = userName;
AuthenticationType = authenticationType;
CredentialKey = credentialKey;
TimestampUtc = timeStampUtc;
OnBehalfOf = onBehalfOf;
}

public static Task<AuditActor> GetAspNetOnBehalfOfAsync()
{
// Use HttpContext to build an actor representing the user performing the action
var context = HttpContext.Current;
if (context == null)
{
return Task.FromResult<AuditActor>(null);
}

return GetAspNetOnBehalfOfAsync(new HttpContextWrapper(context));
}

public static Task<AuditActor> GetAspNetOnBehalfOfAsync(HttpContextBase context)
{
// Try to identify the client IP using various server variables
var clientIpAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(clientIpAddress)) // Try REMOTE_ADDR server variable
{
clientIpAddress = context.Request.ServerVariables["REMOTE_ADDR"];
}

if (string.IsNullOrEmpty(clientIpAddress)) // Try UserHostAddress property
{
clientIpAddress = context.Request.UserHostAddress;
}

if (!string.IsNullOrEmpty(clientIpAddress) && clientIpAddress.IndexOf(".", StringComparison.Ordinal) > 0)
{
clientIpAddress = clientIpAddress.Substring(0, clientIpAddress.LastIndexOf(".", StringComparison.Ordinal)) + ".0";
}

string user = null;
string authType = null;
if (context.User != null)
{
user = context.User.Identity.Name;
authType = context.User.Identity.AuthenticationType;
}

return Task.FromResult(new AuditActor(
null,
clientIpAddress,
user,
authType,
DateTime.UtcNow));
}

public static Task<AuditActor> GetCurrentMachineActorAsync()
{
return GetCurrentMachineActorAsync(null);
Expand All @@ -51,7 +97,6 @@ public static async Task<AuditActor> GetCurrentMachineActorAsync(AuditActor onBe
ipAddress,
$@"{Environment.UserDomainName}\{Environment.UserName}",
"MachineUser",
string.Empty,
DateTime.UtcNow,
onBehalfOf);
}
Expand Down
4 changes: 2 additions & 2 deletions src/NuGetGallery/App_Start/DefaultDependenciesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ private static IAuditingService GetAuditingServiceForLocalFileSystem(IGalleryCon
FileSystemFileStorageService.ResolvePath(configuration.Current.FileStorageDirectory),
FileSystemAuditingService.DefaultContainerName);

return new FileSystemAuditingService(auditingPath, AuditingHelper.GetAspNetOnBehalfOfAsync);
return new FileSystemAuditingService(auditingPath, AuditActor.GetAspNetOnBehalfOfAsync);
}

private static void ConfigureForAzureStorage(ContainerBuilder builder, IGalleryConfigurationService configuration)
Expand Down Expand Up @@ -567,7 +567,7 @@ private static IAuditingService GetAuditingServiceForAzureStorage(ContainerBuild

var localIp = AuditActor.GetLocalIpAddressAsync().Result;

var service = new CloudAuditingService(instanceId, localIp, configuration.Current.AzureStorage_Auditing_ConnectionString, AuditingHelper.GetAspNetOnBehalfOfAsync);
var service = new CloudAuditingService(instanceId, localIp, configuration.Current.AzureStorage_Auditing_ConnectionString, AuditActor.GetAspNetOnBehalfOfAsync);

builder.RegisterInstance(service)
.As<ICloudStorageStatusDependency>()
Expand Down
3 changes: 0 additions & 3 deletions src/NuGetGallery/Authentication/NuGetClaims.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ public static class NuGetClaims
public const string ApiKey = "https://claims.nuget.org/apikey";

public const string Scope = "https://claims.nuget.org/scope";

// Allows identifying the credential that was used by his DB key.
public const string CredentialKey = "https://claims.nuget.org/credentialkey";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
authUser.User,
AuthenticationTypes.ApiKey,
new Claim(NuGetClaims.ApiKey, apiKey),
new Claim(NuGetClaims.Scope, scopes),
new Claim(NuGetClaims.CredentialKey, authUser.CredentialUsed.Key.ToString())),
new Claim(NuGetClaims.Scope, scopes)),
new AuthenticationProperties());
}
else
Expand Down
68 changes: 0 additions & 68 deletions src/NuGetGallery/Helpers/AuditingHelper.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/NuGetGallery/NuGetGallery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,6 @@
<Compile Include="Controllers\PagesController.cs" />
<Compile Include="Filters\ApiScopeRequiredAttribute.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Helpers\AuditingHelper.cs" />
<Compile Include="Helpers\GravatarHelper.cs" />
<Compile Include="Helpers\RegexEx.cs" />
<Compile Include="Services\AsynchronousPackageValidationInitiator.cs" />
Expand Down
Loading