LocalhostSourceLocalhostSystemLinux

module Linux

Linux specific system operations.

Definitions

ANCHORS_PATH = "/etc/ca-certificates/trust-source/anchors/"

This appears to be the standard path for the system trust store on many Linux distributions.

LOCAL_CERTIFICATES_PATH = "/usr/local/share/ca-certificates/"

This is an older method for systems that do not use update-ca-trust.

def self.install(certificate)

Install a certificate into the system trust store.

Signature

parameter certificate String

The path to the certificate file.

Implementation

def self.install(certificate)
	filename = File.basename(certificate)
	command = nil
	
	if File.exist?(ANCHORS_PATH)
		# For systems using `update-ca-trust`.
		destination = File.join(ANCHORS_PATH, filename)
		command = UPDATE_CA_TRUST
	elsif File.exist?(LOCAL_CERTIFICATES_PATH)
		# For systems using `update-ca-certificates`.
		destination = File.join(LOCAL_CERTIFICATES_PATH, filename)
		command = UPDATE_CA_CERTIFICATES
	else
		raise "No known system trust store found. Please install the certificate manually."
	end
	
	success = system("sudo", "cp", certificate, destination)
	success &= system("sudo", command)
	
	if success
		$stderr.puts "Installed certificate to #{destination}"
		
		return true
	else
		raise "Failed to install certificate: #{certificate}"
	end
end