Class: Kitsune::Kit::Operations::ServiceFirewall

Inherits:
Object
  • Object
show all
Defined in:
lib/kitsune/kit/operations/service_firewall.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, type:, service:, transport:, state_store:, resource:) ⇒ ServiceFirewall

Returns a new instance of ServiceFirewall.



11
12
13
14
15
16
17
18
19
20
# File 'lib/kitsune/kit/operations/service_firewall.rb', line 11

def initialize(config:, type:, service:, transport:, state_store:, resource:)
  @config = config
  @type = type
  @service = service
  @transport = transport
  @state_store = state_store
  @resource = resource
  @owned_rules = []
  @drop_owned = false
end

Instance Attribute Details

#drop_ownedObject (readonly)

Returns the value of attribute drop_owned.



9
10
11
# File 'lib/kitsune/kit/operations/service_firewall.rb', line 9

def drop_owned
  @drop_owned
end

#owned_rulesObject (readonly)

Returns the value of attribute owned_rules.



9
10
11
# File 'lib/kitsune/kit/operations/service_firewall.rb', line 9

def owned_rules
  @owned_rules
end

Instance Method Details

#reconcile(previous) ⇒ Object



22
23
24
25
26
27
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
# File 'lib/kitsune/kit/operations/service_firewall.rb', line 22

def reconcile(previous)
  unless service.publish
    remove_state(previous)
    return
  end

  @transaction_added = []
  @transaction_deleted = []
  ensure_drop_rule(previous, tracked: true)
  added = service.allowed_cidrs.reject { |cidr| rule_exists?(allow_rule(cidr)) }.each do |cidr|
    add_tracked_rule(allow_rule(cidr))
  end
  retained = previous_rules_for_current_port(previous) & service.allowed_cidrs
  @owned_rules = (retained + added).uniq
  remove_stale(previous, tracked: true)
rescue StandardError => e
  begin
    rollback_transaction
  rescue StandardError => recovery_error
    raise Errors::VerificationError.new(
      "Docker firewall reconciliation and recovery both failed",
      hint: "Inspect DOCKER-USER rules marked by Kitsune Kit before retrying.",
      context: {
        original_error: error_name(e), recovery_error: error_name(recovery_error)
      }
    )
  end
  raise e
ensure
  @transaction_added = nil
  @transaction_deleted = nil
end

#removeObject



55
56
57
# File 'lib/kitsune/kit/operations/service_firewall.rb', line 55

def remove
  remove_state(managed_state)
end

#restore(previous) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/kitsune/kit/operations/service_firewall.rb', line 59

def restore(previous)
  return unless previous["published"]

  port = previous.fetch("port")
  ensure_rule(drop_rule(port: port))
  previous.fetch("firewall_rules_added", []).each do |cidr|
    ensure_rule(allow_rule(cidr, port: port))
  end
end

#restore_after_failure(previous) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/kitsune/kit/operations/service_firewall.rb', line 69

def restore_after_failure(previous)
  remove_state(
    "published" => service.publish,
    "port" => service.port,
    "firewall_rules_added" => @owned_rules,
    "firewall_drop_added" => @drop_owned
  )
  restore(previous || {})
end