Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove runtime dependency on base64 #2378

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# frozen_string_literal: true

require 'json'
require 'base64'
require 'new_relic/base64'

module NewRelic
module Agent
Expand Down Expand Up @@ -78,7 +78,7 @@ def from_json(serialized_payload)
end

def from_http_safe(http_safe_payload)
decoded_payload = Base64.strict_decode64(http_safe_payload)
decoded_payload = NewRelic::Base64.strict_decode64(http_safe_payload)
from_json(decoded_payload)
end

Expand Down Expand Up @@ -156,7 +156,7 @@ def text
#
# @api public
def http_safe
Base64.strict_encode64(text)
NewRelic::Base64.strict_encode64(text)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/new_relic/agent/javascript_instrumentor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

require 'base64'
require 'json'
require 'new_relic/agent/obfuscator'

Expand Down
4 changes: 2 additions & 2 deletions lib/new_relic/agent/new_relic_service/encoders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

require 'base64'
require 'json'
require 'stringio'
require 'zlib'
require 'new_relic/base64'

module NewRelic
module Agent
Expand Down Expand Up @@ -45,7 +45,7 @@ def self.encode(data, opts = {})
data = NewRelic::Agent::EncodingNormalizer.normalize_object(data)
end
json = ::JSON.dump(data)
Base64.encode64(Compressed::Deflate.encode(json))
NewRelic::Base64.encode64(Compressed::Deflate.encode(json))
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/new_relic/agent/obfuscator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

# require 'base64'

module NewRelic
module Agent
class Obfuscator
Expand Down
4 changes: 2 additions & 2 deletions lib/new_relic/agent/pipe_channel_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

require 'base64'
require 'new_relic/base64'

module NewRelic
module Agent
Expand Down Expand Up @@ -257,7 +257,7 @@ def unmarshal(data)
Marshal.load(data)
rescue StandardError => e
::NewRelic::Agent.logger.error('Failure unmarshalling message from Resque child process', e)
::NewRelic::Agent.logger.debug(Base64.encode64(data))
::NewRelic::Agent.logger.debug(NewRelic::Base64.encode64(data))
nil
end

Expand Down
1 change: 0 additions & 1 deletion lib/new_relic/agent/sql_sampler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# frozen_string_literal: true

require 'zlib'
require 'base64'
require 'digest/md5'

module NewRelic
Expand Down
25 changes: 25 additions & 0 deletions lib/new_relic/base64.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module NewRelic
module Base64
extend self

def encode64(bin)
[bin].pack('m')
end

def decode64(str)
str.unpack1('m')
end

def strict_encode64(bin)
[bin].pack('m0')
end

def strict_decode64(str)
str.unpack1('m0')
end
end
end
2 changes: 0 additions & 2 deletions newrelic_rpm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ Gem::Specification.new do |s|
s.require_paths = ['lib']
s.summary = 'New Relic Ruby Agent'

s.add_dependency 'base64'

s.add_development_dependency 'bundler'
s.add_development_dependency 'feedjira', '3.2.1' unless ENV['CI'] || RUBY_VERSION < '2.5' # for Gabby
s.add_development_dependency 'httparty' unless ENV['CI'] # for perf tests and Gabby
Expand Down
2 changes: 1 addition & 1 deletion test/agent_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ def with_ignore_error_filter(filter, &blk)
end

def json_dump_and_encode(object)
Base64.encode64(JSON.dump(object))
NewRelic::Base64.encode64(JSON.dump(object))
end

def get_last_analytics_event
Expand Down
6 changes: 3 additions & 3 deletions test/multiverse/lib/multiverse/suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

require_relative '../../../warning_test_helper'
require_relative '../../../simplecov_test_helper'
require_relative '../../../../lib/new_relic/base64'

require 'rubygems'
require 'base64'
require 'fileutils'
require 'digest'
require_relative 'bundler_patch'
Expand All @@ -33,11 +33,11 @@ def initialize(directory, opts = {})
end

def self.encode_options(decoded_opts)
Base64.encode64(Marshal.dump(decoded_opts)).delete("\n")
NewRelic::Base64.encode64(Marshal.dump(decoded_opts)).delete("\n")
end

def self.decode_options(encoded_opts)
Marshal.load(Base64.decode64(encoded_opts))
Marshal.load(NewRelic::Base64.decode64(encoded_opts))
end

def suite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ def test_cross_app_doesnt_modify_without_header
end

def test_cross_app_doesnt_modify_with_invalid_header
get('/', nil, {'HTTP_X_NEWRELIC_ID' => Base64.encode64('otherjunk')})
get('/', nil, {'HTTP_X_NEWRELIC_ID' => NewRelic::Base64.encode64('otherjunk')})

refute last_response.headers['X-NewRelic-App-Data']
end

def test_cross_app_writes_out_information
get('/', nil, {'HTTP_X_NEWRELIC_ID' => Base64.encode64('1#234')})
get('/', nil, {'HTTP_X_NEWRELIC_ID' => NewRelic::Base64.encode64('1#234')})

refute_nil last_response.headers['X-NewRelic-App-Data']
assert_metrics_recorded(['ClientApplication/1#234/all'])
end

def test_cross_app_doesnt_modify_if_txn_is_ignored
get('/', {'transaction_name' => 'ignored_transaction'}, {'HTTP_X_NEWRELIC_ID' => Base64.encode64('1#234')})
get('/', {'transaction_name' => 'ignored_transaction'}, {'HTTP_X_NEWRELIC_ID' => NewRelic::Base64.encode64('1#234')})

refute last_response.headers['X-NewRelic-App-Data']
end

def test_cross_app_error_attaches_process_id_to_intrinsics
assert_raises(RuntimeError) do
get('/', {'fail' => 'true'}, {'HTTP_X_NEWRELIC_ID' => Base64.encode64('1#234')})
get('/', {'fail' => 'true'}, {'HTTP_X_NEWRELIC_ID' => NewRelic::Base64.encode64('1#234')})
end

assert_includes attributes_for(last_traced_error, :intrinsic), :client_cross_process_id
Expand All @@ -66,7 +66,7 @@ def test_cross_app_error_attaches_process_id_to_intrinsics
if !test_case['outboundRequests']
if test_case['inboundPayload']
request_headers = {
'HTTP_X_NEWRELIC_ID' => Base64.encode64('1#234'),
'HTTP_X_NEWRELIC_ID' => NewRelic::Base64.encode64('1#234'),
'HTTP_X_NEWRELIC_TRANSACTION' => json_dump_and_encode(test_case['inboundPayload'])
}
else
Expand Down
2 changes: 1 addition & 1 deletion test/multiverse/suites/rails/ignore_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_ignored_transaction_traces_dont_leak

def test_should_not_write_cat_response_headers_for_ignored_transactions
get('/ignored/action_to_ignore',
headers: {'X-NewRelic-ID' => Base64.encode64('1#234')})
headers: {'X-NewRelic-ID' => NewRelic::Base64.encode64('1#234')})

refute @response.headers['X-NewRelic-App-Data']
end
Expand Down
4 changes: 2 additions & 2 deletions test/multiverse/suites/rails/request_statistics_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def test_request_should_include_referring_guid_if_needed
:'encoding_key' => "\0",
:'trusted_account_ids' => [1]) do
rack_env = {
'HTTP_X_NEWRELIC_ID' => Base64.encode64('1#234'),
'HTTP_X_NEWRELIC_TRANSACTION' => Base64.encode64('["8badf00d",1]')
'HTTP_X_NEWRELIC_ID' => NewRelic::Base64.encode64('1#234'),
'HTTP_X_NEWRELIC_TRANSACTION' => NewRelic::Base64.encode64('["8badf00d",1]')
}

get('/request_stats/cross_app_action', headers: rack_env)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# frozen_string_literal: true

require_relative '../../../test_helper'
require 'base64'
require 'thread'
require 'timeout'
require 'zlib'
require 'new_relic/agent/threading/backtrace_service'
require 'new_relic/agent/threading/threaded_test_case'
require 'new_relic/agent/commands/thread_profiler_session'
require 'new_relic/base64'

module ThreadProfilerSessionTestHelpers
START = {
Expand Down
1 change: 0 additions & 1 deletion test/new_relic/agent/javascript_instrumentor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

require_relative '../../test_helper'
require 'new_relic/agent/javascript_instrumentor'
require 'base64'

class NewRelic::Agent::JavaScriptInstrumentorTest < Minitest::Test
attr_reader :instrumentor
Expand Down
4 changes: 2 additions & 2 deletions test/new_relic/agent/monitors/cross_app_monitor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def when_request_runs(request = for_id(REQUEST_CROSS_APP_ID), name = 'transactio
end

def for_id(id)
encoded_id = id == '' ? '' : Base64.encode64(id)
encoded_id = id == '' ? '' : NewRelic::Base64.encode64(id)
encoded_txn_info = json_dump_and_encode([REF_TRANSACTION_GUID, false])

return {
Expand All @@ -255,7 +255,7 @@ def response_app_data
def unpacked_response
return nil unless response_app_data

::JSON.load(Base64.decode64(response_app_data))
::JSON.load(NewRelic::Base64.decode64(response_app_data))
end
end
end
2 changes: 1 addition & 1 deletion test/new_relic/agent/new_relic_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ def test_normalization_should_account_for_to_collector_array_with_nested_encodin
assert_equal(expected_string, result[0])

base64_encoded_compressed_json_field = result[1]
compressed_json_field = Base64.decode64(base64_encoded_compressed_json_field)
compressed_json_field = NewRelic::Base64.decode64(base64_encoded_compressed_json_field)
json_field = Zlib::Inflate.inflate(compressed_json_field)
field = JSON.parse(json_field)

Expand Down
4 changes: 2 additions & 2 deletions test/new_relic/agent/obfuscator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def assert_encoded(key_length, text, expected)

assert_equal(expected, output)

unoutput = obfuscator.obfuscate(Base64.decode64(output))
unoutput = obfuscator.obfuscate(NewRelic::Base64.decode64(output))

assert_equal Base64.encode64(text).delete("\n"), unoutput
assert_equal NewRelic::Base64.encode64(text).delete("\n"), unoutput
end
end
2 changes: 1 addition & 1 deletion test/new_relic/fake_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def unblob(blob)
def self.unblob(blob)
return unless blob

JSON.load(Zlib::Inflate.inflate(Base64.decode64(blob)))
JSON.load(Zlib::Inflate.inflate(NewRelic::Base64.decode64(blob)))
end
end

Expand Down
Loading