public static void Patch(IPatch patch)
 {
     try
     {
         var original = patch.PatchType().GetMethod(patch.PatchMethod(), patch.PatchParams());
         var prefix   = new HarmonyMethod(patch.GetType().GetMethod("Prefix"));
         var postfix  = new HarmonyMethod(patch.GetType().GetMethod("Postfix"));
         _harmony.Patch(original, prefix, postfix);
         Tracer.Log("Patch ", patch.PatchType(), patch.PatchMethod());
     }
     catch (Exception e)
     {
         Tracer.Log(e);
     }
 }
Exemple #2
0
 /// <summary>
 /// Copy a patch to the clipboard.
 /// </summary>
 /// <param name="patchToPaste"></param>
 /// <param name="patch"></param>
 public virtual void CopyPatch(IPatch patchToPaste, IPatch patch)
 {
     Debug.Assert(patchToPaste.GetType() == patch.GetType());
     Debug.Assert(patchToPaste.ByteLength == patch.ByteLength);
     Util.CopyBytes(this, patchToPaste.ByteOffset, patch.Root.Content, patch.ByteOffset, patch.ByteLength);
     patch.RaisePropertyChanged(string.Empty, false);
 }
        public void ProcessPatches(IEnumerable <KeyValuePair <string, IPatch> > patches, Paths paths)
        {
            foreach (KeyValuePair <string, IPatch> keyValuePair in patches)
            {
                IPatch      patch         = keyValuePair.Value;
                string      assemblyName  = patch.PatchTarget.Name;
                string      patchFullName = patch.GetType().FullName;
                PatchStatus status        = new PatchStatus(patchFullName, Path.GetDirectoryName(keyValuePair.Key));
                if (!_assemblies.TryGetValue(assemblyName, out AssemblyDefinition definition))
                {
                    definition = AssemblyDefinition.ReadAssembly(Path.Combine(paths.ManagedFolderPath, assemblyName + ".dll"));
                    _assemblies.Add(assemblyName, definition);
                }

                try
                {
                    PatchLoaderStatusInfo.Statuses.Add(status.PatchName, status);
                    _logger.Info($"Executing patch {assemblyName} of {keyValuePair.Key}");
                    _assemblies[assemblyName] = patch.Execute(definition, new WithPrefixLogger(_logger, assemblyName), Path.GetDirectoryName(keyValuePair.Key));
                }
                catch (Exception e)
                {
                    _logger.Exception(e, "Patch caused an exception");
                    status.SetError(e.ToString());
                }
            }

            foreach (KeyValuePair <string, AssemblyDefinition> keyValuePair in _assemblies)
            {
                _logger.Info($"Loading assembly: {keyValuePair.Key}");
                LoadAssemblyAndDispose(keyValuePair.Value);
            }
        }
Exemple #4
0
        public static bool HasPatch(this MethodDef method, IPatch patch)
        {
            var patchName = method.CustomAttributes
                            .Where(a => a.TypeFullName == typeof(PatchAttribute).FullName)
                            .Where(a => a.HasConstructorArguments)
                            .Select(a => a.ConstructorArguments[0].Value.ToString()).ToList();

            return(patchName.Any(name => name == patch.GetType().Name));
        }
Exemple #5
0
        public override async Task UpdateAsync(IPatch patch, string correlationIdentifier)
        {
            if (null == patch)
            {
                throw new ArgumentNullException(nameof(patch));
            }

            if (null == patch.ResourceIdentifier)
            {
                throw new ArgumentException(SqlProviderResources.ExceptionInvalidPatch);
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.Identifier))
            {
                throw new ArgumentException(SqlProviderResources.ExceptionInvalidPatch);
            }

            if (null == patch.PatchRequest)
            {
                throw new ArgumentException(SqlProviderResources.ExceptionInvalidPatch);
            }

            PatchRequest2 patchRequest = patch.PatchRequest as PatchRequest2;

            if (null == patchRequest)
            {
                string unsupportedPatchTypeName = patch.GetType().FullName;
                throw new NotSupportedException(unsupportedPatchTypeName);
            }

            Core2EnterpriseUser user = await this.context.Users.Where(u => u.Identifier == patch.ResourceIdentifier.Identifier)
                                       .Include(u => u.Addresses)
                                       .Include(u => u.ElectronicMailAddresses)
                                       .Include(u => u.InstantMessagings)
                                       .Include(u => u.PhoneNumbers)
                                       .FirstOrDefaultAsync();

            if (user == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            user.Apply(patchRequest);
            this.context.Users.Update(user);
            await this.context.SaveChangesAsync();
        }
        public override Task UpdateAsync(IPatch patch, string correlationIdentifier)
        {
            if (null == patch)
            {
                throw new ArgumentNullException(nameof(patch));
            }

            if (null == patch.ResourceIdentifier)
            {
                throw new ArgumentException(SampleServiceResources.ExceptionInvalidPatch);
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.Identifier))
            {
                throw new ArgumentException(SampleServiceResources.ExceptionInvalidPatch);
            }

            if (null == patch.PatchRequest)
            {
                throw new ArgumentException(SampleServiceResources.ExceptionInvalidPatch);
            }

            PatchRequest2 patchRequest =
                patch.PatchRequest as PatchRequest2;

            if (null == patchRequest)
            {
                string unsupportedPatchTypeName = patch.GetType().FullName;
                throw new NotSupportedException(unsupportedPatchTypeName);
            }

            if (this.storage.Groups.TryGetValue(patch.ResourceIdentifier.Identifier, out Core2Group group))
            {
                group.Apply(patchRequest);
            }
            else
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return(Task.CompletedTask);
        }
        public override async Task UpdateAsync(IPatch patch, string correlationIdentifier)
        {
            if (null == patch)
            {
                throw new ArgumentNullException(nameof(patch));
            }

            if (null == patch.ResourceIdentifier)
            {
                throw new ArgumentException("Invalid Patch");
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.Identifier))
            {
                throw new ArgumentException("Invalid Patch");
            }

            if (null == patch.PatchRequest)
            {
                throw new ArgumentException("Invalid Patch");
            }

            PatchRequest2 patchRequest =
                patch.PatchRequest as PatchRequest2;

            if (null == patchRequest)
            {
                string unsupportedPatchTypeName = patch.GetType().FullName;
                throw new NotSupportedException(unsupportedPatchTypeName);
            }

            if (int.TryParse(patch.ResourceIdentifier.Identifier, out int id) && _context.Users.Find(id) is MvcMovie.Models.User modelUser)
            {
                Core2EnterpriseUser scimUser = (Core2EnterpriseUser)modelUser;
                scimUser.Apply(patchRequest);
                await ReplaceAsync(scimUser, correlationIdentifier);
            }
            else
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }
        public override Task UpdateAsync(IPatch patch, string correlationIdentifier)
        {
            if (null == patch)
            {
                throw new ArgumentNullException(nameof(patch));
            }

            if (null == patch.ResourceIdentifier)
            {
                throw new ArgumentException(SampleServiceResources.ExceptionInvalidPatch);
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.Identifier))
            {
                throw new ArgumentException(SampleServiceResources.ExceptionInvalidPatch);
            }

            if (null == patch.PatchRequest)
            {
                throw new ArgumentException(SampleServiceResources.ExceptionInvalidPatch);
            }

            PatchRequest2 patchRequest = patch.PatchRequest as PatchRequest2;

            if (null == patchRequest)
            {
                string unsupportedPatchTypeName = patch.GetType().FullName;
                throw new NotSupportedException(unsupportedPatchTypeName);
            }

            // call service
            TargetGroup target;

            try
            {
                // get group
                target = _storageService.RetrieveGroup(new Guid(patch.ResourceIdentifier.Identifier));

                // patch group
                Core2Group patched = (Core2Group)target;
                patched.Apply(patchRequest);

                // update user
                _storageService.UpdateGroup((TargetGroup)patched);
            }
            catch (Exception err)
            {
                switch (err.Message)
                {
                case "Conflict":
                    throw new HttpResponseException(HttpStatusCode.Conflict);

                case "NotFound":
                    throw new HttpResponseException(HttpStatusCode.NotFound);

                case "InvalidMemberType":
                    throw new HttpResponseException(HttpStatusCode.NotAcceptable);

                default:
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }
            }

            return(Task.CompletedTask);
        }
        private async Task UpdateMembersAsync(Resource resource, IPatch patch)
        {
            if (null == resource)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (null == patch)
            {
                throw new ArgumentNullException(nameof(patch));
            }

            if (null == patch.ResourceIdentifier)
            {
                throw new ArgumentException(FileProviderResources.ExceptionInvalidPatch);
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.Identifier))
            {
                throw new ArgumentException(FileProviderResources.ExceptionInvalidPatch);
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.SchemaIdentifier))
            {
                throw new ArgumentException(FileProviderResources.ExceptionInvalidPatch);
            }

            if (null == patch.PatchRequest)
            {
                throw new ArgumentException(FileProviderResources.ExceptionInvalidPatch);
            }

            PatchRequest2 patchRequest = patch.PatchRequest as PatchRequest2;

            if (null == patchRequest)
            {
                string unsupportedPatchTypeName = patch.GetType().FullName;
                throw new NotSupportedException(unsupportedPatchTypeName);
            }

            if
            (
                !FileProvider
                .SchemaIdentifiersGroup
                .Value
                .Any(
                    (string item) =>
                    string.Equals(item, patch.ResourceIdentifier.SchemaIdentifier, StringComparison.Ordinal))
            )
            {
                return;
            }

            IReadOnlyCollection <PatchOperation> memberOperations =
                patchRequest
                .Operations
                .Where(
                    (PatchOperation item) =>
                    item.Path != null &&
                    string.Equals(item.Path.AttributePath, AttributeNames.Members, StringComparison.Ordinal))
                .ToArray();

            if (!memberOperations.Any())
            {
                return;
            }

            foreach (PatchOperation memberOperation in memberOperations)
            {
                if (null == memberOperation.Value)
                {
                    continue;
                }

                foreach (OperationValue value in memberOperation.Value)
                {
                    if (string.IsNullOrWhiteSpace(value.Value))
                    {
                        continue;
                    }

                    Dictionary <string, string> columnsQuery =
                        new Dictionary <string, string>()
                    {
                        {
                            AttributeNames.Schemas,
                            patch.ResourceIdentifier.SchemaIdentifier
                        },
                        {
                            AttributeNames.Identifier,
                            patch.ResourceIdentifier.Identifier
                        },
                        {
                            AttributeNames.Members,
                            value.Value
                        }
                    };
                    IRow[] rows = await this.file.Query(columnsQuery);

                    switch (memberOperation.Name)
                    {
                    case OperationName.Add:
                        if (rows.Any())
                        {
                            break;
                        }

                        Member member =
                            new Member()
                        {
                            Value = value.Value
                        };
                        MemberColumnsFactory memberColumnsFactory          = new MemberColumnsFactory(resource, member);
                        IReadOnlyDictionary <string, string> columnsMember = memberColumnsFactory.CreateColumns();
                        await this.file.InsertRow(columnsMember);

                        break;

                    case OperationName.Remove:
                        foreach (IRow row in rows)
                        {
                            await this.file.RemoveRow(row.Key);
                        }

                        break;
                    }
                }
            }
        }
        public override async Task UpdateAsync(IPatch patch, string correlationIdentifier)
        {
            if (null == patch)
            {
                throw new ArgumentNullException(nameof(patch));
            }

            if (null == patch.ResourceIdentifier)
            {
                throw new ArgumentException(FileProviderResources.ExceptionInvalidPatch);
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.Identifier))
            {
                throw new ArgumentException(FileProviderResources.ExceptionInvalidPatch);
            }

            if (null == patch.PatchRequest)
            {
                throw new ArgumentException(FileProviderResources.ExceptionInvalidPatch);
            }

            IInformationNotification notification =
                VerboseInformationNotificationFactory.Instance.FormatNotification(
                    FileProviderResources.InformationPatching,
                    correlationIdentifier,
                    FileProvider.NotificationIdentifierUpdateStarting,
                    patch.ResourceIdentifier.SchemaIdentifier,
                    patch.ResourceIdentifier.Identifier);

            this.Monitor.Inform(notification);

            PatchRequest2 patchRequest = patch.PatchRequest as PatchRequest2;

            if (null == patchRequest)
            {
                string unsupportedPatchTypeName = patch.GetType().FullName;
                throw new NotSupportedException(unsupportedPatchTypeName);
            }

            IRow row = await this.file.ReadRow(patch.ResourceIdentifier.Identifier);

            string rowSchema = null;

            if
            (
                !row.Columns.TryGetValue(AttributeNames.Schemas, out rowSchema) ||
                !string.Equals(rowSchema, patch.ResourceIdentifier.SchemaIdentifier, StringComparison.Ordinal)
            )
            {
                return;
            }

            IReadOnlyDictionary <string, string> columns = FileProvider.Apply(patchRequest, rowSchema, row);
            IRow rowReplacement = new Row(row.Key, columns);

            await this.file.ReplaceRow(rowReplacement);

            if (string.Equals(SchemaIdentifiers.WindowsAzureActiveDirectoryGroup, rowSchema, StringComparison.OrdinalIgnoreCase))
            {
                WindowsAzureActiveDirectoryGroup group = new GroupFactory(row).Create();
                await this.UpdateMembersAsync(group, patch);
            }
        }
        public override async Task Update(
            IPatch patch,
            string correlationIdentifier)
        {
            if (null == patch)
            {
                throw new ArgumentNullException(AmazonWebServicesProvider.ArgumentNamePatch);
            }

            if (string.IsNullOrWhiteSpace(correlationIdentifier))
            {
                throw new ArgumentNullException(AmazonWebServicesProvider.ArgumentNameCorrelationIdentifier);
            }

            if (null == patch.ResourceIdentifier)
            {
                throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidPatch);
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.Identifier))
            {
                throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidPatch);
            }

            if (null == patch.PatchRequest)
            {
                throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidPatch);
            }

            string informationStarting =
                string.Format(
                    CultureInfo.InvariantCulture,
                    AmazonProvisioningAgentResources.InformationPatching,
                    patch.ResourceIdentifier.SchemaIdentifier,
                    patch.ResourceIdentifier.Identifier);
            ProvisioningAgentMonitor.Instance.Inform(informationStarting, true, correlationIdentifier);

            PatchRequest2 patchRequest = patch.PatchRequest as PatchRequest2;
            if (null == patchRequest)
            {
                string unsupportedPatchTypeName = patch.GetType().FullName;
                throw new NotSupportedException(unsupportedPatchTypeName);
            }

            if (null == patchRequest.Operations)
            {
                return;
            }

            PatchOperation operation;
            
            operation =
                patchRequest
                .Operations
                .SingleOrDefault(
                    (PatchOperation item) =>
                            OperationName.Replace == item.Name
                        && item.Path != null
                        && string.Equals(item.Path.AttributePath, AttributeNames.ExternalIdentifier, StringComparison.Ordinal));
            if (operation != null)
            {
                string externalIdentifierValue = operation.Value.Single().Value;
                await this.UpdateExternalIdentifier(patch.ResourceIdentifier, correlationIdentifier, externalIdentifierValue);
                return;
            }

            if 
            (
                !string.Equals(
                    patch.ResourceIdentifier.SchemaIdentifier, 
                    SchemaIdentifiers.WindowsAzureActiveDirectoryGroup, 
                    StringComparison.Ordinal)
            )
            {
                return;
            }

            operation =
                patchRequest
                .Operations
                .SingleOrDefault(
                    (PatchOperation item) =>
                            item.Path != null
                        &&  string.Equals(item.Path.AttributePath, AttributeNames.Members, StringComparison.Ordinal));
            if (null == operation)
            {
                return;
            }

            await this.UpdateMembers(patch.ResourceIdentifier, operation, correlationIdentifier);
        }
        private async Task UpdateMembers(Resource resource, IPatch patch)
        {
            if (null == resource)
            {
                throw new ArgumentNullException(FileProvider.ArgumentNameResource);
            }

            if (null == patch)
            {
                throw new ArgumentNullException(FileProvider.ArgumentNamePatch);
            }

            if (null == patch.ResourceIdentifier)
            {
                throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidPatch);
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.Identifier))
            {
                throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidPatch);
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.SchemaIdentifier))
            {
                throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidPatch);
            }

            if (null == patch.PatchRequest)
            {
                throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidPatch);
            }

            PatchRequest2 patchRequest = patch.PatchRequest as PatchRequest2;
            if (null == patchRequest)
            {
                string unsupportedPatchTypeName = patch.GetType().FullName;
                throw new NotSupportedException(unsupportedPatchTypeName);
            }

            if
            (
                !FileProvider
                .SchemaIdentifiersGroup
                .Value
                .Any(
                    (string item) =>
                        string.Equals(item, patch.ResourceIdentifier.SchemaIdentifier, StringComparison.Ordinal))
            )
            {
                return;
            }

            IReadOnlyCollection<PatchOperation> memberOperations =
                patchRequest
                .Operations
                .Where(
                    (PatchOperation item) =>
                            item.Path != null
                        && string.Equals(item.Path.AttributePath, AttributeNames.Members, StringComparison.Ordinal))
                .ToArray();
            if (!memberOperations.Any())
            {
                return;
            }

            foreach (PatchOperation memberOperation in memberOperations)
            {
                if (null == memberOperation.Value)
                {
                    continue;
                }
                
                foreach (OperationValue value in memberOperation.Value)
                {
                    if (string.IsNullOrWhiteSpace(value.Value))
                    {
                        continue;
                    }

                    Dictionary<string, string> columnsQuery = 
                        new Dictionary<string,string>()
                            {
                                {
                                    AttributeNames.Schemas,
                                    patch.ResourceIdentifier.SchemaIdentifier
                                },
                                {
                                    AttributeNames.Identifier,
                                    patch.ResourceIdentifier.Identifier
                                },
                                {
                                    AttributeNames.Members,
                                    value.Value
                                }
                            };
                    IRow[] rows = await this.file.Query(columnsQuery);

                    switch (memberOperation.Name)
                    {
                        case OperationName.Add:
                            if (rows.Any())
                            {
                                break;
                            }

                            Member member = 
                                new Member()
                                    {
                                        Value = value.Value
                                    };
                            MemberColumnsFactory memberColumnsFactory = new MemberColumnsFactory(resource, member);
                            IReadOnlyDictionary<string, string> columnsMember = memberColumnsFactory.CreateColumns();
                            await this.file.InsertRow(columnsMember);
                            break;

                        case OperationName.Remove:
                            foreach (IRow row in rows)
                            {
                                await this.file.RemoveRow(row.Key);
                            }

                            break;
                    }
                }
            }
        }
        public override async Task Update(IPatch patch, string correlationIdentifier)
        {
            if (null == patch)
            {
                throw new ArgumentNullException(FileProvider.ArgumentNamePatch);
            }

            if (null == patch.ResourceIdentifier)
            {
                throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidPatch);
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.Identifier))
            {
                throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidPatch);
            }

            if (null == patch.PatchRequest)
            {
                throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidPatch);
            }

            string informationStarting =
                string.Format(
                    CultureInfo.InvariantCulture,
                    AzureTestProvisioningResources.InformationPatching,
                    patch.ResourceIdentifier.SchemaIdentifier,
                    patch.ResourceIdentifier.Identifier);
            ProvisioningAgentMonitor.Instance.Inform(informationStarting, true, correlationIdentifier);

            PatchRequest2 patchRequest = patch.PatchRequest as PatchRequest2;
            if (null == patchRequest)
            {
                string unsupportedPatchTypeName = patch.GetType().FullName;
                throw new NotSupportedException(unsupportedPatchTypeName);
            }

            IRow row = await this.file.ReadRow(patch.ResourceIdentifier.Identifier);

            string rowSchema = null;
            if
            (
                    !row.Columns.TryGetValue(AttributeNames.Schemas, out rowSchema)
                ||  !string.Equals(rowSchema, patch.ResourceIdentifier.SchemaIdentifier, StringComparison.Ordinal)
            )
            {
                return;
            }

            IReadOnlyDictionary<string, string> columns;
            WindowsAzureActiveDirectoryGroup group = null;
            switch (rowSchema)
            {
                case SchemaIdentifiers.Core2EnterpriseUser:
                    ResourceFactory<Core2EnterpriseUser> userFactory = new UserFactory(row);
                    Core2EnterpriseUser user = userFactory.Create();
                    user.Apply(patchRequest);
                    ColumnsFactory<Core2EnterpriseUser> userColumnsFactory = new UserColumnsFactory(user);
                    columns = userColumnsFactory.CreateColumns();
                    break;

                case SchemaIdentifiers.WindowsAzureActiveDirectoryGroup:
                    ResourceFactory<WindowsAzureActiveDirectoryGroup> groupFactory = new GroupFactory(row);
                    group = groupFactory.Create();
                    group.Apply(patchRequest);
                    ColumnsFactory<WindowsAzureActiveDirectoryGroup> groupColumnsFactory = new GroupColumnsFactory(group);
                    columns = groupColumnsFactory.CreateColumns();
                    break;
                default:
                    throw new NotSupportedException(patch.ResourceIdentifier.SchemaIdentifier);
            }

            IRow rowReplacement = new Row(row.Key, columns);
            await this.file.ReplaceRow(rowReplacement);

            if (group != null)
            {
                await this.UpdateMembers(group, patch);
            }
        }