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 dependency on base64 gem #3559

Merged
merged 1 commit into from
Mar 28, 2024
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
1 change: 0 additions & 1 deletion Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,6 @@ target :datadog do
library 'ipaddr'
library 'net-http'
library 'securerandom'
library 'base64'
library 'digest'
library 'zlib'
library 'time'
Expand Down
3 changes: 0 additions & 3 deletions datadog.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ Gem::Specification.new do |spec|
# rubies, see #1739 and #1336 for an extended discussion about this
spec.add_dependency 'msgpack'

# Requiring base64 from stdlib is deprecated in Ruby v3.3 and will be removed in v3.4
spec.add_dependency 'base64'

# Used by the profiler native extension to support Ruby < 2.6 and > 3.2
#
# We decided to pin it at the latest available version and will manually bump the dependency as needed.
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/appsec/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

require 'json'
require 'zlib'
require 'base64'

require_relative 'rate_limiter'
require_relative '../core/utils/base64'

module Datadog
module AppSec
Expand Down Expand Up @@ -140,7 +140,7 @@ def build_service_entry_tags(event_group)
private

def compressed_and_base64_encoded(value)
Base64.encode64(gzip(value))
Datadog::Core::Utils::Base64.encode64(gzip(value))
rescue TypeError => e
Datadog.logger.debug do
"Failed to compress and encode value when populating AppSec::Event. Error: #{e.message}"
Expand Down
3 changes: 2 additions & 1 deletion lib/datadog/core/remote/client/capabilities.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative '../../utils/base64'
require_relative '../../../appsec/remote'
require_relative '../../../tracing/remote'

Expand Down Expand Up @@ -53,7 +54,7 @@ def capabilities_to_base64
cap_to_hexs = capabilities.reduce(:|).to_s(16).tap { |s| s.size.odd? && s.prepend('0') }.scan(/\h\h/)
binary = cap_to_hexs.each_with_object([]) { |hex, acc| acc << hex }.map { |e| e.to_i(16) }.pack('C*')

Base64.encode64(binary).chomp
Datadog::Core::Utils::Base64.encode64(binary).chomp
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/datadog/core/remote/transport/http/config.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require 'json'
require 'base64'

require_relative '../config'
require_relative 'client'
require_relative '../../../utils/base64'
require_relative '../../../transport/http/response'
require_relative '../../../transport/http/api/endpoint'

Expand Down Expand Up @@ -51,7 +51,7 @@ def initialize(http_response, options = {}) # rubocop:disable Metrics/AbcSize,Me

# TODO: these fallbacks should be improved
roots = payload[:roots] || []
targets = payload[:targets] || Base64.encode64('{}').chomp
targets = payload[:targets] || Datadog::Core::Utils::Base64.encode64('{}').chomp
target_files = payload[:target_files] || []
client_configs = payload[:client_configs] || []

Expand All @@ -61,7 +61,7 @@ def initialize(http_response, options = {}) # rubocop:disable Metrics/AbcSize,Me
raise TypeError.new(String, root) unless root.is_a?(String)

decoded = begin
Base64.strict_decode64(root) # TODO: unprocessed, don't symbolize_names
Datadog::Core::Utils::Base64.strict_decode64(root) # TODO: unprocessed, don't symbolize_names
rescue ArgumentError
raise DecodeError.new(:roots, root)
end
Expand All @@ -81,7 +81,7 @@ def initialize(http_response, options = {}) # rubocop:disable Metrics/AbcSize,Me

@targets = begin
decoded = begin
Base64.strict_decode64(targets)
Datadog::Core::Utils::Base64.strict_decode64(targets)
rescue ArgumentError
raise DecodeError.new(:targets, targets)
end
Expand Down Expand Up @@ -109,7 +109,7 @@ def initialize(http_response, options = {}) # rubocop:disable Metrics/AbcSize,Me
raise TypeError.new(String, raw) unless raw.is_a?(String)

content = begin
Base64.strict_decode64(raw)
Datadog::Core::Utils::Base64.strict_decode64(raw)
rescue ArgumentError
raise DecodeError.new(:target_files, raw)
end
Expand Down
22 changes: 22 additions & 0 deletions lib/datadog/core/utils/base64.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Datadog
module Core
module Utils
# Helper methods for encoding and decoding base64
module Base64
def self.encode64(bin)
[bin].pack('m')
end

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

def self.strict_decode64(str)
str.unpack1('m0')
end
end
end
end
end
14 changes: 14 additions & 0 deletions sig/datadog/core/utils/base64.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Datadog
module Core
module Utils
# Helper methods for encoding and decoding base64
module Base64
def self.encode64: (String bin) -> String

def self.strict_encode64: (String bin) -> String

def self.strict_decode64: (String str) -> untyped
end
end
end
end
17 changes: 9 additions & 8 deletions spec/datadog/core/remote/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'spec_helper'
require 'datadog/core/utils/base64'
require 'datadog/core/remote/transport/http'
require 'datadog/core/remote/client'

Expand Down Expand Up @@ -204,21 +205,21 @@
let(:rules_data_response) do
{
'path' => 'datadog/603646/ASM_DD/latest/config',
'raw' => Base64.strict_encode64(rules_data).chomp
'raw' => Datadog::Core::Utils::Base64.strict_encode64(rules_data).chomp
}
end

let(:blocked_ips_data_response) do
{
'path' => 'datadog/603646/ASM_DATA/blocked_ips/config',
'raw' => Base64.strict_encode64(blocked_ips).chomp
'raw' => Datadog::Core::Utils::Base64.strict_encode64(blocked_ips).chomp
}
end

let(:exclusion_data_response) do
{
'path' => 'datadog/603646/ASM/exclusion_filters/config',
'raw' => Base64.strict_encode64(exclusions).chomp
'raw' => Datadog::Core::Utils::Base64.strict_encode64(exclusions).chomp
}
end

Expand All @@ -228,8 +229,8 @@

let(:response_body) do
{
'roots' => roots.map { |r| Base64.strict_encode64(r.to_json).chomp },
'targets' => Base64.strict_encode64(targets.to_json).chomp,
'roots' => roots.map { |r| Datadog::Core::Utils::Base64.strict_encode64(r.to_json).chomp },
'targets' => Datadog::Core::Utils::Base64.strict_encode64(targets.to_json).chomp,
'target_files' => target_files,
'client_configs' => client_configs,
}.to_json
Expand Down Expand Up @@ -380,12 +381,12 @@
context 'invalid response body' do
let(:response_body) do
{
'roots' => roots.map { |r| Base64.strict_encode64(r.to_json).chomp },
'targets' => Base64.strict_encode64(targets.to_json).chomp,
'roots' => roots.map { |r| Datadog::Core::Utils::Base64.strict_encode64(r.to_json).chomp },
'targets' => Datadog::Core::Utils::Base64.strict_encode64(targets.to_json).chomp,
'target_files' => [
{
'path' => 'datadog/603646/ASM/exclusion_filters/config',
'raw' => Base64.strict_encode64(exclusions).chomp
'raw' => Datadog::Core::Utils::Base64.strict_encode64(exclusions).chomp
}
],
'client_configs' => [
Expand Down
7 changes: 4 additions & 3 deletions spec/datadog/core/remote/transport/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'spec_helper'

require 'datadog/core/utils/base64'
require 'datadog/core/remote/transport/http'
require 'datadog/core/remote/transport/http/negotiation'
require 'datadog/core/remote/transport/negotiation'
Expand Down Expand Up @@ -133,7 +134,7 @@
env: Datadog.configuration.env,
tags: [],
},
capabilities: Base64.encode64(capabilities_binary).chomp,
capabilities: Datadog::Core::Utils::Base64.encode64(capabilities_binary).chomp,
},
cached_target_files: [],
}
Expand All @@ -144,11 +145,11 @@
let(:response_code) { 200 }
let(:response_body) do
encode = proc do |obj|
Base64.strict_encode64(obj).chomp
Datadog::Core::Utils::Base64.strict_encode64(obj).chomp
end

jencode = proc do |obj|
Base64.strict_encode64(JSON.dump(obj)).chomp
Datadog::Core::Utils::Base64.strict_encode64(JSON.dump(obj)).chomp
end

JSON.dump(
Expand Down
3 changes: 2 additions & 1 deletion spec/datadog/core/remote/transport/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'spec_helper'

require 'datadog/core/utils/base64'
require 'datadog/core/remote/transport/http'
require 'datadog/core/remote/transport/http/negotiation'
require 'datadog/core/remote/transport/negotiation'
Expand Down Expand Up @@ -89,7 +90,7 @@
env: Datadog.configuration.env,
tags: [],
},
capabilities: Base64.encode64(capabilities_binary).chomp,
capabilities: Datadog::Core::Utils::Base64.encode64(capabilities_binary).chomp,
},
cached_target_files: [],
}
Expand Down
5 changes: 3 additions & 2 deletions spec/datadog/tracing/contrib/suite/integration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'datadog/core/utils/base64'
require 'datadog/tracing/contrib/support/spec_helper'

require 'datadog'
Expand Down Expand Up @@ -72,7 +73,7 @@ def build(*dynamic_configurations)

target_files << {
'path' => target,
'raw' => Base64.strict_encode64(raw),
'raw' => Datadog::Core::Utils::Base64.strict_encode64(raw),
}

targets_targets[target] = {
Expand All @@ -85,7 +86,7 @@ def build(*dynamic_configurations)

{
'target_files' => target_files,
'targets' => Base64.strict_encode64(targets.to_json),
'targets' => Datadog::Core::Utils::Base64.strict_encode64(targets.to_json),
'client_configs' => client_configs,
}.to_json
end
Expand Down
Loading