Esempio n. 1
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();
                        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);
                    }
                }
            }
        }