summaryrefslogtreecommitdiff
path: root/lib/gitlab/pages_client.rb
blob: 281eafb142ffce0da78631046d53975c4735e3ca (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
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
# frozen_string_literal: true

module Gitlab
  class PagesClient
    class << self
      attr_reader :certificate, :token

      def call(service, rpc, request, timeout: nil)
        kwargs = request_kwargs(timeout)
        stub(service).__send__(rpc, request, kwargs) # rubocop:disable GitlabSecurity/PublicSend
      end

      # This function is not thread-safe. Call it from an initializer only.
      def read_or_create_token
        @token = read_token
      rescue Errno::ENOENT
        # TODO: uncomment this when omnibus knows how to write the token file for us
        # https://gitlab.com/gitlab-org/omnibus-gitlab/merge_requests/2466
        #
        # write_token(SecureRandom.random_bytes(64))
        #
        # # Read from disk in case someone else won the race and wrote the file
        # # before us. If this fails again let the exception bubble up.
        # @token = read_token
      end

      # This function is not thread-safe. Call it from an initializer only.
      def load_certificate
        cert_path = config.certificate
        return unless cert_path.present?

        @certificate = File.read(cert_path)
      end

      def ping
        request = Grpc::Health::V1::HealthCheckRequest.new
        call(:health_check, :check, request, timeout: 5.seconds)
      end

      private

      def request_kwargs(timeout)
        encoded_token = Base64.strict_encode64(token.to_s)
        metadata = {
          'authorization' => "Bearer #{encoded_token}"
        }

        result = { metadata: metadata }

        return result unless timeout

        # Do not use `Time.now` for deadline calculation, since it
        # will be affected by Timecop in some tests, but grpc's c-core
        # uses system time instead of timecop's time, so tests will fail
        # `Time.at(Process.clock_gettime(Process::CLOCK_REALTIME))` will
        # circumvent timecop
        deadline = Time.at(Process.clock_gettime(Process::CLOCK_REALTIME)) + timeout
        result[:deadline] = deadline

        result
      end

      def stub(name)
        stub_class(name).new(address, grpc_creds)
      end

      def stub_class(name)
        if name == :health_check
          Grpc::Health::V1::Health::Stub
        else
          # TODO use pages namespace
          Gitaly.const_get(name.to_s.camelcase.to_sym).const_get(:Stub)
        end
      end

      def address
        addr = config.address
        addr = addr.sub(%r{^tcp://}, '') if URI(addr).scheme == 'tcp'
        addr
      end

      def grpc_creds
        if address.start_with?('unix:')
          :this_channel_is_insecure
        elsif @certificate
          GRPC::Core::ChannelCredentials.new(@certificate)
        else
          # Use system certificate pool
          GRPC::Core::ChannelCredentials.new
        end
      end

      def config
        Gitlab.config.pages.admin
      end

      def read_token
        File.read(token_path)
      end

      def token_path
        Rails.root.join('.gitlab_pages_secret').to_s
      end

      def write_token(new_token)
        Tempfile.open(File.basename(token_path), File.dirname(token_path), encoding: 'ascii-8bit') do |f|
          f.write(new_token)
          f.close
          File.link(f.path, token_path)
        end
      rescue Errno::EACCES => ex
        # TODO stop rescuing this exception in GitLab 11.0 https://gitlab.com/gitlab-org/gitlab-ce/issues/45672
        Rails.logger.error("Could not write pages admin token file: #{ex}") # rubocop:disable Gitlab/RailsLogger
      rescue Errno::EEXIST
        # Another process wrote the token file concurrently with us. Use their token, not ours.
      end
    end
  end
end