-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (93 loc) · 4.69 KB
/
Program.cs
File metadata and controls
110 lines (93 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.Authentication.AzureAuth
{
using System;
using System.Runtime.InteropServices;
using System.Text;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Authentication.AzureAuth.Commands;
using Microsoft.Authentication.MSALWrapper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Office.Lasso;
using Microsoft.Office.Lasso.Telemetry;
/// <summary>
/// The start up program.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
// Use UTF-8 output encoding.
// This will impact the NLog Console Target as well as any other Console usage.
Console.OutputEncoding = Encoding.UTF8;
CommandLineApplication app = new CommandLineApplication<CommandAzureAuth>();
// We always instantiate and depend on telemetry services, but these defaults
// mean telemetry is effectively disabled by using a nonsensical ingest token
// and setting our output to standard out. Nothing will be sent remotely.
string ingestionToken = "Not a real ingestion token, but Lasso requires this be non-empty or dependency injection will break.";
TelemetryOutput backend = TelemetryOutput.StandardOut;
// We will only send telemetry if we are given an ingestion token via
// environment variable or registry. We *have* to do this here, rather than in
// `CommandMain.OnExecute`, because Lasso doesn't have a way of allowing a
// `CommandLineApplication` to dynamically set telemetry configuration. Even if
// it did, we can't guarantee that a failure in command line parsing wouldn't
// trigger telemetry before we ever get to disable it.
//
// To disable telemetry a user need only leave this environment variable unset. It's off by default.
string applicationInsightsIngestionToken = Environment.GetEnvironmentVariable(EnvVars.ApplicationInsightsIngestionTokenEnvVar);
if (string.IsNullOrEmpty(applicationInsightsIngestionToken))
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
applicationInsightsIngestionToken = Win32.Registry.GetValue(
EnvVars.ApplicationInsightsIngestionTokenRegKeyPath,
EnvVars.ApplicationInsightsIngestionTokenRegKeyName,
null) as string;
}
}
if (!string.IsNullOrEmpty(applicationInsightsIngestionToken))
{
ingestionToken = applicationInsightsIngestionToken;
backend = TelemetryOutput.ApplicationInsights;
}
var envVarsToCollect = new[]
{
Ado.Constants.SystemDefinitionId,
EnvVars.CloudBuild,
EnvVars.NoUser,
EnvVars.CorextNonInteractive,
EnvVars.AuthMode,
};
TelemetryConfig telemetryConfig = new TelemetryConfig(
eventNamespace: "azureauth",
backend: backend,
ingestionToken: ingestionToken,
useAsync: true,
envVarsToCollect: envVarsToCollect,
hideAlias: true,
hideMachineName: true);
// We want redirect stdout to get just token output
// while warnings and errors still go to stderr to be seen by a user.
var stdErrLogLevel = Microsoft.Extensions.Logging.LogLevel.Warning;
LassoOptions options = new LassoOptions(
telemetryConfig,
sendExecuteEvent: false,
sendCommandEvents: true,
minStderrLoglevel: stdErrLogLevel);
var loggerFactory = new NLog.Extensions.Logging.NLogLoggerFactory();
var logger = loggerFactory.CreateLogger("AzureAuth");
IServiceCollection services = new ServiceCollection();
services.AddSingleton<IMsalWrapper, MsalWrapper>();
services.AddSingleton<IPublicClientAuth, PublicClientAuth>();
services.AddSingleton<ILogger>(logger);
Console.CancelKeyPress += (_, _) =>
{
logger.LogWarning("Received SIGINT (Ctrl+C), exiting...");
Environment.Exit(2);
};
new LassoMcMaster(app, options, services).Execute(args);
}
}
}