Exemple #1
0
        internal override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var context = await Context.BuildAsync(cancellationToken);

            var azData = new AzureLogon()
            {
                SubscriptionId = Environment.GetEnvironmentVariable("AGGREGATOR_SUBSCRIPTIONID") ?? string.Empty,
                ClientId       = Environment.GetEnvironmentVariable("AGGREGATOR_CLIENTID") ?? string.Empty,
                ClientSecret   = Environment.GetEnvironmentVariable("AGGREGATOR_CLIENTSECRET") ?? string.Empty,
                TenantId       = Environment.GetEnvironmentVariable("AGGREGATOR_TENANTID") ?? string.Empty
            };

            _ = azData.Save();
            // now check for validity
            context.Logger.WriteInfo("Connecting to Azure...");
            var azure = azData.Logon();

            if (azure == null)
            {
                context.Logger.WriteError("Invalid azure credentials");
                return(ExitCodes.InvalidArguments);
            }
            // FIX #60: call some read API to validate parameters
            try
            {
                await azure.Subscriptions.ListAsync(false, cancellationToken);
            }
            catch (Exception ex)
            {
                int    nl = ex.Message.IndexOf(Environment.NewLine);
                string m  = nl != -1 ? ex.Message.Remove(nl) : ex.Message;
                context.Logger.WriteError("Invalid azure credentials: " + m);
                return(ExitCodes.InvalidArguments);
            }

            var data = new DevOpsLogon()
            {
                Url  = Environment.GetEnvironmentVariable("AGGREGATOR_AZDO_URL") ?? string.Empty,
                Mode = (DevOpsTokenType)Enum.Parse(typeof(DevOpsTokenType),
                                                   Environment.GetEnvironmentVariable("AGGREGATOR_AZDO_MODE") ?? "PAT"),
                Token = Environment.GetEnvironmentVariable("AGGREGATOR_AZDO_TOKEN") ?? string.Empty,
            };

            _ = data.Save();
            // now check for validity
            context.Logger.WriteInfo($"Connecting to Azure DevOps using {data.Mode} credential...");
            var devops = await data.LogonAsync(cancellationToken);

            if (devops == null)
            {
                context.Logger.WriteError("Invalid Azure DevOps credentials");
                return(ExitCodes.InvalidArguments);
            }

            return(ExitCodes.Success);
        }
Exemple #2
0
        internal override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var context = await Context.BuildAsync(cancellationToken);

            context.Logger.WriteVerbose($"Clearing cached credentials");
            AzureLogon.Clear();
            DevOpsLogon.Clear();

            return(ExitCodes.Success);
        }
        internal async Task <CommandContext> BuildAsync(CancellationToken cancellationToken)
        {
            IAzure        azure  = null;
            VssConnection devops = null;

            if (azureLogon)
            {
                logger.WriteVerbose($"Authenticating to Azure...");
                var(connection, reason) = AzureLogon.Load();
                if (reason != LogonResult.Succeeded)
                {
                    string msg = TranslateResult(reason);
                    throw new InvalidOperationException(string.Format(msg, "Azure", "logon.azure"));
                }

                azure = connection.Logon();
                logger.WriteInfo($"Connected to subscription {azure.SubscriptionId}");
            }

            if (devopsLogon)
            {
                logger.WriteVerbose($"Authenticating to Azure DevOps...");
                var(connection, reason) = DevOpsLogon.Load();
                if (reason != LogonResult.Succeeded)
                {
                    string msg = TranslateResult(reason);
                    throw new InvalidOperationException(string.Format(msg, "Azure DevOps", "logon.ado"));
                }

                devops = await connection.LogonAsync(cancellationToken);

                logger.WriteInfo($"Connected to {devops.Uri.Host}");
            }

            INamingTemplates naming;

            switch (namingTemplate.ToLower())
            {
            case "builtin":
#pragma warning disable S907 // "goto" statement should not be used
                goto case "";

#pragma warning restore S907 // "goto" statement should not be used
            case "":
                naming = new BuiltInNamingTemplates();
                break;

            default:
                // implement custom Naming Templates, e.g. reading from a file
                naming = new FileNamingTemplates(File.ReadAllText(namingTemplate));
                break;
            }

            return(new CommandContext(logger, azure, devops, naming));
        }
Exemple #4
0
        internal override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var context = await Context.BuildAsync(cancellationToken);

            var data = new AzureLogon()
            {
                SubscriptionId = this.SubscriptionId,
                ClientId       = this.ClientId,
                ClientSecret   = this.ClientSecret,
                TenantId       = this.TenantId
            };
            string path = data.Save();

            // now check for validity
            context.Logger.WriteInfo("Connecting to Azure...");
            var azure = data.Logon();

            if (azure == null)
            {
                context.Logger.WriteError("Invalid azure credentials");
                return(2);
            }
            return(0);
        }
        internal override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var context = await Context.BuildAsync(cancellationToken);

            var data = new AzureLogon()
            {
                SubscriptionId = this.SubscriptionId,
                ClientId       = this.ClientId,
                ClientSecret   = this.ClientSecret,
                TenantId       = this.TenantId
            };

            _ = data.Save();
            // now check for validity
            context.Logger.WriteInfo("Connecting to Azure...");
            var azure = data.Logon();

            if (azure == null)
            {
                context.Logger.WriteError("Invalid azure credentials");
                return(ExitCodes.InvalidArguments);
            }
            // FIX #60: call some read API to validate parameters
            try
            {
                await azure.Subscriptions.ListAsync(false, cancellationToken);
            }
            catch (Exception ex)
            {
                int    nl = ex.Message.IndexOf(Environment.NewLine);
                string m  = nl != -1 ? ex.Message.Remove(nl) : ex.Message;
                context.Logger.WriteError("Invalid azure credentials: " + m);
                return(ExitCodes.InvalidArguments);
            }
            return(ExitCodes.Success);
        }