예제 #1
0
        /// <summary>
        /// Cmdlet begin process. Write to logs, setup Http Tracing and initialize profile
        /// </summary>
        protected override void BeginProcessing()
        {
            PromptForDataCollectionProfileIfNotExists();
            InitializeQosEvent();
            if (string.IsNullOrEmpty(ParameterSetName))
            {
                WriteDebugWithTimestamp(string.Format("{0} begin processing without ParameterSet.", this.GetType().Name));
            }
            else
            {
                WriteDebugWithTimestamp(string.Format("{0} begin processing with ParameterSet '{1}'.", this.GetType().Name, ParameterSetName));
            }

            if (DefaultContext != null && DefaultContext.Account != null && DefaultContext.Account.Id != null)
            {
                WriteDebugWithTimestamp(string.Format("using account id '{0}'...", DefaultContext.Account.Id));
            }

            _httpTracingInterceptor          = _httpTracingInterceptor ?? new RecordingTracingInterceptor(_debugMessages);
            _adalListener                    = _adalListener ?? new DebugStreamTraceListener(_debugMessages);
            _serviceClientTracingInterceptor = _serviceClientTracingInterceptor ?? new ServiceClientTracingInterceptor(_debugMessages);
            RecordingTracingInterceptor.AddToContext(_httpTracingInterceptor);
            DebugStreamTraceListener.AddAdalTracing(_adalListener);
            ServiceClientTracing.AddTracingInterceptor(_serviceClientTracingInterceptor);

            ProductInfoHeaderValue userAgentValue = new ProductInfoHeaderValue(
                ModuleName, string.Format("v{0}", ModuleVersion));

            AzureSession.ClientFactory.UserAgents.Add(userAgentValue);
            AzureSession.ClientFactory.AddHandler(new CmdletInfoHandler(this.CommandRuntime.ToString(), this.ParameterSetName, this._clientRequestId));
            base.BeginProcessing();
        }
        /// <summary>
        /// Operations that happen before the cmdlet is invoked.
        /// </summary>
        protected override void BeginProcessing()
        {
            string commandAlias = GetType().Name;

            if (cancellationSource == null)
            {
                cancellationSource = new CancellationTokenSource();
            }

            httpTracingInterceptor ??= new RecordingTracingInterceptor(PartnerSession.Instance.DebugMessages);

            ServiceClientTracing.IsEnabled = true;
            ServiceClientTracing.AddTracingInterceptor(httpTracingInterceptor);

            if (MyInvocation != null && MyInvocation.MyCommand != null)
            {
                commandAlias = MyInvocation.MyCommand.Name;
            }

            qosEvent = new PartnerQosEvent
            {
                CommandName      = commandAlias,
                IsSuccess        = true,
                ModuleVersion    = GetType().Assembly.GetName().Version.ToString(),
                ParameterSetName = ParameterSetName
            };

            if (MyInvocation != null && MyInvocation.BoundParameters != null && MyInvocation.BoundParameters.Keys != null)
            {
                qosEvent.Parameters = string.Join(" ", MyInvocation.BoundParameters.Keys.Select(
                                                      s => string.Format(CultureInfo.InvariantCulture, "-{0} ***", s)));
            }

            ProcessBreakingChangeAttributesAtRuntime(GetType(), MyInvocation, WriteWarning);
        }
예제 #3
0
파일: Program.cs 프로젝트: kaevans/tls-demo
        static void Main(string[] args)
        {
            inputValidator = new InputValidator(args);

            ConfigureServicePointManager();

            ServiceClientTracing.AddTracingInterceptor(new ConsoleTracingInterceptor());
            ServiceClientTracing.IsEnabled = true;


            keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(
                                                    (authority, resource, scope) => GetAccessToken(authority, resource, scope)));

            KeyBundle keyBundle = null;
            string    keyName   = default(string);

            try
            {
                keyBundle = CreateKey(keyBundle, out keyName);
                WrapUnwrap(keyBundle);
            }
            catch (KeyVaultClientException keyVaultoops)
            {
                //Something's up with KeyVault, check permissions,
                //TODO: use transient fault handling to retry
                WriteException(keyVaultoops, 0);
            }
            catch (HttpRequestException networkoops)
            {
                //Check network availability, TLS settings
                WriteException(networkoops, 0);
            }
        }
예제 #4
0
        public AzureFixture(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger <AzureFixture>();

            ServiceClientTracing.IsEnabled = true;
            ServiceClientTracing.AddTracingInterceptor(new LoggingInterceptor(loggerFactory.CreateLogger(nameof(ServiceClientTracing))));

            var clientId     = GetRequiredEnvironmentVariable("AZURE_AUTH_CLIENT_ID");
            var clientSecret = GetRequiredEnvironmentVariable("AZURE_AUTH_CLIENT_SECRET");
            var tenant       = GetRequiredEnvironmentVariable("AZURE_AUTH_TENANT");

            var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
                clientId, clientSecret, tenant, AzureEnvironment.AzureGlobalCloud);

            Azure = Microsoft.Azure.Management.Fluent.Azure.Configure()
                    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                    .Authenticate(credentials)
                    .WithDefaultSubscription();

            var testRunName = GetTimestampedName("FunctionalTests");

            _logger.LogInformation("Creating resource group {TestRunName}", testRunName);
            ResourceGroup = Azure.ResourceGroups
                            .Define(testRunName)
                            .WithRegion(Region.USWest2)
                            .Create();
        }
예제 #5
0
        /// <summary>
        /// Create a tracer for the given invocation ID.
        /// </summary>
        /// <param name="invocationId">Invocation ID to create IServiceTracer for</param>
        /// <param name="logger">Logging source to send trace messages to</param>
        /// <returns>Service tracer</returns>
        public virtual IServiceTracer CreateTracer(long invocationId, Logger logger)
        {
            ClientRuntimeServiceTracer tracer = new ClientRuntimeServiceTracer(invocationId, logger);

            ServiceClientTracing.AddTracingInterceptor(tracer);
            return(tracer);
        }
 protected override void SetupDebuggingTraces()
 {
     base.SetupDebuggingTraces();
     _serviceClientTracingInterceptor = _serviceClientTracingInterceptor
                                        ?? new ServiceClientTracingInterceptor(DebugMessages);
     ServiceClientTracing.AddTracingInterceptor(_serviceClientTracingInterceptor);
 }
예제 #7
0
        private IAzure CreateAzureClient(AzureEnvironment azureCloud, string tenantId, string subscriptionId, string applicationId, string applicationSecret, IOptions <AzureMonitorLoggingConfiguration> azureMonitorLoggingConfiguration, ILoggerFactory loggerFactory, MetricSinkWriter metricSinkWriter, IRuntimeMetricsCollector metricsCollector)
        {
            var credentials = _azureCredentialsFactory.FromServicePrincipal(applicationId, applicationSecret, tenantId, azureCloud);

            var throttlingLogger = loggerFactory.CreateLogger <AzureResourceManagerThrottlingRequestHandler>();
            var monitorHandler   = new AzureResourceManagerThrottlingRequestHandler(tenantId, subscriptionId, applicationId, metricSinkWriter, metricsCollector, throttlingLogger);

            var azureClientConfiguration = Azure.Configure()
                                           .WithDelegatingHandler(monitorHandler);
            var azureMonitorLogging = azureMonitorLoggingConfiguration.Value;

            if (azureMonitorLogging.IsEnabled)
            {
                var integrationLogger = loggerFactory.CreateLogger <AzureMonitorIntegrationLogger>();
                ServiceClientTracing.AddTracingInterceptor(new AzureMonitorIntegrationLogger(integrationLogger));
                ServiceClientTracing.IsEnabled = true;

                azureClientConfiguration = azureClientConfiguration.WithDelegatingHandler(new HttpLoggingDelegatingHandler())
                                           .WithLogLevel(azureMonitorLogging.InformationLevel);
            }

            return(azureClientConfiguration
                   .Authenticate(credentials)
                   .WithSubscription(subscriptionId));
        }
예제 #8
0
        private IAzure CreateAzureClient(AzureEnvironment azureCloud, string tenantId, string subscriptionId, AzureAuthenticationInfo azureAuthenticationInfo, ILoggerFactory loggerFactory, MetricSinkWriter metricSinkWriter, IAzureScrapingPrometheusMetricsCollector azureScrapingPrometheusMetricsCollector, IOptions <AzureMonitorLoggingConfiguration> azureMonitorLoggingConfiguration)
        {
            var credentials      = AzureAuthenticationFactory.CreateAzureAuthentication(azureCloud, tenantId, azureAuthenticationInfo, _azureCredentialsFactory);
            var throttlingLogger = loggerFactory.CreateLogger <AzureResourceManagerThrottlingRequestHandler>();
            var monitorHandler   = new AzureResourceManagerThrottlingRequestHandler(tenantId, subscriptionId, azureAuthenticationInfo, metricSinkWriter, azureScrapingPrometheusMetricsCollector, throttlingLogger);

            var azureClientConfiguration = Microsoft.Azure.Management.Fluent.Azure.Configure()
                                           .WithDelegatingHandler(monitorHandler);

            var azureMonitorLogging = azureMonitorLoggingConfiguration.Value;

            if (azureMonitorLogging.IsEnabled)
            {
                var integrationLogger = loggerFactory.CreateLogger <AzureMonitorIntegrationLogger>();
                ServiceClientTracing.AddTracingInterceptor(new AzureMonitorIntegrationLogger(integrationLogger));
                ServiceClientTracing.IsEnabled = true;

                azureClientConfiguration = azureClientConfiguration.WithDelegatingHandler(new HttpLoggingDelegatingHandler())
                                           .WithLogLevel(azureMonitorLogging.InformationLevel);
            }

            return(azureClientConfiguration
                   .Authenticate(credentials)
                   .WithSubscription(subscriptionId));
        }
 protected override void SetupDebuggingTraces()
 {
     ServiceClientTracing.IsEnabled = true;
     base.SetupDebuggingTraces();
     _serviceClientTracingInterceptor = _serviceClientTracingInterceptor
                                        ?? new ServiceClientTracingInterceptor(DebugMessages, _matchers, _clientRequestId);
     ServiceClientTracing.AddTracingInterceptor(_serviceClientTracingInterceptor);
 }
 public Startup(IConfiguration configuration, ILoggerFactory loggerFactory, ILogger <Startup> logger)
 {
     Configuration = configuration;
     LoggerFactory = loggerFactory;
     _logger       = logger;
     ServiceClientTracing.IsEnabled = true;
     ServiceClientTracing.AddTracingInterceptor(new LoggerTracingInterceptor(logger));
 }
예제 #11
0
        /// <inheritdoc />
        public ApiClientProvider(ApiConfiguration configuration, ILogger logger)
        {
            Configuration = configuration;
            Logger        = logger.ForContext <ApiClientProvider>();

            ServiceClientTracing.IsEnabled = true;
            ServiceClientTracing.AddTracingInterceptor(
                new LoggerServiceClientTracingInterceptor(Logger, LogEventLevel.Verbose));
        }
예제 #12
0
        public void OnComponentInitialized()
        {
            // You can override the service uri by setting a ServiceUri string resource..
            var uriOverride = ComponentManager.KeyValueParameters.GetValue("ServiceUri");

            this.ServiceClient = string.IsNullOrEmpty(uriOverride) ? new SwaggerPetstore() : new SwaggerPetstore(new Uri(uriOverride));
            ServiceClientTracing.AddTracingInterceptor(new SwaggerPetstoreServiceClientTracingInterceptor());
            ServiceClientTracing.IsEnabled = true;
            LogInfo(string.Format("Initialized service client. URL: {0}", this.ServiceClient.BaseUri));
        }
        public static IFunctionsHostBuilder UseTracingOptions(this IFunctionsHostBuilder builder, TracingOptions options)
        {
            if (options.TraceEnabled)
            {
                ServiceClientTracing.AddTracingInterceptor(new AutoRestTracingInterceptor(options));
                ServiceClientTracing.IsEnabled = true;
            }

            return(builder);
        }
예제 #14
0
 public ExampleCapture(string secretsFile, string outputFolder, string outputFolderWorkarounds = null)
 {
     this.secrets                 = ExampleHelpers.ReadSecretsFile(secretsFile);
     this.outputFolder            = outputFolder;
     this.outputFolderWorkarounds = outputFolderWorkarounds;
     this.client      = ExampleHelpers.GetRealClient(secrets);
     this.rmClient    = ExampleHelpers.GetRealRmClient(secrets);
     this.authClient  = ExampleHelpers.GetAuthorizationClient(secrets);
     this.interceptor = new ExampleTracingInterceptor(client.SubscriptionId, client.ApiVersion);
     ServiceClientTracing.AddTracingInterceptor(interceptor);
 }
예제 #15
0
        static void Main(string[] args)
        {
            if (Environment.GetEnvironmentVariable(AZURE_AUTH_LOCATION) == null)
            {
                throw new ArgumentException("Please set the environment variable 'AZURE_AUTH_LOCATION'");
            }

            ServiceClientTracing.AddTracingInterceptor(new TracingInterceptor());
            ServiceClientTracing.IsEnabled = true;

            CancellationTokenSource tokenSource = new CancellationTokenSource();

            (new Program()).RunAsync(tokenSource.Token).Wait();
        }
예제 #16
0
#pragma warning disable 162

        public async Task AssertTracingFor(
            Func <Task> doTest,
            string tracedMethodName,
            bool isSuccesful = true,
            Func <HttpRequestMessage, bool> assertHttpRequestMessage = null)
        {
            tracedMethodName = tracedMethodName.EndsWith("Async") ? tracedMethodName.Remove(tracedMethodName.LastIndexOf("Async")) : tracedMethodName;

            var traceInterceptor = new Mock <IServiceClientTracingInterceptor>();
            var invocationIds    = new List <string>();

            traceInterceptor.Setup(
                i => i.EnterMethod(It.IsAny <string>(), It.IsAny <object>(), tracedMethodName, It.IsAny <IDictionary <string, object> >()))
            .Callback((string id, object instance, string method, IDictionary <string, object> parameters) => invocationIds.Add(id));

            ServiceClientTracing.AddTracingInterceptor(traceInterceptor.Object);
            var wasTracingEnabled = ServiceClientTracing.IsEnabled;

            ServiceClientTracing.IsEnabled = true;

            await doTest();

            ServiceClientTracing.IsEnabled = wasTracingEnabled;

            var invocationId = invocationIds.Last();

            traceInterceptor.Verify(
                i => i.EnterMethod(invocationId, It.IsAny <object>(), tracedMethodName, It.IsAny <IDictionary <string, object> >()), Times.Once());
            traceInterceptor.Verify(
                i => i.SendRequest(invocationId, It.IsAny <HttpRequestMessage>()), Times.Once());
            traceInterceptor.Verify(
                i => i.ReceiveResponse(invocationId, It.IsAny <HttpResponseMessage>()), Times.Once());

            if (isSuccesful)
            {
                traceInterceptor.Verify(
                    i => i.ExitMethod(invocationId, It.IsAny <HttpOperationResponse>()), Times.Once());
            }
            else
            {
                traceInterceptor.Verify(
                    i => i.TraceError(invocationId, It.IsAny <Exception>()), Times.Once());
            }

            if (assertHttpRequestMessage != null)
            {
                traceInterceptor.Verify(
                    i => i.SendRequest(invocationId, It.Is <HttpRequestMessage>(h => assertHttpRequestMessage(h))), "HttpRequestMessage does not validate condition.");
            }
        }
예제 #17
0
        static void Main(string[] args)
        {
            string resourceGroupPrefix = Program.GetConfigItem("resourceGroupPrefix", "throttlingtest");
            string resourceGroupName   = resourceGroupPrefix + new Random().Next(MIN_RG_NUMBER, MAX_RG_NUMBER);
            string subscriptionId      = Program.GetConfigItem("subscriptionId");
            string tenantId            = Program.GetConfigItem("tenantId");
            string clientIds           = Program.GetConfigItem("clientIds");
            string clientSecrets       = Program.GetConfigItem("clientSecrets");

            string[] clientIdArray     = clientIds.Split(",");
            string[] clientSecretArray = clientSecrets.Split(",");

            if (clientIdArray.Length != clientSecretArray.Length)
            {
                return;
            }
            if (clientIdArray.Length == 0)
            {
                return;
            }

            AzureCredentialsFactory factory = new AzureCredentialsFactory();

            IAzure[] azureConnections = new IAzure[clientIdArray.Length];
            ServiceClientTracing.AddTracingInterceptor(new ConsoleTracer());
            ServiceClientTracing.IsEnabled = true;

            for (int i = 0; i < clientIdArray.Length; i++)
            {
                string clientId     = clientIdArray[i];
                string clientSecret = clientSecretArray[i];

                AzureCredentials azureCreds = factory.FromServicePrincipal(clientId, clientSecret, tenantId,
                                                                           AzureEnvironment.AzureGlobalCloud);
                IAzure azure = Azure.Configure().
                               WithDelegatingHandler(new HttpLoggingDelegatingHandler()).
                               WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders).
                               WithRetryPolicy(new RetryPolicy(new TransientErrorIgnoreStrategy(), 0)).
                               Authenticate(azureCreds).
                               WithSubscription(subscriptionId);
                azureConnections[i] = azure;
            }

            //new NICTest().Run(azureConnections, resourceGroupName).Wait();
            new StorageTest().Run(azureConnections, resourceGroupName).Wait();

            Log("Done");
            Console.ReadLine();
        }
        public void CanSetMultipleDelegateHandlers()
        {
            using (var context = FluentMockContext.Start(GetType().FullName))
            {
                string rgName  = TestUtilities.GenerateName("rg");
                string stgName = TestUtilities.GenerateName("stg");

                IAzure azure = null;
                // Sets the interceptor so that logging and user agent in log can be asserted.
                var logAndUserAgentInterceptor = new LogAndUserAgentInterceptor();
                ServiceClientTracing.AddTracingInterceptor(logAndUserAgentInterceptor);
                ServiceClientTracing.IsEnabled = true;
                try
                {
                    AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
                    credentials.WithDefaultSubscription(null); // Clearing subscriptionId loaded from the auth
                                                               // file so that below WithDefaultSubscription()
                                                               // will force listing subscriptions and fetching
                                                               // the first.
                    azure = Microsoft.Azure.Management.Fluent.Azure
                            .Configure()
                            .WithUserAgent("azure-fluent-test", "1.0.0-prelease")
                            .WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders)
                            .WithDelegatingHandlers(TestHelper.GetHandlers())
                            .Authenticate(credentials)
                            .WithDefaultSubscription();

                    IStorageAccount storageAccount = azure.StorageAccounts
                                                     .Define(stgName)
                                                     .WithRegion(Region.USEast)
                                                     .WithNewResourceGroup(rgName)
                                                     .Create();

                    Assert.Equal(storageAccount.ResourceGroupName, rgName);
                }
                finally
                {
                    ServiceClientTracing.RemoveTracingInterceptor(logAndUserAgentInterceptor);
                    ServiceClientTracing.IsEnabled = false;
                    if (azure != null)
                    {
                        azure.ResourceGroups.DeleteByName(rgName);
                    }
                }
                Assert.True(logAndUserAgentInterceptor.FoundUserAgentInLog);
            }
        }
        public DataLakeStoreTraceLogger(Cmdlet commandToLog, string logFilePath = null, LogLevel logLevel = LogLevel.Information)
        {
            LogFilePath = logFilePath;
            LogLevel    = logLevel;
            if (Directory.Exists(LogFilePath)) // the user passed in a directory instead of a file
            {
                commandToLog.WriteWarning(string.Format(Resources.DiagnosticDirectoryAlreadyExists, LogFilePath));
                return;
            }

            try
            {
                // always create the directory, since it is a no-op if the path exists
                // we also do not do heavy validation here, since any exception will be caught and reported back as a warning.
                Directory.CreateDirectory(Path.GetDirectoryName(LogFilePath));
                TraceStream = new FileStream(LogFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
            }
            catch (Exception ex)
            {
                commandToLog.WriteWarning(string.Format(Resources.TraceStreamFailure, LogFilePath, ex.Message));
                return;
            }

            TextListener = new TextWriterTraceListener(TraceStream);
            Trace.CorrelationManager.ActivityId = Guid.NewGuid();
            PreviousAutoFlush = Trace.AutoFlush;

            Trace.Listeners.Add(TextListener);
            Trace.AutoFlush       = true;
            SdkTracingInterceptor = new DataLakeStoreTracingInterceptor(this);

            PreviousAdalSourceLevel = AdalTrace.TraceSource.Switch.Level;
            PreviousAdalTraceLevel  = AdalTrace.LegacyTraceSwitch.Level;

            // Ignore ADAL trace logs if debug logging isn't selected.
            if (LogLevel != LogLevel.Debug)
            {
                AdalTrace.TraceSource.Switch.Level = SourceLevels.Warning;
                AdalTrace.LegacyTraceSwitch.Level  = TraceLevel.Warning;
            }

            if (SdkTracingInterceptor != null)
            {
                ServiceClientTracing.AddTracingInterceptor(SdkTracingInterceptor);
            }
        }
예제 #20
0
        /// <summary>
        /// Operations that happen before the cmdlet is invoked.
        /// </summary>
        protected override void BeginProcessing()
        {
            string commandAlias = GetType().Name;

            if (cancellationSource == null)
            {
                cancellationSource = new CancellationTokenSource();
            }

            if (correlationId == default)
            {
                correlationId = Guid.NewGuid();
            }

            httpTracingInterceptor ??= new RecordingTracingInterceptor(PartnerSession.Instance.DebugMessages);

            ServiceClientTracing.IsEnabled = true;
            ServiceClientTracing.AddTracingInterceptor(httpTracingInterceptor);

            if (MyInvocation != null && MyInvocation.MyCommand != null)
            {
                commandAlias = MyInvocation.MyCommand.Name;
            }

            qosEvent = new PartnerQosEvent
            {
                CommandName      = commandAlias,
                IsSuccess        = true,
                ModuleVersion    = GetType().Assembly.GetName().Version.ToString(),
                ParameterSetName = ParameterSetName,
                SessionId        = sessionId.ToString(),
            };

            if (!string.IsNullOrEmpty(PartnerSession.Instance?.Context?.Account?.Identifier))
            {
                qosEvent.UserId = GenerateSha256HashString(PartnerSession.Instance.Context.Account.Identifier)?.Replace("-", string.Empty)?.ToLowerInvariant();
            }

            if (MyInvocation != null && MyInvocation.BoundParameters != null && MyInvocation.BoundParameters.Keys != null)
            {
                qosEvent.Parameters = string.Join(" ", MyInvocation.BoundParameters.Keys.Select(
                                                      s => string.Format(CultureInfo.InvariantCulture, "-{0} ***", s)));
            }

            ProcessBreakingChangeAttributesAtRuntime(GetType(), MyInvocation, WriteWarning);
        }
예제 #21
0
        static void Main(string[] args)
        {
            ServiceClientTracing.IsEnabled = true;
            ServiceClientTracing.AddTracingInterceptor(new DebugTracer(ExtConsole));

            if (ConfigRepo.NotConfigure())
            {
                ExtConsole.WriteLine("You must complete the config.json file.");
                ExtConsole.WriteLine("Create an account on https://developer.clashroyale.com and genrate a key(token)");
                ExtConsole.ReadKey();
                return;
            }

            client = new ClientCR(ConfigRepo.Config, ExtConsole);

            if (client.Player != null)
            {
                ExtConsole.WriteLine($"Player : {client.Player}");
            }
            if (client.Clan != null)
            {
                ExtConsole.WriteLine($"Clan   : {client.Clan}");
            }

            ExtConsole
            .NewChoice(ConsoleKey.D1, "Rank of your clan", ExtConsole
                       .NewChoice(ConsoleKey.D1, "Local", RankLocalClan)
                       .NewChoice(ConsoleKey.D2, "General", RankGeneralClan))
            .NewChoice(ConsoleKey.D2, "Various functions", ExtConsole
                       .NewChoice(ConsoleKey.D1, "Upcomming chests", UpcommingChests)
                       .NewChoice(ConsoleKey.D2, "Open tournaments", OpenTournaments)
                       .NewChoice(ConsoleKey.D3, "Cards list", ListCards))
            .NewChoice(ConsoleKey.D3, "My cards", ExtConsole
                       .NewChoice(ConsoleKey.D1, "Completed cards", CompletedCards)
                       .NewChoice(ConsoleKey.D2, "Missing cards", MissingCards))
            .NewChoice(ConsoleKey.D4, "Stats players", ExtConsole
                       .NewChoice(ConsoleKey.D1, "Usage cards in local top 200", UsageCardsTop)
                       .NewChoice(ConsoleKey.D2, "Winrate card local top 200", WinrateCardTop)
                       .NewChoice(ConsoleKey.D3, "Winrate card by trophes (is long)", WinrateCardByClanTrophe, "Minimum clan trophy : "))
            .NewChoice(ConsoleKey.D9, "Change player and clan", ExtConsole
                       .NewChoice(ConsoleKey.D1, "Change player and clan", SettingPlayerClan)
                       .NewChoice(ConsoleKey.D2, "Change just clan", SettingClan))
            .WaitKey(ConsoleKey.X, client);
        }
예제 #22
0
        public override async Task Run()
        {
            ServicePointManager.DefaultConnectionLimit  = 64;
            ServicePointManager.MaxServicePointIdleTime = 10000;

            var tracingInterceptor = _serviceProvider.GetRequiredService <IServiceClientTracingInterceptor>();

            try
            {
                ServiceClientTracing.IsEnabled = true;
                ServiceClientTracing.AddTracingInterceptor(tracingInterceptor);

                await _serviceProvider.GetRequiredService <T>().ExecuteAsync();
            }
            finally
            {
                ServiceClientTracing.RemoveTracingInterceptor(tracingInterceptor);
            }
        }
예제 #23
0
        /// <summary>
        /// Operations that happen before the cmdlet is invoked.
        /// </summary>
        protected override void BeginProcessing()
        {
            if (cancellationSource == null)
            {
                cancellationSource = new CancellationTokenSource();
            }

            if (correlationId == default)
            {
                correlationId = Guid.NewGuid();
            }

            httpTracingInterceptor ??= new RecordingTracingInterceptor(PartnerSession.Instance.DebugMessages);

            ServiceClientTracing.IsEnabled = true;
            ServiceClientTracing.AddTracingInterceptor(httpTracingInterceptor);

            ProcessBreakingChangeAttributesAtRuntime(GetType(), MyInvocation, WriteWarning);
        }
예제 #24
0
        private static void Main(string[] args)
        {
            try
            {
                var parser    = new CommandLineParser.CommandLineParser();
                var arguments = new Arguments();
                parser.ExtractArgumentAttributes(arguments);
                parser.ParseCommandLine(args);
                if (!parser.ParsingSucceeded)
                {
                    parser.ShowUsage();
                }
                else
                {
                    using (var cts = new CancellationTokenSource())
                    {
                        Console.CancelKeyPress += (_, a) =>
                        {
                            a.Cancel = true;
                            cts.Cancel();
                        };

                        ServiceClientTracing.IsEnabled = true;
                        ServiceClientTracing.AddTracingInterceptor(new ConsoleTracingInterceptor());

                        RunAsync(arguments, cts.Token).GetAwaiter().GetResult();
                    }
                }
            }
            catch (Exception ex)
            {
#pragma warning disable ERP023 // Only ex.Message property was observed in exception block!
                Console.WriteLine($"error: {ex.Message}");
#pragma warning restore ERP023 // Only ex.Message property was observed in exception block!
            }

            if (Debugger.IsAttached)
            {
                Debugger.Break();
            }
        }
예제 #25
0
        protected override void Load(ContainerBuilder builder)
        {
            // IKubernetesClient
            builder.Register(
                c =>
            {
                if (this.enableServiceCallTracing)
                {
                    // enable tracing of k8s requests made by the client
                    var loggerFactory = c.Resolve <ILoggerFactory>();
                    ILogger logger    = loggerFactory.CreateLogger(typeof(Kubernetes));
                    ServiceClientTracing.IsEnabled = true;
                    ServiceClientTracing.AddTracingInterceptor(new DebugTracer(logger));
                }

                // load the k8s config from KUBECONFIG or $HOME/.kube/config or in-cluster if its available
                KubernetesClientConfiguration kubeConfig = Option.Maybe(Environment.GetEnvironmentVariable("KUBECONFIG"))
                                                           .Else(() => Option.Maybe(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".kube", "config")))
                                                           .Filter(File.Exists)
                                                           .Map(path => KubernetesClientConfiguration.BuildConfigFromConfigFile(path))
                                                           .GetOrElse(KubernetesClientConfiguration.InClusterConfig);

                return(new Kubernetes(kubeConfig));
            })
            .As <IKubernetes>()
            .SingleInstance();

            // IModuleClientProvider
            builder.Register(
                c => new ModuleClientProvider(
                    c.Resolve <ISdkModuleClientProvider>(),
                    this.upstreamProtocol,
                    this.proxy,
                    this.productInfo.OrDefault(),
                    this.closeOnIdleTimeout,
                    this.idleTimeout))
            .As <IModuleClientProvider>()
            .SingleInstance();

            // IModuleManager
            builder.Register(c => new ModuleManagementHttpClient(this.managementUri, this.apiVersion, Core.Constants.EdgeletClientApiVersion))
            .As <IModuleManager>()
            .As <IIdentityManager>()
            .SingleInstance();

            // IModuleIdentityLifecycleManager
            var identityBuilder = new ModuleIdentityProviderServiceBuilder(this.resourceName.Hostname, this.resourceName.DeviceId, this.edgeDeviceHostName);

            builder.Register(c => new KubernetesModuleIdentityLifecycleManager(c.Resolve <IIdentityManager>(), identityBuilder, this.workloadUri))
            .As <IModuleIdentityLifecycleManager>()
            .SingleInstance();

            // CombinedKubernetesConfigProvider
            builder.Register(
                c =>
            {
                bool enableKubernetesExtensions = this.experimentalFeatures.Enabled && this.experimentalFeatures.EnableExtensions;
                return(new CombinedKubernetesConfigProvider(this.dockerAuthConfig, this.workloadUri, this.managementUri, enableKubernetesExtensions));
            })
            .As <ICombinedConfigProvider <CombinedKubernetesConfig> >()
            .SingleInstance();

            // ICommandFactory
            builder.Register(
                c =>
            {
                var loggerFactory            = c.Resolve <ILoggerFactory>();
                var kubernetesCommandFactory = new KubernetesCommandFactory();
                ICommandFactory factory      = new LoggingCommandFactory(kubernetesCommandFactory, loggerFactory);
                return(Task.FromResult(factory));
            })
            .As <Task <ICommandFactory> >()
            .SingleInstance();

            // IPlanner
            builder.Register(
                async c =>
            {
                var configProvider             = c.Resolve <ICombinedConfigProvider <CombinedKubernetesConfig> >();
                ICommandFactory commandFactory = await c.Resolve <Task <ICommandFactory> >();
                IPlanner planner = new KubernetesPlanner(this.deviceNamespace, this.resourceName, c.Resolve <IKubernetes>(), commandFactory, configProvider);
                return(planner);
            })
            .As <Task <IPlanner> >()
            .SingleInstance();

            // KubernetesRuntimeInfoProvider
            builder.Register(c => new KubernetesRuntimeInfoProvider(this.deviceNamespace, c.Resolve <IKubernetes>(), c.Resolve <IModuleManager>()))
            .As <IRuntimeInfoProvider>()
            .As <IRuntimeInfoSource>()
            .SingleInstance();

            // KubernetesDeploymentProvider
            builder.Register(
                c => new KubernetesDeploymentMapper(
                    this.deviceNamespace,
                    this.edgeDeviceHostName,
                    this.proxyImage,
                    this.proxyConfigPath,
                    this.proxyConfigVolumeName,
                    this.proxyConfigMapName,
                    this.proxyTrustBundlePath,
                    this.proxyTrustBundleVolumeName,
                    this.proxyTrustBundleConfigMapName,
                    this.persistentVolumeName,
                    this.storageClassName,
                    this.apiVersion,
                    this.workloadUri,
                    this.managementUri))
            .As <IKubernetesDeploymentMapper>();

            // KubernetesServiceMapper
            builder.Register(c => new KubernetesServiceMapper(this.defaultMapServiceType))
            .As <IKubernetesServiceMapper>();

            // KubernetesPvcMapper
            builder.Register(c => new KubernetesPvcMapper(this.persistentVolumeName, this.storageClassName, this.persistentVolumeClaimSizeMb))
            .As <IKubernetesPvcMapper>();

            // KubernetesServiceAccountProvider
            builder.Register(c => new KubernetesServiceAccountMapper())
            .As <IKubernetesServiceAccountMapper>();

            // EdgeDeploymentController
            builder.Register(
                c =>
            {
                var deploymentSelector = $"{Constants.K8sEdgeDeviceLabel}={KubeUtils.SanitizeK8sValue(this.resourceName.DeviceId)},{Constants.K8sEdgeHubNameLabel}={KubeUtils.SanitizeK8sValue(this.resourceName.Hostname)}";
                IEdgeDeploymentController watchOperator = new EdgeDeploymentController(
                    this.resourceName,
                    deploymentSelector,
                    this.deviceNamespace,
                    c.Resolve <IKubernetes>(),
                    c.Resolve <IModuleIdentityLifecycleManager>(),
                    c.Resolve <IKubernetesServiceMapper>(),
                    c.Resolve <IKubernetesDeploymentMapper>(),
                    c.Resolve <IKubernetesPvcMapper>(),
                    c.Resolve <IKubernetesServiceAccountMapper>());

                return(watchOperator);
            })
            .As <IEdgeDeploymentController>()
            .SingleInstance();

            // IEdgeDeploymentOperator
            builder.Register(
                c =>
            {
                IEdgeDeploymentOperator watchOperator = new EdgeDeploymentOperator(
                    this.resourceName,
                    this.deviceNamespace,
                    c.Resolve <IKubernetes>(),
                    c.Resolve <IEdgeDeploymentController>());

                return(watchOperator);
            })
            .As <IEdgeDeploymentOperator>()
            .SingleInstance();

            // IKubernetesEnvironmentOperator
            builder.Register(
                c =>
            {
                IKubernetesEnvironmentOperator watchOperator = new KubernetesEnvironmentOperator(
                    this.deviceNamespace,
                    c.Resolve <IRuntimeInfoSource>(),
                    c.Resolve <IKubernetes>());

                return(watchOperator);
            })
            .As <IKubernetesEnvironmentOperator>()
            .SingleInstance();

            // Task<IEnvironmentProvider>
            builder.Register(
                async c =>
            {
                var moduleStateStore     = c.Resolve <IEntityStore <string, ModuleState> >();
                var restartPolicyManager = c.Resolve <IRestartPolicyManager>();
                IRuntimeInfoProvider runtimeInfoProvider       = c.Resolve <IRuntimeInfoProvider>();
                IEnvironmentProvider dockerEnvironmentProvider = await DockerEnvironmentProvider.CreateAsync(runtimeInfoProvider, moduleStateStore, restartPolicyManager);
                return(dockerEnvironmentProvider);
            })
            .As <Task <IEnvironmentProvider> >()
            .SingleInstance();
        }
 static UiPathCmdlet()
 {
     VerboseTracer = new RestVerboseTracer();
     ServiceClientTracing.AddTracingInterceptor(VerboseTracer);
     ServiceClientTracing.IsEnabled = true;
 }
예제 #27
0
        static void Main(string[] args)
        {
            KeyBundle            keyBundle            = null; // The key specification and attributes
            SecretBundle         secret               = null;
            CertificateBundle    certificateBundle    = null;
            CertificateOperation certificateOperation = null;
            string keyName               = string.Empty;
            string secretName            = string.Empty;
            string certificateName       = string.Empty;
            string certificateCreateName = string.Empty;

            inputValidator = new InputValidator(args);

            ServiceClientTracing.AddTracingInterceptor(new ConsoleTracingInterceptor());
            ServiceClientTracing.IsEnabled = inputValidator.GetTracingEnabled();

            var clientId             = ConfigurationManager.AppSettings["AuthClientId"];
            var cerificateThumbprint = ConfigurationManager.AppSettings["AuthCertThumbprint"];

            var certificate   = FindCertificateByThumbprint(cerificateThumbprint);
            var assertionCert = new ClientAssertionCertificate(clientId, certificate);

            keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(
                                                    (authority, resource, scope) => GetAccessToken(authority, resource, scope, assertionCert)),
                                                new InjectHostHeaderHttpMessageHandler());

            // SECURITY: DO NOT USE IN PRODUCTION CODE; FOR TEST PURPOSES ONLY
            //ServicePointManager.ServerCertificateValidationCallback += ( sender, cert, chain, sslPolicyErrors ) => true;

            List <KeyOperationType> successfulOperations = new List <KeyOperationType>();
            List <KeyOperationType> failedOperations     = new List <KeyOperationType>();

            foreach (var operation in inputValidator.GetKeyOperations())
            {
                try
                {
                    Console.Out.WriteLine("\n\n {0} is in process ...", operation);
                    switch (operation)
                    {
                    case KeyOperationType.CREATE_KEY:
                        keyBundle = CreateKey(keyBundle, out keyName);
                        break;

                    case KeyOperationType.IMPORT_KEY:
                        keyBundle = ImportKey(out keyName);
                        break;

                    case KeyOperationType.GET_KEY:
                        keyBundle = GetKey(keyBundle);
                        break;

                    case KeyOperationType.LIST_KEYVERSIONS:
                        ListKeyVersions(keyName);
                        break;

                    case KeyOperationType.UPDATE_KEY:
                        keyBundle = UpdateKey(keyName);
                        break;

                    case KeyOperationType.DELETE_KEY:
                        DeleteKey(keyName);
                        break;

                    case KeyOperationType.BACKUP_RESTORE:
                        keyBundle = BackupRestoreKey(keyName);
                        break;

                    case KeyOperationType.SIGN_VERIFY:
                        SignVerify(keyBundle);
                        break;

                    case KeyOperationType.ENCRYPT_DECRYPT:
                        EncryptDecrypt(keyBundle);
                        break;

                    case KeyOperationType.ENCRYPT:
                        Encrypt(keyBundle);
                        break;

                    case KeyOperationType.DECRYPT:
                        Decrypt(keyBundle);
                        break;

                    case KeyOperationType.WRAP_UNWRAP:
                        WrapUnwrap(keyBundle);
                        break;

                    case KeyOperationType.CREATE_SECRET:
                        secret = CreateSecret(out secretName);
                        break;

                    case KeyOperationType.GET_SECRET:
                        secret = GetSecret(secret.Id);
                        break;

                    case KeyOperationType.LIST_SECRETS:
                        ListSecrets();
                        break;

                    case KeyOperationType.DELETE_SECRET:
                        secret = DeleteSecret(secretName);
                        break;

                    case KeyOperationType.CREATE_CERTIFICATE:
                        certificateOperation = CreateCertificate(out certificateCreateName);
                        break;

                    case KeyOperationType.IMPORT_CERTIFICATE:
                        certificateBundle = ImportCertificate(out certificateName);
                        break;

                    case KeyOperationType.EXPORT_CERTIFICATE:
                        var x509Certificate = ExportCertificate(certificateBundle);
                        break;

                    case KeyOperationType.LIST_CERTIFICATEVERSIONS:
                        ListCertificateVersions(certificateName);
                        break;

                    case KeyOperationType.LIST_CERTIFICATES:
                        ListCertificates();
                        break;

                    case KeyOperationType.DELETE_CERTIFICATE:
                        certificateBundle = DeleteCertificate(certificateName);
                        certificateBundle = DeleteCertificate(certificateCreateName);
                        break;
                    }
                    successfulOperations.Add(operation);
                }
                catch (KeyVaultErrorException exception)
                {
                    // The Key Vault exceptions are logged but not thrown to avoid blocking execution for other commands running in batch
                    Console.Out.WriteLine("Operation failed: {0}", exception.Body.Error.Message);
                    failedOperations.Add(operation);
                }
            }

            Console.Out.WriteLine("\n\n---------------Successful Key Vault operations:---------------");
            foreach (KeyOperationType type in successfulOperations)
            {
                Console.Out.WriteLine("\t{0}", type);
            }

            if (failedOperations.Count > 0)
            {
                Console.Out.WriteLine("\n\n---------------Failed Key Vault operations:---------------");
                foreach (KeyOperationType type in failedOperations)
                {
                    Console.Out.WriteLine("\t{0}", type);
                }
            }

            Console.Out.WriteLine();
            Console.Out.Write("Press enter to continue . . .");
            Console.In.Read();
        }
예제 #28
0
        static async Task <int> Main(string[] args)
        {
            IOperator basicOperator = null;

            // Setup logging
            using var loggerFactory = SetupLogging(args);
            var logger = loggerFactory.CreateLogger <Program>();

            try
            {
                logger.LogDebug($"Environment variables: {JsonConvert.SerializeObject(Environment.GetEnvironmentVariables())}");

                // Setup termination handlers
                SetupSignalHandlers();

                // Setup the Kubernetes client
                using var client = SetupClient(args);

                // Setup the operator
                var configuration = GetOperatorConfiguration();
                basicOperator = new Operator(configuration, client, loggerFactory);
                basicOperator.AddControllerOfType <MyResourceController>();

                // Start the operator
                return(await basicOperator.StartAsync());
            }
            catch (Exception exception)
            {
                logger.LogError(exception, "Operator error");
                return(1);
            }

            IKubernetes SetupClient(string[] args)
            {
                // Load the Kubernetes configuration
                KubernetesClientConfiguration config = null;

                if (KubernetesClientConfiguration.IsInCluster())
                {
                    logger.LogDebug("Loading cluster configuration");
                    config = KubernetesClientConfiguration.InClusterConfig();
                }
                else
                {
                    logger.LogDebug("Loading local configuration");
                    config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
                }

                if (logger.IsEnabled(LogLevel.Debug))
                {
                    logger.LogDebug($"Client configuration: {JsonConvert.SerializeObject(config)}");
                }

                return(new Kubernetes(config));
            }

            ILoggerFactory SetupLogging(string[] args)
            {
                if (!System.Enum.TryParse <LogLevel>(Environment.GetEnvironmentVariable("LOG_LEVEL"), true, out LogLevel logLevel))
                {
                    logLevel = LogLevel.Debug;
                }

                var loggerFactory = LoggerFactory.Create(builder => builder
                                                         .AddConsole(options => options.Format = ConsoleLoggerFormat.Systemd)
                                                         .SetMinimumLevel(logLevel)
                                                         );

                // Enable Kubernetes client logging if level = DEBUG
                ServiceClientTracing.IsEnabled = logLevel <= LogLevel.Debug;
                ServiceClientTracing.AddTracingInterceptor(new ConsoleTracingInterceptor());

                return(loggerFactory);
            }

            void SetupSignalHandlers()
            {
                // SIGTERM: signal the operator to shut down gracefully
                AppDomain.CurrentDomain.ProcessExit += (s, e) =>
                {
                    logger.LogDebug("Received SIGTERM");
                    basicOperator?.Stop();
                };

                // SIGINT: try to shut down gracefully on the first attempt
                Console.CancelKeyPress += (s, e) =>
                {
                    bool isFirstSignal = !basicOperator.IsDisposing;
                    logger.LogDebug($"Received SIGINT, first signal: {isFirstSignal}");
                    if (isFirstSignal)
                    {
                        e.Cancel = true;
                        Environment.Exit(0);
                    }
                };
            }
        }
        public async Task <Azure.IAuthenticated> GetAzureConnection()
        {
            if (_azure != null)
            {
                return(_azure);
            }

            AzureCredentials credentials;
            string           localDevelopment = Environment.GetEnvironmentVariable("LocalDevelopment", EnvironmentVariableTarget.Process);

            if (!string.IsNullOrEmpty(localDevelopment) &&
                string.Equals(localDevelopment, "true", StringComparison.InvariantCultureIgnoreCase))
            {
                Log.LogDebug($"Get the local service principal for local login");
                var localDevSp = new Principal
                {
                    UserPrincipalName = "LocalLogin",
                    AppId             = Environment.GetEnvironmentVariable("ClientId", EnvironmentVariableTarget.Process),
                    TenantId          = Environment.GetEnvironmentVariable("TenantId", EnvironmentVariableTarget.Process)
                };
                string clientSecret = Environment.GetEnvironmentVariable("ClientSecret", EnvironmentVariableTarget.Process);
                Log.LogDebug($"AppId: {localDevSp.AppId}, TenantId: {localDevSp.TenantId}");

                credentials = SdkContext
                              .AzureCredentialsFactory
                              .FromServicePrincipal(localDevSp.AppId, clientSecret, localDevSp.TenantId, AzureEnvironment.AzureGlobalCloud);
            }
            else
            {
                Log.LogDebug($"Get the MSI credentials");

                // Because MSI isn't really nicely supported in this nuget package disable it for now and user workaround
                ////credentials = SdkContext
                ////     .AzureCredentialsFactory
                ////     .FromMSI(new MSILoginInformation(MSIResourceType.AppService), AzureEnvironment.AzureGlobalCloud);

                try
                {
                    // START workaround until MSI in this package is really supported
                    string tenantId = Environment.GetEnvironmentVariable("TenantId", EnvironmentVariableTarget.Process);
                    Log.LogDebug($"TenantId: {tenantId}");

                    AzureServiceTokenProvider astp = new AzureServiceTokenProvider();
                    string graphToken = await astp.GetAccessTokenAsync("https://graph.windows.net/", tenantId);

                    AzureServiceTokenProvider astp2 = new AzureServiceTokenProvider();
                    string rmToken = await astp2.GetAccessTokenAsync("https://management.azure.com/", tenantId);

                    Log.LogDebug("Logging with tokens from Token Provider");

                    AzureCredentials customTokenProvider = new AzureCredentials(
                        new TokenCredentials(rmToken),
                        new TokenCredentials(graphToken),
                        tenantId,
                        AzureEnvironment.AzureGlobalCloud);

                    RestClient client = RestClient
                                        .Configure()
                                        .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
                                        .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                                        //.WithRetryPolicy(new RetryPolicy(new HttpStatusCodeErrorDetectionStrategy(), new IncrementalRetryStrategy(2, TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(1))))
                                        .WithCredentials(customTokenProvider)
                                        .Build();

                    return(Azure.Authenticate(client, tenantId));
                    // END workaround until MSI in this package is really supported
                }
                catch (Exception ex)
                {
                    Log.LogError(ex, ex.Message);
                    if (ex.InnerException != null)
                    {
                        Log.LogError(ex.InnerException, ex.InnerException.Message);
                    }
                    throw;
                }
            }

            ServiceClientTracing.AddTracingInterceptor(new MicrosoftExtensionsLoggingTracer(Log));
            ServiceClientTracing.IsEnabled = true;

            _azure = Azure
                     .Configure()
                     .WithDelegatingHandler(new HttpLoggingDelegatingHandler())
                     .WithLogLevel(HttpLoggingDelegatingHandler.Level.None)
                     .Authenticate(credentials);

            return(_azure);
        }
예제 #30
0
 static AcceptanceTests()
 {
     _interceptor = new TestTracingInterceptor();
     ServiceClientTracing.AddTracingInterceptor(_interceptor);
 }