Commit 5b797471 authored by Alishan Ladhani's avatar Alishan Ladhani

Move files into Terraform module

- Model
- Uploader
- Tests
parent 24002101
# frozen_string_literal: true
module Terraform
def self.table_name_prefix
'terraform_'
end
end
# frozen_string_literal: true
module Terraform
class State < ApplicationRecord
belongs_to :project
validates :project_id, presence: true
after_save :update_file_store, if: :saved_change_to_file?
mount_uploader :file, StateUploader
def update_file_store
# The file.object_store is set during `uploader.store!`
# which happens after object is inserted/updated
self.update_column(:file_store, file.object_store)
end
def file_store
super || StateUploader.default_store
end
end
end
# frozen_string_literal: true
class TerraformState < ApplicationRecord
belongs_to :project
validates :project_id, presence: true
after_save :update_file_store, if: :saved_change_to_file?
mount_uploader :file, TerraformStateUploader
def update_file_store
# The file.object_store is set during `uploader.store!`
# which happens after object is inserted/updated
self.update_column(:file_store, file.object_store)
end
def file_store
super || TerraformStateUploader.default_store
end
end
# frozen_string_literal: true
module Terraform
class StateUploader < GitlabUploader
include ObjectStorage::Concern
storage_options Gitlab.config.terraform_state
delegate :project_id, to: :model
# Use Lockbox to encrypt/decrypt the stored file (registers CarrierWave callbacks)
encrypt(key: :key)
def filename
"#{model.id}.tfstate"
end
def store_dir
project_id.to_s
end
def key
OpenSSL::HMAC.digest('SHA256', Gitlab::Application.secrets.db_key_base, project_id.to_s)
end
class << self
def direct_upload_enabled?
false
end
def background_upload_enabled?
false
end
def proxy_download_enabled?
true
end
def default_store
object_store_enabled? ? ObjectStorage::Store::REMOTE : ObjectStorage::Store::LOCAL
end
end
end
end
# frozen_string_literal: true
class TerraformStateUploader < GitlabUploader
include ObjectStorage::Concern
storage_options Gitlab.config.terraform_state
delegate :project_id, to: :model
# Use Lockbox to encrypt/decrypt the stored file (registers CarrierWave callbacks)
encrypt(key: :key)
def filename
"#{model.id}.tfstate"
end
def store_dir
project_id.to_s
end
def key
OpenSSL::HMAC.digest('SHA256', Gitlab::Application.secrets.db_key_base, project_id.to_s)
end
class << self
def direct_upload_enabled?
false
end
def background_upload_enabled?
false
end
def proxy_download_enabled?
true
end
def default_store
object_store_enabled? ? ObjectStorage::Store::REMOTE : ObjectStorage::Store::LOCAL
end
end
end
# frozen_string_literal: true
FactoryBot.define do
factory :terraform_state do
factory :terraform_state, class: 'Terraform::State' do
project { create(:project) }
trait :with_file do
file { fixture_file_upload('spec/fixtures/terraform.tfstate') }
file { fixture_file_upload('spec/fixtures/terraform/terraform.tfstate') }
end
end
end
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe TerraformState do
describe Terraform::State do
subject { create(:terraform_state, :with_file) }
it { is_expected.to belong_to(:project) }
......@@ -10,14 +10,14 @@ describe TerraformState do
it { is_expected.to validate_presence_of(:project_id) }
before do
stub_terraform_state_object_storage(TerraformStateUploader)
stub_terraform_state_object_storage(Terraform::StateUploader)
end
describe '#file_store' do
context 'when no value is set' do
it 'returns the default store of the uploader' do
[ObjectStorage::Store::LOCAL, ObjectStorage::Store::REMOTE].each do |store|
expect(TerraformStateUploader).to receive(:default_store).and_return(store)
expect(Terraform::StateUploader).to receive(:default_store).and_return(store)
expect(described_class.new.file_store).to eq(store)
end
end
......
......@@ -2,10 +2,10 @@
require 'spec_helper'
describe TerraformStateUploader do
describe Terraform::StateUploader do
subject { terraform_state.file }
let(:terraform_state) { create(:terraform_state, file: fixture_file_upload('spec/fixtures/terraform.tfstate')) }
let(:terraform_state) { create(:terraform_state, file: fixture_file_upload('spec/fixtures/terraform/terraform.tfstate')) }
before do
stub_terraform_state_object_storage
......@@ -36,11 +36,11 @@ describe TerraformStateUploader do
describe 'encryption' do
it 'encrypts the stored file' do
expect(subject.file.read).not_to eq(fixture_file('terraform.tfstate'))
expect(subject.file.read).not_to eq(fixture_file('terraform/terraform.tfstate'))
end
it 'decrypts the file when reading' do
expect(subject.read).to eq(fixture_file('terraform.tfstate'))
expect(subject.read).to eq(fixture_file('terraform/terraform.tfstate'))
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