Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Sandboxing/SandboxManager.cs @ 2604

Last change on this file since 2604 was 2592, checked in by gkronber, 15 years ago

Merged missing changes (r2587, r2589) in plugin infrastructure from branch to trunk. #799

File size: 3.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.PluginInfrastructure.Manager;
6using System.IO;
7using System.ComponentModel;
8using HeuristicLab.PluginInfrastructure.UpdateLocationReference;
9using System.Security;
10using System.Security.Permissions;
11using System.Security.Policy;
12using System.Reflection;
13using System.Diagnostics;
14
15namespace HeuristicLab.PluginInfrastructure.Sandboxing {
16  public class SandboxManager {
17
18    private static StrongName CreateStrongName(Assembly assembly) {
19      if (assembly == null)
20        throw new ArgumentNullException("assembly");
21
22      AssemblyName assemblyName = assembly.GetName();
23      Trace.Assert(assemblyName != null, "Could not get assembly name");
24
25      // get the public key blob
26      byte[] publicKey = assemblyName.GetPublicKey();
27      if (publicKey == null || publicKey.Length == 0)
28        throw new InvalidOperationException("Assembly is not strongly named");
29
30      StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey);
31
32      // and create the StrongName
33      return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version);
34    }
35
36    #region ISandboxManager Members
37
38    public static AppDomain CreateAndInitSandbox(string name) {
39      return CreateAndInitSandbox(name, Enumerable.Empty<byte[]>());
40    }
41
42
43    public static AppDomain CreateAndInitSandbox(string name, IEnumerable<byte[]> files) {
44      PermissionSet pset;
45
46      #region permission set for sandbox
47      // Uncomment code for sandboxed appdomain
48      //pset = new PermissionSet(PermissionState.None);
49      //pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
50      //pset.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess));
51      //FileIOPermission fPerm = new FileIOPermission(PermissionState.None);
52
53      //foreach (IPluginDescription plugin in ApplicationManager.Manager.Plugins) {
54      //  fPerm.AddPathList(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, plugin.Files.ToArray());
55      //}
56
57      //pset.AddPermission(fPerm);
58      #endregion
59
60      #region permission set of unrestricted appdomain
61      // unrestricted appdomain
62      pset = new PermissionSet(PermissionState.Unrestricted);
63      #endregion
64
65      AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
66      //setup.PrivateBinPath = pluginDir;
67      setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
68      AppDomain applicationDomain = AppDomain.CreateDomain(name, AppDomain.CurrentDomain.Evidence, setup, pset, CreateStrongName(Assembly.GetExecutingAssembly()));
69      Type applicationManagerType = typeof(ApplicationManager);
70      ApplicationManager applicationManager =
71        (ApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null);
72      ApplicationDescription[] apps = ApplicationManager.Manager.Applications.Cast<ApplicationDescription>().ToArray();
73      PluginDescription[] plugins = ApplicationManager.Manager.Plugins.Cast<PluginDescription>().ToArray();
74      applicationManager.PrepareApplicationDomain(apps, plugins);
75      if (files != null && files.Count() > 0)
76        applicationManager.LoadAssemblies(files);
77      return applicationDomain;
78    }
79
80    #endregion
81  }
82}
Note: See TracBrowser for help on using the repository browser.