Skip to content
Snippets Groups Projects
Commit ec29d299 authored by Allen Cook's avatar Allen Cook :three:
Browse files

Merge branch '406659-remove-jitsu-authentication' into 'master'

Remove jitsu authentication

See merge request !125520



Merged-by: Allen Cook's avatarAllen Cook <acook@gitlab.com>
Approved-by: default avatarAakriti Gupta <agupta@gitlab.com>
Approved-by: Allen Cook's avatarAllen Cook <acook@gitlab.com>
Reviewed-by: default avatarAakriti Gupta <agupta@gitlab.com>
Reviewed-by: default avatarHalil Coban <hcoban@gitlab.com>
Co-authored-by: default avatarHalil Coban <hcoban@gitlab.com>
parents 8666aaad 1147afc7
No related branches found
No related tags found
1 merge request!125520Remove jitsu authentication
Pipeline #926385242 passed
......@@ -10,7 +10,6 @@ Lint/SymbolConversion:
- 'config/puma.rb'
- 'ee/app/components/billing/plan_component.rb'
- 'ee/app/controllers/projects/security/scanned_resources_controller.rb'
- 'ee/app/models/product_analytics/jitsu_authentication.rb'
- 'ee/app/serializers/integrations/zentao_serializers/issue_entity.rb'
- 'ee/db/fixtures/development/35_merge_request_predictions.rb'
- 'ee/lib/api/analytics/product_analytics.rb'
......
......@@ -44,7 +44,6 @@ RSpec/ExpectChange:
- 'ee/spec/models/group_wiki_spec.rb'
- 'ee/spec/models/incident_management/issuable_escalation_status_spec.rb'
- 'ee/spec/models/member_spec.rb'
- 'ee/spec/models/product_analytics/jitsu_authentication_spec.rb'
- 'ee/spec/models/project_import_state_spec.rb'
- 'ee/spec/models/push_rule_spec.rb'
- 'ee/spec/models/security/orchestration_policy_configuration_spec.rb'
......
......@@ -1185,7 +1185,6 @@ RSpec/MissingFeatureCategory:
- 'ee/spec/models/path_lock_spec.rb'
- 'ee/spec/models/plan_spec.rb'
- 'ee/spec/models/preloaders/environments/protected_environment_preloader_spec.rb'
- 'ee/spec/models/product_analytics/jitsu_authentication_spec.rb'
- 'ee/spec/models/productivity_analytics_spec.rb'
- 'ee/spec/models/project_alias_spec.rb'
- 'ee/spec/models/project_ci_cd_setting_spec.rb'
......
# frozen_string_literal: true
module ProductAnalytics
class JitsuAuthentication
def initialize(jid, project)
@jid = jid
@project = project
settings = ProductAnalytics::Settings.for_project(project)
@root_url = settings.jitsu_host
@clickhouse_connection_string = settings.product_analytics_clickhouse_connection_string
@jitsu_project_xid = settings.jitsu_project_xid
@jitsu_administrator_email = settings.jitsu_administrator_email
@jitsu_administrator_password = settings.jitsu_administrator_password
end
def create_api_key!
response = Gitlab::HTTP.post(
"#{@root_url}/api/v2/objects/#{@jitsu_project_xid}/api_keys",
allow_local_requests: true,
headers: {
Authorization: "Bearer #{generate_access_token}"
},
body: {
'comment': @project.to_global_id.to_s,
'jsAuth': SecureRandom.uuid
}.to_json
)
json = Gitlab::Json.parse(response.body)
if response.success?
@project.project_setting.update(jitsu_key: json['jsAuth'])
return { jsAuth: json['jsAuth'], uid: json['uid'] }
end
log_jitsu_api_error(json)
rescue StandardError => e
Gitlab::ErrorTracking.track_exception(e)
end
def create_clickhouse_destination!
id = SecureRandom.uuid
response = Gitlab::HTTP.post(
"#{@root_url}/api/v2/objects/#{@jitsu_project_xid}/destinations",
allow_local_requests: true,
headers: {
Authorization: "Bearer #{generate_access_token}"
},
body: {
_type: 'clickhouse',
_onlyKeys: [create_api_key![:uid]],
_id: id,
_uid: SecureRandom.uuid,
_connectionTestOk: true,
_formData: {
ch_database: "gitlab_project_#{@project.id}",
mode: 'stream',
tableName: "jitsu",
ch_dsns_list: [@clickhouse_connection_string]
}
}.to_json
)
response.success? ? id : log_jitsu_api_error(Gitlab::Json.parse(response.body))
rescue StandardError => e
Gitlab::ErrorTracking.track_exception(e)
end
def generate_access_token
response = Gitlab::HTTP.post(
"#{@root_url}/api/v1/users/signin",
allow_local_requests: true,
headers: { 'Content-Type' => 'application/json' },
body: {
'email': @jitsu_administrator_email,
'password': @jitsu_administrator_password
}.to_json
)
json = Gitlab::Json.parse(response.body)
response.success? ? json['access_token'] : log_jitsu_api_error(json)
rescue StandardError => e
Gitlab::ErrorTracking.track_exception(e)
end
private
def log_jitsu_api_error(json)
Gitlab::AppLogger.error(
message: 'Jitsu API error',
error: json['error'],
jitsu_error_message: json['message'],
project_id: @project.id,
job_id: @jid
)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ProductAnalytics::JitsuAuthentication do
let(:jid) { '12345678' }
let(:error_message) { '' }
let(:jitsu_error_message) { '' }
let_it_be(:project) { create(:project) }
subject(:auth) { described_class.new(jid, project) }
before do
stub_application_setting(
jitsu_host: 'http://jitsu.dev',
jitsu_project_xid: 'testtesttesttestprj',
jitsu_administrator_email: 'test@test.com',
jitsu_administrator_password: 'testtest'
)
end
shared_examples 'returns nil and logs the API error' do
it do
expect(Gitlab::AppLogger).to receive(:error).with(
message: 'Jitsu API error',
error: error_message,
jitsu_error_message: jitsu_error_message,
project_id: project.id,
job_id: jid
)
expect(subject).to be_nil
end
end
shared_examples 'returns nil and logs the exception' do
it do
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(instance_of(Gitlab::HTTP::Error))
expect(subject).to be_nil
end
end
describe '#generate_access_token' do
subject { auth.generate_access_token }
context 'when request is successful' do
before do
stub_signin_success
end
it { is_expected.to eq('thisisanaccesstoken') }
end
context 'when request is unsuccessful' do
let(:error_message) { 'invalid password' }
let(:jitsu_error_message) { 'Authorization failed: invalid password' }
before do
stub_signin_failure
end
it_behaves_like 'returns nil and logs the API error'
end
context 'when request throws an exception' do
before do
stub_signin_exception
end
it_behaves_like 'returns nil and logs the exception'
end
end
describe '#create_api_key!' do
subject { auth.create_api_key! }
context 'when request is successful' do
before do
stub_signin_success
stub_api_key_success
allow(auth).to receive(:access_token).and_return('testtoken')
end
it { is_expected.to eq({ jsAuth: 'Mp1N4PYvRXNk1KIh2MLDE7BYghnSwdnt', uid: 'yijlmncqjot0xy9h6rv54p.s7zz20' }) }
it do
expect { subject }.to change(project.reload.project_setting, :jitsu_key).from(nil)
.to('Mp1N4PYvRXNk1KIh2MLDE7BYghnSwdnt')
end
end
context 'when request is unsuccessful' do
let(:error_message) { 'token required' }
let(:jitsu_error_message) { 'Authorization failed: token required' }
before do
stub_signin_success
stub_api_key_failure
allow(auth).to receive(:access_token).and_return('testtoken')
end
it_behaves_like 'returns nil and logs the API error'
end
context 'when request throws an exception' do
before do
stub_signin_success
stub_api_key_exception
allow(auth).to receive(:access_token).and_return('testtoken')
end
it_behaves_like 'returns nil and logs the exception'
end
end
describe '#create_clickhouse_destination' do
subject { auth.create_clickhouse_destination! }
context 'when request is successful' do
before do
stub_signin_success
stub_api_key_success
stub_clickhouse_success
allow(auth).to receive(:access_token).and_return('testtoken')
end
it { is_expected.not_to be_nil }
end
context 'when request is unsuccessful' do
let(:error_message) { 'token required' }
let(:jitsu_error_message) { 'Authorization failed: token required' }
before do
stub_signin_success
stub_api_key_success
stub_clickhouse_failure
allow(auth).to receive(:access_token).and_return('testtoken')
end
it_behaves_like 'returns nil and logs the API error'
end
context 'when request throws an exception' do
before do
stub_signin_success
stub_api_key_success
stub_clickhouse_exception
allow(auth).to receive(:access_token).and_return('testtoken')
end
it_behaves_like 'returns nil and logs the exception'
end
end
private
def stub_signin_success
stub_request(:post, "http://jitsu.dev/api/v1/users/signin")
.with(body: "{\"email\":\"test@test.com\",\"password\":\"testtest\"}")
.to_return(status: 200, body: { access_token: 'thisisanaccesstoken' }.to_json, headers: {})
end
def stub_signin_failure
stub_request(:post, "http://jitsu.dev/api/v1/users/signin")
.with(body: "{\"email\":\"test@test.com\",\"password\":\"testtest\"}")
.to_return(
status: 401,
body: { error: 'invalid password', message: 'Authorization failed: invalid password' }.to_json,
headers: {}
)
end
def stub_signin_exception
stub_request(:post, "http://jitsu.dev/api/v1/users/signin")
.with(body: "{\"email\":\"test@test.com\",\"password\":\"testtest\"}")
.to_raise(Gitlab::HTTP::Error)
end
def stub_api_key_success
stub_request(:post, "http://jitsu.dev/api/v2/objects/testtesttesttestprj/api_keys")
.to_return(
status: 200,
body: "{\"jsAuth\":\"Mp1N4PYvRXNk1KIh2MLDE7BYghnSwdnt\",\"uid\":\"yijlmncqjot0xy9h6rv54p.s7zz20\"}",
headers: {}
)
end
def stub_api_key_failure
stub_request(:post, "http://jitsu.dev/api/v2/objects/testtesttesttestprj/api_keys")
.to_return(
status: 401,
body: { error: 'token required', message: 'Authorization failed: token required' }.to_json,
headers: {}
)
end
def stub_api_key_exception
stub_request(:post, "http://jitsu.dev/api/v2/objects/testtesttesttestprj/api_keys")
.to_raise(Gitlab::HTTP::Error)
end
def stub_clickhouse_success
stub_request(:post, "http://jitsu.dev/api/v2/objects/testtesttesttestprj/destinations")
.to_return(status: 200)
end
def stub_clickhouse_failure
stub_request(:post, "http://jitsu.dev/api/v2/objects/testtesttesttestprj/destinations")
.to_return(
status: 401,
body: { error: 'token required', message: 'Authorization failed: token required' }.to_json,
headers: {}
)
end
def stub_clickhouse_exception
stub_request(:post, "http://jitsu.dev/api/v2/objects/testtesttesttestprj/destinations")
.to_raise(Gitlab::HTTP::Error)
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment