Sha256: 3f69895df85c978f02e024a5025f93f26329384dc095dbdcd5009d778fc94741

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

require 'net/http'
require 'uri'

module NdrPseudonymise
  module NdrEncrypt
    # Defines a local ndr_encrypt working copy
    class RemoteRepository
      # rubocop:disable Style/SlicingWithRange
      def initialize(base_url: nil)
        # We need to support ruby 2.0 so cannot use required keyword arguments syntax
        raise(ArgumentError, 'missing keyword: :base_url') unless base_url

        @base_url = base_url
      end

      # Retrieve remote file(s) based on git_blobid
      def cat_remote(git_blobid, key_name: nil, private_key: nil, passin: nil)
        # We need to support ruby 2.0 so cannot use required keyword arguments syntax
        raise(ArgumentError, 'missing keyword: :key_name') unless key_name
        raise(ArgumentError, 'missing keyword: :private_key') unless private_key

        encrypted_id = NdrEncrypt::EncryptedObject.encrypted_id(git_blobid, key_name: key_name)
        rawdata = retrieve_remote_url(encrypted_id)
        contents = NdrEncrypt::EncryptedObject.decrypt(rawdata, private_key: private_key,
                                                                passin: passin)
        blob = NdrEncrypt::EncryptedObject.decompress(contents)
        NdrEncrypt::EncryptedObject.unpack_blob(blob)
      end

      private

      # Retrieve remote encrypted file(s) based on encrypted_id
      def retrieve_remote_url(encrypted_id)
        uri = URI.join(@base_url, "#{encrypted_id[0..1]}/#{encrypted_id[2..-1]}")
        res = Net::HTTP.get_response(uri)
        # TODO: More finegrained error messages
        raise(ArgumentError, 'Could not retrieve URL') unless res.is_a?(Net::HTTPSuccess)

        res.body
      end
      # rubocop:enable Style/SlicingWithRange
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ndr_pseudonymise-0.4.2 lib/ndr_pseudonymise/ndr_encrypt/remote_repository.rb
ndr_pseudonymise-0.4.1 lib/ndr_pseudonymise/ndr_encrypt/remote_repository.rb