-
Notifications
You must be signed in to change notification settings - Fork 645
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
Telemetry for temp keys #3662
Merged
+154
−33
Merged
Telemetry for temp keys #3662
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -389,20 +389,28 @@ public static string GetAuthenticationType(this IIdentity self) | |
return identity?.GetClaimOrDefault(ClaimTypes.AuthenticationMethod); | ||
} | ||
|
||
public static bool IsScopedAuthentication(this IIdentity self) | ||
private static string GetScopeClaim(this IIdentity self) | ||
{ | ||
var identity = self as ClaimsIdentity; | ||
|
||
if (identity == null) | ||
{ | ||
return false; | ||
} | ||
return identity?.GetClaimOrDefault(NuGetClaims.Scope); | ||
} | ||
|
||
var scopeClaim = identity.GetClaimOrDefault(NuGetClaims.Scope); | ||
public static bool IsScopedAuthentication(this IIdentity self) | ||
{ | ||
var scopeClaim = self.GetScopeClaim(); | ||
|
||
return !ScopeEvaluator.IsEmptyScopeClaim(scopeClaim); | ||
} | ||
|
||
public static bool HasVerifyScope(this IIdentity self) | ||
{ | ||
var scopeClaim = self.GetScopeClaim(); | ||
|
||
return ScopeEvaluator.ScopeClaimsAllowsActionForSubject(scopeClaim, null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
new [] { NuGetScopes.PackageVerify }); | ||
} | ||
|
||
// This is a method because the first call will perform a database call | ||
/// <summary> | ||
/// Get the current user, from the database, or if someone in this request has already | ||
|
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 |
---|---|---|
|
@@ -4,38 +4,76 @@ | |
using System; | ||
using System.Collections.Generic; | ||
using System.Security.Principal; | ||
using System.Web; | ||
|
||
namespace NuGetGallery | ||
{ | ||
public class TelemetryService : ITelemetryService | ||
{ | ||
// ODataQueryFilter | ||
public const string ODataQueryFilter = "ODataQueryFilter"; | ||
// Event types | ||
public const string ODataQueryFilterEvent = "ODataQueryFilter"; | ||
public const string PackagePushEvent = "PackagePush"; | ||
public const string CreatePackageVerificationKeyEvent = "CreatePackageVerificationKeyEvent"; | ||
public const string VerifyPackageKeyEvent = "VerifyPackageKeyEvent"; | ||
|
||
// ODataQueryFilter properties | ||
public const string CallContext = "CallContext"; | ||
public const string IsEnabled = "IsEnabled"; | ||
public const string IsAllowed = "IsAllowed"; | ||
public const string QueryPattern = "QueryPattern"; | ||
|
||
// Package push | ||
public const string PackagePush = "PackagePush"; | ||
public const string AuthenticatinMethod = "AuthenticationMethod"; | ||
// Package push properties | ||
public const string AuthenticationMethod = "AuthenticationMethod"; | ||
public const string AccountCreationDate = "AccountCreationDate"; | ||
public const string ClientVersion = "ClientVersion"; | ||
public const string IsScoped = "IsScoped"; | ||
public const string PackageId = "PackageId"; | ||
public const string PackageVersion = "PackageVersion"; | ||
|
||
// Verify package properties | ||
public const string HasVerifyScope = "HasVerifyScope"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider renaming HasVerifyScope to "IsVerificationKeyUsed". The usage of scopes in this scenario is an implementation detail.. |
||
public const string VerifyPackageKeyStatusCode = "VerifyPackageKeyStatusCode"; | ||
|
||
public void TrackODataQueryFilterEvent(string callContext, bool isEnabled, bool isAllowed, string queryPattern) | ||
{ | ||
var telemetryProperties = new Dictionary<string, string>(); | ||
TrackEvent(ODataQueryFilterEvent, properties => | ||
{ | ||
properties.Add(CallContext, callContext); | ||
properties.Add(IsEnabled, $"{isEnabled}"); | ||
|
||
telemetryProperties.Add(CallContext, callContext); | ||
telemetryProperties.Add(IsEnabled, $"{isEnabled}"); | ||
properties.Add(IsAllowed, $"{isAllowed}"); | ||
properties.Add(QueryPattern, queryPattern); | ||
}); | ||
} | ||
|
||
telemetryProperties.Add(IsAllowed, $"{isAllowed}"); | ||
telemetryProperties.Add(QueryPattern, queryPattern); | ||
public void TrackPackagePushEvent(Package package, User user, IIdentity identity) | ||
{ | ||
if (package == null) | ||
{ | ||
throw new ArgumentNullException(nameof(package)); | ||
} | ||
|
||
Telemetry.TrackEvent(ODataQueryFilter, telemetryProperties, metrics: null); | ||
if (user == null) | ||
{ | ||
throw new ArgumentNullException(nameof(user)); | ||
} | ||
|
||
if (identity == null) | ||
{ | ||
throw new ArgumentNullException(nameof(identity)); | ||
} | ||
|
||
TrackEvent(PackagePushEvent, properties => { | ||
properties.Add(ClientVersion, GetClientVersion()); | ||
properties.Add(PackageId, package.PackageRegistration.Id); | ||
properties.Add(PackageVersion, package.Version); | ||
properties.Add(AuthenticationMethod, identity.GetAuthenticationType()); | ||
properties.Add(AccountCreationDate, GetAccountCreationDate(user)); | ||
properties.Add(IsScoped, identity.IsScopedAuthentication().ToString()); | ||
}); | ||
} | ||
|
||
public void TrackPackagePushEvent(User user, IIdentity identity) | ||
public void TrackCreatePackageVerificationKeyEvent(string packageId, string packageVersion, User user, IIdentity identity) | ||
{ | ||
if (user == null) | ||
{ | ||
|
@@ -47,15 +85,53 @@ public void TrackPackagePushEvent(User user, IIdentity identity) | |
throw new ArgumentNullException(nameof(identity)); | ||
} | ||
|
||
string authenticationMethod = identity.GetAuthenticationType(); | ||
bool isScoped = identity.IsScopedAuthentication(); | ||
TrackEvent(CreatePackageVerificationKeyEvent, properties => { | ||
properties.Add(ClientVersion, GetClientVersion()); | ||
properties.Add(PackageId, packageId); | ||
properties.Add(PackageVersion, packageVersion); | ||
properties.Add(AccountCreationDate, GetAccountCreationDate(user)); | ||
properties.Add(IsScoped, identity.IsScopedAuthentication().ToString()); | ||
}); | ||
} | ||
|
||
public void TrackVerifyPackageKeyEvent(string packageId, string packageVersion, User user, IIdentity identity, int statusCode) | ||
{ | ||
if (user == null) | ||
{ | ||
throw new ArgumentNullException(nameof(user)); | ||
} | ||
|
||
if (identity == null) | ||
{ | ||
throw new ArgumentNullException(nameof(identity)); | ||
} | ||
|
||
TrackEvent(VerifyPackageKeyEvent, properties => | ||
{ | ||
properties.Add(PackageId, packageId); | ||
properties.Add(PackageVersion, packageVersion); | ||
properties.Add(HasVerifyScope, identity.HasVerifyScope().ToString()); | ||
properties.Add(VerifyPackageKeyStatusCode, statusCode.ToString()); | ||
}); | ||
} | ||
|
||
private static string GetClientVersion() | ||
{ | ||
return HttpContext.Current?.Request?.Headers[Constants.ClientVersionHeaderName]; | ||
} | ||
|
||
private static string GetAccountCreationDate(User user) | ||
{ | ||
return user.CreatedUtc != null ? user.CreatedUtc.Value.ToString("d") : "N/A"; | ||
} | ||
|
||
private static void TrackEvent(string eventName, Action<Dictionary<string, string>> addProperties) | ||
{ | ||
var telemetryProperties = new Dictionary<string, string>(); | ||
telemetryProperties.Add(AuthenticatinMethod, authenticationMethod); | ||
telemetryProperties.Add(AccountCreationDate, user.CreatedUtc != null ? user.CreatedUtc.Value.ToString("d") : "N/A"); | ||
telemetryProperties.Add(IsScoped, isScoped.ToString()); | ||
|
||
Telemetry.TrackEvent(PackagePush, telemetryProperties, metrics: null); | ||
addProperties(telemetryProperties); | ||
|
||
Telemetry.TrackEvent(eventName, telemetryProperties, metrics: null); | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the cast necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes due to ??, since EmptyResult can't be cast as HttpStatusCodeWithBodyResult.