Sha256: 6d76c595da7d43e9b7308a42547993cf804ff0ea0551ce1a4a0b0b9267304daa

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

module Jets::Resource::Iam
  class PolicyDocument
    extend Memoist

    attr_reader :definitions
    def initialize(*definitions)
      @definitions = definitions.flatten
      # empty starting policy that will be altered
      @policy = {
        version: "2012-10-17",
        statement: []
      }
      # https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_sid.html
      @sid = 0 # counter
    end

    def policy_document
      definitions.map { |definition| standardize(definition) }
      Jets::Pascalize.pascalize(@policy)
    end
    memoize :policy_document # only process policy_document once

    def standardize(definition)
      @sid += 1
      case definition
      when String
        @policy[:statement] << {
          sid: "Stmt#{@sid}",
          action: [definition],
          effect: "Allow",
          resource: "*",
        }
      when Hash
        definition = definition.stringify_keys
        if definition.key?("Version") # special case where we replace the policy entirely
          @policy = definition
        else
          @policy[:statement] << definition
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jets-0.8.4 lib/jets/resource/iam/policy_document.rb
jets-0.8.3 lib/jets/resource/iam/policy_document.rb
jets-0.8.2 lib/jets/resource/iam/policy_document.rb
jets-0.8.1 lib/jets/resource/iam/policy_document.rb