summaryrefslogtreecommitdiff
path: root/lib/api/deploy_keys.rb
blob: 825e05fbae3d72c930b3bff691d2339b796c4c4f (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
module API
  # Projects API
  class DeployKeys < Grape::API
    before { authenticate! }

    get "deploy_keys" do
      authenticated_as_admin!

      keys = DeployKey.all
      present keys, with: Entities::SSHKey
    end

    params do
      requires :id, type: String, desc: 'The ID of the project'
    end
    resource :projects do
      before { authorize_admin_project }

      # Routing "projects/:id/keys/..." is DEPRECATED and WILL BE REMOVED in version 9.0
      # Use "projects/:id/deploy_keys/..." instead.
      #
      %w(keys deploy_keys).each do |path|
        desc "Get a specific project's deploy keys" do
          success Entities::SSHKey
        end
        get ":id/#{path}" do
          present user_project.deploy_keys, with: Entities::SSHKey
        end

        desc 'Get single deploy key' do
          success Entities::SSHKey
        end
        params do
          requires :key_id, type: Integer, desc: 'The ID of the deploy key'
        end
        get ":id/#{path}/:key_id" do
          key = user_project.deploy_keys.find params[:key_id]
          present key, with: Entities::SSHKey
        end

        # TODO: for 9.0 we should check if params are there with the params block
        # grape provides, at this point we'd change behaviour so we can't
        # Behaviour now if you don't provide all required params: it renders a
        # validation error or two.
        desc 'Add new deploy key to currently authenticated user' do
          success Entities::SSHKey
        end
        post ":id/#{path}" do
          attrs = attributes_for_keys [:title, :key]
          attrs[:key].strip! if attrs[:key]

          key = user_project.deploy_keys.find_by(key: attrs[:key])
          present key, with: Entities::SSHKey if key

          # Check for available deploy keys in other projects
          key = current_user.accessible_deploy_keys.find_by(key: attrs[:key])
          if key
            user_project.deploy_keys << key
            present key, with: Entities::SSHKey
          end

          key = DeployKey.new attrs

          if key.valid? && user_project.deploy_keys << key
            present key, with: Entities::SSHKey
          else
            render_validation_error!(key)
          end
        end

        desc 'Enable a deploy key for a project' do
          detail 'This feature was added in GitLab 8.11'
          success Entities::SSHKey
        end
        params do
          requires :key_id, type: Integer, desc: 'The ID of the deploy key'
        end
        post ":id/#{path}/:key_id/enable" do
          key = ::Projects::EnableDeployKeyService.new(user_project,
                                                        current_user, declared(params)).execute

          if key
            present key, with: Entities::SSHKey
          else
            not_found!('Deploy Key')
          end
        end

        desc 'Disable a deploy key for a project' do
          detail 'This feature was added in GitLab 8.11'
          success Entities::SSHKey
        end
        params do
          requires :key_id, type: Integer, desc: 'The ID of the deploy key'
        end
        delete ":id/#{path}/:key_id/disable" do
          key = user_project.deploy_keys_projects.find_by(deploy_key_id: params[:key_id])
          key.destroy

          present key.deploy_key, with: Entities::SSHKey
        end

        desc 'Delete existing deploy key of currently authenticated user' do
          success Key
        end
        params do
          requires :key_id, type: Integer, desc: 'The ID of the deploy key'
        end
        delete ":id/#{path}/:key_id" do
          key = user_project.deploy_keys.find(params[:key_id])
          key.destroy
        end
      end
    end
  end
end