2020-07-20 05:09:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
# Kubernetes Internal API
|
|
|
|
module Internal
|
|
|
|
class Kubernetes < Grape::API::Instance
|
2020-08-21 08:10:22 -04:00
|
|
|
before do
|
|
|
|
authenticate_gitlab_kas_request!
|
|
|
|
end
|
|
|
|
|
2020-07-20 05:09:22 -04:00
|
|
|
helpers do
|
2020-08-21 08:10:22 -04:00
|
|
|
def authenticate_gitlab_kas_request!
|
|
|
|
unauthorized! unless Gitlab::Kas.verify_api_request(headers)
|
|
|
|
end
|
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
def agent_token
|
|
|
|
@agent_token ||= cluster_agent_token_from_authorization_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def agent
|
|
|
|
@agent ||= agent_token.agent
|
|
|
|
end
|
|
|
|
|
2020-07-20 05:09:22 -04:00
|
|
|
def repo_type
|
|
|
|
Gitlab::GlRepository::PROJECT
|
|
|
|
end
|
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
def gitaly_info(project)
|
|
|
|
shard = repo_type.repository_for(project).shard
|
|
|
|
{
|
|
|
|
address: Gitlab::GitalyClient.address(shard),
|
|
|
|
token: Gitlab::GitalyClient.token(shard),
|
|
|
|
features: Feature::Gitaly.server_feature_flags
|
|
|
|
}
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
def gitaly_repository(project)
|
|
|
|
{
|
|
|
|
storage_name: project.repository_storage,
|
|
|
|
relative_path: project.disk_path + '.git',
|
|
|
|
gl_repository: repo_type.identifier_for_container(project),
|
|
|
|
gl_project_path: repo_type.repository_for(project).full_path
|
|
|
|
}
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_feature_enabled
|
|
|
|
not_found! unless Feature.enabled?(:kubernetes_agent_internal_api)
|
|
|
|
end
|
2020-08-11 23:10:17 -04:00
|
|
|
|
|
|
|
def check_agent_token
|
|
|
|
forbidden! unless agent_token
|
|
|
|
end
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
namespace 'internal' do
|
|
|
|
namespace 'kubernetes' do
|
2020-08-11 23:10:17 -04:00
|
|
|
before do
|
|
|
|
check_feature_enabled
|
|
|
|
check_agent_token
|
|
|
|
end
|
|
|
|
|
2020-07-20 05:09:22 -04:00
|
|
|
desc 'Gets agent info' do
|
|
|
|
detail 'Retrieves agent info for the given token'
|
|
|
|
end
|
|
|
|
route_setting :authentication, cluster_agent_token_allowed: true
|
|
|
|
get '/agent_info' do
|
2020-08-11 23:10:17 -04:00
|
|
|
project = agent.project
|
2020-07-20 05:09:22 -04:00
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
status 200
|
|
|
|
{
|
|
|
|
project_id: project.id,
|
|
|
|
agent_id: agent.id,
|
|
|
|
agent_name: agent.name,
|
|
|
|
gitaly_info: gitaly_info(project),
|
|
|
|
gitaly_repository: gitaly_repository(project)
|
|
|
|
}
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Gets project info' do
|
|
|
|
detail 'Retrieves project info (if authorized)'
|
|
|
|
end
|
|
|
|
route_setting :authentication, cluster_agent_token_allowed: true
|
|
|
|
get '/project_info' do
|
2020-08-11 23:10:17 -04:00
|
|
|
project = find_project(params[:id])
|
2020-07-20 05:09:22 -04:00
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
# TODO sort out authorization for real
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/220912
|
|
|
|
if !project || !project.public?
|
|
|
|
not_found!
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
2020-08-11 23:10:17 -04:00
|
|
|
|
|
|
|
status 200
|
|
|
|
{
|
|
|
|
project_id: project.id,
|
|
|
|
gitaly_info: gitaly_info(project),
|
|
|
|
gitaly_repository: gitaly_repository(project)
|
|
|
|
}
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|