Class: Vaultez::Config
- Inherits:
-
Object
- Object
- Vaultez::Config
- Defined in:
- lib/vaultez/config.rb
Constant Summary collapse
- CONFIG_PATH =
File.("~/.vaultez/config.yml")
Class Method Summary collapse
- .api_url ⇒ Object
- .clear ⇒ Object
- .default_company ⇒ Object
- .delete(key) ⇒ Object
- .get(key) ⇒ Object
- .read ⇒ Object
- .set(key, value) ⇒ Object
- .tighten_permissions ⇒ Object
- .token ⇒ Object
- .write(data) ⇒ Object
Class Method Details
.api_url ⇒ Object
32 33 34 |
# File 'lib/vaultez/config.rb', line 32 def self.api_url get("api_url") || "https://vaultez.app" end |
.clear ⇒ Object
24 25 26 |
# File 'lib/vaultez/config.rb', line 24 def self.clear FileUtils.rm_f(CONFIG_PATH) end |
.default_company ⇒ Object
36 37 38 |
# File 'lib/vaultez/config.rb', line 36 def self.default_company get("default_company") end |
.delete(key) ⇒ Object
18 19 20 21 22 |
# File 'lib/vaultez/config.rb', line 18 def self.delete(key) data = read data.delete(key.to_s) write(data) end |
.get(key) ⇒ Object
8 9 10 |
# File 'lib/vaultez/config.rb', line 8 def self.get(key) read[key.to_s] end |
.read ⇒ Object
42 43 44 45 46 |
# File 'lib/vaultez/config.rb', line 42 def self.read return {} unless File.exist?(CONFIG_PATH) YAML.safe_load(File.read(CONFIG_PATH)) || {} end |
.set(key, value) ⇒ Object
12 13 14 15 16 |
# File 'lib/vaultez/config.rb', line 12 def self.set(key, value) data = read data[key.to_s] = value write(data) end |
.tighten_permissions ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/vaultez/config.rb', line 62 def self. dir = File.dirname(CONFIG_PATH) File.chmod(0o700, dir) if File.directory?(dir) && (File.stat(dir).mode & 0o777) != 0o700 File.chmod(0o600, CONFIG_PATH) if (File.stat(CONFIG_PATH).mode & 0o777) != 0o600 rescue StandardError nil # best-effort; never block a read over a permissions fix end |
.token ⇒ Object
28 29 30 |
# File 'lib/vaultez/config.rb', line 28 def self.token ENV["VAULTEZ_TOKEN"] || get("token") end |
.write(data) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/vaultez/config.rb', line 48 def self.write(data) dir = File.dirname(CONFIG_PATH) FileUtils.mkdir_p(dir, mode: 0o700) # The mode argument to mkdir_p/File.open only applies at creation time, # so explicitly chmod too - this also retroactively tightens a config # directory/file that was created before this fix, which would # otherwise stay world-readable forever (0644/0755 by default umask). File.chmod(0o700, dir) File.open(CONFIG_PATH, File::CREAT | File::TRUNC | File::WRONLY, 0o600) do |f| f.write(data.to_yaml) end File.chmod(0o600, CONFIG_PATH) end |