Class: AgentsControl::Secrets::Providers::File

Inherits:
Object
  • Object
show all
Defined in:
lib/agents_control/secrets.rb

Overview

Last resort: a file with 0600 permissions.

Needed where there's no keyring daemon at all — a headless server or a container. Worse than the other options, so we warn about it.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: self.class.path) ⇒ File

Returns a new instance of File.



200
201
202
# File 'lib/agents_control/secrets.rb', line 200

def initialize(path: self.class.path)
  @path = path
end

Class Method Details

.pathObject



195
196
197
198
# File 'lib/agents_control/secrets.rb', line 195

def self.path
  base = ENV["XDG_STATE_HOME"] || ::File.expand_path("~/.local/state")
  ::File.join(base, "agents_control", "credentials.json")
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


206
# File 'lib/agents_control/secrets.rb', line 206

def available? = true

#delete(key) ⇒ Object



228
229
230
231
232
233
# File 'lib/agents_control/secrets.rb', line 228

def delete(key)
  data = read
  return false unless data.key?(key)

  set_all(data.except(key))
end

#get(key) ⇒ Object



212
# File 'lib/agents_control/secrets.rb', line 212

def get(key) = read[key]

#insecure?Boolean

Returns:

  • (Boolean)


210
# File 'lib/agents_control/secrets.rb', line 210

def insecure? = true

#nameObject



204
# File 'lib/agents_control/secrets.rb', line 204

def name = "file #{@path} (0600)"

#set(key, value) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/agents_control/secrets.rb', line 214

def set(key, value)
  data = read.merge(key => value)
  FileUtils.mkdir_p(::File.dirname(@path))

  # Permissions are set before writing: otherwise there's a window
  # between file creation and chmod where the secret is
  # world-readable.
  ::File.open(@path, ::File::WRONLY | ::File::CREAT | ::File::TRUNC, 0o600) do |file|
    file.write(JSON.generate(data))
  end

  true
end

#writable?Boolean

Returns:

  • (Boolean)


208
# File 'lib/agents_control/secrets.rb', line 208

def writable? = true