forked from microsoftgraph/msgraph-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
239 lines (222 loc) · 11.4 KB
/
Copy pathProgram.cs
File metadata and controls
239 lines (222 loc) · 11.4 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Hosting;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using System.Diagnostics.Tracing;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ApiSdk;
using ApiSdk.Models.ODataErrors;
using Azure.Core.Diagnostics;
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Graph.Authentication;
using Microsoft.Graph.Cli.Core.Authentication;
using Microsoft.Graph.Cli.Core.Commands.Authentication;
using Microsoft.Graph.Cli.Core.Configuration;
using Microsoft.Graph.Cli.Core.Http;
using Microsoft.Graph.Cli.Core.IO;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Cli.Commons.Extensions;
using Microsoft.Kiota.Cli.Commons.Http;
using Microsoft.Kiota.Cli.Commons.Http.Headers;
using Microsoft.Kiota.Http.HttpClientLibrary;
using Microsoft.Kiota.Serialization.Form;
using Microsoft.Kiota.Serialization.Json;
using Microsoft.Kiota.Serialization.Text;
namespace Microsoft.Graph.Cli
{
class Program
{
private static bool debugEnabled = false;
private static AzureEventSourceListener? listener = null;
static async Task<int> Main(string[] args)
{
Console.InputEncoding = Encoding.UTF8;
Console.OutputEncoding = Encoding.UTF8;
var builder = BuildCommandLine()
.UseDefaults()
.UseHost(CreateHostBuilder)
.UseRequestAdapter(ic =>
{
var host = ic.GetHost();
var adapter = host.Services.GetRequiredService<IRequestAdapter>();
var client = host.Services.GetRequiredService<HttpClient>();
if (string.IsNullOrEmpty(adapter.BaseUrl))
{
adapter.BaseUrl = client.BaseAddress?.ToString();
}
adapter.BaseUrl = adapter.BaseUrl?.TrimEnd('/');
return adapter;
})
.RegisterCommonServices()
.RegisterHeadersOption(() => InMemoryHeadersStore.Instance);
builder.AddMiddleware(async (ic, next) =>
{
var host = ic.GetHost();
ic.BindingContext.AddService(_ => host.Services.GetRequiredService<IAuthenticationCacheManager>());
ic.BindingContext.AddService(_ => host.Services.GetRequiredService<AuthenticationServiceFactory>());
// Needed by LogoutCommand
ic.BindingContext.AddService(_ => host.Services.GetRequiredService<LogoutService>());
await next(ic);
});
builder.UseExceptionHandler((ex, context) =>
{
var message = ex switch
{
_ when ex is AuthenticationRequiredException => "Token acquisition failed. Run mgc login command first to get an access token.",
_ when ex is TaskCanceledException => string.Empty,
ODataError _e when ex is ODataError => $"Error {_e.ResponseStatusCode}({_e.Error?.Code}) from API:\n {_e.Error?.Message}",
ApiException _e when ex is ApiException => $"Error {_e.ResponseStatusCode} from API.",
AuthenticationFailedException e => $"Authentication failed: {e.Message}",
Identity.Client.MsalException e => $"Authentication failed: {e.Message}",
_ => ex.Message
};
var exitCode = ex switch
{
_ when ex is AuthenticationRequiredException => 1,
_ when ex is TaskCanceledException => 0,
_ => -1
};
if (!string.IsNullOrEmpty(message))
{
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
context.Console.Error.WriteLine(message);
Console.ResetColor();
}
context.ExitCode = exitCode;
});
try
{
var parser = builder.Build();
return await parser.InvokeAsync(args);
}
finally
{
listener?.Dispose();
}
}
static CommandLineBuilder BuildCommandLine()
{
var rootCommand = new GraphClient().BuildRootCommand();
rootCommand.Description = "Microsoft Graph CLI";
var builder = new CommandLineBuilder(rootCommand);
var debugOption = new Option<bool>("--debug", "Enable debug output");
builder.AddMiddleware(async (ic, next) =>
{
debugEnabled = ic.ParseResult.GetValueForOption<bool>(debugOption);
if (debugEnabled)
{
listener = CreateStdErrLogger(EventLevel.LogAlways);
}
else
{
listener = CreateStdErrLogger(EventLevel.Error);
}
await next(ic);
});
rootCommand.AddCommand(new LoginCommand(builder));
rootCommand.AddCommand(new LogoutCommand());
rootCommand.AddGlobalOption(debugOption);
return builder;
}
static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder().ConfigureHostConfiguration((configHost) =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
}).ConfigureAppConfiguration((ctx, config) =>
{
ConfigureAppConfiguration(config, args);
}).ConfigureServices((ctx, services) =>
{
var authSection = ctx.Configuration.GetSection(nameof(AuthenticationOptions));
services.Configure<AuthenticationOptions>(authSection);
services.AddTransient<LoggingHandler>();
services.AddSingleton<HttpClient>(p =>
{
var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
var options = new GraphClientOptions
{
GraphProductPrefix = "graph-cli",
GraphServiceLibraryClientVersion = $"{assemblyVersion?.Major ?? 0}.{assemblyVersion?.Minor ?? 0}.{assemblyVersion?.Build ?? 0}",
GraphServiceTargetVersion = "1.0"
};
var headersHandler = new NativeHttpHeadersHandler(() => InMemoryHeadersStore.Instance, p.GetService<ILogger<NativeHttpHeadersHandler>>());
return GraphCliClientFactory.GetDefaultClient(options, loggingHandler: p.GetRequiredService<LoggingHandler>(), middlewares: new[] { headersHandler });
});
services.AddSingleton<IAuthenticationProvider>(p =>
{
var authSettings = p.GetRequiredService<IOptions<AuthenticationOptions>>()?.Value;
var serviceFactory = p.GetRequiredService<AuthenticationServiceFactory>();
AuthenticationStrategy authStrategy = authSettings?.Strategy ?? AuthenticationStrategy.DeviceCode;
var credential = serviceFactory.GetTokenCredentialAsync(authStrategy, authSettings?.TenantId, authSettings?.ClientId, authSettings?.ClientCertificateName, authSettings?.ClientCertificateThumbPrint);
credential.Wait();
var client = p.GetRequiredService<HttpClient>();
return new AzureIdentityAuthenticationProvider(credential.Result);
});
services.AddSingleton<IRequestAdapter>(p =>
{
var authProvider = p.GetRequiredService<IAuthenticationProvider>();
var client = p.GetRequiredService<HttpClient>();
// Serializers needed for error parsing
ApiClientBuilder.RegisterDefaultSerializer<JsonSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultSerializer<TextSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultSerializer<FormSerializationWriterFactory>();
// Deserializers needed for error parsing & request body deserialization
ApiClientBuilder.RegisterDefaultDeserializer<JsonParseNodeFactory>();
ApiClientBuilder.RegisterDefaultDeserializer<TextParseNodeFactory>();
ApiClientBuilder.RegisterDefaultDeserializer<FormParseNodeFactory>();
return new HttpClientRequestAdapter(authProvider, httpClient: client);
});
services.AddSingleton<IPathUtility, PathUtility>();
services.AddSingleton<IAuthenticationCacheManager, AuthenticationCacheManager>();
services.AddSingleton<LogoutService>();
services.AddSingleton<AuthenticationServiceFactory>(p =>
{
var authSettings = p.GetRequiredService<IOptions<AuthenticationOptions>>()?.Value;
var pathUtil = p.GetRequiredService<IPathUtility>();
var cacheUtil = p.GetRequiredService<IAuthenticationCacheManager>();
return new AuthenticationServiceFactory(pathUtil, cacheUtil, authSettings);
});
}).ConfigureLogging((ctx, logBuilder) =>
{
logBuilder.SetMinimumLevel(LogLevel.Warning);
logBuilder.ClearProviders();
// Log everything to stderr. Investigate if this breaks scripts that check for stderr instead of the exit code.
logBuilder.AddConsole(c => c.LogToStandardErrorThreshold = LogLevel.Trace);
// Allow runtime selection of log level
logBuilder.AddFilter("Microsoft.Graph.Cli", level => level >= (debugEnabled ? LogLevel.Debug : LogLevel.Warning));
});
static void ConfigureAppConfiguration(IConfigurationBuilder builder, string[] args)
{
builder.Sources.Clear();
builder.AddJsonFile(Path.Combine(System.AppContext.BaseDirectory, "app-settings.json"), optional: true);
var pathUtil = new PathUtility();
var authCache = new AuthenticationCacheManager(pathUtil);
var dataDir = pathUtil.GetApplicationDataDirectory();
var userConfigPath = Path.Combine(dataDir, "settings.json");
builder.AddJsonFile(userConfigPath, optional: true);
builder.AddJsonFile(authCache.GetAuthenticationCacheFilePath(), optional: true, reloadOnChange: true);
builder.AddEnvironmentVariables(prefix: "MGC_");
}
static AzureEventSourceListener CreateStdErrLogger(EventLevel level = EventLevel.Informational)
{
return new AzureEventSourceListener(delegate (EventWrittenEventArgs eventData, string text)
{
// By default, AzureEventSourceListener.CreateConsoleLogger logs to stdout. Use stderr instead.
Console.Error.WriteLine("[{1}] {0}: {2}", eventData.EventSource.Name, eventData.Level, text);
}, level);
}
}
}