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 class Projects::DeployTokensController < Projects::ApplicationController
before_action :authorize_admin_project! 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 def revoke
@token = @project.deploy_tokens.find(params[:id]) @token = @project.deploy_tokens.find(params[:id])
@token.revoke! @token.revoke!
......
...@@ -4,14 +4,30 @@ module Projects ...@@ -4,14 +4,30 @@ module Projects
before_action :authorize_admin_project! before_action :authorize_admin_project!
def show 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_keys = DeployKeysPresenter.new(@project, current_user: current_user)
@deploy_tokens = DeployTokensPresenter.new(@project.deploy_tokens.active, current_user: current_user, project: project) @deploy_tokens = DeployTokensPresenter.new(@project.deploy_tokens.active, current_user: current_user, project: project)
define_deploy_token define_deploy_token
define_protected_refs define_protected_refs
end
private render 'show'
end
def define_protected_refs def define_protected_refs
@protected_branches = @project.protected_branches.order(:name).page(params[:page]) @protected_branches = @project.protected_branches.order(:name).page(params[:page])
...@@ -55,9 +71,11 @@ module Projects ...@@ -55,9 +71,11 @@ module Projects
end end
def define_deploy_token def define_deploy_token
attributes = @deploy_tokens.attributes_deploy_token @new_deploy_token ||= DeployToken.new
@deploy_token = DeployToken.new(attributes) end
@deploy_token.valid? unless attributes.empty?
def deploy_token_params
params.require(:deploy_token).permit(:name, :expires_at, :read_repository, :read_registry)
end end
end end
end end
......
module DeployTokensHelper module DeployTokensHelper
def expand_deploy_tokens_section?(temporal_token, deploy_token) def expand_deploy_tokens_section?(deploy_token)
temporal_token.present? || deploy_token.persisted? ||
deploy_token.errors.present? || deploy_token.errors.present? ||
Rails.env.test? Rails.env.test?
end end
......
...@@ -14,29 +14,6 @@ module Projects ...@@ -14,29 +14,6 @@ module Projects
yield deploy_token yield deploy_token
end end
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 end
end end
module DeployTokens module DeployTokens
class CreateService < BaseService class CreateService < BaseService
def execute def execute
@project.deploy_tokens.build.tap do |deploy_token| @project.deploy_tokens.build(params).tap(&:save)
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
end end
end end
end end
%p.profile-settings-content %p.profile-settings-content
= s_("DeployTokens|Pick a name for the application, and we'll give you a unique deploy token.") = 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_errors(token)
.form-group .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) } %section.settings.no-animate{ class: ('expanded' if expanded) }
.settings-header .settings-header
...@@ -8,11 +8,11 @@ ...@@ -8,11 +8,11 @@
%p %p
= s_('DeployTokens|Deploy tokens allow read-only access to your repository and registry images.') = s_('DeployTokens|Deploy tokens allow read-only access to your repository and registry images.')
.settings-content .settings-content
- if @deploy_tokens.temporal_token - if @new_deploy_token.persisted?
= render 'projects/deploy_tokens/new_deploy_token', new_token: @deploy_tokens.temporal_token = render 'projects/deploy_tokens/new_deploy_token', deploy_token: @new_deploy_token
- else
%h5.prepend-top-0 %h5.prepend-top-0
= s_('DeployTokens|Add a deploy token') = s_('DeployTokens|Add a deploy token')
= render 'projects/deploy_tokens/form', project: @project, token: @deploy_token, presenter: @deploy_tokens = render 'projects/deploy_tokens/form', project: @project, token: @new_deploy_token, presenter: @deploy_tokens
%hr %hr
= render 'projects/deploy_tokens/table', project: @project, active_tokens: @deploy_tokens = render 'projects/deploy_tokens/table', project: @project, active_tokens: @deploy_tokens
.created-deploy-token-container .created-deploy-token-container
%h5.prepend-top-0 %h5.prepend-top-0
= s_('DeployTokens|Your New Deploy Token') = 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 .form-group
= text_field_tag 'deploy-token', new_token, readonly: true, class: 'deploy-token-field form-control js-select-on-focus' = text_field_tag 'deploy-token', deploy_token.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') = clipboard_button(text: deploy_token.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.") %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 %hr
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
%thead %thead
%tr %tr
%th= s_('DeployTokens|Name') %th= s_('DeployTokens|Name')
%th= s_('DeployTokens|Username')
%th= s_('DeployTokens|Created') %th= s_('DeployTokens|Created')
%th= s_('DeployTokens|Expires') %th= s_('DeployTokens|Expires')
%th= s_('DeployTokens|Scopes') %th= s_('DeployTokens|Scopes')
...@@ -14,6 +15,7 @@ ...@@ -14,6 +15,7 @@
- active_tokens.each do |token| - active_tokens.each do |token|
%tr %tr
%td= token.name %td= token.name
%td= token.username
%td= token.created_at.to_date.to_s(:medium) %td= token.created_at.to_date.to_s(:medium)
%td %td
- if token.expires? - if token.expires?
......
...@@ -88,7 +88,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do ...@@ -88,7 +88,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
end end
end end
resources :deploy_tokens, constraints: { id: /\d+/ }, only: :create do resources :deploy_tokens, constraints: { id: /\d+/ }, only: [] do
member do member do
put :revoke put :revoke
end end
...@@ -432,7 +432,9 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do ...@@ -432,7 +432,9 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
post :reset_cache post :reset_cache
end end
resource :integrations, only: [:show] 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 end
# Since both wiki and repository routing contains wildcard characters # Since both wiki and repository routing contains wildcard characters
......
...@@ -7,13 +7,12 @@ class CreateProjectDeployTokens < ActiveRecord::Migration ...@@ -7,13 +7,12 @@ class CreateProjectDeployTokens < ActiveRecord::Migration
create_table :project_deploy_tokens do |t| create_table :project_deploy_tokens do |t|
t.integer :project_id, null: false t.integer :project_id, null: false
t.integer :deploy_token_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 :deploy_tokens, column: :deploy_token_id, on_delete: :cascade
t.foreign_key :projects, column: :project_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 end
add_index :project_deploy_tokens, [:project_id, :deploy_token_id], unique: true
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