Sha256: a53efb47d1489924f1954cd73565c1fcf00e1991d32fe01b830ce68477881ee0

Contents?: true

Size: 1.74 KB

Versions: 74

Compression:

Stored size: 1.74 KB

Contents

require "resource_support/aws/aws_singular_resource_mixin"
require "resource_support/aws/aws_backend_base"
require "aws-sdk-sns"

class AwsSnsTopic < Inspec.resource(1)
  name "aws_sns_topic"
  desc "Verifies settings for an SNS Topic"
  example <<~EXAMPLE
    describe aws_sns_topic('arn:aws:sns:us-east-1:123456789012:some-topic') do
      it { should exist }
      its('confirmed_subscription_count') { should_not be_zero }
    end
  EXAMPLE
  supports platform: "aws"

  include AwsSingularResourceMixin
  attr_reader :arn, :confirmed_subscription_count

  private

  def validate_params(raw_params)
    validated_params = check_resource_param_names(
      raw_params: raw_params,
      allowed_params: [:arn],
      allowed_scalar_name: :arn,
      allowed_scalar_type: String
    )
    # Validate the ARN
    unless validated_params[:arn] =~ /^arn:aws:sns:[\w\-]+:\d{12}:[\S]+$/
      raise ArgumentError, "Malformed ARN for SNS topics.  Expected an ARN of the form " \
                           "'arn:aws:sns:REGION:ACCOUNT-ID:TOPIC-NAME'"
    end
    validated_params
  end

  def fetch_from_api
    aws_response = BackendFactory.create(inspec_runner).get_topic_attributes(topic_arn: @arn).attributes
    @exists = true

    # The response has a plain hash with CamelCase plain string keys and string values
    @confirmed_subscription_count = aws_response["SubscriptionsConfirmed"].to_i
  rescue Aws::SNS::Errors::NotFound
    @exists = false
  end

  # Uses the SDK API to really talk to AWS
  class Backend
    class AwsClientApi < AwsBackendBase
      BackendFactory.set_default_backend(self)
      self.aws_client_class = Aws::SNS::Client

      def get_topic_attributes(criteria)
        aws_service_client.get_topic_attributes(criteria)
      end
    end
  end
end

Version data entries

74 entries across 74 versions & 1 rubygems

Version Path
inspec-4.56.58 lib/resources/aws/aws_sns_topic.rb
inspec-4.56.20 lib/resources/aws/aws_sns_topic.rb
inspec-4.56.19 lib/resources/aws/aws_sns_topic.rb
inspec-4.56.17 lib/resources/aws/aws_sns_topic.rb
inspec-4.52.9 lib/resources/aws/aws_sns_topic.rb
inspec-4.50.3 lib/resources/aws/aws_sns_topic.rb
inspec-4.49.0 lib/resources/aws/aws_sns_topic.rb
inspec-4.46.13 lib/resources/aws/aws_sns_topic.rb
inspec-4.41.20 lib/resources/aws/aws_sns_topic.rb
inspec-4.41.2 lib/resources/aws/aws_sns_topic.rb
inspec-4.38.9 lib/resources/aws/aws_sns_topic.rb
inspec-4.38.3 lib/resources/aws/aws_sns_topic.rb
inspec-4.37.30 lib/resources/aws/aws_sns_topic.rb
inspec-4.37.25 lib/resources/aws/aws_sns_topic.rb
inspec-4.37.23 lib/resources/aws/aws_sns_topic.rb
inspec-4.37.20 lib/resources/aws/aws_sns_topic.rb
inspec-4.37.17 lib/resources/aws/aws_sns_topic.rb
inspec-4.37.8 lib/resources/aws/aws_sns_topic.rb
inspec-4.37.0 lib/resources/aws/aws_sns_topic.rb
inspec-4.36.4 lib/resources/aws/aws_sns_topic.rb