BigW Consortium Gitlab

internal.rb 2.81 KB
Newer Older
1
module API
2
  # Internal access API
Dmitriy Zaporozhets committed
3
  class Internal < Grape::API
4
    before { authenticate_by_gitlab_shell_token! }
5

6
    namespace 'internal' do
7
      # Check if git command is allowed to project
8
      #
9
      # Params:
10 11
      #   key_id - ssh key id for Git over SSH
      #   user_id - user id for Git over HTTP
12 13 14
      #   project - project path with namespace
      #   action - git action (git-upload-pack or git-receive-pack)
      #   ref - branch name
15
      #   forced_push - forced_push
16
      #   protocol - Git access protocol being used, e.g. HTTP or SSH
17
      #
18 19

      helpers do
20
        def wiki?
21 22 23
          @wiki ||= params[:project].end_with?('.wiki') &&
            !Project.find_with_namespace(params[:project])
        end
24 25 26 27 28 29 30 31 32 33 34 35 36 37

        def project
          @project ||= begin
            project_path = params[:project]

            # Check for *.wiki repositories.
            # Strip out the .wiki from the pathname before finding the
            # project. This applies the correct project permissions to
            # the wiki repository as well.
            project_path.chomp!('.wiki') if wiki?

            Project.find_with_namespace(project_path)
          end
        end
38 39
      end

40
      post "/allowed" do
41
        status 200
42

43
        actor =
44 45 46 47 48
          if params[:key_id]
            Key.find_by(id: params[:key_id])
          elsif params[:user_id]
            User.find_by(id: params[:user_id])
          end
49

50 51
        protocol = params[:protocol]

52
        access =
53
          if wiki?
54
            Gitlab::GitAccessWiki.new(actor, project, protocol)
55
          else
56
            Gitlab::GitAccess.new(actor, project, protocol)
57
          end
58

59 60 61 62 63 64 65
        access_status = access.check(params[:action], params[:changes])

        response = { status: access_status.status, message: access_status.message }

        if access_status.status
          # Return the repository full path so that gitlab-shell has it when
          # handling ssh commands
66 67 68 69 70 71
          response[:repository_path] =
            if wiki?
              project.wiki.repository.path_to_repo
            else
              project.repository.path_to_repo
            end
72 73 74
        end

        response
75 76
      end

77 78 79 80
      get "/merge_request_urls" do
        ::MergeRequests::GetUrlsService.new(project).execute(params[:changes])
      end

81 82 83 84 85
      #
      # Discover user by ssh key
      #
      get "/discover" do
        key = Key.find(params[:key_id])
86
        present key.user, with: Entities::UserSafe
87
      end
Dmitriy Zaporozhets committed
88 89 90

      get "/check" do
        {
91
          api_version: API.version,
92 93
          gitlab_version: Gitlab::VERSION,
          gitlab_rev: Gitlab::REVISION,
Dmitriy Zaporozhets committed
94 95
        }
      end
96 97 98 99

      get "/broadcast_message" do
        if message = BroadcastMessage.current
          present message, with: Entities::BroadcastMessage
100 101
        else
          {}
102 103
        end
      end
Dmitriy Zaporozhets committed
104 105 106
    end
  end
end