Exemplo n.º 1
0
        protected override void CreatePipes()
        {
            this.logger.LogDebug("Creating pipes..");

            if (!Directory.Exists(this.workingDir))
            {
                Directory.CreateDirectory(this.workingDir);
            }

            do
            {
                InPipe = InPipePrefix + Guid8Chars();
            } while (File.Exists(InPipeAbsolutePath));

            do
            {
                OutPipe = OutPipePrefix + Guid8Chars();
            } while (File.Exists(OutPipeAbsolutePath));

            this.logger.LogDebug($"InPipeAbsolutePath = {InPipeAbsolutePath}");
            this.logger.LogDebug($"OutPipeAbsolutePath = {OutPipeAbsolutePath}");

            try
            {
                var cmd = $"mkfifo -m=0666 {InPipeAbsolutePath} {OutPipeAbsolutePath}";
                this.logger.LogDebug($"[BASH] {cmd}");
                var mkFifoResult = BashHelper.Run(cmd);
                this.logger.LogInformation($"[BASH] {cmd} result: {mkFifoResult}");
            }
            catch (Exception e)
            {
                this.FatalError("Error creating pipes", e, false);
            }

            var time = Stopwatch.GetTimestamp();

            while (!File.Exists(InPipeAbsolutePath) || !File.Exists(OutPipeAbsolutePath))
            {
                try
                {
                    Thread.Sleep(10);
                }
                catch (Exception)
                {
                }

                if ((Stopwatch.GetTimestamp() - time) / Stopwatch.Frequency > 15)
                {
                    this.FatalError("Pipes did not show up after calling mkfifo", false);
                }
            }

            this.logger.LogDebug("Pipes created successfully.");
        }
Exemplo n.º 2
0
        private string ExtractBinaries()
        {
            lock (EXTRACT_BIN_MUTEX)
            {
                var watchFiles    = new List <string>();
                var os            = GetOSPlatform();
                var root          = "amazon_kinesis_producer_native_binaries";
                var tempDirectory = this.config.TempDirectory;
                if (tempDirectory.Trim().Length == 0)
                {
                    tempDirectory = Path.GetTempPath();
                }

                tempDirectory     = Path.Combine(tempDirectory, root);
                this.pathToTmpDir = tempDirectory;

                var binPath = config.NativeExecutable;
                if (!string.IsNullOrWhiteSpace(binPath))
                {
                    this.pathToExecutable = binPath.Trim();
                    this.logger.LogWarning("Using non-default native binary at " + this.pathToExecutable);
                    this.pathToLibDir = string.Empty;
                    return(string.Empty);
                }
                else
                {
                    this.logger.LogInformation("Extracting binaries to " + tempDirectory);
                    try
                    {
                        if (!Directory.Exists(tempDirectory) && Directory.CreateDirectory(tempDirectory) == null)
                        {
                            throw new IOException("Could not create tmp dir " + tempDirectory);
                        }

                        var extension      = os == "windows" ? ".exe" : "";
                        var executableName = "kinesis_producer" + extension;
                        var bin            = IOUtils.GetResourceFile($"{root}.{os}.{executableName}", os);
                        var mdHex          = SHA1Hash(bin);

                        this.pathToExecutable = Path.Combine(this.pathToTmpDir, $"kinesis_producer_{mdHex}{extension}");
                        watchFiles.Add(this.pathToExecutable);

                        var pathToLock = Path.Combine(this.pathToTmpDir, $"kinesis_producer_{mdHex}.lock");
                        using (var fileLock = new FileStream(pathToLock, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
                        {
                            if (File.Exists(this.pathToExecutable))
                            {
                                var contentEqual = false;
                                if (new FileInfo(this.pathToExecutable).Length == bin.Length)
                                {
                                    byte[] existingBin = File.ReadAllBytes(this.pathToExecutable);
                                    contentEqual = bin.SequenceEqual(existingBin);
                                }

                                if (!contentEqual)
                                {
                                    throw new SecurityException($"The contents of the binary {Path.GetFullPath(pathToExecutable)} is not what it's expected to be.");
                                }
                            }
                            else
                            {
                                File.WriteAllBytes(pathToExecutable, bin);

                                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                                {
                                    this.logger.LogDebug("Setting execute permissions on native executable..");
                                    var setExecutePermissionsResult = BashHelper.Run($"chmod +x {pathToExecutable}");
                                    this.logger.LogDebug($"Bash result: {setExecutePermissionsResult}");
                                }
                            }
                        }

                        var certificateExtractor = new CertificateExtractor(config.LogLevel);
                        var caDirectory          = certificateExtractor.ExtractCertificates(pathToTmpDir);

                        watchFiles.AddRange(certificateExtractor.ExtractedCertificates);
                        this.pathToLibDir = this.pathToTmpDir;
                        FileAgeManager.Instance.RegisterFiles(watchFiles);

                        return(caDirectory);
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Could not copy native binaries to temp directory " + tempDirectory, e);
                    }
                }
            }
        }