summaryrefslogtreecommitdiff
path: root/spec/helpers/x509_helper_spec.rb
blob: 4e3e8c8d3f6ae87c8cb3f000b11acccd806f0f69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe X509Helper do
  describe '#x509_subject' do
    let(:search_uppercase) { %w[CN OU O] }
    let(:search_lowercase) { %w[cn ou o] }
    let(:certificate_attributes) do
      {
        'CN' => 'CA Issuing',
        'OU' => 'Trust Center',
        'O' => 'Example'
      }
    end

    context 'with uppercase DN' do
      let(:upper_dn) { 'CN=CA Issuing,OU=Trust Center,O=Example,L=World,C=Galaxy' }

      it 'returns the attributes on any case search' do
        expect(x509_subject(upper_dn, search_lowercase)).to eq(certificate_attributes)
        expect(x509_subject(upper_dn, search_uppercase)).to eq(certificate_attributes)
      end
    end

    context 'with lowercase DN' do
      let(:lower_dn) { 'cn=CA Issuing,ou=Trust Center,o=Example,l=World,c=Galaxy' }

      it 'returns the attributes on any case search' do
        expect(x509_subject(lower_dn, search_lowercase)).to eq(certificate_attributes)
        expect(x509_subject(lower_dn, search_uppercase)).to eq(certificate_attributes)
      end
    end

    context 'with comma within DN' do
      let(:comma_dn) { 'cn=CA\, Issuing,ou=Trust Center,o=Example,l=World,c=Galaxy' }
      let(:certificate_attributes) do
        {
          'CN' => 'CA, Issuing',
          'OU' => 'Trust Center',
          'O' => 'Example'
        }
      end

      it 'returns the attributes on any case search' do
        expect(x509_subject(comma_dn, search_lowercase)).to eq(certificate_attributes)
        expect(x509_subject(comma_dn, search_uppercase)).to eq(certificate_attributes)
      end
    end

    context 'with mal formed DN' do
      let(:bad_dn) { 'cn=CA, Issuing,ou=Trust Center,o=Example,l=World,c=Galaxy' }

      it 'returns nil on any case search' do
        expect(x509_subject(bad_dn, search_lowercase)).to eq({})
        expect(x509_subject(bad_dn, search_uppercase)).to eq({})
      end
    end
  end

  describe '#x509_signature?' do
    let(:x509_signature) { create(:x509_commit_signature) }
    let(:gpg_signature) { create(:gpg_signature) }

    it 'detects a x509 signed commit' do
      signature = Gitlab::X509::Signature.new(
        X509Helpers::User1.signed_commit_signature,
        X509Helpers::User1.signed_commit_base_data,
        X509Helpers::User1.certificate_email,
        X509Helpers::User1.signed_commit_time
      )

      expect(x509_signature?(x509_signature)).to be_truthy
      expect(x509_signature?(signature)).to be_truthy
      expect(x509_signature?(gpg_signature)).to be_falsey
    end
  end
end