2019-11-01 03:06:26 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 06:09:01 +00:00
|
|
|
RSpec.describe Clusters::Aws::FetchCredentialsService do
|
2019-11-01 03:06:26 +00:00
|
|
|
describe '#execute' do
|
2019-11-13 06:06:38 +00:00
|
|
|
let(:user) { create(:user) }
|
2019-12-12 00:07:43 +00:00
|
|
|
let(:provider) { create(:cluster_provider_aws, region: 'ap-southeast-2') }
|
2019-11-01 03:06:26 +00:00
|
|
|
|
|
|
|
let(:gitlab_access_key_id) { 'gitlab-access-key-id' }
|
|
|
|
let(:gitlab_secret_access_key) { 'gitlab-secret-access-key' }
|
|
|
|
|
|
|
|
let(:gitlab_credentials) { Aws::Credentials.new(gitlab_access_key_id, gitlab_secret_access_key) }
|
2019-11-13 06:06:38 +00:00
|
|
|
let(:sts_client) { Aws::STS::Client.new(credentials: gitlab_credentials, region: region) }
|
2019-11-01 03:06:26 +00:00
|
|
|
let(:assumed_role) { instance_double(Aws::AssumeRoleCredentials, credentials: assumed_role_credentials) }
|
|
|
|
|
|
|
|
let(:assumed_role_credentials) { double }
|
|
|
|
|
2019-12-12 00:07:43 +00:00
|
|
|
subject { described_class.new(provision_role, provider: provider).execute }
|
2019-11-01 03:06:26 +00:00
|
|
|
|
|
|
|
context 'provision role is configured' do
|
2019-11-13 06:06:38 +00:00
|
|
|
let(:provision_role) { create(:aws_role, user: user) }
|
2019-11-01 03:06:26 +00:00
|
|
|
|
|
|
|
before do
|
2019-11-08 06:06:24 +00:00
|
|
|
stub_application_setting(eks_access_key_id: gitlab_access_key_id)
|
|
|
|
stub_application_setting(eks_secret_access_key: gitlab_secret_access_key)
|
2019-11-01 03:06:26 +00:00
|
|
|
|
|
|
|
expect(Aws::Credentials).to receive(:new)
|
|
|
|
.with(gitlab_access_key_id, gitlab_secret_access_key)
|
|
|
|
.and_return(gitlab_credentials)
|
|
|
|
|
|
|
|
expect(Aws::STS::Client).to receive(:new)
|
2019-11-13 06:06:38 +00:00
|
|
|
.with(credentials: gitlab_credentials, region: region)
|
2019-11-01 03:06:26 +00:00
|
|
|
.and_return(sts_client)
|
|
|
|
|
|
|
|
expect(Aws::AssumeRoleCredentials).to receive(:new)
|
|
|
|
.with(
|
|
|
|
client: sts_client,
|
|
|
|
role_arn: provision_role.role_arn,
|
2019-11-13 06:06:38 +00:00
|
|
|
role_session_name: session_name,
|
2019-12-12 00:07:43 +00:00
|
|
|
external_id: provision_role.role_external_id,
|
|
|
|
policy: session_policy
|
2019-11-01 03:06:26 +00:00
|
|
|
).and_return(assumed_role)
|
|
|
|
end
|
|
|
|
|
2019-11-13 06:06:38 +00:00
|
|
|
context 'provider is specified' do
|
2019-12-12 00:07:43 +00:00
|
|
|
let(:region) { provider.region }
|
2019-11-13 06:06:38 +00:00
|
|
|
let(:session_name) { "gitlab-eks-cluster-#{provider.cluster_id}-user-#{user.id}" }
|
2019-12-12 00:07:43 +00:00
|
|
|
let(:session_policy) { nil }
|
2019-11-13 06:06:38 +00:00
|
|
|
|
|
|
|
it { is_expected.to eq assumed_role_credentials }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'provider is not specifed' do
|
|
|
|
let(:provider) { nil }
|
2019-12-12 00:07:43 +00:00
|
|
|
let(:region) { Clusters::Providers::Aws::DEFAULT_REGION }
|
2019-11-13 06:06:38 +00:00
|
|
|
let(:session_name) { "gitlab-eks-autofill-user-#{user.id}" }
|
2019-12-12 00:07:43 +00:00
|
|
|
let(:session_policy) { 'policy-document' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(File).to receive(:read)
|
|
|
|
.with(Rails.root.join('vendor', 'aws', 'iam', 'eks_cluster_read_only_policy.json'))
|
|
|
|
.and_return(session_policy)
|
|
|
|
end
|
2019-11-13 06:06:38 +00:00
|
|
|
|
|
|
|
it { is_expected.to eq assumed_role_credentials }
|
|
|
|
end
|
2019-11-01 03:06:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'provision role is not configured' do
|
2019-11-13 06:06:38 +00:00
|
|
|
let(:provision_role) { nil }
|
2019-11-01 03:06:26 +00:00
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject }.to raise_error(described_class::MissingRoleError, 'AWS provisioning role not configured')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|