Commit 2c6c6181 authored by Kamil Trzciński's avatar Kamil Trzciński Committed by Mayra Cabrera

Get rid of Redis when dealing with deploy tokens

We use controller actions to pass a newly created token and errors
parent 6b2954ec
class Projects::DeployTokensController < Projects::ApplicationController
before_action :authorize_admin_project!
def create
@token = DeployTokens::CreateService.new(@project, current_user, deploy_token_params).execute
if @token.valid?
flash[:notice] = 'Your new project deploy token has been created.'
end
redirect_to project_settings_repository_path(project)
end
def revoke
@token = @project.deploy_tokens.find(params[:id])
@token.revoke!
......
......@@ -4,14 +4,30 @@ module Projects
before_action :authorize_admin_project!
def show
render_show
end
def create_deploy_token
@new_deploy_token = DeployTokens::CreateService.new(@project, current_user, deploy_token_params).execute
if @new_deploy_token.valid?
flash[:notice] = 'Your new project deploy token has been created.'
end
render_show
end
private
def render_show
@deploy_keys = DeployKeysPresenter.new(@project, current_user: current_user)
@deploy_tokens = DeployTokensPresenter.new(@project.deploy_tokens.active, current_user: current_user, project: project)
define_deploy_token
define_protected_refs
end
private
render 'show'
end
def define_protected_refs
@protected_branches = @project.protected_branches.order(:name).page(params[:page])
......@@ -55,9 +71,11 @@ module Projects
end
def define_deploy_token
attributes = @deploy_tokens.attributes_deploy_token
@deploy_token = DeployToken.new(attributes)
@deploy_token.valid? unless attributes.empty?
@new_deploy_token ||= DeployToken.new
end
def deploy_token_params
params.require(:deploy_token).permit(:name, :expires_at, :read_repository, :read_registry)
end
end
end
......
module DeployTokensHelper
def expand_deploy_tokens_section?(temporal_token, deploy_token)
temporal_token.present? ||
def expand_deploy_tokens_section?(deploy_token)
deploy_token.persisted? ||
deploy_token.errors.present? ||
Rails.env.test?
end
......
......@@ -14,29 +14,6 @@ module Projects
yield deploy_token
end
end
def temporal_token
@temporal_token ||= Gitlab::Redis::SharedState.with do |redis|
token = redis.get(deploy_token_key)
redis.del(deploy_token_key)
token
end
end
def attributes_deploy_token
@attributes_deploy_token ||= Gitlab::Redis::SharedState.with do |redis|
attributes_key = deploy_token_key + ":attributes"
attributes_content = redis.get(attributes_key) || '{}'
redis.del(attributes_key)
JSON.parse(attributes_content)
end
end
private
def deploy_token_key
@deploy_token_key ||= DeployToken.redis_shared_state_key(current_user.id)
end
end
end
end
module DeployTokens
class CreateService < BaseService
def execute
@project.deploy_tokens.build.tap do |deploy_token|
deploy_token.attributes = params
deploy_token.save
store_deploy_token_info_in_redis(deploy_token)
end
end
private
def store_deploy_token_info_in_redis(deploy_token)
deploy_token_key = DeployToken.redis_shared_state_key(current_user.id)
if deploy_token.persisted?
store_in_redis(deploy_token_key, deploy_token.token)
else
store_deploy_attributes(deploy_token_key, deploy_token)
end
end
def store_deploy_attributes(deploy_token_key, deploy_token)
attributes = deploy_token.attributes.slice("name", "expires_at")
deploy_token_attributes_key = deploy_token_key + ":attributes"
store_in_redis(deploy_token_attributes_key, attributes.to_json)
end
def store_in_redis(key, value)
Gitlab::Redis::SharedState.with do |redis|
redis.set(key, value, ex: 3.minutes)
end
@project.deploy_tokens.build(params).tap(&:save)
end
end
end
%p.profile-settings-content
= s_("DeployTokens|Pick a name for the application, and we'll give you a unique deploy token.")
= form_for token, url: project_deploy_tokens_path(project), method: :post do |f|
= form_for token, url: create_deploy_token_namespace_project_settings_repository_path(project.namespace, project), method: :post do |f|
= form_errors(token)
.form-group
......
- expanded = expand_deploy_tokens_section?(@deploy_tokens.temporal_token, @deploy_token)
- expanded = expand_deploy_tokens_section?(@new_deploy_token)
%section.settings.no-animate{ class: ('expanded' if expanded) }
.settings-header
......@@ -8,11 +8,11 @@
%p
= s_('DeployTokens|Deploy tokens allow read-only access to your repository and registry images.')
.settings-content
- if @deploy_tokens.temporal_token
= render 'projects/deploy_tokens/new_deploy_token', new_token: @deploy_tokens.temporal_token
%h5.prepend-top-0
= s_('DeployTokens|Add a deploy token')
= render 'projects/deploy_tokens/form', project: @project, token: @deploy_token, presenter: @deploy_tokens
%hr
- if @new_deploy_token.persisted?
= render 'projects/deploy_tokens/new_deploy_token', deploy_token: @new_deploy_token
- else
%h5.prepend-top-0
= s_('DeployTokens|Add a deploy token')
= render 'projects/deploy_tokens/form', project: @project, token: @new_deploy_token, presenter: @deploy_tokens
%hr
= render 'projects/deploy_tokens/table', project: @project, active_tokens: @deploy_tokens
.created-deploy-token-container
%h5.prepend-top-0
= s_('DeployTokens|Your New Deploy Token')
.form-group
= text_field_tag 'deploy-token-user', deploy_token.username, readonly: true, class: 'deploy-token-field form-control js-select-on-focus'
= clipboard_button(text: deploy_token.username, title: s_('DeployTokens|Copy deploy token username to clipboard'), placement: 'left')
%span.help-block.prepend-top-5.text-success= s_("DeployTokens|Use this username as a login.")
.form-group
= text_field_tag 'deploy-token', new_token, readonly: true, class: 'deploy-token-field form-control js-select-on-focus'
= clipboard_button(text: new_token, title: s_('DeployTokens|Copy deploy token to clipboard'), placement: 'left')
%span.deploy-token-help-block.prepend-top-5.text-danger= s_("DeployTokens|Make sure you save it - you won't be able to access it again.")
= text_field_tag 'deploy-token', deploy_token.token, readonly: true, class: 'deploy-token-field form-control js-select-on-focus'
= clipboard_button(text: deploy_token.token, title: s_('DeployTokens|Copy deploy token to clipboard'), placement: 'left')
%span.help-block.prepend-top-5.text-danger= s_("DeployTokens|Use this token as a password. Make sure you save it - you won't be able to access it again.")
%hr
......@@ -6,6 +6,7 @@
%thead
%tr
%th= s_('DeployTokens|Name')
%th= s_('DeployTokens|Username')
%th= s_('DeployTokens|Created')
%th= s_('DeployTokens|Expires')
%th= s_('DeployTokens|Scopes')
......@@ -14,6 +15,7 @@
- active_tokens.each do |token|
%tr
%td= token.name
%td= token.username
%td= token.created_at.to_date.to_s(:medium)
%td
- if token.expires?
......
......@@ -88,7 +88,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
end
end
resources :deploy_tokens, constraints: { id: /\d+/ }, only: :create do
resources :deploy_tokens, constraints: { id: /\d+/ }, only: [] do
member do
put :revoke
end
......@@ -432,7 +432,9 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
post :reset_cache
end
resource :integrations, only: [:show]
resource :repository, only: [:show], controller: :repository
resource :repository, only: [:show], controller: :repository do
post :create_deploy_token, path: 'deploy_token/create'
end
end
# Since both wiki and repository routing contains wildcard characters
......
......@@ -7,13 +7,12 @@ class CreateProjectDeployTokens < ActiveRecord::Migration
create_table :project_deploy_tokens do |t|
t.integer :project_id, null: false
t.integer :deploy_token_id, null: false
t.timestamps null: false
t.foreign_key :deploy_tokens, column: :deploy_token_id, on_delete: :cascade
t.foreign_key :projects, column: :project_id, on_delete: :cascade
t.timestamps null: false
t.index [:project_id, :deploy_token_id], unique: true
end
add_index :project_deploy_tokens, [:project_id, :deploy_token_id], unique: true
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