Class: Hiera::Backend::Eyaml::Encryptors::Age

Inherits:
Encryptor
  • Object
show all
Defined in:
lib/hiera/backend/eyaml/encryptors/age.rb

Constant Summary collapse

VERSION =
Hiera::Backend::Eyaml::Encryptors::AgeVersion::VERSION

Class Method Summary collapse

Class Method Details

.create_keysObject



122
123
124
# File 'lib/hiera/backend/eyaml/encryptors/age.rb', line 122

def self.create_keys
  warn "The age encryptor does not support creation of keys, use the age command line tools instead"
end

.decrypt(ciphertext) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/hiera/backend/eyaml/encryptors/age.rb', line 72

def self.decrypt(ciphertext)
  env_var = option(:identity_env_var)

  if env_var
    raise ArgumentError, "env #{env_var} is not set" unless ENV[env_var]

    # Pass the identity via a pipe rather than a temp file so the key
    # material never touches disk. age's --identity accepts /dev/fd/N.
    # Ruby 2.0+ opens FDs with O_CLOEXEC by default, so we must
    # explicitly preserve the read end across the exec boundary.
    r_fd, w_fd = IO.pipe
    w_fd.write(ENV[env_var])
    w_fd.close
    identity_arg = "/dev/fd/#{r_fd.fileno}"
    extra_opts = { r_fd.fileno => r_fd }
  else
    identity_file = option(:identity_file)
    debug("age identity file is #{identity_file}")

    if identity_file.nil? || identity_file.empty?
      raise ArgumentError,
            "No age identity file configured, check age_identity_file configuration value is correct"
    end

    identity_arg = identity_file
    extra_opts = {}
  end

  stdout, stderr, status =
    Open3.capture3(
      option(:age_binary_path),
      "--decrypt",
      "--identity",
      identity_arg,
      stdin_data: ciphertext,
      binmode: true,
      **extra_opts
    )

  r_fd&.close

  unless status.success?
    warn("age decrypt failed (run with --trace for details, including errors from age which may be sensitive)")
    debug("age decrypt stderr: #{stderr.strip}")
    raise StandardError, "age decrypt failed"
  end

  stdout
end

.encrypt(plaintext) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hiera/backend/eyaml/encryptors/age.rb', line 43

def self.encrypt(plaintext)
  recipients = determine_recipients
  debug("Recipients are #{recipients}")

  if recipients.empty?
    raise RecoverableError,
          "No recipients provided, don't know who to encrypt to"
  end

  recipient_args =
    recipients.flat_map { |recipient| ["-r", recipient] }

  stdout, stderr, status =
    Open3.capture3(
      option(:age_binary_path),
      "--encrypt",
      *recipient_args,
      stdin_data: plaintext,
      binmode: true
    )
  unless status.success?
    warn("age encrypt failed (run with --trace for details, including errors from age which may be sensitive)")
    debug("age encrypt stderr: #{stderr.strip}")
    raise RecoverableError, "age encrypt failed"
  end

  stdout
end