internal EvaluationContext(
            ITraceWriter trace,
            ISecretMasker secretMasker,
            Object state,
            EvaluationOptions options,
            ExpressionNode node)
        {
            ArgumentUtility.CheckForNull(trace, nameof(trace));
            ArgumentUtility.CheckForNull(secretMasker, nameof(secretMasker));
            Trace        = trace;
            SecretMasker = secretMasker;
            State        = state;

            // Copy the options
            options = new EvaluationOptions(copy: options);
            if (options.MaxMemory == 0)
            {
                // Set a reasonable default max memory
                options.MaxMemory = 1048576; // 1 mb
            }
            Options = options;
            Memory  = new EvaluationMemory(options.MaxMemory, node);

            m_traceResults = new Dictionary <ExpressionNode, String>();
            m_traceMemory  = new MemoryCounter(null, options.MaxMemory);
        }
        internal static String FormatValue(
            ISecretMasker secretMasker,
            Object value,
            ValueKind kind)
        {
            switch (kind)
            {
            case ValueKind.Null:
                return(ExpressionConstants.Null);

            case ValueKind.Boolean:
                return(((Boolean)value) ? ExpressionConstants.True : ExpressionConstants.False);

            case ValueKind.Number:
                var strNumber = ((Double)value).ToString(ExpressionConstants.NumberFormat, CultureInfo.InvariantCulture);
                return(secretMasker != null?secretMasker.MaskSecrets(strNumber) : strNumber);

            case ValueKind.String:
                // Mask secrets before string-escaping.
                var strValue = secretMasker != null?secretMasker.MaskSecrets(value as String) : value as String;

                return($"'{StringEscape(strValue)}'");

            case ValueKind.Array:
            case ValueKind.Object:
                return(kind.ToString());

            default:     // Should never reach here.
                throw new NotSupportedException($"Unable to convert to realized expression. Unexpected value kind: {kind}");
            }
        }
예제 #3
0
        public override void Initialize(IHostContext hostContext)
        {
            base.Initialize(hostContext);

            _jobServerQueue = HostContext.GetService <IJobServerQueue>();
            _secretMasker   = HostContext.GetService <ISecretMasker>();
        }
예제 #4
0
        public Variables(IHostContext hostContext, IDictionary <string, VariableValue> copy)
        {
            // Store/Validate args.
            _hostContext  = hostContext;
            _secretMasker = _hostContext.SecretMasker;
            _trace        = _hostContext.GetTrace(nameof(Variables));
            ArgUtil.NotNull(hostContext, nameof(hostContext));

            // Validate the dictionary, remove any variable with empty variable name.
            ArgUtil.NotNull(copy, nameof(copy));
            if (copy.Keys.Any(k => string.IsNullOrWhiteSpace(k)))
            {
                _trace.Info($"Remove {copy.Keys.Count(k => string.IsNullOrWhiteSpace(k))} variables with empty variable name.");
            }

            // Initialize the variable dictionary.
            List <Variable> variables = new List <Variable>();

            foreach (var variable in copy)
            {
                if (!string.IsNullOrWhiteSpace(variable.Key))
                {
                    variables.Add(new Variable(variable.Key, variable.Value.Value, variable.Value.IsSecret));
                }
            }

            foreach (Variable variable in variables)
            {
                // Store the variable. The initial secret values have already been
                // registered by the Worker class.
                _variables[variable.Name] = variable;
            }
        }
        internal ExpressionException(ISecretMasker secretMasker, String message)
        {
            if (secretMasker != null)
            {
                message = secretMasker.MaskSecrets(message);
            }

            m_message = message;
        }
예제 #6
0
        public CommandLineParser(IHostContext hostContext, string[] secretArgNames)
        {
            _secretMasker = hostContext.SecretMasker;
            _trace        = hostContext.GetTrace(nameof(CommandLineParser));

            Commands       = new List <string>();
            Flags          = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            Args           = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            SecretArgNames = new HashSet <string>(secretArgNames ?? new string[0], StringComparer.OrdinalIgnoreCase);
        }
예제 #7
0
        public CommandLineParser(IHostContext hostContext, string[] secretArgNames)
        {
            _secretMasker = hostContext.GetService<ISecretMasker>();
            _trace = hostContext.GetTrace(nameof(CommandLineParser));

            Commands = new List<string>();
            Flags = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
            Args = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            SecretArgNames = new HashSet<string>(secretArgNames ?? new string[0], StringComparer.OrdinalIgnoreCase);
        }
예제 #8
0
        public TraceManager(HostTraceListener traceListener, TraceSetting traceSetting, ISecretMasker secretMasker)
        {
            // Validate and store params.
            ArgUtil.NotNull(traceListener, nameof(traceListener));
            ArgUtil.NotNull(traceSetting, nameof(traceSetting));
            ArgUtil.NotNull(secretMasker, nameof(secretMasker));
            _hostTraceListener = traceListener;
            _traceSetting      = traceSetting;
            _secretMasker      = secretMasker;

            Switch = new SourceSwitch("VSTSAgentSwitch")
            {
                Level = _traceSetting.DefaultTraceLevel.ToSourceLevels()
            };
        }
예제 #9
0
        public TraceManager(HostTraceListener traceListener, TraceSetting traceSetting, ISecretMasker secretMasker)
        {
            // Validate and store params.
            ArgUtil.NotNull(traceListener, nameof(traceListener));
            ArgUtil.NotNull(traceSetting, nameof(traceSetting));
            ArgUtil.NotNull(secretMasker, nameof(secretMasker));
            _hostTraceListener = traceListener;
            _traceSetting = traceSetting;
            _secretMasker = secretMasker;

            Switch = new SourceSwitch("VSTSAgentSwitch")
            {
                Level = _traceSetting.DefaultTraceLevel.ToSourceLevels()
            };
        }
예제 #10
0
        public Tracing(string name, ISecretMasker secretMasker, SourceSwitch sourceSwitch, HostTraceListener traceListener)
        {
            ArgUtil.NotNull(secretMasker, nameof(secretMasker));
            _secretMasker = secretMasker;
            _traceSource = new TraceSource(name);
            _traceSource.Switch = sourceSwitch;

            // Remove the default trace listener.
            if (_traceSource.Listeners.Count > 0 &&
                _traceSource.Listeners[0] is DefaultTraceListener)
            {
                _traceSource.Listeners.RemoveAt(0);
            }

            _traceSource.Listeners.Add(traceListener);
        }
예제 #11
0
        public Tracing(string name, ISecretMasker secretMasker, SourceSwitch sourceSwitch, HostTraceListener traceListener)
        {
            ArgUtil.NotNull(secretMasker, nameof(secretMasker));
            _secretMasker       = secretMasker;
            _traceSource        = new TraceSource(name);
            _traceSource.Switch = sourceSwitch;

            // Remove the default trace listener.
            if (_traceSource.Listeners.Count > 0 &&
                _traceSource.Listeners[0] is DefaultTraceListener)
            {
                _traceSource.Listeners.RemoveAt(0);
            }

            _traceSource.Listeners.Add(traceListener);
        }
예제 #12
0
        // Constructor.
        public CommandSettings(IHostContext context, string[] args)
        {
            ArgUtil.NotNull(context, nameof(context));
            _context       = context;
            _promptManager = context.GetService <IPromptManager>();
            _secretMasker  = context.GetService <ISecretMasker>();
            _trace         = context.GetTrace(nameof(CommandSettings));

            // Parse the command line args.
            _parser = new CommandLineParser(
                hostContext: context,
                secretArgNames: Constants.Agent.CommandLine.Args.Secrets);
            _parser.Parse(args);

            // Store and remove any args passed via environment variables.
            IDictionary environment = Environment.GetEnvironmentVariables();
            string      envPrefix   = "VSTS_AGENT_INPUT_";

            foreach (DictionaryEntry entry in environment)
            {
                // Test if starts with VSTS_AGENT_INPUT_.
                string fullKey = entry.Key as string ?? string.Empty;
                if (fullKey.StartsWith(envPrefix, StringComparison.OrdinalIgnoreCase))
                {
                    string val = (entry.Value as string ?? string.Empty).Trim();
                    if (!string.IsNullOrEmpty(val))
                    {
                        // Extract the name.
                        string name = fullKey.Substring(envPrefix.Length);

                        // Mask secrets.
                        bool secret = Constants.Agent.CommandLine.Args.Secrets.Any(x => string.Equals(x, name, StringComparison.OrdinalIgnoreCase));
                        if (secret)
                        {
                            _secretMasker.AddValue(val);
                        }

                        // Store the value.
                        _envArgs[name] = val;
                    }

                    // Remove from the environment block.
                    _trace.Info($"Removing env var: '{fullKey}'");
                    Environment.SetEnvironmentVariable(fullKey, null);
                }
            }
        }
예제 #13
0
        public Variables(IHostContext hostContext, IDictionary <string, string> copy, IList <MaskHint> maskHints, out List <string> warnings)
        {
            // Store/Validate args.
            _hostContext  = hostContext;
            _secretMasker = _hostContext.GetService <ISecretMasker>();
            _trace        = _hostContext.GetTrace(nameof(Variables));
            ArgUtil.NotNull(hostContext, nameof(hostContext));

            // Validate the dictionary, rmeove any variable with empty variable name.
            ArgUtil.NotNull(copy, nameof(copy));
            if (copy.Keys.Any(k => string.IsNullOrWhiteSpace(k)))
            {
                _trace.Info($"Remove {copy.Keys.Count(k => string.IsNullOrWhiteSpace(k))} variables with empty variable name.");
            }

            // Filter/validate the mask hints.
            ArgUtil.NotNull(maskHints, nameof(maskHints));
            MaskHint[] variableMaskHints = maskHints.Where(x => x.Type == MaskType.Variable).ToArray();
            foreach (MaskHint maskHint in variableMaskHints)
            {
                string maskHintValue = maskHint.Value;
                ArgUtil.NotNullOrEmpty(maskHintValue, nameof(maskHintValue));
            }

            // Initialize the variable dictionary.
            IEnumerable <Variable> variables =
                from string name in copy.Keys
                where !string.IsNullOrWhiteSpace(name)
                join MaskHint maskHint in variableMaskHints // Join the variable names with the variable mask hints.
                on name.ToUpperInvariant() equals maskHint.Value.ToUpperInvariant()
                into maskHintGrouping
                select new Variable(
                    name: name,
                    value: copy[name] ?? string.Empty,
                    secret: maskHintGrouping.Any());

            foreach (Variable variable in variables)
            {
                // Store the variable. The initial secret values have already been
                // registered by the Worker class.
                _nonexpanded[variable.Name] = variable;
            }

            // Recursively expand the variables.
            RecalculateExpanded(out warnings);
        }
        /// <summary>
        /// IExpressionNode entry point.
        /// </summary>
        EvaluationResult IExpressionNode.Evaluate(
            ITraceWriter trace,
            ISecretMasker secretMasker,
            Object state,
            EvaluationOptions options)
        {
            if (Container != null)
            {
                // Do not localize. This is an SDK consumer error.
                throw new NotSupportedException($"Expected {nameof(IExpressionNode)}.{nameof(Evaluate)} to be called on root node only.");
            }


            var originalSecretMasker = secretMasker;

            try
            {
                // Evaluate
                secretMasker = secretMasker?.Clone() ?? new SecretMasker();
                trace        = new EvaluationTraceWriter(trace, secretMasker);
                var context = new EvaluationContext(trace, secretMasker, state, options, this);
                trace.Info($"Evaluating: {ConvertToExpression()}");
                var result = Evaluate(context);

                // Trace the result
                TraceTreeResult(context, result.Value, result.Kind);

                return(result);
            }
            finally
            {
                if (secretMasker != null && secretMasker != originalSecretMasker)
                {
                    (secretMasker as IDisposable)?.Dispose();
                    secretMasker = null;
                }
            }
        }
예제 #15
0
 public override void Initialize(IHostContext hostContext)
 {
     base.Initialize(hostContext);
     _secretMasker           = hostContext.GetService <ISecretMasker>();
     Console.CancelKeyPress += Console_CancelKeyPress;
 }
 internal static String FormatValue(
     ISecretMasker secretMasker,
     EvaluationResult evaluationResult)
 {
     return(FormatValue(secretMasker, evaluationResult.Value, evaluationResult.Kind));
 }
예제 #17
0
        public override void Initialize(IHostContext hostContext)
        {
            base.Initialize(hostContext);

            _jobServerQueue = HostContext.GetService<IJobServerQueue>();
            _secretMasker = HostContext.GetService<ISecretMasker>();
        }
예제 #18
0
 public TraceManager(HostTraceListener traceListener, ISecretMasker secretMasker)
     : this(traceListener, new TraceSetting(), secretMasker)
 {
 }
예제 #19
0
 public override void Initialize(IHostContext hostContext)
 {
     base.Initialize(hostContext);
     _secretMasker = hostContext.GetService<ISecretMasker>();
     Console.CancelKeyPress += Console_CancelKeyPress;
 }
예제 #20
0
 public TraceManager(HostTraceListener traceListener, ISecretMasker secretMasker)
     : this(traceListener, new TraceSetting(), secretMasker)
 {
 }
 public LoggedSecretMasker(ISecretMasker secretMasker)
 {
     this._secretMasker = secretMasker;
 }
예제 #22
0
 public EvaluationTraceWriter(ITraceWriter trace, ISecretMasker secretMasker)
 {
     ArgumentUtility.CheckForNull(secretMasker, nameof(secretMasker));
     m_trace        = trace;
     m_secretMasker = secretMasker;
 }