Sha256: 4c1c3320353135346aa0df4dae0883a807343f1ee71361ad9df270bfc5009a1b

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'docile'

module CferExt
  module AWS
    module IAM
      class PolicyGenerator < Cfer::BlockHash
        def initialize
          self[:Version] = '2012-10-17'
          self[:Statement] = []
        end

        def statement(options = {}, &block)
          statement = Cfer::BlockHash.new(&block)
          statement.merge! options
          statement.build_from_block(&block)
          self[:Statement].unshift statement
        end

        def allow(&block)
          statement Effect: :Allow, &block
        end

        def deny(&block)
          statement Effect: :Deny, &block
        end
      end

      module WithPolicyDocument
      end

      module WithPolicies
        def policy(doc = nil, &block)
          self[:Properties][:Policies] ||= []
          doc = CferExt::AWS::IAM.generate_policy(&block) if doc == nil
          get_property(:Policies) << doc.to_h
        end
      end

      def self.generate_policy(&block)
        policy = PolicyGenerator.new
        policy.build_from_block(&block)
        policy
      end

      EC2_ASSUME_ROLE_POLICY_DOCUMENT =
        CferExt::AWS::IAM.generate_policy do
          allow do
            principal Service: 'ec2.amazonaws.com'
            action 'sts:AssumeRole'
          end
        end.freeze
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cfer-0.4.0 lib/cferext/aws/iam/policy_generator.rb