|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using DocoptNet; |
| 4 | +using Seq.Api; |
| 5 | +using Seq.Api.Model.Settings; |
| 6 | + |
| 7 | +namespace SeqEnableAAD |
| 8 | +{ |
| 9 | + class Program |
| 10 | + { |
| 11 | + const string Usage = @"seq-enable-aad: enable authentication on your Seq server (for initial setup of a new Seq server only). |
| 12 | +
|
| 13 | +Usage: |
| 14 | + seq-enable-aad.exe <server> --uname=<un> --tenantid=<tid> --clientid=<cid> --clientkey=<ckey> [--authority=<a>] |
| 15 | + seq-enable-aad.exe (-h | --help) |
| 16 | +
|
| 17 | +Options: |
| 18 | + -h --help Show this screen. |
| 19 | + --uname=<un> Username. Azure Active Directory usernames must take the form of an email address. |
| 20 | + --tenantid=<tid> Tenant ID. |
| 21 | + --clientid=<cid> Client ID. |
| 22 | + --clientkey=<ckey> Client key. |
| 23 | + --authority=<a> Authority (optional, defaults to 'login.windows.net'). |
| 24 | + "; |
| 25 | + static void Main(string[] args) |
| 26 | + { |
| 27 | + Task.Run(async () => |
| 28 | + { |
| 29 | + try |
| 30 | + { |
| 31 | + var arguments = new Docopt().Apply(Usage, args, version: "Seq Enable AAD 0.1", exit: true); |
| 32 | + |
| 33 | + var server = arguments["<server>"].ToString(); |
| 34 | + var username = Normalize(arguments["--uname"]); |
| 35 | + var tenantId = Normalize(arguments["--tenantid"]); |
| 36 | + var clientId = Normalize(arguments["--clientid"]); |
| 37 | + var clientKey = Normalize(arguments["--clientkey"]); |
| 38 | + var authority = Normalize(arguments["--authority"]); |
| 39 | + |
| 40 | + await Run(server, username, tenantId, clientId, clientKey, authority); |
| 41 | + } |
| 42 | + catch (Exception ex) |
| 43 | + { |
| 44 | + Console.ForegroundColor = ConsoleColor.White; |
| 45 | + Console.BackgroundColor = ConsoleColor.Red; |
| 46 | + Console.WriteLine("seq-enable-aad: {0}", ex); |
| 47 | + Console.ResetColor(); |
| 48 | + Environment.Exit(-1); |
| 49 | + } |
| 50 | + }).Wait(); |
| 51 | + } |
| 52 | + |
| 53 | + static string Normalize(ValueObject v) |
| 54 | + { |
| 55 | + if (v == null) return null; |
| 56 | + var s = v.ToString(); |
| 57 | + return string.IsNullOrWhiteSpace(s) ? null : s; |
| 58 | + } |
| 59 | + |
| 60 | + static async Task Run(string server, string username, string tenantId, string clientId, string clientKey, string authority="login.windows.net") |
| 61 | + { |
| 62 | + var connection = new SeqConnection(server); |
| 63 | + |
| 64 | + var user = await connection.Users.FindCurrentAsync(); |
| 65 | + var provider = await connection.Settings.FindNamedAsync(SettingName.AuthenticationProvider); |
| 66 | + var cid = await connection.Settings.FindNamedAsync(SettingName.AzureADClientId); |
| 67 | + var ckey = await connection.Settings.FindNamedAsync(SettingName.AzureADClientKey); |
| 68 | + var aut = await connection.Settings.FindNamedAsync(SettingName.AzureADAuthority); |
| 69 | + var tid = await connection.Settings.FindNamedAsync(SettingName.AzureADTenantId); |
| 70 | + |
| 71 | + user.Username = username; |
| 72 | + provider.Value = "Azure Active Directory"; |
| 73 | + cid.Value = clientId; |
| 74 | + ckey.Value = clientKey; |
| 75 | + tid.Value = tenantId; |
| 76 | + aut.Value = authority; |
| 77 | + |
| 78 | + await connection.Users.UpdateAsync(user); |
| 79 | + await connection.Settings.UpdateAsync(cid); |
| 80 | + await connection.Settings.UpdateAsync(ckey); |
| 81 | + await connection.Settings.UpdateAsync(tid); |
| 82 | + await connection.Settings.UpdateAsync(aut); |
| 83 | + |
| 84 | + await connection.Settings.UpdateAsync(provider); // needs to go before IsAuthenticationEnabled but after the other settings |
| 85 | + |
| 86 | + var iae = await connection.Settings.FindNamedAsync(SettingName.IsAuthenticationEnabled); |
| 87 | + iae.Value = true; |
| 88 | + await connection.Settings.UpdateAsync(iae); // this update needs to happen last, as enabling auth will lock this connection out |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments