Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to run ruby script in windows #135

Open
shubhammugale opened this issue Aug 3, 2023 · 0 comments
Open

Not able to run ruby script in windows #135

shubhammugale opened this issue Aug 3, 2023 · 0 comments

Comments

@shubhammugale
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant