Skip to content

Commit

Permalink
Use az-cli for login context
Browse files Browse the repository at this point in the history
Closes #617
Closes #275
Closes #180
Closes #620
Closes #351
Closes #237
Closes #408
Closes #402
Closes #325
Closes #308
Closes #297
Closes #296
  • Loading branch information
ahmelsayed committed Aug 20, 2018
1 parent f6cb723 commit d776e40
Show file tree
Hide file tree
Showing 57 changed files with 552 additions and 1,539 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Azure.Functions.Cli.Interfaces;
using static Azure.Functions.Cli.Common.OutputTheme;
using Azure.Functions.Cli.Common;
using Azure.Functions.Cli.Helpers;

namespace Azure.Functions.Cli.Actions.AzureActions
{
Expand All @@ -18,8 +19,7 @@ internal class AddStorageAccountSettingAction : BaseAzureAction

public string StorageAccountName { get; set; }

public AddStorageAccountSettingAction(IArmManager armManager, ISettings settings, ISecretsManager secretsManager)
: base(armManager)
public AddStorageAccountSettingAction(ISettings settings, ISecretsManager secretsManager)
{
_settings = settings;
_secretsManager = secretsManager;
Expand All @@ -42,24 +42,13 @@ public override ICommandLineParserResult ParseArgs(string[] args)

public override async Task RunAsync()
{
var storageAccounts = await _armManager.GetStorageAccountsAsync();
var storageAccount = storageAccounts.FirstOrDefault(st => st.StorageAccountName.Equals(StorageAccountName, StringComparison.OrdinalIgnoreCase));
var storageAccount = await AzureHelper.GetStorageAccount(StorageAccountName, AccessToken);

if (storageAccount == null)
{
ColoredConsole
.Error
.WriteLine(ErrorColor($"Can't find storage account with name {StorageAccountName} in current subscription ({_settings.CurrentSubscription})"));
}
else
{
storageAccount = await _armManager.LoadAsync(storageAccount);
var name = $"{storageAccount.StorageAccountName}_STORAGE";
_secretsManager.SetSecret(name, storageAccount.GetConnectionString());
ColoredConsole
.WriteLine($"Secret saved locally in {SecretsManager.AppSettingsFileName} under name {ExampleColor(name)}.")
.WriteLine();
}
var name = $"{storageAccount.StorageAccountName}_STORAGE";
_secretsManager.SetSecret(name, storageAccount.ConnectionString);
ColoredConsole
.WriteLine($"Secret saved locally in {SecretsManager.AppSettingsFileName} under name {ExampleColor(name)}.")
.WriteLine();
}
}
}

This file was deleted.

118 changes: 113 additions & 5 deletions src/Azure.Functions.Cli/Actions/AzureActions/BaseAzureAction.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,122 @@
using Azure.Functions.Cli.Arm;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Azure.Functions.Cli.Arm;
using Azure.Functions.Cli.Common;
using Azure.Functions.Cli.Interfaces;
using Fclp;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Azure.Functions.Cli.Actions.AzureActions
{
abstract class BaseAzureAction : BaseAction
abstract class BaseAzureAction : BaseAction, IInitializableAction
{
protected readonly IArmManager _armManager;
public string AccessToken { get; set; }
public bool ReadStdin { get; set; }

protected BaseAzureAction(IArmManager armManager)
public override ICommandLineParserResult ParseArgs(string[] args)
{
_armManager = armManager;
Parser
.Setup<string>("access-token")
.WithDescription("Access token to use for performing authenticated azure actions")
.Callback(t => AccessToken = t);
Parser
.Setup<bool>("access-token-stdin")
.WithDescription("Read access token from stdin e.g: az account get-access-token | func ... --access-token-stdin")
.Callback(f => ReadStdin = f);

return base.ParseArgs(args);
}

public async Task Initialize()
{
if (!string.IsNullOrEmpty(AccessToken))
{
return;
}
else if (ReadStdin && System.Console.In != null)
{
var accessToken = System.Console.In.ReadToEnd().Trim(' ', '\n', '\r', '"');
if (accessToken.StartsWith("{"))
{
var json = JsonConvert.DeserializeObject<JObject>(accessToken);
AccessToken = json["accessToken"].ToString();
}
else
{
AccessToken = accessToken;
}
if (string.IsNullOrEmpty(AccessToken))
{
throw new CliException("Unable to set access token from stdin.");
}
}
else if (ReadStdin && System.Console.In == null)
{
throw new CliException("Stdin unavailable");
}
else
{
AccessToken = await GetAccessToken();
}
}

private async Task<string> GetAccessToken()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return await AzureCliGetToken();
}
else
{
try
{
return await AzureCliGetToken();
}
catch (FileNotFoundException)
{
try
{
return await AzurePowershellGetToken();
}
catch (FileNotFoundException)
{
throw new CliException("Cannot get accessToken from az cli or Azure powershell. Please make sure to have either one installed.");
}
}
}
}

private async Task<string> AzureCliGetToken()
{
if (CommandChecker.CommandExists("az"))
{
var az = new Executable("az", "account get-access-token --query \"accessToken\"");
var stdout = new StringBuilder();
var stderr = new StringBuilder();
var exitCode = await az.RunAsync(o => stdout.AppendLine(o), e => stderr.AppendLine(e));
if (exitCode != 0)
{
throw new CliException(stderr.ToString().Trim(' ', '\n', '\r'));
}
else
{
return stdout.ToString().Trim(' ', '\n', '\r', '"');
}
}
else
{
throw new FileNotFoundException("Cannot find az cli. Please make sure to install az cli.");
}
}

private Task<string> AzurePowershellGetToken()
{
throw new FileNotFoundException("Cannot find powershell");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ abstract class BaseFunctionAppAction : BaseAzureAction
{
public string FunctionAppName { get; set; }

public BaseFunctionAppAction(IArmManager armManager) : base(armManager)
{ }

public override ICommandLineParserResult ParseArgs(string[] args)
{
if (args.Any() && !args.First().StartsWith("-"))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Threading.Tasks;
using Azure.Functions.Cli.Common;

namespace Azure.Functions.Cli.Actions.AzureActions
{
[Action(Name = "enable-git-repo", Context = Context.Azure, SubContext = Context.FunctionApp, ShowInHelp = false)]
[Action(Name = "get-publish-username", Context = Context.Azure, ShowInHelp = false)]
[Action(Name = "list", Context = Context.Azure, SubContext = Context.Account, ShowInHelp = false)]
[Action(Name = "list", Context = Context.Azure, SubContext = Context.Subscriptions, ShowInHelp = false)]
[Action(Name = "list", Context = Context.Azure, SubContext = Context.FunctionApp, ShowInHelp = false)]
[Action(Name = "list", Context = Context.Azure, SubContext = Context.Storage, ShowInHelp = false)]
[Action(Name = "login", Context = Context.Azure, ShowInHelp = false)]
[Action(Name = "logout", Context = Context.Azure, ShowInHelp = false)]
[Action(Name = "set", Context = Context.Azure, SubContext = Context.Account, ShowInHelp = false)]
[Action(Name = "set", Context = Context.Azure, SubContext = Context.Subscriptions, ShowInHelp = false)]
[Action(Name = "set-publish-password", Context = Context.Azure, ShowInHelp = false)]
[Action(Name = "set-publish-username", Context = Context.Azure, ShowInHelp = false)]
internal class DeprecatedAzureActions : BaseAction
{
public override Task RunAsync()
{
throw new CliException("This command has been removed. Please use az-cli (https://docs.microsoft.com/en-us/cli/azure/install-azure-cli) or Azure Powershell (https://docs.microsoft.com/en-us/powershell/azure/overview) for management commands.");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Azure.Functions.Cli.Arm;
using Azure.Functions.Cli.Interfaces;
using static Azure.Functions.Cli.Common.OutputTheme;
using Azure.Functions.Cli.Helpers;

namespace Azure.Functions.Cli.Actions.AzureActions
{
Expand All @@ -13,20 +14,18 @@ internal class FetchAppSettingsAction : BaseFunctionAppAction
{
private ISecretsManager _secretsManager;

public FetchAppSettingsAction(IArmManager armManager, ISecretsManager secretsManager)
: base(armManager)
public FetchAppSettingsAction(ISecretsManager secretsManager)
{
_secretsManager = secretsManager;
}

public override async Task RunAsync()
{
var functionApp = await _armManager.GetFunctionAppAsync(FunctionAppName);
var functionApp = await AzureHelper.GetFunctionApp(FunctionAppName, AccessToken);
if (functionApp != null)
{
ColoredConsole.WriteLine(TitleColor("App Settings:"));
var appSettings = await _armManager.GetFunctionAppAppSettings(functionApp);
foreach (var pair in appSettings)
foreach (var pair in functionApp.AzureAppSettings)
{
ColoredConsole.WriteLine($"Loading {pair.Key} = *****");
_secretsManager.SetSecret(pair.Key, pair.Value);
Expand All @@ -35,11 +34,10 @@ public override async Task RunAsync()
ColoredConsole.WriteLine();

ColoredConsole.WriteLine(TitleColor("Connection Strings:"));
var connectionStrings = await _armManager.GetFunctionAppConnectionStrings(functionApp);
foreach (var connectionString in connectionStrings)
foreach (var connectionString in functionApp.ConnectionStrings)
{
ColoredConsole.WriteLine($"Loading {connectionString.Key} = *****");
_secretsManager.SetConnectionString(connectionString.Key, connectionString.Value.Value);
_secretsManager.SetConnectionString(connectionString.Key, connectionString.Value.value);
}

}
Expand Down
Loading

0 comments on commit d776e40

Please sign in to comment.