-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'anurse/253-msftaccount' into dev
Conflicts: src/NuGetGallery.Cloud/ServiceConfiguration.Local.cscfg src/NuGetGallery.Cloud/ServiceDefinition.csdef src/NuGetGallery/App_Start/AppActivator.cs src/NuGetGallery/Authentication/Providers/ApiKey/ApiKeyAuthenticationHandler.cs src/NuGetGallery/Content/Logos/herowithlogo.png src/NuGetGallery/Content/Logos/nugetLogoFooter.png src/NuGetGallery/Content/Site.css src/NuGetGallery/RouteNames.cs src/NuGetGallery/T4MVC.cs src/NuGetGallery/Views/Authentication/_RegisterForm.cshtml src/NuGetGallery/Views/Packages/Edit.cshtml src/NuGetGallery/Web.config tests/NuGetGallery.Facts/Authentication/Providers/ApiKey/ApiKeyAuthenticationHandlerFacts.cs
- Loading branch information
Showing
165 changed files
with
10,914 additions
and
2,905 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.NetworkInformation; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NuGetGallery.Auditing | ||
{ | ||
public class AuditActor | ||
{ | ||
public string MachineName { get; set; } | ||
public string MachineIP { get; set; } | ||
public string UserName { get; set; } | ||
public string AuthenticationType { get; set; } | ||
public DateTime TimestampUtc { get; set; } | ||
|
||
public AuditActor OnBehalfOf { get; set; } | ||
|
||
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, DateTime timeStampUtc, AuditActor onBehalfOf) | ||
{ | ||
MachineName = machineName; | ||
UserName = userName; | ||
AuthenticationType = authenticationType; | ||
TimestampUtc = timeStampUtc; | ||
OnBehalfOf = onBehalfOf; | ||
} | ||
|
||
public static Task<AuditActor> GetCurrentMachineActor() | ||
{ | ||
return GetCurrentMachineActor(null); | ||
} | ||
|
||
public static async Task<AuditActor> GetCurrentMachineActor(AuditActor onBehalfOf) | ||
{ | ||
// Try to get local IP | ||
string ipAddress = await GetLocalIP(); | ||
|
||
return new AuditActor( | ||
Environment.MachineName, | ||
ipAddress, | ||
String.Format(@"{0}\{1}", Environment.UserDomainName, Environment.UserName), | ||
"MachineUser", | ||
DateTime.UtcNow, | ||
onBehalfOf); | ||
} | ||
|
||
public static async Task<string> GetLocalIP() | ||
{ | ||
string ipAddress = null; | ||
if (NetworkInterface.GetIsNetworkAvailable()) | ||
{ | ||
var entry = await Dns.GetHostEntryAsync(Dns.GetHostName()); | ||
if (entry != null) | ||
{ | ||
ipAddress = | ||
TryGetAddress(entry.AddressList, AddressFamily.InterNetworkV6) ?? | ||
TryGetAddress(entry.AddressList, AddressFamily.InterNetwork); | ||
} | ||
} | ||
return ipAddress; | ||
} | ||
|
||
private static string TryGetAddress(IEnumerable<IPAddress> addrs, AddressFamily family) | ||
{ | ||
return addrs.Where(a => a.AddressFamily == family).Select(a => a.ToString()).FirstOrDefault(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NuGetGallery.Auditing | ||
{ | ||
/// <summary> | ||
/// Represents the actual data stored in an audit entry, an AuditRecord/AuditEnvironment pair | ||
/// </summary> | ||
public class AuditEntry | ||
{ | ||
public AuditRecord Record { get; set; } | ||
public AuditActor Actor { get; set; } | ||
|
||
public AuditEntry() | ||
{ | ||
} | ||
|
||
public AuditEntry(AuditRecord record, AuditActor actor) | ||
{ | ||
Record = record; | ||
Actor = actor; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace NuGetGallery.Auditing | ||
{ | ||
public abstract class AuditRecord | ||
{ | ||
private string _resourceType; | ||
|
||
public abstract string GetPath(); | ||
|
||
public virtual string GetResourceType() | ||
{ | ||
return _resourceType ?? (_resourceType = InferResourceType()); | ||
} | ||
|
||
public abstract string GetAction(); | ||
|
||
private string InferResourceType() | ||
{ | ||
string type = GetType().Name; | ||
if (type.EndsWith("AuditRecord", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return type.Substring(0, type.Length - 11); | ||
} | ||
return type; | ||
} | ||
} | ||
|
||
public abstract class AuditRecord<T> : AuditRecord | ||
where T : struct | ||
{ | ||
public T Action { get; set; } | ||
|
||
protected AuditRecord(T action) | ||
{ | ||
Action = action; | ||
} | ||
|
||
public override string GetAction() | ||
{ | ||
return Action.ToString().ToLowerInvariant(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
|
||
namespace NuGetGallery.Auditing | ||
{ | ||
public abstract class AuditingService | ||
{ | ||
public static readonly AuditingService None = new NullAuditingService(); | ||
|
||
private static readonly JsonSerializerSettings _auditRecordSerializerSettings; | ||
|
||
static AuditingService() | ||
{ | ||
var settings = new JsonSerializerSettings() | ||
{ | ||
DateFormatHandling = DateFormatHandling.IsoDateFormat, | ||
DateTimeZoneHandling = DateTimeZoneHandling.Utc, | ||
DefaultValueHandling = DefaultValueHandling.Include, | ||
Formatting = Formatting.Indented, | ||
MaxDepth = 10, | ||
MissingMemberHandling = MissingMemberHandling.Ignore, | ||
NullValueHandling = NullValueHandling.Include, | ||
TypeNameHandling = TypeNameHandling.None | ||
}; | ||
settings.Converters.Add(new StringEnumConverter()); | ||
_auditRecordSerializerSettings = settings; | ||
} | ||
|
||
public virtual async Task<Uri> SaveAuditRecord(AuditRecord record) | ||
{ | ||
// Build an audit entry | ||
var entry = new AuditEntry(record, await GetActor()); | ||
|
||
// Serialize to json | ||
string rendered = RenderAuditEntry(entry); | ||
|
||
// Save the record | ||
return await SaveAuditRecord(rendered, record.GetResourceType(), record.GetPath(), record.GetAction(), entry.Actor.TimestampUtc); | ||
} | ||
|
||
public virtual string RenderAuditEntry(AuditEntry entry) | ||
{ | ||
return JsonConvert.SerializeObject(entry, _auditRecordSerializerSettings); | ||
} | ||
|
||
/// <summary> | ||
/// Performs the actual saving of audit data to an audit store | ||
/// </summary> | ||
/// <param name="auditData">The data to store in the audit record</param> | ||
/// <param name="resourceType">The type of resource affected by the audit (usually used as the first-level folder)</param> | ||
/// <param name="filePath">The file-system path to use to identify the audit record</param> | ||
/// <param name="action">The action recorded in this audit record</param> | ||
/// <param name="timestamp">A timestamp indicating when the record was created</param> | ||
/// <returns>The URI identifying the audit record resource</returns> | ||
protected abstract Task<Uri> SaveAuditRecord(string auditData, string resourceType, string filePath, string action, DateTime timestamp); | ||
|
||
protected virtual Task<AuditActor> GetActor() | ||
{ | ||
return AuditActor.GetCurrentMachineActor(); | ||
} | ||
|
||
private class NullAuditingService : AuditingService | ||
{ | ||
protected override Task<Uri> SaveAuditRecord(string auditData, string resourceType, string filePath, string action, DateTime timestamp) | ||
{ | ||
return Task.FromResult<Uri>(new Uri("http://auditing.local/" + resourceType + "/" + filePath + "/" + timestamp.ToString("s") + "-" + action.ToLowerInvariant())); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.