Ejemplo n.º 1
0
        private static void Execute(ToolOptions opts)
        {
            try
            {
                var config = new LoggerConfiguration();
                config.WriteTo.ColoredConsole(outputTemplate: "{Message:lj}{NewLine}{Exception}");

                if (opts.Verbose)
                {
                    config.MinimumLevel.Debug();
                }
                else
                {
                    config.MinimumLevel.Information();
                }

                Log.Logger = config.CreateLogger();

                ISafeguardConnection connection;
                if (!string.IsNullOrEmpty(opts.Username))
                {
                    var password = HandlePassword(opts.ReadPassword);
                    connection = Safeguard.Connect(opts.Appliance, opts.IdentityProvider, opts.Username, password,
                                                   opts.ApiVersion, opts.Insecure);
                }
                else if (!string.IsNullOrEmpty(opts.CertificateFile))
                {
                    var password = HandlePassword(opts.ReadPassword);
                    connection = Safeguard.Connect(opts.Appliance, opts.CertificateFile, password, opts.ApiVersion,
                                                   opts.Insecure);
                }
                else if (!string.IsNullOrEmpty(opts.Thumbprint))
                {
                    connection = Safeguard.Connect(opts.Appliance, opts.Thumbprint, opts.ApiVersion, opts.Insecure);
                }
                else if (opts.Anonymous)
                {
                    connection = Safeguard.Connect(opts.Appliance, opts.ApiVersion, opts.Insecure);
                }
                else
                {
                    throw new Exception("Must specify Anonymous, Username, CertificateFile, or Thumbprint");
                }

                Log.Debug($"Access Token Lifetime Remaining: {connection.GetAccessTokenLifetimeRemaining()}");

                var responseBody = opts.Csv
                    ? connection.InvokeMethodCsv(opts.Service, opts.Method, opts.RelativeUrl, opts.Body)
                    : connection.InvokeMethod(opts.Service, opts.Method, opts.RelativeUrl, opts.Body);
                //Log.Information(responseBody); // if JSON is nested too deep Serilog swallows a '}' -- need to file issue with them
                Console.WriteLine(responseBody);

                connection.LogOut();
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Fatal exception occurred");
                Environment.Exit(1);
            }
        }
Ejemplo n.º 2
0
        private static string HandleStreamingRequest(ToolOptions opts, ISafeguardConnection connection)
        {
            if (opts.Method == Method.Post)
            {
                using FileStream fs = File.OpenRead(opts.File);
                var progress = opts.Verbose ? new Progress <TransferProgress>(p =>
                {
                    Console.Write("\rUploading: {0,3}% ({1}/{2})                                  ", p.PercentComplete, p.BytesTransferred, p.BytesTotal);
                }) : null;
                return(connection.Streaming.UploadAsync(opts.Service, opts.RelativeUrl, fs, progress, cancellationToken: Cts.Token).Result);
            }
            else if (opts.Method == Method.Get)
            {
                if (File.Exists(opts.File))
                {
                    throw new Exception($"File exists, remove it first: {opts.File}");
                }
                var progress = opts.Verbose ? new Progress <TransferProgress>(p =>
                {
                    if (p.BytesTotal == 0)
                    {
                        Console.Write("\rDownloading: {0}", p.BytesTransferred);
                    }
                    else
                    {
                        Console.Write("\rDownloading: {0,3}% ({1}/{2})                                  ", p.PercentComplete, p.BytesTransferred, p.BytesTotal);
                    }
                }) : null;

                // This is the alternate way to download directly to a file:
                // connection.Streaming.DownloadAsync(opts.Service, opts.RelativeUrl, opts.File, progress: progress, cancellationToken: Cts.Token).Wait();

                using (var streamResult = connection.Streaming.DownloadStreamAsync(opts.Service, opts.RelativeUrl, progress: progress, cancellationToken: Cts.Token).Result)
                {
                    using (var fs = new FileStream(opts.File, FileMode.Create, FileAccess.ReadWrite))
                    {
                        var downloadStream = streamResult.GetStream().Result;
                        downloadStream.CopyToAsync(fs, 81920).Wait();
                    }
                }
                return($"Download written to {opts.File}");
            }
            else
            {
                throw new Exception($"Streaming is not supported for HTTP method: {opts.Method}");
            }
        }
Ejemplo n.º 3
0
 private static string HandleStreamingRequest(ToolOptions opts, ISafeguardConnection connection)
 {
     if (opts.Method == Method.Post)
     {
         using FileStream fs = File.OpenRead(opts.File);
         var progress = opts.Verbose ? new Progress <TransferProgress>(p =>
         {
             Console.Write("\rUploading: {0,3}% ({1}/{2})                                  ", p.PercentComplete, p.BytesTransferred, p.BytesTotal);
         }) : null;
         return(connection.Streaming.UploadAsync(opts.Service, opts.RelativeUrl, fs, progress, cancellationToken: Cts.Token).Result);
     }
     else if (opts.Method == Method.Get)
     {
         if (File.Exists(opts.File))
         {
             throw new Exception($"File exists, remove it first: {opts.File}");
         }
         var progress = opts.Verbose ? new Progress <TransferProgress>(p =>
         {
             if (p.BytesTotal == 0)
             {
                 Console.Write("\rDownloading: {0}", p.BytesTransferred);
             }
             else
             {
                 Console.Write("\rDownloading: {0,3}% ({1}/{2})                                  ", p.PercentComplete, p.BytesTransferred, p.BytesTotal);
             }
         }) : null;
         connection.Streaming.DownloadAsync(opts.Service, opts.RelativeUrl, opts.File, progress: progress, cancellationToken: Cts.Token).Wait(Cts.Token);
         return($"Download written to {opts.File}");
     }
     else
     {
         throw new Exception($"Streaming is not supported for HTTP method: {opts.Method}");
     }
 }