BigW Consortium Gitlab

workhorse.rb 7.5 KB
Newer Older
1 2
require 'base64'
require 'json'
3
require 'securerandom'
4
require 'uri'
5 6 7

module Gitlab
  class Workhorse
8 9 10 11
    SEND_DATA_HEADER = 'Gitlab-Workhorse-Send-Data'.freeze
    VERSION_FILE = 'GITLAB_WORKHORSE_VERSION'.freeze
    INTERNAL_API_CONTENT_TYPE = 'application/vnd.gitlab-workhorse+json'.freeze
    INTERNAL_API_REQUEST_HEADER = 'Gitlab-Workhorse-Api-Request'.freeze
12
    NOTIFICATION_CHANNEL = 'workhorse:notifications'.freeze
13 14 15 16

    # Supposedly the effective key size for HMAC-SHA256 is 256 bits, i.e. 32
    # bytes https://tools.ietf.org/html/rfc4868#section-2.6
    SECRET_LENGTH = 32
17

Jacob Vosmaer committed
18
    class << self
19 20
      def git_http_ok(repository, is_wiki, user, action)
        project = repository.project
21
        repo_path = repository.path_to_repo
22
        params = {
23
          GL_ID: Gitlab::GlId.gl_id(user),
24
          GL_REPOSITORY: Gitlab::GlRepository.gl_repository(project, is_wiki),
25
          GL_USERNAME: user&.username,
26
          RepoPath: repo_path
27
        }
28 29 30 31 32
        server = {
          address: Gitlab::GitalyClient.address(project.repository_storage),
          token: Gitlab::GitalyClient.token(project.repository_storage)
        }
        params[:Repository] = repository.gitaly_repository.to_h
33

34 35 36 37
        feature_enabled = case action.to_s
                          when 'git_receive_pack'
                            Gitlab::GitalyClient.feature_enabled?(:post_receive_pack)
                          when 'git_upload_pack'
38
                            true
39 40 41 42 43 44 45
                          when 'info_refs'
                            true
                          else
                            raise "Unsupported action: #{action}"
                          end
        if feature_enabled
          params[:GitalyServer] = server
46
        end
47 48

        params
49 50
      end

51 52 53 54
      def lfs_upload_ok(oid, size)
        {
          StoreLFSPath: "#{Gitlab.config.lfs.storage_path}/tmp/upload",
          LfsOid: oid,
55
          LfsSize: size
56 57 58 59 60 61 62
        }
      end

      def artifact_upload_ok
        { TempPath: ArtifactUploader.artifacts_upload_path }
      end

63
      def send_git_blob(repository, blob)
64
        params = if Gitlab::GitalyClient.feature_enabled?(:workhorse_raw_show)
65 66 67 68 69 70 71 72 73 74 75 76 77 78
                   {
                     'GitalyServer' => gitaly_server_hash(repository),
                     'GetBlobRequest' => {
                       repository: repository.gitaly_repository.to_h,
                       oid: blob.id,
                       limit: -1
                     }
                   }
                 else
                   {
                     'RepoPath' => repository.path_to_repo,
                     'BlobId' => blob.id
                   }
                 end
79 80

        [
81
          SEND_DATA_HEADER,
Douwe Maan committed
82
          "git-blob:#{encode(params)}"
83 84
        ]
      end
85

86
      def send_git_archive(repository, ref:, format:)
87 88
        format ||= 'tar.gz'
        format.downcase!
89
        params = repository.archive_metadata(ref, Gitlab.config.gitlab.repository_downloads_path, format)
90 91
        raise "Repository or ref not found" if params.empty?

92 93 94 95 96 97 98
        if Gitlab::GitalyClient.feature_enabled?(:workhorse_archive)
          params.merge!(
            'GitalyServer' => gitaly_server_hash(repository),
            'GitalyRepository' => repository.gitaly_repository.to_h
          )
        end

99 100
        [
          SEND_DATA_HEADER,
Douwe Maan committed
101
          "git-archive:#{encode(params)}"
102 103
        ]
      end
104

Douwe Maan committed
105
      def send_git_diff(repository, diff_refs)
106 107 108 109 110 111 112 113 114 115
        params = if Gitlab::GitalyClient.feature_enabled?(:workhorse_send_git_diff)
                   {
                     'GitalyServer' => gitaly_server_hash(repository),
                     'RawDiffRequest' => Gitaly::RawDiffRequest.new(
                       gitaly_diff_or_patch_hash(repository, diff_refs)
                     ).to_json
                   }
                 else
                   workhorse_diff_or_patch_hash(repository, diff_refs)
                 end
116 117 118 119

        [
          SEND_DATA_HEADER,
          "git-diff:#{encode(params)}"
120 121
        ]
      end
122

Douwe Maan committed
123
      def send_git_patch(repository, diff_refs)
124 125 126 127 128 129 130 131 132 133
        params = if Gitlab::GitalyClient.feature_enabled?(:workhorse_send_git_patch)
                   {
                     'GitalyServer' => gitaly_server_hash(repository),
                     'RawPatchRequest' => Gitaly::RawPatchRequest.new(
                       gitaly_diff_or_patch_hash(repository, diff_refs)
                     ).to_json
                   }
                 else
                   workhorse_diff_or_patch_hash(repository, diff_refs)
                 end
134 135

        [
136
          SEND_DATA_HEADER,
137 138 139 140
          "git-format-patch:#{encode(params)}"
        ]
      end

141
      def send_artifacts_entry(build, entry)
142 143
        params = {
          'Archive' => build.artifacts_file.path,
144
          'Entry' => Base64.encode64(entry.to_s)
145 146 147 148 149 150 151 152
        }

        [
          SEND_DATA_HEADER,
          "artifacts-entry:#{encode(params)}"
        ]
      end

153 154 155 156 157
      def terminal_websocket(terminal)
        details = {
          'Terminal' => {
            'Subprotocols' => terminal[:subprotocols],
            'Url' => terminal[:url],
158
            'Header' => terminal[:headers],
159
            'MaxSessionTime' => terminal[:max_session_time]
160 161
          }
        }
162
        details['Terminal']['CAPem'] = terminal[:ca_pem] if terminal.key?(:ca_pem)
163 164 165 166

        details
      end

167
      def version
168 169
        path = Rails.root.join(VERSION_FILE)
        path.readable? ? path.read.chomp : 'unknown'
170 171
      end

172 173
      def secret
        @secret ||= begin
174
          bytes = Base64.strict_decode64(File.read(secret_path).chomp)
175 176 177 178
          raise "#{secret_path} does not contain #{SECRET_LENGTH} bytes" if bytes.length != SECRET_LENGTH
          bytes
        end
      end
179

180 181
      def write_secret
        bytes = SecureRandom.random_bytes(SECRET_LENGTH)
182
        File.open(secret_path, 'w:BINARY', 0600) do |f|
183
          f.chmod(0600) # If the file already existed, the '0600' passed to 'open' above was a no-op.
184 185 186
          f.write(Base64.strict_encode64(bytes))
        end
      end
187

188
      def verify_api_request!(request_headers)
189 190 191 192
        decode_jwt(request_headers[INTERNAL_API_REQUEST_HEADER])
      end

      def decode_jwt(encoded_message)
193
        JWT.decode(
194
          encoded_message,
195 196
          secret,
          true,
197
          { iss: 'gitlab-workhorse', verify_iss: true, algorithm: 'HS256' }
198 199 200 201
        )
      end

      def secret_path
202
        Gitlab.config.workhorse.secret_file
203
      end
204

205
      def set_key_and_notify(key, value, expire: nil, overwrite: true)
206
        Gitlab::Redis::Queues.with do |redis|
207 208
          result = redis.set(key, value, ex: expire, nx: !overwrite)
          if result
209
            redis.publish(NOTIFICATION_CHANNEL, "#{key}=#{value}")
210 211 212 213 214 215 216
            value
          else
            redis.get(key)
          end
        end
      end

217
      protected
218

219 220 221
      def encode(hash)
        Base64.urlsafe_encode64(JSON.dump(hash))
      end
222 223 224 225 226 227 228

      def gitaly_server_hash(repository)
        {
          address: Gitlab::GitalyClient.address(repository.project.repository_storage),
          token: Gitlab::GitalyClient.token(repository.project.repository_storage)
        }
      end
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

      def workhorse_diff_or_patch_hash(repository, diff_refs)
        {
          'RepoPath'  => repository.path_to_repo,
          'ShaFrom'   => diff_refs.base_sha,
          'ShaTo'     => diff_refs.head_sha
        }
      end

      def gitaly_diff_or_patch_hash(repository, diff_refs)
        {
          repository: repository.gitaly_repository,
          left_commit_id: diff_refs.base_sha,
          right_commit_id: diff_refs.head_sha
        }
      end
245 246
    end
  end
Jacob Vosmaer committed
247
end