public override VssCredentials GetVssCredentials(IHostContext context) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(PersonalAccessToken)); trace.Info(nameof(GetVssCredentials)); ArgUtil.NotNull(CredentialData, nameof(CredentialData)); string token; if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.Token, out token)) { token = null; } ArgUtil.NotNullOrEmpty(token, nameof(token)); trace.Info("token retrieved: {0} chars", token.Length); // PAT uses a basic credential VssBasicCredential basicCred = new VssBasicCredential("VstsAgent", token); VssCredentials creds = new VssCredentials(null, basicCred, CredentialPromptType.DoNotPrompt); trace.Info("cred created"); return(creds); }
public override VssCredentials GetVssCredentials(IHostContext context) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(AlternateCredential)); trace.Info(nameof(GetVssCredentials)); // TODO: This is busted. /* * if (CredentialData == null || !CredentialData.Data.ContainsKey("token")) * { * throw new InvalidOperationException("Must call ReadCredential first."); * } * * string username = CredentialData.Data["Username"]; * trace.Info("username retrieved: {0} chars", username.Length); * * string password = CredentialData.Data["Password"]; * trace.Info("password retrieved: {0} chars", password.Length); * * // PAT uses a basic credential * VssBasicCredential loginCred = new VssBasicCredential(username, password); * VssCredentials creds = new VssClientCredentials(loginCred); * trace.Verbose("cred created"); * * return creds; */ throw new NotImplementedException(); }
public override VssCredentials GetVssCredentials(IHostContext context) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(AlternateCredential)); trace.Info(nameof(GetVssCredentials)); string username; if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.UserName, out username)) { username = null; } string password; if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.Password, out password)) { password = null; } ArgUtil.NotNull(username, nameof(username)); ArgUtil.NotNull(password, nameof(password)); trace.Info("username retrieved: {0} chars", username.Length); trace.Info("password retrieved: {0} chars", password.Length); VssBasicCredential loginCred = new VssBasicCredential(username, password); VssCredentials creds = new VssCredentials(null, loginCred, CredentialPromptType.DoNotPrompt); trace.Info("cred created"); return(creds); }
public override VssCredentials GetVssCredentials(IHostContext context) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(PersonalAccessToken)); trace.Info(nameof(GetVssCredentials)); ArgUtil.NotNull(CredentialData, nameof(CredentialData)); string token; if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.Token, out token)) { token = null; } string username; if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.UserName, out username)) { username = null; } ArgUtil.NotNullOrEmpty(token, nameof(token)); ArgUtil.NotNullOrEmpty(username, nameof(username)); trace.Info("token retrieved: {0} chars", token.Length); // ServiceIdentity uses a service identity credential VssServiceIdentityToken identityToken = new VssServiceIdentityToken(token); VssServiceIdentityCredential serviceIdentityCred = new VssServiceIdentityCredential(username, "", identityToken); VssCredentials creds = new VssCredentials(serviceIdentityCred); trace.Verbose("cred created"); return creds; }
public static void ExpandEnvironmentVariables(IHostContext context, IDictionary<string, string> target) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(VarUtil)); trace.Entering(); // Determine which string comparer to use for the environment variable dictionary. StringComparer comparer; switch (Constants.Agent.Platform) { case Constants.OSPlatform.Linux: case Constants.OSPlatform.OSX: comparer = StringComparer.CurrentCulture; break; case Constants.OSPlatform.Windows: comparer = StringComparer.CurrentCultureIgnoreCase; break; default: throw new NotSupportedException(); } // Copy the environment variables into a dictionary that uses the correct comparer. var source = new Dictionary<string, string>(comparer); IDictionary environment = Environment.GetEnvironmentVariables(); foreach (DictionaryEntry entry in environment) { string key = entry.Key as string ?? string.Empty; string val = entry.Value as string ?? string.Empty; source[key] = val; } // Expand the target values. ExpandValues(context, source, target); }
public CapabilitiesBuilder(IHostContext hostContext, CancellationToken cancellationToken) { ArgUtil.NotNull(hostContext, nameof(hostContext)); _hostContext = hostContext; _cancellationToken = cancellationToken; _trace = _hostContext.GetTrace(this.GetType().Name); }
public override VssCredentials GetVssCredentials(IHostContext context) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(ServiceIdentityCredential)); trace.Info(nameof(GetVssCredentials)); ArgUtil.NotNull(CredentialData, nameof(CredentialData)); string token; if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.Token, out token)) { token = null; } string username; if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.UserName, out username)) { username = null; } ArgUtil.NotNullOrEmpty(token, nameof(token)); ArgUtil.NotNullOrEmpty(username, nameof(username)); trace.Info("token retrieved: {0} chars", token.Length); // ServiceIdentity uses a service identity credential VssServiceIdentityToken identityToken = new VssServiceIdentityToken(token); VssServiceIdentityCredential serviceIdentityCred = new VssServiceIdentityCredential(username, "", identityToken); VssCredentials creds = new VssCredentials(null, serviceIdentityCred, CredentialPromptType.DoNotPrompt); trace.Info("cred created"); return(creds); }
public CommandLineParser(IHostContext hostContext) { _trace = hostContext.GetTrace("CommandLineParser"); Commands = new List <string>(); Flags = new HashSet <string>(StringComparer.OrdinalIgnoreCase); Args = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); }
public override void EnsureCredential(IHostContext context, CommandSettings command, string serverUrl) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(PersonalAccessToken)); trace.Info(nameof(EnsureCredential)); ArgUtil.NotNull(command, nameof(command)); CredentialData.Data[Constants.Agent.CommandLine.Args.Token] = command.GetToken(); }
public virtual void Initialize(IHostContext hostContext) { ArgUtil.NotNull(hostContext, nameof(hostContext)); HostContext = hostContext; Trace = HostContext.GetTrace(TraceName); Trace.Entering(); }
public static void ExpandValues(IHostContext context, IDictionary <string, string> source, IDictionary <string, string> target) { ArgUtil.NotNull(context, nameof(context)); ArgUtil.NotNull(source, nameof(source)); Tracing trace = context.GetTrace(nameof(VarUtil)); trace.Entering(); target = target ?? new Dictionary <string, string>(); // This algorithm does not perform recursive replacement. // Process each key in the target dictionary. foreach (string targetKey in target.Keys.ToArray()) { trace.Verbose($"Processing expansion for: '{targetKey}'"); int startIndex = 0; int prefixIndex; int suffixIndex; string targetValue = target[targetKey] ?? string.Empty; // Find the next macro within the target value. while (startIndex < targetValue.Length && (prefixIndex = targetValue.IndexOf(Constants.Variables.MacroPrefix, startIndex, StringComparison.Ordinal)) >= 0 && (suffixIndex = targetValue.IndexOf(Constants.Variables.MacroSuffix, prefixIndex + Constants.Variables.MacroPrefix.Length, StringComparison.Ordinal)) >= 0) { // A candidate was found. string variableKey = targetValue.Substring( startIndex: prefixIndex + Constants.Variables.MacroPrefix.Length, length: suffixIndex - prefixIndex - Constants.Variables.MacroPrefix.Length); trace.Verbose($"Found macro candidate: '{variableKey}'"); string variableValue; if (!string.IsNullOrEmpty(variableKey) && TryGetValue(trace, source, variableKey, out variableValue)) { // A matching variable was found. // Update the target value. trace.Verbose("Macro found."); targetValue = string.Concat( targetValue.Substring(0, prefixIndex), variableValue ?? string.Empty, targetValue.Substring(suffixIndex + Constants.Variables.MacroSuffix.Length)); // Bump the start index to prevent recursive replacement. startIndex = prefixIndex + (variableValue ?? string.Empty).Length; } else { // A matching variable was not found. trace.Verbose("Macro not found."); startIndex = prefixIndex + 1; } } target[targetKey] = targetValue ?? string.Empty; } }
public override void EnsureCredential(IHostContext context, CommandSettings command, string serverUrl) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(AadDeviceCodeAccessToken)); trace.Info(nameof(EnsureCredential)); ArgUtil.NotNull(command, nameof(command)); CredentialData.Data[Constants.Agent.CommandLine.Args.Url] = serverUrl; CredentialData.Data[Constants.Agent.CommandLine.Flags.LaunchBrowser] = command.GetAutoLaunchBrowser().ToString(); }
private bool IsEqualCommand(IHostContext hc, ActionCommand e1, ActionCommand e2) { try { if (!string.Equals(e1.Command, e2.Command, StringComparison.OrdinalIgnoreCase)) { hc.GetTrace("CommandEqual").Info("Command 1={0}, Command 2={1}", e1.Command, e2.Command); return(false); } if (!string.Equals(e1.Data, e2.Data, StringComparison.OrdinalIgnoreCase) && (!string.IsNullOrEmpty(e1.Data) && !string.IsNullOrEmpty(e2.Data))) { hc.GetTrace("CommandEqual").Info("Data 1={0}, Data 2={1}", e1.Data, e2.Data); return(false); } if (e1.Properties.Count != e2.Properties.Count) { hc.GetTrace("CommandEqual").Info("Logging events contain different numbers of Properties,{0} to {1}", e1.Properties.Count, e2.Properties.Count); return(false); } if (!e1.Properties.SequenceEqual(e2.Properties)) { hc.GetTrace("CommandEqual").Info("Logging events contain different Properties"); hc.GetTrace("CommandEqual").Info("Properties for event 1:"); foreach (var data in e1.Properties) { hc.GetTrace("CommandEqual").Info("Key={0}, Value={1}", data.Key, data.Value); } hc.GetTrace("CommandEqual").Info("Properties for event 2:"); foreach (var data in e2.Properties) { hc.GetTrace("CommandEqual").Info("Key={0}, Value={1}", data.Key, data.Value); } return(false); } } catch (Exception ex) { hc.GetTrace("CommandEqual").Info("Catch Exception during compare:{0}", ex.ToString()); return(false); } return(true); }
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); }
public override void EnsureCredential(IHostContext context, CommandSettings command, string serverUrl) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(AlternateCredential)); trace.Info(nameof(EnsureCredential)); ArgUtil.NotNull(command, nameof(command)); CredentialData.Data[Constants.Agent.CommandLine.Args.UserName] = command.GetUserName(); CredentialData.Data[Constants.Agent.CommandLine.Args.Password] = command.GetPassword(); }
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); }
public override VssCredentials GetVssCredentials(IHostContext context) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(NegotiateCredential)); trace.Info(nameof(GetVssCredentials)); ArgUtil.NotNull(CredentialData, nameof(CredentialData)); // Get the user name from the credential data. string userName; if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.UserName, out userName)) { userName = null; } ArgUtil.NotNullOrEmpty(userName, nameof(userName)); trace.Info("User name retrieved."); // Get the password from the credential data. string password; if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.Password, out password)) { password = null; } ArgUtil.NotNullOrEmpty(password, nameof(password)); trace.Info("Password retrieved."); // Get the URL from the credential data. string url; if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.Url, out url)) { url = null; } ArgUtil.NotNullOrEmpty(url, nameof(url)); trace.Info($"URL retrieved: {url}"); // Create the Negotiate and NTLM credential object. var credential = new NetworkCredential(userName, password); var credentialCache = new CredentialCache(); switch (Constants.Agent.Platform) { case Constants.OSPlatform.Linux: case Constants.OSPlatform.OSX: credentialCache.Add(new Uri(url), "NTLM", credential); break; case Constants.OSPlatform.Windows: credentialCache.Add(new Uri(url), "Negotiate", credential); break; } VssCredentials creds = new VssClientCredentials(new WindowsCredential(credentialCache)); trace.Verbose("cred created"); return creds; }
public static void ExpandValues(IHostContext context, IDictionary<string, string> source, IDictionary<string, string> target) { ArgUtil.NotNull(context, nameof(context)); ArgUtil.NotNull(source, nameof(source)); Tracing trace = context.GetTrace(nameof(VarUtil)); trace.Entering(); target = target ?? new Dictionary<string, string>(); // This algorithm does not perform recursive replacement. // Process each key in the target dictionary. foreach (string targetKey in target.Keys.ToArray()) { trace.Verbose($"Processing expansion for: '{targetKey}'"); int startIndex = 0; int prefixIndex; int suffixIndex; string targetValue = target[targetKey] ?? string.Empty; // Find the next macro within the target value. while (startIndex < targetValue.Length && (prefixIndex = targetValue.IndexOf(Constants.Variables.MacroPrefix, startIndex, StringComparison.Ordinal)) >= 0 && (suffixIndex = targetValue.IndexOf(Constants.Variables.MacroSuffix, prefixIndex + Constants.Variables.MacroPrefix.Length, StringComparison.Ordinal)) >= 0) { // A candidate was found. string variableKey = targetValue.Substring( startIndex: prefixIndex + Constants.Variables.MacroPrefix.Length, length: suffixIndex - prefixIndex - Constants.Variables.MacroPrefix.Length); trace.Verbose($"Found macro candidate: '{variableKey}'"); string variableValue; if (!string.IsNullOrEmpty(variableKey) && TryGetValue(trace, source, variableKey, out variableValue)) { // A matching variable was found. // Update the target value. trace.Verbose("Macro found."); targetValue = string.Concat( targetValue.Substring(0, prefixIndex), variableValue ?? string.Empty, targetValue.Substring(suffixIndex + Constants.Variables.MacroSuffix.Length)); // Bump the start index to prevent recursive replacement. startIndex = prefixIndex + (variableValue ?? string.Empty).Length; } else { // A matching variable was not found. trace.Verbose("Macro not found."); startIndex = prefixIndex + 1; } } target[targetKey] = targetValue ?? string.Empty; } }
public override VssCredentials GetVssCredentials(IHostContext context) { Tracing trace = context.GetTrace("PersonalAccessToken"); trace.Info("GetVssCredentials()"); VssBasicCredential loginCred = new VssBasicCredential("test", "password"); VssCredentials creds = new VssClientCredentials(loginCred); trace.Verbose("cred created"); return creds; }
public static async Task <int> MainAsync(IHostContext context, string[] args) { Tracing trace = context.GetTrace(nameof(GitHub.Runner.Worker)); if (StringUtil.ConvertToBoolean(Environment.GetEnvironmentVariable("GITHUB_ACTIONS_RUNNER_ATTACH_DEBUGGER"))) { await WaitForDebugger(trace); } // We may want to consider registering this handler in Worker.cs, similiar to the unloading/SIGTERM handler //ITerminal registers a CTRL-C handler, which keeps the Runner.Worker process running //and lets the Runner.Listener handle gracefully the exit. var term = context.GetService <ITerminal>(); try { trace.Info($"Version: {BuildConstants.RunnerPackage.Version}"); trace.Info($"Commit: {BuildConstants.Source.CommitHash}"); trace.Info($"Culture: {CultureInfo.CurrentCulture.Name}"); trace.Info($"UI Culture: {CultureInfo.CurrentUICulture.Name}"); context.WritePerfCounter("WorkerProcessStarted"); // Validate args. ArgUtil.NotNull(args, nameof(args)); ArgUtil.Equal(3, args.Length, nameof(args.Length)); ArgUtil.NotNullOrEmpty(args[0], $"{nameof(args)}[0]"); ArgUtil.Equal("spawnclient", args[0].ToLowerInvariant(), $"{nameof(args)}[0]"); ArgUtil.NotNullOrEmpty(args[1], $"{nameof(args)}[1]"); ArgUtil.NotNullOrEmpty(args[2], $"{nameof(args)}[2]"); var worker = context.GetService <IWorker>(); // Run the worker. return(await worker.RunAsync( pipeIn : args[1], pipeOut : args[2])); } catch (Exception ex) { // Populate any exception that cause worker failure back to runner. Console.WriteLine(ex.ToString()); try { trace.Error(ex); } catch (Exception e) { // make sure we don't crash the app on trace error. // since IOException will throw when we run out of disk space. Console.WriteLine(e.ToString()); } } return(1); }
private bool IsEqualCommand(IHostContext hc, Command e1, Command e2) { try { if (!string.Equals(e1.Area, e2.Area, StringComparison.OrdinalIgnoreCase)) { hc.GetTrace("CommandEqual").Info("Area 1={0}, Area 2={1}", e1.Area, e2.Area); return false; } if (!string.Equals(e1.Event, e2.Event, StringComparison.OrdinalIgnoreCase)) { hc.GetTrace("CommandEqual").Info("Event 1={0}, Event 2={1}", e1.Event, e2.Event); return false; } if (!string.Equals(e1.Data, e2.Data, StringComparison.OrdinalIgnoreCase) && (!string.IsNullOrEmpty(e1.Data) && !string.IsNullOrEmpty(e2.Data))) { hc.GetTrace("CommandEqual").Info("Data 1={0}, Data 2={1}", e1.Data, e2.Data); return false; } if (e1.Properties.Count != e2.Properties.Count) { hc.GetTrace("CommandEqual").Info("Logging events contain different numbers of Properties,{0} to {1}", e1.Properties.Count, e2.Properties.Count); return false; } if (!e1.Properties.SequenceEqual(e2.Properties)) { hc.GetTrace("CommandEqual").Info("Logging events contain different Properties"); hc.GetTrace("CommandEqual").Info("Properties for event 1:"); foreach (var data in e1.Properties) { hc.GetTrace("CommandEqual").Info("Key={0}, Value={1}", data.Key, data.Value); } hc.GetTrace("CommandEqual").Info("Properties for event 2:"); foreach (var data in e2.Properties) { hc.GetTrace("CommandEqual").Info("Key={0}, Value={1}", data.Key, data.Value); } return false; } } catch (Exception ex) { hc.GetTrace("CommandEqual").Info("Catch Exception during compare:{0}", ex.ToString()); return false; } return true; }
public override void EnsureCredential(IHostContext context, CommandSettings command, string serverUrl) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(PersonalAccessToken)); trace.Info(nameof(EnsureCredential)); ArgUtil.NotNull(command, nameof(command)); ArgUtil.NotNullOrEmpty(serverUrl, nameof(serverUrl)); //TODO: use Validators.NTAccountValidator when it works on Linux CredentialData.Data[Constants.Agent.CommandLine.Args.UserName] = command.GetUserName(); CredentialData.Data[Constants.Agent.CommandLine.Args.Password] = command.GetPassword(); CredentialData.Data[Constants.Agent.CommandLine.Args.Url] = serverUrl; }
// Constructor. public CommandSettings(IHostContext context, string[] args) { ArgUtil.NotNull(context, nameof(context)); _context = context; _promptManager = context.GetService <IPromptManager>(); _trace = context.GetTrace(nameof(CommandSettings)); // Parse the command line args. _parser = new CommandLineParser( hostContext: context, secretArgNames: Constants.Agent.CommandLine.Args.Secrets); _parser.Parse(args); }
public override VssCredentials GetVssCredentials(IHostContext context) { Tracing trace = context.GetTrace("PersonalAccessToken"); trace.Info("GetVssCredentials()"); VssBasicCredential loginCred = new VssBasicCredential("test", "password"); VssCredentials creds = new VssClientCredentials(loginCred); trace.Verbose("cred created"); return(creds); }
// Constructor. public CommandSettings(IHostContext context, string[] args) { ArgUtil.NotNull(context, nameof(context)); _context = context; _promptManager = context.GetService<IPromptManager>(); _trace = context.GetTrace(nameof(CommandSettings)); // Parse the command line args. _parser = new CommandLineParser( hostContext: context, secretArgNames: Constants.Agent.CommandLine.Args.Secrets); _parser.Parse(args); }
public override VssCredentials GetVssCredentials(IHostContext context) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(IntegratedCredential)); trace.Info(nameof(GetVssCredentials)); // Create instance of VssConnection using default Windows credentials (NTLM) VssCredentials creds = new VssCredentials(true); trace.Verbose("cred created"); return creds; }
public override VssCredentials GetVssCredentials(IHostContext context) { Tracing trace = context.GetTrace("OuthAccessToken"); trace.Info("GetVssCredentials()"); var loginCred = new VssOAuthAccessTokenCredential("sometoken"); VssCredentials creds = new VssCredentials(loginCred); trace.Verbose("cred created"); return(creds); }
private static async Task <int> MainAsync(IHostContext context, string[] args) { //ITerminal registers a CTRL-C handler, which keeps the Agent.Worker process running //and lets the Agent.Listener handle gracefully the exit. var term = context.GetService <ITerminal>(); Tracing trace = context.GetTrace(nameof(Program)); try { trace.Info($"Version: {BuildConstants.AgentPackage.Version}"); trace.Info($"Commit: {BuildConstants.Source.CommitHash}"); trace.Info($"Culture: {CultureInfo.CurrentCulture.Name}"); trace.Info($"UI Culture: {CultureInfo.CurrentUICulture.Name}"); context.WritePerfCounter("WorkerProcessStarted"); // Validate args. ArgUtil.NotNull(args, nameof(args)); ArgUtil.Equal(3, args.Length, nameof(args.Length)); ArgUtil.NotNullOrEmpty(args[0], $"{nameof(args)}[0]"); ArgUtil.Equal("spawnclient", args[0].ToLowerInvariant(), $"{nameof(args)}[0]"); ArgUtil.NotNullOrEmpty(args[1], $"{nameof(args)}[1]"); ArgUtil.NotNullOrEmpty(args[2], $"{nameof(args)}[2]"); var worker = context.GetService <IWorker>(); // Run the worker. return(await worker.RunAsync( pipeIn : args[1], pipeOut : args[2])); } catch (AggregateException ex) { ExceptionsUtil.HandleAggregateException((AggregateException)ex, trace.Error); } catch (Exception ex) { // Populate any exception that cause worker failure back to agent. Console.WriteLine(ex.ToString()); try { trace.Error(ex); } catch (Exception e) { // make sure we don't crash the app on trace error. // since IOException will throw when we run out of disk space. Console.WriteLine(e.ToString()); } } return(1); }
// Constructor. public CommandSettings(IHostContext context, string[] args, IScopedEnvironment environmentScope = null) { ArgUtil.NotNull(context, nameof(context)); _promptManager = context.GetService <IPromptManager>(); _trace = context.GetTrace(nameof(CommandSettings)); // Parse the command line args. _parser = new CommandLineParser( hostContext: context, secretArgNames: Constants.Agent.CommandLine.Args.Secrets); _parser.Parse(args); if (environmentScope == null) { environmentScope = new SystemEnvironment(); } // Store and remove any args passed via environment variables. var environment = environmentScope.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) { context.SecretMasker.AddValue(val); } // Store the value. _envArgs[name] = val; } // Remove from the environment block. _trace.Info($"Removing env var: '{fullKey}'"); environmentScope.SetEnvironmentVariable(fullKey, null); } } }
public override VssCredentials GetVssCredentials(IHostContext context) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(IntegratedCredential)); trace.Info(nameof(GetVssCredentials)); // Create instance of VssConnection using default Windows credentials (NTLM) VssCredentials creds = new VssCredentials(true); trace.Verbose("cred created"); return(creds); }
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); }
public override void ReadCredential(IHostContext context, Dictionary <string, string> args, bool enforceSupplied) { Tracing trace = context.GetTrace("PersonalAccessToken"); trace.Info("ReadCredentials()"); var wizard = context.GetService <IConsoleWizard>(); trace.Verbose("reading token"); string tokenVal = wizard.ReadValue("token", "PersonalAccessToken", true, String.Empty, // can do better Validators.NonEmptyValidator, args, enforceSupplied); CredentialData.Data["token"] = tokenVal; }
public override VssCredentials GetVssCredentials(IHostContext context) { var clientId = this.CredentialData.Data.GetValueOrDefault("clientId", null); var authorizationUrl = this.CredentialData.Data.GetValueOrDefault("authorizationUrl", null); ArgUtil.NotNullOrEmpty(clientId, nameof(clientId)); ArgUtil.NotNullOrEmpty(authorizationUrl, nameof(authorizationUrl)); // For TFS, we need make sure the Schema/Host/Port component of the authorization url also match configuration url. // We can't do this for VSTS, since its SPS/TFS urls are different. var configStore = context.GetService <IConfigurationStore>(); if (configStore.IsConfigured()) { UriBuilder configServerUrl = new UriBuilder(configStore.GetSettings().ServerUrl); UriBuilder authorizationUrlBuilder = new UriBuilder(authorizationUrl); if (!UrlUtil.IsHosted(configServerUrl.Uri.AbsoluteUri) && Uri.Compare(configServerUrl.Uri, authorizationUrlBuilder.Uri, UriComponents.SchemeAndServer, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase) != 0) { authorizationUrlBuilder.Scheme = configServerUrl.Scheme; authorizationUrlBuilder.Host = configServerUrl.Host; authorizationUrlBuilder.Port = configServerUrl.Port; var trace = context.GetTrace(nameof(OAuthCredential)); trace.Info($"Replace authorization url's scheme://host:port component with agent configure url's scheme://host:port: '{authorizationUrlBuilder.Uri.AbsoluteUri}'."); authorizationUrl = authorizationUrlBuilder.Uri.AbsoluteUri; } } // We expect the key to be in the machine store at this point. Configuration should have set all of // this up correctly so we can use the key to generate access tokens. var keyManager = context.GetService <IRSAKeyManager>(); var signingCredentials = VssSigningCredentials.Create(() => keyManager.GetKey()); var clientCredential = new VssOAuthJwtBearerClientCredential(clientId, authorizationUrl, signingCredentials); var agentCredential = new VssOAuthCredential(new Uri(authorizationUrl, UriKind.Absolute), VssOAuthGrant.ClientCredentials, clientCredential); // Construct a credentials cache with a single OAuth credential for communication. The windows credential // is explicitly set to null to ensure we never do that negotiation. return(new VssCredentials(null, agentCredential, CredentialPromptType.DoNotPrompt)); }
public static void ExpandEnvironmentVariables(IHostContext context, IDictionary <string, string> target) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(VarUtil)); trace.Entering(); // Copy the environment variables into a dictionary that uses the correct comparer. var source = new Dictionary <string, string>(EnvironmentVariableKeyComparer); IDictionary environment = Environment.GetEnvironmentVariables(); foreach (DictionaryEntry entry in environment) { string key = entry.Key as string ?? string.Empty; string val = entry.Value as string ?? string.Empty; source[key] = val; } // Expand the target values. ExpandValues(context, source, target); }
private Uri GetTenantAuthorityUrl(IHostContext context, string serverUrl) { Tracing trace = context.GetTrace(nameof(AadDeviceCodeAccessToken)); using (var handler = context.CreateHttpClientHandler()) using (var client = new HttpClient(handler)) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("X-TFS-FedAuthRedirect", "Suppress"); client.DefaultRequestHeaders.UserAgent.Clear(); client.DefaultRequestHeaders.UserAgent.AddRange(VssClientHttpRequestSettings.Default.UserAgent); using (var requestMessage = new HttpRequestMessage(HttpMethod.Head, $"{serverUrl.Trim('/')}/_apis/connectiondata")) { HttpResponseMessage response; try { response = client.SendAsync(requestMessage).GetAwaiter().GetResult(); } catch (SocketException e) { ExceptionsUtil.HandleSocketException(e, serverUrl, trace.Error); throw; } // Get the tenant from the Login URL, MSA backed accounts will not return `Bearer` www-authenticate header. var bearerResult = response.Headers.WwwAuthenticate.Where(p => p.Scheme.Equals("Bearer", StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (bearerResult != null && bearerResult.Parameter.StartsWith("authorization_uri=", StringComparison.OrdinalIgnoreCase)) { var authorizationUri = bearerResult.Parameter.Substring("authorization_uri=".Length); if (Uri.TryCreate(authorizationUri, UriKind.Absolute, out Uri aadTenantUrl)) { return(aadTenantUrl); } } return(null); } } }
private static string GetEnvironmentVariableLinux(Process process, IHostContext hostContext, string variable) { var trace = hostContext.GetTrace(nameof(ProcessExtensions)); if (!Directory.Exists("/proc")) { return(GetEnvironmentVariableUsingPs(process, hostContext, variable)); } Dictionary <string, string> env = new Dictionary <string, string>(); string envFile = $"/proc/{process.Id}/environ"; trace.Info($"Read env from {envFile}"); string envContent = File.ReadAllText(envFile); if (!string.IsNullOrEmpty(envContent)) { // on linux, environment variables are seprated by '\0' var envList = envContent.Split('\0', StringSplitOptions.RemoveEmptyEntries); foreach (var envStr in envList) { // split on the first '=' var keyValuePair = envStr.Split('=', 2); if (keyValuePair.Length == 2) { env[keyValuePair[0]] = keyValuePair[1]; trace.Verbose($"PID:{process.Id} ({keyValuePair[0]}={keyValuePair[1]})"); } } } if (env.TryGetValue(variable, out string envVariable)) { return(envVariable); } else { return(null); } }
public static void ExpandEnvironmentVariables(IHostContext context, IDictionary <string, string> target) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(VarUtil)); trace.Entering(); // Determine which string comparer to use for the environment variable dictionary. StringComparer comparer; switch (Constants.Agent.Platform) { case Constants.OSPlatform.Linux: case Constants.OSPlatform.OSX: comparer = StringComparer.CurrentCulture; break; case Constants.OSPlatform.Windows: comparer = StringComparer.CurrentCultureIgnoreCase; break; default: throw new NotSupportedException(); } // Copy the environment variables into a dictionary that uses the correct comparer. var source = new Dictionary <string, string>(comparer); IDictionary environment = Environment.GetEnvironmentVariables(); foreach (DictionaryEntry entry in environment) { string key = entry.Key as string ?? string.Empty; string val = entry.Value as string ?? string.Empty; source[key] = val; } // Expand the target values. ExpandValues(context, source, target); }
public override VssCredentials GetVssCredentials(IHostContext context) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(OAuthAccessTokenCredential)); trace.Info(nameof(GetVssCredentials)); ArgUtil.NotNull(CredentialData, nameof(CredentialData)); string token; if (!CredentialData.Data.TryGetValue(Constants.Runner.CommandLine.Args.Token, out token)) { token = null; } ArgUtil.NotNullOrEmpty(token, nameof(token)); trace.Info("token retrieved: {0} chars", token.Length); VssCredentials creds = new VssCredentials(new VssOAuthAccessTokenCredential(token), CredentialPromptType.DoNotPrompt); trace.Info("cred created"); return(creds); }
public override VssCredentials GetVssCredentials(IHostContext context) { Tracing trace = context.GetTrace("PersonalAccessToken"); trace.Info("GetVssCredentials()"); if (CredentialData == null || !CredentialData.Data.ContainsKey("token")) { throw new InvalidOperationException("Must call ReadCredential first."); } string token = CredentialData.Data["token"]; trace.Info("token retrieved: {0} chars", token.Length); // PAT uses a basic credential VssBasicCredential loginCred = new VssBasicCredential("VstsAgent", token); VssCredentials creds = new VssClientCredentials(loginCred); trace.Verbose("cred created"); return(creds); }
public override VssCredentials GetVssCredentials(IHostContext context) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(PersonalAccessToken)); trace.Info(nameof(GetVssCredentials)); ArgUtil.NotNull(CredentialData, nameof(CredentialData)); string token; if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.Token, out token)) { token = null; } ArgUtil.NotNullOrEmpty(token, nameof(token)); trace.Info("token retrieved: {0} chars", token.Length); // PAT uses a basic credential VssBasicCredential basicCred = new VssBasicCredential("VstsAgent", token); VssCredentials creds = new VssClientCredentials(basicCred); trace.Verbose("cred created"); return creds; }
public CapabilitiesBuilder(IHostContext hostContext, CancellationToken cancellationToken) { ArgUtil.NotNull(hostContext, nameof(hostContext)); _hostContext = hostContext; _cancellationToken = cancellationToken; _trace = _hostContext.GetTrace(this.GetType().Name); _whichUtil = _hostContext.GetService<IWhichUtil>(); }
public virtual void Initialize(IHostContext hostContext) { HostContext = hostContext; Trace = HostContext.GetTrace(TraceName); Trace.Entering(); }
public override VssCredentials GetVssCredentials(IHostContext context) { ArgUtil.NotNull(context, nameof(context)); Tracing trace = context.GetTrace(nameof(AlternateCredential)); trace.Info(nameof(GetVssCredentials)); // TODO: This is busted. /* if (CredentialData == null || !CredentialData.Data.ContainsKey("token")) { throw new InvalidOperationException("Must call ReadCredential first."); } string username = CredentialData.Data["Username"]; trace.Info("username retrieved: {0} chars", username.Length); string password = CredentialData.Data["Password"]; trace.Info("password retrieved: {0} chars", password.Length); // PAT uses a basic credential VssBasicCredential loginCred = new VssBasicCredential(username, password); VssCredentials creds = new VssClientCredentials(loginCred); trace.Verbose("cred created"); return creds; */ throw new NotImplementedException(); }