Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions lib/aikido/zen/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,8 @@ def start!
end

if @config.realtime_settings_updates_enabled?
if @api_stream.can_connect?
@api_stream.handle("config-updated") { |event| settings_updated(event) }
@api_stream.start!

# Use the realtime setting updates endpoint when polling to check
# whether settings should be fetched.
@api_client.should_fetch_settings_endpoint = @config.realtime_settings_updates_endpoint
else
@config.logger.warn("Can't reach #{Aikido::Zen.config.realtime_settings_updates_endpoint}, make sure it's in your outbound firewall allowlist. Realtime config updates won't be available, switched to polling.")
end
@api_stream.handle("config-updated") { |event| settings_updated(event) }
@api_stream.start!
end

poll_for_setting_updates
Expand Down
27 changes: 1 addition & 26 deletions lib/aikido/zen/api_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(
@thread = nil
@http = nil

endpoint = @config.realtime_settings_updates_endpoint
endpoint = @config.realtime_endpoint

@host = endpoint.host
@port = endpoint.port
Expand All @@ -37,31 +37,6 @@ def initialize(
@handlers = Concurrent::Array.new
end

# @return [Boolean] whether we could connect to the realtime endpoint
def can_connect?
http = Net::HTTP.new(@host, @port)
http.use_ssl = @use_ssl
http.open_timeout = 5
http.write_timeout = 5
http.read_timeout = 5
http.max_retries = 0

request = Net::HTTP::Get.new("/config")
request["Authorization"] = @token

begin
http.request(request)

return true
rescue Timeout::Error, SocketError, IOError, SystemCallError, OpenSSL::OpenSSLError => err
@config.logger.debug("Error probing realtime endpoint: #{err.class}: #{err.message}")
rescue => err
@config.logger.error("Error probing realtime endpoint: #{err.class}: #{err.message}")
end

false
end

def running?
@running.true?
end
Expand Down
15 changes: 0 additions & 15 deletions lib/aikido/zen/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,6 @@ class Config
attr_accessor :realtime_settings_updates_enabled
alias_method :realtime_settings_updates_enabled?, :realtime_settings_updates_enabled

# @return [URI] The HTTP host for realtime settings updates.
# Defaults to +https://zen.aikido.dev+.
attr_reader :realtime_settings_updates_endpoint

def initialize
self.insert_middleware_after = ::ActionDispatch::RemoteIp
self.disabled = read_boolean_from_env(ENV.fetch("AIKIDO_DISABLE", false)) || read_boolean_from_env(ENV.fetch("AIKIDO_DISABLED", false))
Expand Down Expand Up @@ -271,7 +267,6 @@ def initialize
self.idor_excluded_table_names = []
self.idor_max_cache_entries = 1000
self.realtime_settings_updates_enabled = false
self.realtime_settings_updates_endpoint = ENV.fetch("AIKIDO_REALTIME_SETTINGS_UPDATES_ENDPOINT", DEFAULT_REALTIME_SETTINGS_UPDATES_BASE_URL)
end

# Set the base URL for API requests.
Expand All @@ -288,13 +283,6 @@ def realtime_endpoint=(url)
@realtime_endpoint = URI(url)
end

# Set the base URL for the realtime settings updates feature.
#
# @param url [String, URI]
def realtime_settings_updates_endpoint=(url)
@realtime_settings_updates_endpoint = URI(url)
end

# Set the logger and configure its severity level according to agent's debug mode
# @param logger [::Logger]
def logger=(logger)
Expand Down Expand Up @@ -364,9 +352,6 @@ def read_boolean_from_env(value)
# @!visibility private
DEFAULT_RUNTIME_BASE_URL = "https://runtime.aikido.dev"

# @!visibility private
DEFAULT_REALTIME_SETTINGS_UPDATES_BASE_URL = "https://zen.aikido.dev"

# @!visibility private
DEFAULT_JSON_ENCODER = JSON.method(:dump)

Expand Down
100 changes: 0 additions & 100 deletions test/aikido/zen/agent_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ def work
end
end

def stub_probe_realtime_endpoint
stub_request(:get, "#{@config.realtime_settings_updates_endpoint}/config")
end

setup do
@config = Aikido::Zen.config
@config.api_token = "TOKEN"
Expand All @@ -59,8 +55,6 @@ def stub_probe_realtime_endpoint
end

test "knows if it has started" do
stub_probe_realtime_endpoint

refute @agent.started?

@agent.start!
Expand All @@ -71,8 +65,6 @@ def stub_probe_realtime_endpoint
end

test "#start! fails if attempted to start multiple times" do
stub_probe_realtime_endpoint

@agent.start!

err = assert_raises Aikido::ZenError do
Expand All @@ -83,16 +75,12 @@ def stub_probe_realtime_endpoint
end

test "#start! sets the start time for our stats funnel" do
stub_probe_realtime_endpoint

assert_changes "@collector.stats.started_at", from: nil do
@agent.start!
end
end

test "#start! warns if blocking mode is disabled" do
stub_probe_realtime_endpoint

@config.blocking_mode = false
@agent.start!

Expand All @@ -101,8 +89,6 @@ def stub_probe_realtime_endpoint
end

test "#start! notifies if blocking mode is enabled" do
stub_probe_realtime_endpoint

@config.blocking_mode = true
@agent.start!

Expand All @@ -111,8 +97,6 @@ def stub_probe_realtime_endpoint
end

test "#start! notifies if an API token has been set" do
stub_probe_realtime_endpoint

@config.api_token = "TOKEN"
@agent.start!

Expand All @@ -121,88 +105,14 @@ def stub_probe_realtime_endpoint
end

test "#start! warns if there's no API token set" do
stub_probe_realtime_endpoint

@config.api_token = nil
@agent.start!

assert_logged :warn, /no api token set! reporting has been disabled/i
refute_logged :debug, /api token set! reporting has been enabled/i
end

test "#start! probes the realtime endpoint" do
request = stub_probe_realtime_endpoint
.to_return(status: 200, body: "")

@config.api_token = "TOKEN"
@agent.start!

assert_requested request

refute_logged :debug, /error probing realtime endpoint/i
refute_logged :error, /error probing realtime endpoint/i
refute_logged :warn, /can't reach #{Aikido::Zen.config.realtime_settings_updates_endpoint}/i
end

test "#start! probes the realtime endpoint and logs warning after open timeout" do
request = stub_probe_realtime_endpoint
.to_raise(Net::OpenTimeout)

@config.api_token = "TOKEN"
@agent.start!

assert_requested request

assert_logged :debug, /error probing realtime endpoint/i
refute_logged :error, /error probing realtime endpoint/i
assert_logged :warn, /can't reach #{Aikido::Zen.config.realtime_settings_updates_endpoint}/i
end

test "#start! probes the realtime endpoint and logs warning after write timeout" do
request = stub_probe_realtime_endpoint
.to_raise(Net::WriteTimeout)

@config.api_token = "TOKEN"
@agent.start!

assert_requested request

assert_logged :debug, /error probing realtime endpoint/i
refute_logged :error, /error probing realtime endpoint/i
assert_logged :warn, /can't reach #{Aikido::Zen.config.realtime_settings_updates_endpoint}/i
end

test "#start! probes the realtime endpoint and logs warning after read timeout" do
request = stub_probe_realtime_endpoint
.to_raise(Net::ReadTimeout)

@config.api_token = "TOKEN"
@agent.start!

assert_requested request

assert_logged :debug, /error probing realtime endpoint/i
refute_logged :error, /error probing realtime endpoint/i
assert_logged :warn, /can't reach #{Aikido::Zen.config.realtime_settings_updates_endpoint}/i
end

test "#start! probes the realtime endpoint and logs error after unexpected error" do
request = stub_probe_realtime_endpoint
.to_raise(RuntimeError)

@config.api_token = "TOKEN"
@agent.start!

assert_requested request

refute_logged :debug, /error probing realtime endpoint/i
assert_logged :error, /error probing realtime endpoint/i
assert_logged :warn, /can't reach #{Aikido::Zen.config.realtime_settings_updates_endpoint}/i
end

test "#start! reports a STARTED event" do
stub_probe_realtime_endpoint

@api_client.expect :report, {}, [Aikido::Zen::Events::Started]

@agent.start!
Expand All @@ -211,8 +121,6 @@ def stub_probe_realtime_endpoint
end

test "#start! takes the response of the STARTED event as runtime settings" do
stub_probe_realtime_endpoint

@api_client.expect :report,
{"configUpdatedAt" => 1234567890},
[Aikido::Zen::Events::Started]
Expand All @@ -238,8 +146,6 @@ def @api_client.report(event)
end

test "#start! starts polling for setting updates every minute" do
stub_probe_realtime_endpoint

@api_client.expect :should_fetch_settings?, false

assert_difference "@worker.jobs.size", +1 do
Expand All @@ -255,8 +161,6 @@ def @api_client.report(event)
end

test "#start! updates the runtime settings after polling if needed" do
stub_probe_realtime_endpoint

@api_client.expect :should_fetch_settings?, true
@api_client.expect :fetch_runtime_config, {"configUpdatedAt" => 1234567890}

Expand Down Expand Up @@ -437,8 +341,6 @@ def @api_client.report(event)
end

test "#start! queues a one-off tasks for each initial heartbeat delay" do
stub_probe_realtime_endpoint

size = @config.initial_heartbeat_delays.size

assert_difference "@worker.delayed.size", size do
Expand All @@ -454,8 +356,6 @@ def @api_client.report(event)
end

test "#start! successfully sends the initial heartbeats" do
stub_probe_realtime_endpoint

# Make sure there are _some_ stats
@collector.track_request

Expand Down
2 changes: 1 addition & 1 deletion test/aikido/zen/api_stream_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Aikido::Zen::StreamTest < ActiveSupport::TestCase
config = Aikido::Zen.config
config.api_token = "TOKEN"

@endpoint = "#{config.realtime_settings_updates_endpoint}/api/runtime/stream"
@endpoint = "#{config.realtime_endpoint}/api/runtime/stream"

@api_stream = Aikido::Zen::APIStream.new(
min_backoff: 0.02,
Expand Down
1 change: 0 additions & 1 deletion test/aikido/zen/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class Aikido::Zen::ConfigTest < ActiveSupport::TestCase
assert_equal [], @config.idor_excluded_table_names
assert_equal 1000, @config.idor_max_cache_entries
assert_equal false, @config.realtime_settings_updates_enabled?
assert_equal URI("https://zen.aikido.dev"), @config.realtime_settings_updates_endpoint
end

test "can set AIKIDO_DISABLE to configure if the agent should be turned off" do
Expand Down
Loading