Skip to content

Commit

Permalink
Add bucket checking to resources
Browse files Browse the repository at this point in the history
  • Loading branch information
angusmcleod committed Sep 17, 2024
1 parent 10eb297 commit d28cb63
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Lint/ConstantDefinitionInBlock:
Exclude:
- 'spec/**/*.rb'

Lint/UselessAssignment:
Enabled: false

Metrics/BlockLength:
Exclude:
- 'omnievent.gemspec'
Expand Down Expand Up @@ -61,4 +64,4 @@ Style/ClassVars:
Enabled: false

Style/MutableConstant:
Enabled: false
Enabled: false
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -238,6 +255,7 @@ PLATFORMS
DEPENDENCIES
active_model_serializers (~> 0.8.3)
annotate
aws-sdk-s3
byebug
discourse_subscription_client!
excon
Expand Down
30 changes: 29 additions & 1 deletion app/models/subscription_client_resource.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions discourse_subscription_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d28cb63

Please sign in to comment.