From d28cb63fb2384c5edaf0503e29df79584f696478 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 17 Sep 2024 20:40:22 +0200 Subject: [PATCH] Add bucket checking to resources --- .rubocop.yml | 5 +++- Gemfile.lock | 18 +++++++++++++ app/models/subscription_client_resource.rb | 30 +++++++++++++++++++++- discourse_subscription_client.gemspec | 1 + 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index d6a368e..9130127 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -8,6 +8,9 @@ Lint/ConstantDefinitionInBlock: Exclude: - 'spec/**/*.rb' +Lint/UselessAssignment: + Enabled: false + Metrics/BlockLength: Exclude: - 'omnievent.gemspec' @@ -61,4 +64,4 @@ Style/ClassVars: Enabled: false Style/MutableConstant: - Enabled: false + Enabled: false \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 1944f71..b4a4695 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,6 +79,22 @@ GEM activerecord (>= 3.2, < 8.0) rake (>= 10.4, < 14.0) ast (2.4.2) + aws-eventstream (1.3.0) + aws-partitions (1.976.0) + aws-sdk-core (3.205.0) + aws-eventstream (~> 1, >= 1.3.0) + aws-partitions (~> 1, >= 1.651.0) + aws-sigv4 (~> 1.9) + jmespath (~> 1, >= 1.6.1) + aws-sdk-kms (1.91.0) + aws-sdk-core (~> 3, >= 3.205.0) + aws-sigv4 (~> 1.5) + aws-sdk-s3 (1.162.0) + aws-sdk-core (~> 3, >= 3.205.0) + aws-sdk-kms (~> 1) + aws-sigv4 (~> 1.5) + aws-sigv4 (1.9.1) + aws-eventstream (~> 1, >= 1.0.2) builder (3.2.4) byebug (11.1.3) concurrent-ruby (1.2.0) @@ -96,6 +112,7 @@ GEM hashdiff (1.0.1) i18n (1.12.0) concurrent-ruby (~> 1.0) + jmespath (1.6.2) json (2.6.3) loofah (2.19.1) crass (~> 1.0.2) @@ -238,6 +255,7 @@ PLATFORMS DEPENDENCIES active_model_serializers (~> 0.8.3) annotate + aws-sdk-s3 byebug discourse_subscription_client! excon diff --git a/app/models/subscription_client_resource.rb b/app/models/subscription_client_resource.rb index 660c4f9..d8ba960 100644 --- a/app/models/subscription_client_resource.rb +++ b/app/models/subscription_client_resource.rb @@ -1,13 +1,41 @@ # frozen_string_literal: true +require "aws-sdk-s3" + class SubscriptionClientResource < ActiveRecord::Base belongs_to :supplier, class_name: "SubscriptionClientSupplier" has_many :notices, class_name: "SubscriptionClientNotice", as: :notice_subject, dependent: :destroy has_many :subscriptions, foreign_key: "resource_id", class_name: "SubscriptionClientSubscription", dependent: :destroy - def source_url(bucket) + def get_source_url(bucket) + return nil unless access_key_id && secret_access_key + return nil unless can_access_bucket?(bucket) + "s3://#{access_key_id}:#{secret_access_key}@#{bucket}" end + + def can_access_bucket?(bucket) + begin + s3_client.head_bucket(bucket: bucket) + rescue Aws::S3::Errors::BadRequest, + Aws::S3::Errors::Forbidden, + Aws::S3::Errors::NotFound => e + return false + end + true + end + + def s3_client + @s3_client ||= begin + return nil unless access_key_id && secret_access_key + + Aws::S3::Client.new( + region: "us-east-1", + access_key_id: access_key_id, + secret_access_key: secret_access_key + ) + end + end end # == Schema Information diff --git a/discourse_subscription_client.gemspec b/discourse_subscription_client.gemspec index 3e094c0..d7b196e 100644 --- a/discourse_subscription_client.gemspec +++ b/discourse_subscription_client.gemspec @@ -37,4 +37,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rubocop" spec.add_development_dependency "sprockets-rails" spec.add_development_dependency "webmock" + spec.add_development_dependency "aws-sdk-s3" end