Commit 20f60014 authored by Bogdan Denkovych's avatar Bogdan Denkovych

Remove `Spec::Support::Helpers::KeyGeneratorHelper` class

`Spec::Support::Helpers::KeyGeneratorHelper` was added in
857d0391 to generate RSA public key
in openssh format.
At that time we didn't have a gem in Gemfile to do so.

Later, we added `sshkey` gem that allows generating public keys.

Also, we used to have `SSHKeygen` class to generate RSA public keys
in openssh format for specs:

In https://gitlab.com/gitlab-org/gitlab/-/merge_requests/77424,
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/78532
we started using `ssh_data` gem instead of `net-ssh`, `sshkey`,
`net-ssh`, `ed25519`, `bcrypt_pbkdf`, and `SSHKeygen`-class.

We can remove `Spec::Support::Helpers::KeyGeneratorHelper`
in favor of `ssh_data` gem too.
parent a7f29713
......@@ -231,7 +231,6 @@ Naming/HeredocDelimiterNaming:
Naming/MethodParameterName:
Exclude:
- 'lib/gitlab/diff/inline_diff.rb'
- 'spec/support/helpers/key_generator_helper.rb'
# Offense count: 218
# Cop supports --auto-correct.
......
# frozen_string_literal: true
require_relative '../support/helpers/key_generator_helper'
FactoryBot.define do
factory :key do
title
key { Spec::Support::Helpers::KeyGeneratorHelper.new(1024).generate + ' dummy@gitlab.com' }
key { SSHData::PrivateKey::RSA.generate(1024, unsafe_allow_small_key: true).public_key.openssh(comment: 'dummy@gitlab.com') }
factory :key_without_comment do
key { Spec::Support::Helpers::KeyGeneratorHelper.new(1024).generate }
key { SSHData::PrivateKey::RSA.generate(1024, unsafe_allow_small_key: true).public_key.openssh }
end
factory :deploy_key, class: 'DeployKey'
......
# frozen_string_literal: true
module Spec
module Support
module Helpers
class KeyGeneratorHelper
# The components in a openssh .pub / known_host RSA public key.
RSA_COMPONENTS = ['ssh-rsa', :e, :n].freeze
attr_reader :size
def initialize(size = 2048)
@size = size
end
def generate
key = OpenSSL::PKey::RSA.generate(size)
components = RSA_COMPONENTS.map do |component|
key.respond_to?(component) ? encode_mpi(key.public_send(component)) : component
end
# Ruby tries to be helpful and adds new lines every 60 bytes :(
'ssh-rsa ' + [pack_pubkey_components(components)].pack('m').delete("\n")
end
private
# Encodes an openssh-mpi-encoded integer.
def encode_mpi(n) # rubocop:disable Naming/UncommunicativeMethodParamName
chars = []
n = n.to_i
chars << (n & 0xff) && n >>= 8 while n != 0
chars << 0 if chars.empty? || chars.last >= 0x80
chars.reverse.pack('C*')
end
# Packs string components into an openssh-encoded pubkey.
def pack_pubkey_components(strings)
(strings.flat_map { |s| [s.length].pack('N') }).zip(strings).join
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment