Commit c4aa70b9 authored by Pedro Pombeiro's avatar Pedro Pombeiro

Add RunnersRegistrationToken::Reset mutation

Add matching `:reset_runners_registration_token` ability
parent 1d4458a4
# frozen_string_literal: true
module Mutations
module Ci
module RunnersRegistrationToken
class Reset < BaseMutation
graphql_name 'RunnersRegistrationTokenReset'
authorize :reset_runners_registration_token
argument :full_path, GraphQL::ID_TYPE,
required: false,
description: 'Full path of the project or group to reset the token for. Omit if resetting instance runner token.'
field :token,
GraphQL::STRING_TYPE,
null: true,
description: 'The runner token after mutation.'
def resolve(**args)
full_path = args[:full_path]
scope = full_path.blank? ? :global : authorized_find!(full_path: full_path)
if scope == :global
authorize!(scope)
ApplicationSetting.current.reset_runners_registration_token!
token = ApplicationSetting.current.runners_registration_token
else
scope.reset_runners_token!
token = scope.runners_token
end
{
token: token,
errors: []
}
end
private
def find_object(full_path:)
return unless full_path
GitlabSchema.object_from_id(full_path, expected_type: [::Project, ::Group])
end
end
end
end
end
......@@ -101,6 +101,7 @@ module Types
mount_mutation Mutations::Ci::Job::Retry
mount_mutation Mutations::Ci::Runner::Update, feature_flag: :runner_graphql_query
mount_mutation Mutations::Ci::Runner::Delete, feature_flag: :runner_graphql_query
mount_mutation Mutations::Ci::RunnersRegistrationToken::Reset, feature_flag: :runner_graphql_query
mount_mutation Mutations::Namespace::PackageSettings::Update
mount_mutation Mutations::UserCallouts::Create
end
......
......@@ -115,6 +115,7 @@ class GlobalPolicy < BasePolicy
enable :approve_user
enable :reject_user
enable :read_usage_trends_measurement
enable :reset_runners_registration_token
end
# We can't use `read_statistics` because the user may have different permissions for different projects
......
......@@ -144,6 +144,7 @@ class GroupPolicy < BasePolicy
enable :admin_cluster
enable :read_deploy_token
enable :create_jira_connect_subscription
enable :reset_runners_registration_token
end
rule { owner }.policy do
......
......@@ -419,6 +419,7 @@ class ProjectPolicy < BasePolicy
enable :update_freeze_period
enable :destroy_freeze_period
enable :admin_feature_flags_client
enable :reset_runners_registration_token
end
rule { public_project & metrics_dashboard_allowed }.policy do
......
......@@ -3527,6 +3527,27 @@ Input type: `RunnerUpdateInput`
| <a id="mutationrunnerupdateerrors"></a>`errors` | [`[String!]!`](#string) | Errors encountered during execution of the mutation. |
| <a id="mutationrunnerupdaterunner"></a>`runner` | [`CiRunner`](#cirunner) | The runner after mutation. |
### `Mutation.runnersRegistrationTokenReset`
Available only when feature flag `runner_graphql_query` is enabled.
Input type: `RunnersRegistrationTokenResetInput`
#### Arguments
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="mutationrunnersregistrationtokenresetclientmutationid"></a>`clientMutationId` | [`String`](#string) | A unique identifier for the client performing the mutation. |
| <a id="mutationrunnersregistrationtokenresetfullpath"></a>`fullPath` | [`ID`](#id) | Full path of the project or group to reset the token for. Omit if resetting instance runner token. |
#### Fields
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="mutationrunnersregistrationtokenresetclientmutationid"></a>`clientMutationId` | [`String`](#string) | A unique identifier for the client performing the mutation. |
| <a id="mutationrunnersregistrationtokenreseterrors"></a>`errors` | [`[String!]!`](#string) | Errors encountered during execution of the mutation. |
| <a id="mutationrunnersregistrationtokenresettoken"></a>`token` | [`String`](#string) | The runner token after mutation. |
### `Mutation.terraformStateDelete`
Input type: `TerraformStateDeleteInput`
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'RunnersRegistrationTokenReset' do
include GraphqlHelpers
let(:mutation) { graphql_mutation(:runners_registration_token_reset, input) }
def mutation_response
graphql_mutation_response(:runners_registration_token_reset)
end
subject { post_graphql_mutation(mutation, current_user: user) }
shared_examples 'unauthorized' do
it 'returns an error' do
subject
expect(graphql_errors).not_to be_empty
expect(graphql_errors).to include(a_hash_including('message' => "The resource that you are attempting to access does not exist or you don't have permission to perform this action"))
end
end
shared_context 'when unauthorized' do |scope|
context 'when unauthorized' do
let_it_be(:user) { create(:user) }
context "when not a #{scope} member" do
it_behaves_like 'unauthorized'
end
context "with a non-admin #{scope} member" do
before do
target.add_developer(user)
end
it_behaves_like 'unauthorized'
end
end
end
shared_context 'when authorized' do
it 'resets runner registration token' do
expect { subject }.to change { get_token }
expect(response).to have_gitlab_http_status(:success)
expect(mutation_response).not_to be_nil
expect(mutation_response['errors']).to be_empty
expect(mutation_response['token']).not_to be_empty
expect(mutation_response['token']).to eq(get_token)
end
context 'when bad arguments are provided' do
let(:input) { { full_path: 'some string' } }
it 'returns errors' do
expect { subject }.not_to change { get_token }
expect(graphql_errors).not_to be_empty
expect(mutation_response).to be_nil
end
end
end
context 'applied to project' do
let_it_be(:project) { create_default(:project) }
let(:input) { { full_path: project.to_global_id.to_s } }
include_context 'when unauthorized', 'project' do
let(:target) { project }
end
include_context 'when authorized' do
let_it_be(:user) { project.owner }
def get_token
project.reload.runners_token
end
end
end
context 'applied to group' do
let_it_be(:group) { create_default(:group) }
let(:input) { { full_path: group.to_global_id.to_s } }
include_context 'when unauthorized', 'group' do
let(:target) { group }
end
include_context 'when authorized' do
let_it_be(:user) { create_default(:group_member, :maintainer, user: create(:user), group: group ).user }
def get_token
group.reload.runners_token
end
end
end
context 'applied to instance' do
before do
ApplicationSetting.create_from_defaults
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
end
let(:input) { {} }
context 'when unauthorized' do
let(:user) { create(:user) }
it_behaves_like 'unauthorized'
end
include_context 'when authorized' do
let_it_be(:user) { create(:user, :admin) }
def get_token
ApplicationSetting.current_without_cache.runners_registration_token
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment