Commit 9de6e57f authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '293720-token-created-by' into 'master'

Add created_by_user to cluster_agent_tokens

See merge request gitlab-org/gitlab!54019
parents 753808d4 da2efd24
......@@ -8,6 +8,7 @@ module Clusters
self.table_name = 'cluster_agent_tokens'
belongs_to :agent, class_name: 'Clusters::Agent'
belongs_to :created_by_user, class_name: 'User', optional: true
before_save :ensure_token
end
......
---
title: Add created_by_user to cluster agent tokens
merge_request: 54019
author:
type: added
# frozen_string_literal: true
class AddCreatedByUserForClusterAgentToken < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = 'index_cluster_agent_tokens_on_created_by_user_id'
disable_ddl_transaction!
def up
unless column_exists?(:cluster_agent_tokens, :created_by_user_id)
add_column :cluster_agent_tokens, :created_by_user_id, :bigint
end
add_concurrent_index :cluster_agent_tokens, :created_by_user_id, name: INDEX_NAME
add_concurrent_foreign_key :cluster_agent_tokens, :users, column: :created_by_user_id, on_delete: :nullify
end
def down
with_lock_retries do
remove_foreign_key_if_exists :cluster_agent_tokens, :users, column: :created_by_user_id
end
remove_concurrent_index_by_name :cluster_agent_tokens, INDEX_NAME
remove_column :cluster_agent_tokens, :created_by_user_id
end
end
484338ddc83bfb44523d08da92ac7f5b9d13e1a66ad1c9c3f7590f91fc9305c0
\ No newline at end of file
......@@ -11024,6 +11024,7 @@ CREATE TABLE cluster_agent_tokens (
updated_at timestamp with time zone NOT NULL,
agent_id bigint NOT NULL,
token_encrypted text NOT NULL,
created_by_user_id bigint,
CONSTRAINT check_c60daed227 CHECK ((char_length(token_encrypted) <= 255))
);
......@@ -21843,6 +21844,8 @@ CREATE UNIQUE INDEX index_ci_variables_on_project_id_and_key_and_environment_sco
CREATE INDEX index_cluster_agent_tokens_on_agent_id ON cluster_agent_tokens USING btree (agent_id);
CREATE INDEX index_cluster_agent_tokens_on_created_by_user_id ON cluster_agent_tokens USING btree (created_by_user_id);
CREATE UNIQUE INDEX index_cluster_agent_tokens_on_token_encrypted ON cluster_agent_tokens USING btree (token_encrypted);
CREATE UNIQUE INDEX index_cluster_agents_on_project_id_and_name ON cluster_agents USING btree (project_id, name);
......@@ -24369,6 +24372,9 @@ ALTER TABLE ONLY vulnerabilities
ALTER TABLE ONLY index_statuses
ADD CONSTRAINT fk_74b2492545 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
ALTER TABLE ONLY cluster_agent_tokens
ADD CONSTRAINT fk_75008f3553 FOREIGN KEY (created_by_user_id) REFERENCES users(id) ON DELETE SET NULL;
ALTER TABLE ONLY vulnerabilities
ADD CONSTRAINT fk_76bc5f5455 FOREIGN KEY (resolved_by_id) REFERENCES users(id) ON DELETE SET NULL;
......@@ -3521,6 +3521,11 @@ type ClusterAgentToken {
"""
createdAt: Time
"""
The user who created the token.
"""
createdByUser: User
"""
Global ID of the token.
"""
......
......@@ -9547,6 +9547,20 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "createdByUser",
"description": "The user who created the token.",
"args": [
],
"type": {
"kind": "OBJECT",
"name": "User",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "id",
"description": "Global ID of the token.",
......@@ -557,6 +557,7 @@ Autogenerated return type of ClusterAgentDelete.
| ----- | ---- | ----------- |
| `clusterAgent` | ClusterAgent | Cluster agent this token is associated with. |
| `createdAt` | Time | Timestamp the token was created. |
| `createdByUser` | User | The user who created the token. |
| `id` | ClustersAgentTokenID! | Global ID of the token. |
### ClusterAgentTokenCreatePayload
......
......@@ -19,6 +19,11 @@ module Types
null: true,
description: 'Timestamp the token was created.'
field :created_by_user,
Types::UserType,
null: true,
description: 'The user who created the token.'
field :id,
::Types::GlobalIDType[::Clusters::AgentToken],
null: false,
......
......@@ -7,7 +7,7 @@ module Clusters
return error_feature_not_available unless container.feature_available?(:cluster_agents)
return error_no_permissions unless current_user.can?(:create_cluster, container)
token = ::Clusters::AgentToken.new(agent: cluster_agent)
token = ::Clusters::AgentToken.new(agent: cluster_agent, created_by_user: current_user)
if token.save
ServiceResponse.success(payload: { secret: token.token, token: token })
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe GitlabSchema.types['ClusterAgentToken'] do
let(:fields) { %i[cluster_agent created_at id] }
let(:fields) { %i[cluster_agent created_at created_by_user id] }
it { expect(described_class.graphql_name).to eq('ClusterAgentToken') }
......
......@@ -55,7 +55,14 @@ RSpec.describe Clusters::AgentTokens::CreateService do
result = service.execute(cluster_agent)
expect(result.status).to eq(:success)
expect(result.message).to be_nil
end
it 'returns token information', :aggregate_failures do
result = service.execute(cluster_agent)
expect(result.payload[:secret]).not_to be_nil
expect(result.payload[:token].created_by_user).to eq(user)
end
end
end
......
......@@ -4,6 +4,7 @@ require 'spec_helper'
RSpec.describe Clusters::AgentToken do
it { is_expected.to belong_to(:agent).class_name('Clusters::Agent') }
it { is_expected.to belong_to(:created_by_user).class_name('User').optional }
describe '#token' do
it 'is generated on save' do
......
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