Class: Packages::Debian::GenerateDistributionKeyService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/services/packages/debian/generate_distribution_key_service.rb

Constant Summary collapse

NEWLINE_REGEX =
/\R/
INVALID_PASSPHRASE_ERROR =
ServiceResponse.error(
  message: 'Passphrase contains invalid characters', reason: :invalid_passphrase
).freeze

Instance Method Summary collapse

Constructor Details

#initialize(params: {}) ⇒ GenerateDistributionKeyService

Returns a new instance of GenerateDistributionKeyService.



13
14
15
# File 'app/services/packages/debian/generate_distribution_key_service.rb', line 13

def initialize(params: {})
  @params = params
end

Instance Method Details

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/packages/debian/generate_distribution_key_service.rb', line 17

def execute
  return INVALID_PASSPHRASE_ERROR if invalid_passphrase?

  using_pinentry do |ctx|
    # Generate key
    ctx.generate_key generate_key_params

    key = ctx.keys.first # rubocop:disable Gitlab/KeysFirstAndValuesFirst
    fingerprint = key.fingerprint

    # Export private key
    data = GPGME::Data.new
    ctx.export_keys fingerprint, data, GPGME::EXPORT_MODE_SECRET
    data.seek 0
    private_key = data.read

    # Export public key
    data = GPGME::Data.new
    ctx.export_keys fingerprint, data
    data.seek 0
    public_key = data.read

    ServiceResponse.success(
      payload: {
        private_key: private_key,
        public_key: public_key,
        passphrase: passphrase,
        fingerprint: fingerprint
      }
    )
  end
end