Exemple #1
0
        /// <summary>
        /// Look up and evaluation of security policies for the specified action.
        /// </summary>
        public async Task <SecurityPolicyResult> EvaluateAsync(SecurityPolicyAction action, HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            var user = httpContext.GetCurrentUser();

            foreach (var handler in UserHandlers.Where(h => h.Action == action))
            {
                var foundPolicies = user.SecurityPolicies.Where(p => p.Name.Equals(handler.Name, StringComparison.OrdinalIgnoreCase));
                if (foundPolicies.Any())
                {
                    var result = handler.Evaluate(new UserSecurityPolicyEvaluationContext(httpContext, foundPolicies));

                    await Auditing.SaveAuditRecordAsync(new UserSecurityPolicyAuditRecord(
                                                            user.Username, GetAuditAction(action), foundPolicies, result.Success, result.ErrorMessage));

                    if (!result.Success)
                    {
                        Diagnostics.Information(
                            $"Security policy '{handler.Name}' failed for user '{user.Username}' with error '{result.ErrorMessage}'.");

                        return(result);
                    }
                }
            }

            return(SecurityPolicyResult.SuccessResult);
        }
 /// <summary>
 /// Evaluate organization security policies for the specified action.
 /// Note that the policy source (organization) and policy target (member) accounts are required in
 /// order to look up and evaluate policies against the relevant accounts.
 /// </summary>
 /// <param name="action">Gallery action to evaluate.</param>
 /// <param name="organization">Organization (policy source) account.</param>
 /// <param name="account">Member, current or future, (policy target) account.</param>
 /// <returns></returns>
 public Task <SecurityPolicyResult> EvaluateOrganizationPoliciesAsync(
     SecurityPolicyAction action,
     Organization organization,
     User account)
 {
     return(EvaluateInternalAsync(action, organization.SecurityPolicies, organization, account, auditSuccess: true));
 }
Exemple #3
0
        /// <summary>
        /// Look up and evaluation of security policies for the specified action.
        /// </summary>
        public async Task <SecurityPolicyResult> EvaluateAsync(SecurityPolicyAction action, HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            // Evaluate default policies
            if (Configuration.EnforceDefaultSecurityPolicies)
            {
                var defaultPolicies = DefaultSubscription.Policies;

                var result = await EvaluateInternalAsync(defaultPolicies, httpContext, action, auditSuccess : false);

                if (!result.Success)
                {
                    return(result);
                }
            }

            // Evaluate user specific policies
            var user = httpContext.GetCurrentUser();

            return(await EvaluateInternalAsync(user.SecurityPolicies, httpContext, action, auditSuccess : true));
        }
 public RequiredSignerPolicy(string policyName, SecurityPolicyAction action)
     : base(policyName, action)
 {
     Policies = new[]
     {
         new UserSecurityPolicy(policyName, policyName)
     };
 }
Exemple #5
0
        public UserSecurityPolicyHandler(string name, SecurityPolicyAction action)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            Name   = name;
            Action = action;
        }
 private Task <SecurityPolicyResult> EvaluateUserPoliciesInternalAsync(
     SecurityPolicyAction action,
     User currentUser,
     HttpContextBase httpContext,
     IEnumerable <UserSecurityPolicy> policies = null,
     bool auditSuccess = true)
 {
     policies = policies ?? currentUser.SecurityPolicies;
     return(EvaluateInternalAsync(action, policies, currentUser, currentUser, httpContext, auditSuccess));
 }
        /// <summary>
        /// Evaluate package security policies for the specified action.
        /// </summary>
        /// <param name="action">Gallery action to evaluate.</param>
        /// <param name="httpContext">Current http context.</param>
        /// <param name="package">The package to evaluate.</param>
        /// <param name="packageRegistration">The package registration. Will be <code>null</code> if the <paramref name="package"/> has a new package ID.</param>
        /// <returns></returns>
        public Task <SecurityPolicyResult> EvaluatePackagePoliciesAsync(
            SecurityPolicyAction action,
            Package package,
            User currentUser,
            User owner,
            HttpContextBase httpContext)
        {
            currentUser = currentUser ?? throw new ArgumentNullException(nameof(currentUser));
            owner       = owner ?? throw new ArgumentNullException(nameof(owner));
            httpContext = httpContext ?? throw new ArgumentNullException(nameof(httpContext));

            return(EvaluatePackagePoliciesInternalAsync(action, package, currentUser, owner, httpContext));
        }
Exemple #8
0
        private Task <SecurityPolicyResult> EvaluateUserPoliciesInternalAsync(
            SecurityPolicyAction action,
            HttpContextBase httpContext,
            IEnumerable <UserSecurityPolicy> policies = null,
            bool auditSuccess = true)
        {
            httpContext = httpContext ?? throw new ArgumentNullException(nameof(httpContext));

            var account = httpContext.GetCurrentUser();

            policies = policies ?? account.SecurityPolicies;
            return(EvaluateInternalAsync(action, policies, account, account, httpContext, auditSuccess));
        }
Exemple #9
0
        private AuditedSecurityPolicyAction GetAuditAction(SecurityPolicyAction policyAction)
        {
            switch (policyAction)
            {
            case SecurityPolicyAction.PackagePush:
                return(AuditedSecurityPolicyAction.Create);

            case SecurityPolicyAction.PackageVerify:
                return(AuditedSecurityPolicyAction.Verify);

            default:
                throw new NotSupportedException($"Policy action '{nameof(policyAction)}' is not supported");
            }
        }
Exemple #10
0
        /// <summary>
        /// Evaluate user security policies for the specified action.
        /// Note that http context is required here, as previous user security policies have required it
        /// in order to get he current user and request details. This API is not currently used since
        /// previous user policies were removed from the Gallery.
        /// </summary>
        /// <param name="action">Gallery action to evaluate.</param>
        /// <param name="httpContext">Current http context.</param>
        /// <returns></returns>
        public async Task <SecurityPolicyResult> EvaluateUserPoliciesAsync(
            SecurityPolicyAction action,
            HttpContextBase httpContext)
        {
            // Evaluate default policies
            if (Configuration.EnforceDefaultSecurityPolicies)
            {
                var defaultPolicies = DefaultSubscription.Policies;

                var result = await EvaluateUserPoliciesInternalAsync(action, httpContext, defaultPolicies, auditSuccess : false);

                if (!result.Success)
                {
                    return(result);
                }
            }

            // Evaluate user specific policies
            return(await EvaluateUserPoliciesInternalAsync(action, httpContext, auditSuccess : true));
        }
Exemple #11
0
        /// <summary>
        /// Look up and evaluation of security policies for the specified action.
        /// </summary>
        public SecurityPolicyResult Evaluate(SecurityPolicyAction action, HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            var user = httpContext.GetCurrentUser();

            foreach (var handler in UserPolicyHandlers.Where(h => h.Action == action))
            {
                var foundPolicies = user.SecurityPolicies.Where(p => p.Name.Equals(handler.Name, StringComparison.OrdinalIgnoreCase));
                if (foundPolicies.Any())
                {
                    var result = handler.Evaluate(new UserSecurityPolicyContext(httpContext, foundPolicies));
                    if (!result.Success)
                    {
                        return(result);
                    }
                }
            }
            return(SecurityPolicyResult.SuccessResult);
        }
        private async Task <SecurityPolicyResult> EvaluateInternalAsync(
            SecurityPolicyAction action,
            IEnumerable <UserSecurityPolicy> policies,
            User sourceAccount,
            User targetAccount,
            HttpContextBase httpContext = null,
            bool auditSuccess           = true)
        {
            var relevantHandlers = UserHandlers.Where(h => h.Action == action).ToList();

            foreach (var handler in relevantHandlers)
            {
                var foundPolicies = policies.Where(p => p.Name.Equals(handler.Name, StringComparison.OrdinalIgnoreCase)).ToList();

                if (foundPolicies.Any())
                {
                    var context = new UserSecurityPolicyEvaluationContext(foundPolicies, sourceAccount, targetAccount, httpContext);
                    var result  = await handler.EvaluateAsync(context);

                    if (auditSuccess || !result.Success)
                    {
                        await Auditing.SaveAuditRecordAsync(new UserSecurityPolicyAuditRecord(
                                                                context.TargetAccount.Username, GetAuditAction(action), foundPolicies, result.Success, result.ErrorMessage));
                    }

                    if (!result.Success)
                    {
                        Diagnostics.Information(
                            $"Security policy from subscription '{foundPolicies.First().Subscription}' - '{handler.Name}' failed with error '{result.ErrorMessage}'.");

                        return(result);
                    }
                }
            }

            return(SecurityPolicyResult.SuccessResult);
        }
        /// <summary>
        /// Evaluate user security policies for the specified action.
        /// Note that http context is required here, as previous user security policies have required it
        /// in order to get he current user and request details. This API is not currently used since
        /// previous user policies were removed from the Gallery.
        /// </summary>
        /// <param name="action">Gallery action to evaluate.</param>
        /// <param name="httpContext">Current http context.</param>
        /// <returns></returns>
        public async Task <SecurityPolicyResult> EvaluateUserPoliciesAsync(
            SecurityPolicyAction action,
            User currentUser,
            HttpContextBase httpContext)
        {
            currentUser = currentUser ?? throw new ArgumentNullException(nameof(currentUser));
            httpContext = httpContext ?? throw new ArgumentNullException(nameof(httpContext));

            // Evaluate default policies
            if (Configuration.EnforceDefaultSecurityPolicies)
            {
                var defaultPolicies = DefaultSubscription.Policies;

                var result = await EvaluateUserPoliciesInternalAsync(action, currentUser, httpContext, defaultPolicies, auditSuccess : false);

                if (!result.Success)
                {
                    return(result);
                }
            }

            // Evaluate user specific policies
            return(await EvaluateUserPoliciesInternalAsync(action, currentUser, httpContext, auditSuccess : true));
        }
Exemple #14
0
 public UserSecurityPolicyHandler(string name, SecurityPolicyAction action)
     : base(name, action)
 {
 }
        private async Task <SecurityPolicyResult> EvaluatePackagePoliciesInternalAsync(
            SecurityPolicyAction action,
            Package package,
            User sourceAccount,
            User targetAccount,
            HttpContextBase httpContext,
            IEnumerable <UserSecurityPolicy> policies = null,
            bool auditSuccess = true)
        {
            policies = policies ?? targetAccount.SecurityPolicies;

            var relevantHandlers = PackageHandlers.Where(h => h.Action == action).ToList();

            var packagePoliciesResult = SecurityPolicyResult.SuccessResult;

            foreach (var handler in relevantHandlers)
            {
                var foundPolicies = policies.Where(p => p.Name.Equals(handler.Name, StringComparison.OrdinalIgnoreCase)).ToList();

                if (foundPolicies.Any())
                {
                    var context = new PackageSecurityPolicyEvaluationContext(
                        _userService.Value,
                        _packageOwnershipManagementService.Value,
                        _telemetryService,
                        foundPolicies,
                        package,
                        sourceAccount,
                        targetAccount,
                        httpContext);

                    var result = await handler.EvaluateAsync(context);

                    if (auditSuccess || !result.Success)
                    {
                        await Auditing.SaveAuditRecordAsync(new UserSecurityPolicyAuditRecord(
                                                                context.TargetAccount.Username, GetAuditAction(action), foundPolicies, result.Success, result.ErrorMessage));
                    }

                    if (!result.Success)
                    {
                        Diagnostics.Information(
                            $"Security policy from subscription '{foundPolicies.First().Subscription}' - '{handler.Name}' failed with error '{result.ErrorMessage}'.");

                        return(result);
                    }

                    if (result.HasWarnings)
                    {
                        if (packagePoliciesResult == SecurityPolicyResult.SuccessResult)
                        {
                            packagePoliciesResult = result;
                        }
                        else
                        {
                            packagePoliciesResult.AddWarnings(result.WarningMessages);
                        }
                    }
                }
            }

            return(packagePoliciesResult);
        }
 public Task <SecurityPolicyResult> EvaluateOrganizationPoliciesAsync(SecurityPolicyAction action, Organization organization, User account)
 {
     throw new NotImplementedException();
 }
 public ApiAuthorizeAttribute(SecurityPolicyAction action)
 {
     SecurityPolicyAction = action;
 }
Exemple #18
0
 public PackageSecurityPolicyHandler(string name, SecurityPolicyAction action)
     : base(name, action)
 {
 }
 public Task <SecurityPolicyResult> EvaluateUserPoliciesAsync(SecurityPolicyAction action, User user, HttpContextBase httpContext)
 {
     throw new NotImplementedException();
 }
 public Task <SecurityPolicyResult> EvaluatePackagePoliciesAsync(SecurityPolicyAction action, Package package, User currentUser, User owner, HttpContextBase httpContext)
 {
     throw new NotImplementedException();
 }