Skip to content
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
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ PATH
remote: .
specs:
aws_advanced_ruby_driver_wrapper (1.0.1)
bigdecimal
concurrent-ruby (~> 1.3, >= 1.3.7)
logger

GEM
remote: https://rubygems.org/
Expand Down
5 changes: 5 additions & 0 deletions aws_advanced_ruby_driver_wrapper.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

# The wrapper requires bigdecimal and logger, and Ruby no longer installs them by default from 3.4
# (bigdecimal) and 4.0 (logger) on. Declaring them keeps a bundle that does not otherwise include
# them, such as an app using the drivers without Active Record, from failing to load the wrapper.
spec.add_dependency 'bigdecimal'
spec.add_dependency 'concurrent-ruby', '~> 1.3', '>= 1.3.7'
spec.add_dependency 'logger'

spec.post_install_message = <<~MSG
═══════════════════════════════════════════════════════════════
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require 'cgi'
require 'cgi/escape'
require 'uri'
require_relative 'base_saml_auth_plugin'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require 'cgi'
require 'cgi/escape'
require 'json'
require_relative 'base_saml_auth_plugin'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require 'base64'
require 'digest'
require 'securerandom'
require_relative '../../logging'
Expand Down Expand Up @@ -105,7 +104,7 @@ def decrypt_data_key(encrypted_data_key, master_key_arn = nil)

plaintext = with_retry('DECRYPT_DATA_KEY') do
response = @kms_client.decrypt(
ciphertext_blob: Base64.strict_decode64(encrypted_data_key.to_s.delete("\n")),
ciphertext_blob: strict_decode64(encrypted_data_key.to_s.delete("\n")),
**(master_key_arn ? { key_id: master_key_arn } : {})
)
response.plaintext.dup.b
Expand All @@ -130,7 +129,7 @@ def generate_data_key(master_key_arn)
response = @kms_client.generate_data_key(key_id: master_key_arn, key_spec: KMS_KEY_SPEC)
GeneratedDataKey.new(
plaintext: response.plaintext.dup.b,
encrypted_data_key: Base64.strict_encode64(response.ciphertext_blob),
encrypted_data_key: strict_encode64(response.ciphertext_blob),
hmac_key: SecureRandom.bytes(HMAC_KEY_LENGTH)
)
end
Expand Down Expand Up @@ -272,11 +271,21 @@ def generate_key_id
# @param encrypted_data_key [String]
# @return [String]
def data_key_cache_key(encrypted_data_key)
"#{DATA_KEY_CACHE_PREFIX}#{Base64.strict_encode64(Digest::SHA256.digest(encrypted_data_key.to_s))}"
"#{DATA_KEY_CACHE_PREFIX}#{strict_encode64(Digest::SHA256.digest(encrypted_data_key.to_s))}"
end

private

# Strict base64 (RFC 4648: no line breaks, padding required), using the encoding built into
# Array#pack rather than the base64 gem. Decoding raises ArgumentError on invalid input.
def strict_encode64(bytes)
[bytes].pack('m0')
end

def strict_decode64(encoded)
encoded.unpack1('m0')
end

def authorize_master_key!(master_key_arn)
return if @config.master_key_allowed?(master_key_arn)

Expand Down
3 changes: 3 additions & 0 deletions spec/unit/plugins/encryption/key_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# The key manager encodes with Array#pack; the expected values here come from Base64, so the specs
# also check that the two agree.
require 'base64'
require_relative '../../../spec_helper'
require 'aws-sdk-kms'
require 'aws_advanced_ruby_driver_wrapper/plugins/kms_encryption/data_key_cache'
Expand Down
Loading