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

8
    namespace 'internal' do
9
      # Check if git command is allowed to project
10
      #
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
11
      # Params:
12 13
      #   key_id - ssh key id for Git over SSH
      #   user_id - user id for Git over HTTP
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
14 15 16
      #   project - project path with namespace
      #   action - git action (git-upload-pack or git-receive-pack)
      #   ref - branch name
17
      #   forced_push - forced_push
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
18
      #
19
      post "/allowed" do
20
        status 200
21
        project_path = params[:project]
22

23 24 25 26
        # 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.
27 28
        access =
          if project_path =~ /\.wiki\Z/
29
            project_path.sub!(/\.wiki\Z/, '')
30 31 32 33 34
            Gitlab::GitAccessWiki.new
          else
            Gitlab::GitAccess.new
          end

35
        project = Project.find_with_namespace(project_path)
36 37 38 39

        unless project
          return Gitlab::GitAccessStatus.new(false, 'No such project')
        end
40

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

47 48 49
        unless actor
          return Gitlab::GitAccessStatus.new(false, 'No such user or key')
        end
50

51
        access.check(
52 53 54
          actor,
          params[:action],
          project,
55
          params[:changes]
56
        )
57 58 59 60 61 62 63
      end

      #
      # Discover user by ssh key
      #
      get "/discover" do
        key = Key.find(params[:key_id])
64
        present key.user, with: Entities::UserSafe
65
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
66 67 68

      get "/check" do
        {
69
          api_version: API.version,
70 71
          gitlab_version: Gitlab::VERSION,
          gitlab_rev: Gitlab::REVISION,
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
72 73
        }
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
74 75 76
    end
  end
end