fluent/fluent-plugin-sql

Not able to run ruby script in windows

shubhammugale opened this issue · 0 comments

I want to run the above script to encrypt the password for the td-agent by using the below command

MY_CREDENTIAL_KEY=mykey ruby /etc/fluentd/mycredential.rb encrypt

But I am not able to run that command in windows via Powershell and cmd. I have done all the steps like setting up env. variables installed in the latest ruby version. still not able to run the command in windows. can someone help me with that?

#!/usr/bin/env ruby

require 'openssl'
require 'fileutils'

class MyCredential
PATH = ENV["MY_CREDENTIAL_PATH"] || "/etc/fluentd/credential.txt"
ENCRYPTED_PATH = PATH + ".enc"
KEY = ENV["MY_CREDENTIAL_KEY"]
SALT = ENV["MY_CREDENTIAL_SALT"] || "a9bab8f6-5db7-4693-81e8-93951d2c2468"

def self.encrypt
data = File.read(PATH)

enc = OpenSSL::Cipher.new("AES-256-CBC")
enc.encrypt
key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(KEY, SALT, 2000,
                                         enc.key_len + enc.iv_len)
enc.key = key_iv[0, enc.key_len]
enc.iv = key_iv[enc.key_len, enc.iv_len]

encrypted_data = ""
encrypted_data << enc.update(data)
encrypted_data << enc.final

encrypted_data

end

def self.decrypt(field=nil)
data = File.read(ENCRYPTED_PATH)

dec = OpenSSL::Cipher.new("AES-256-CBC")
dec.decrypt
key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(KEY, SALT, 2000,
                                         dec.key_len + dec.iv_len)
dec.key = key_iv[0, dec.key_len]
dec.iv = key_iv[dec.key_len, dec.iv_len]

decrypted_data = ""
decrypted_data << dec.update(data)
decrypted_data << dec.final

if field == :user
  decrypted_data.split(/\R/)[0]
elsif field == :pass
  decrypted_data.split(/\R/)[1]
else
  decrypted_data
end

end

def self.encrypt_file
data = encrypt
File.open(ENCRYPTED_PATH, "wb") do |f|
f.write(data)
end
FileUtils.chmod(0660, ENCRYPTED_PATH)
FileUtils.rm_f(PATH)
end

def self.decrypt_file
data = decrypt
File.open(PATH, "wb") do |f|
f.write(data)
end
FileUtils.chmod(0660, PATH)
FileUtils.rm_f(ENCRYPTED_PATH)
end
end

if $0 == FILE
case ARGV[0]
when "encrypt"
MyCredential.encrypt_file
when "decrypt"
MyCredential.decrypt_file
else
puts "Unknown command: #{ARGV[0]}"
end
end