IHandlerRuntime CreateHandlerRuntime(ActionItem a) { HandlerInfo handler = a.Handler; bool cancel = OnProgress(a.Name, "CreateHandlerRuntime: " + handler.Type, "Start", StatusType.Initializing, a.InstanceId, -1); if (cancel) { _wantsCancel = true; return(new EmptyHandler()); } IHandlerRuntime hr = AssemblyLoader.Load(handler.Type, DefaultHandlerType); if (hr != null) { handler.Type = hr.RuntimeType; hr.ActionName = a.Name; string config = handler.HasConfig ? handler.Config.GetSerializedValues(Crypto) : null; hr.Initialize(config); } else { throw new Exception($"Could not load {handler.Type}."); } return(hr); }
HandlerResult ExecuteHandlerProcess_(SecurityContext parentSecurityContext, ActionItem a, Dictionary <string, string> dynamicData, bool dryRun = false) { HandlerResult returnResult = HandlerResult.Emtpy; string parms = ResolveConfigAndParameters(a, dynamicData); IHandlerRuntime rt = CreateHandlerRuntime(a.Name, a.Handler); rt.Progress += rt_Progress; if (!WantsStopOrPause()) { SecurityContext sc = a.HasRunAs ? a.RunAs : parentSecurityContext; sc?.Impersonate(); HandlerResult r = rt.Execute(parms, dryRun); sc?.Undo(); if (r.Status > returnResult.Status) { returnResult = r; } } return(returnResult); }
ExecuteResult ExecuteHandlerProcessInProc(SecurityContext parentSecurityContext, ActionItem a, Dictionary <string, string> dynamicData, object parentExitData, bool dryRun = false) { try { //string parms = ResolveConfigAndParameters( a, dynamicData ); string parms = a.Parameters.GetSerializedValues(Crypto, out string safeSerializedValues); IHandlerRuntime rt = CreateHandlerRuntime(a); rt.Progress += rt_Progress; rt.LogMessage += rt_LogMessage; if (!WantsStopOrPause()) { a.IngestParentSecurityContext(parentSecurityContext); SecurityContext sc = a.RunAs; //just an alias sc?.Crypto?.InheritSettingsIfRequired(Crypto); HandlerStartInfo startInfo = new HandlerStartInfo(StartInfo) { Parameters = parms, IsDryRun = dryRun, PlanInstanceId = InstanceId, InstanceId = a.InstanceId, ParentExitData = parentExitData, RunAs = sc, Crypto = a.Parameters?.Crypto }; a.Handler.StartInfo = new HandlerStartInfoData(startInfo); sc?.Impersonate(Crypto); a.Result = rt.Execute(startInfo); a.Handler.StartInfo.Parameters = safeSerializedValues; //avoids serializing decrypted values to the History Plan (bug #93) a.Result.BranchStatus = a.Result.Status; a.Result.SecurityContext = System.Security.Principal.WindowsIdentity.GetCurrent().Name; SaveExitDataAs(a); sc?.Undo(); } return(a.Result); } catch (Exception ex) { WriteUnhandledActionException(a, ex); return(new ExecuteResult() { Status = StatusType.Failed, ExitData = ex.Message }); } }
public ExecuteResult ExecuteHandlerProcess_SingleAction(ActionItem a, Dictionary <string, string> dynamicData, string parentExitData, bool dryRun = false) { ExecuteResult returnResult = new ExecuteResult(); ResolveConfigAndParameters(a, dynamicData, parentExitData); IHandlerRuntime rt = CreateHandlerRuntime(a); rt.Progress += rt_Progress; rt.LogMessage += rt_LogMessage; if (!WantsStopOrPause()) { try { HandlerStartInfo startInfo = new HandlerStartInfo(StartInfo) { Parameters = a.Parameters.GetSerializedValues(Crypto), IsDryRun = dryRun, PlanInstanceId = InstanceId, InstanceId = a.InstanceId, ParentExitData = parentExitData }; a.RunAs?.Impersonate(Crypto); ExecuteResult r = rt.Execute(startInfo); SaveExitDataAs(a); a.RunAs?.Undo(); if (r.Status > returnResult.Status) { returnResult = r; } } catch (Exception ex) { WriteUnhandledActionException(a, ex); returnResult = new ExecuteResult() { Status = StatusType.Failed }; } } return(returnResult); }
HandlerResult ProcessRecursive(List <ActionItem> actions, HandlerResult result, Dictionary <string, string> dynamicData, bool dryRun = false) { if (CheckStopOrPause()) { return(result); } HandlerResult returnResult = HandlerResult.Emtpy; IEnumerable <ActionItem> actionList = actions.Where(a => a.ExecuteCase == result.Status); foreach (ActionItem a in actionList) { if (CheckStopOrPause()) { break; } string parms = ResolveConfigAndParameters(a, dynamicData); IHandlerRuntime rt = CreateHandlerRuntime(a.Name, a.Handler); rt.Progress += rt_Progress; if (CheckStopOrPause()) { break; } HandlerResult r = rt.Execute(parms, dryRun); if (r.Status > returnResult.Status) { returnResult = r; } if (a.HasActions) { r = ProcessRecursive(a.Actions, r, dynamicData, dryRun); if (r.Status > returnResult.Status) { returnResult = r; } } } return(returnResult); }
public static IHandlerRuntime Load(string handlerType, string defaultHandler) { IHandlerRuntime hr = null; if (_knownFiles == null) { _knownFiles = new HashSet <string>(); _knownFiles.Add("log4net"); _knownFiles.Add("microsoft.visualstudio.qualitytools.unittestframework"); _knownFiles.Add("nunit.framework"); _knownFiles.Add("sqlite.interop"); _knownFiles.Add("synapse.service.common"); _knownFiles.Add("synapse.unittests"); _knownFiles.Add("system.data.sqlite"); _knownFiles.Add("yamldotnet"); } if (string.IsNullOrWhiteSpace(defaultHandler)) { defaultHandler = "Synapse.Handlers.CommandLine:CommandHandler"; } if (string.IsNullOrWhiteSpace(handlerType)) { handlerType = defaultHandler; } else if (!handlerType.ToLower().EndsWith("handler")) { handlerType = $"{handlerType}Handler"; } if (handlerType.Contains(":")) { string[] typeInfo = handlerType.Split(':'); AssemblyName an = new AssemblyName(typeInfo[0]); Assembly hrAsm = Assembly.Load(an); Type t = hrAsm.GetType(typeInfo[1], true); hr = Activator.CreateInstance(t) as IHandlerRuntime; hr.RuntimeType = t.AssemblyQualifiedName; } else { string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); DirectoryInfo dirInfo = new DirectoryInfo(currentDir); List <FileInfo> files = new List <FileInfo>( dirInfo.EnumerateFiles("*.dll", SearchOption.AllDirectories)); IEnumerator <FileInfo> fileList = files.GetEnumerator(); while (fileList.MoveNext() && hr == null) { string current = fileList.Current.Name.ToLower().Replace(fileList.Current.Extension.ToLower(), string.Empty); if (!_knownFiles.Contains(current)) { //assume that the name is complete, including namespace (if there is one) try { AssemblyName an = new AssemblyName(current); Assembly hrAsm = Assembly.Load(an); Type t = hrAsm.GetType(handlerType, true); hr = Activator.CreateInstance(t) as IHandlerRuntime; hr.RuntimeType = t.AssemblyQualifiedName; } catch { //probe all the Types, looking for partial match in name try { AssemblyName an = new AssemblyName(current); Assembly hrAsm = Assembly.Load(an); string ht = handlerType.ToLower(); Type[] types = hrAsm.GetTypes(); foreach (Type t in types) { if (t.GetInterfaces().Contains(typeof(IHandlerRuntime)) && t.Name.ToLower().Contains(ht)) { hr = Activator.CreateInstance(t) as IHandlerRuntime; hr.RuntimeType = t.AssemblyQualifiedName; break; } } } catch { } } } } } return(hr); }
static void CreateSamplePlan(string handlerCsvList, string outPath, bool verbose = false) { if (string.IsNullOrWhiteSpace(handlerCsvList)) { return; } Plan p = new Plan() { Name = "SamplePlan", Description = "Example Config/Parameters", StartInfo = null }; if (verbose) { p = Plan.CreateSample(); } List <string> handlers = GetHandlerList(handlerCsvList); foreach (string handlerType in handlers) { ActionItem a = new ActionItem() { Name = handlerType, Description = $"Example Action for {handlerType}." }; a.Handler = new HandlerInfo(); a.Actions = null; IHandlerRuntime hr = null; try { hr = AssemblyLoader.Load(handlerType, handlerType); } catch { } if (hr != null) { a.Description = $"Resolved Handler from [{hr.RuntimeType}]."; a.Handler.Type = handlerType; a.Handler.Config = new ParameterInfo { Values = hr.GetConfigInstance() }; a.Parameters = new ParameterInfo { Values = hr.GetParametersInstance() }; } else { a.Handler.Type = $"** Error - Could not load [{handlerType}]."; } p.Actions.Add(a); } if (!string.IsNullOrWhiteSpace(outPath)) { File.WriteAllText(outPath, p.ToYaml(verbose)); Console.WriteLine($"Created sample Plan at [{outPath}]."); } else { Console.WriteLine(p.ToYaml(verbose)); } }