Class: Cumulus::SecurityGroups::SecurityGroupConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/security/models/SecurityGroupConfig.rb

Overview

Public: An object representing configuration for a security group

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, vpc_id, json = nil) ⇒ SecurityGroupConfig

Public: Constructor.

name - the name of the security group vpc_id - the id of the vpc the security group belongs in json - a hash containing the JSON configuration for the security group



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/security/models/SecurityGroupConfig.rb', line 28

def initialize(name, vpc_id, json = nil)
  @name = name
  @vpc_id = vpc_id
  if !json.nil?
    @description = if !json["description"].nil? then json["description"] else "" end
    @tags = if !json["tags"].nil? then json["tags"] else {} end


    includes = (json["rules"]["includes"] || []).map { |rule| Loader.rule(rule) }
    inbound_includes = includes.reduce([]) { |sofar, inc| sofar + (inc["inbound"] || []) }.flatten.compact
    outbound_includes = includes.reduce([]) { |sofar, inc| sofar + (inc["outbound"] || []) }.flatten.compact

    combined_inbound = (json["rules"]["inbound"] || []) + inbound_includes
    @inbound = combined_inbound.map(&RuleConfig.method(:expand_ports)).flatten

    combined_outbound = (json["rules"]["outbound"] || []) + outbound_includes
    @outbound = if !json["rules"]["outbound"].nil?
      combined_outbound.map(&RuleConfig.method(:expand_ports)).flatten
    else
      if Configuration.instance.security.outbound_default_all_allowed
        [RuleConfig.allow_all]
      else
        outbound_includes
      end
    end
  end
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



15
16
17
# File 'lib/security/models/SecurityGroupConfig.rb', line 15

def description
  @description
end

#inboundObject (readonly)

Returns the value of attribute inbound.



17
18
19
# File 'lib/security/models/SecurityGroupConfig.rb', line 17

def inbound
  @inbound
end

#includesObject (readonly)

Returns the value of attribute includes.



16
17
18
# File 'lib/security/models/SecurityGroupConfig.rb', line 16

def includes
  @includes
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/security/models/SecurityGroupConfig.rb', line 18

def name
  @name
end

#outboundObject (readonly)

Returns the value of attribute outbound.



19
20
21
# File 'lib/security/models/SecurityGroupConfig.rb', line 19

def outbound
  @outbound
end

#tagsObject (readonly)

Returns the value of attribute tags.



20
21
22
# File 'lib/security/models/SecurityGroupConfig.rb', line 20

def tags
  @tags
end

#vpc_idObject (readonly)

Returns the value of attribute vpc_id.



21
22
23
# File 'lib/security/models/SecurityGroupConfig.rb', line 21

def vpc_id
  @vpc_id
end

Instance Method Details

#diff(aws) ⇒ Object

Public: Produce an array of the differences between this local configuration and the configuration in AWS

aws - the aws resource

Returns an array of the SecurityGroupDiffs that were found



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/security/models/SecurityGroupConfig.rb', line 62

def diff(aws)
  diffs = []

  if @description != aws.description
    diffs << SecurityGroupDiff.new(SecurityGroupChange::DESCRIPTION, aws, self)
  end

  if @tags != Hash[aws.tags.map { |t| [t.key, t.value] }]
    diffs << SecurityGroupDiff.new(SecurityGroupChange::TAGS, aws, self)
  end

  inbound_diffs = diff_rules(@inbound, aws.ip_permissions)
  if !inbound_diffs.empty?
    diffs << SecurityGroupDiff.inbound(aws, self, inbound_diffs)
  end

  outbound_diffs = diff_rules(@outbound, aws.ip_permissions_egress)
  if !outbound_diffs.empty?
    diffs << SecurityGroupDiff.outbound(aws, self, outbound_diffs)
  end

  diffs
end

#populate!(aws) ⇒ Object

Public: Populate this SecurityGroupConfig from an AWS resource

aws - the aws resource



89
90
91
92
93
94
95
# File 'lib/security/models/SecurityGroupConfig.rb', line 89

def populate!(aws)
  @vpc_id = aws.vpc_id
  @description = aws.description
  @tags = Hash[aws.tags.map { |t| [t.key, t.value] }]
  @inbound = combine_rules(aws.ip_permissions.map { |rule| RuleConfig.from_aws(rule) })
  @outbound = combine_rules(aws.ip_permissions_egress.map { |rule| RuleConfig.from_aws(rule) })
end

#pretty_jsonObject

Public: Get the config as a prettified JSON string.

Returns the JSON string



100
101
102
103
104
105
106
107
108
109
# File 'lib/security/models/SecurityGroupConfig.rb', line 100

def pretty_json
  JSON.pretty_generate({
    "description" => @description,
    "tags" => @tags,
    "rules" => {
      "inbound" => @inbound.map(&:hash),
      "outbound" => @outbound.map(&:hash),
    }
  }.reject { |k, v| v.nil? })
end