Move upgrade path calculation into its own class

pull/620/head
Rick Carlino 2018-01-14 08:01:37 -06:00
parent 5c74d24697
commit e5469a8af2
6 changed files with 25 additions and 17 deletions

View File

@ -0,0 +1,16 @@
class CalculateUpgrade < Mutations::Command
# If version <= this, you can't just fast forward to the latest FBOS version.
FBOS_CUTOFF = Gem::Version.new("5.0.6")
# If you have a really, really old FBOS
OLD_OS_URL = "https://api.github.com/repos/farmbot/farmbot_os"+
"/releases/8772352"
OS_RELEASE = ENV.fetch("OS_UPDATE_SERVER") { DEFAULT_OS }
required do
model :version, class: Gem::Version
end
def execute
(version <= FBOS_CUTOFF) ? OLD_OS_URL : OS_RELEASE
end
end

View File

@ -4,7 +4,6 @@ class SessionToken < AbstractJwtToken
MUST_VERIFY = "Verify account first"
DEFAULT_OS = "https://api.github.com/repos/" \
"farmbot/farmbot_os/releases/latest"
OS_RELEASE = ENV.fetch("OS_UPDATE_SERVER") { DEFAULT_OS }
MQTT = ENV.fetch("MQTT_HOST")
# If you are not using the standard MQTT broker (eg: you use a 3rd party
# MQTT vendor), you will need to change this line.
@ -15,11 +14,6 @@ class SessionToken < AbstractJwtToken
end
EXPIRY = 40.days
VHOST = ENV.fetch("MQTT_VHOST") { "/" }
# If version <= this, you can't just fast forward to the latest FBOS version.
FBOS_CUTOFF = Gem::Version.new("5.0.6")
# If you have a really, really old FBOS
OLD_OS_URL = "https://api.github.com/repos/" +
"farmbot/farmbot_os/releases/8772352"
BETA_OS_URL = ENV["BETA_OTA_URL"] || "NOT_SET"
def self.issue_to(user,
iat: Time.now.to_i,
@ -32,7 +26,7 @@ class SessionToken < AbstractJwtToken
Rollbar.info("Verification Error", email: user.email)
raise Errors::Forbidden, MUST_VERIFY
end
url = (fbos_version <= FBOS_CUTOFF) ? OLD_OS_URL : OS_RELEASE
url = CalculateUpgrade.run!(version: fbos_version)
self.new([{ aud: aud,
sub: user.id,
iat: iat,

View File

@ -38,7 +38,6 @@ module FarmBot
[ENV["API_HOST"]] + (ENV["EXTRA_DOMAINS"] || "").split(",")
)
.map { |x| x.present? ? "#{x}:#{ENV["API_PORT"]}" : nil }.compact
puts ALL_LOCAL_URIS.inspect
SecureHeaders::Configuration.default do |config|
config.hsts = "max-age=#{1.week.to_i}"
config.x_frame_options = "DENY"

View File

@ -2,7 +2,6 @@ require_relative "./log_service_support"
begin
# Listen to all logs on the message broker and store them in the database.
puts "Starting device log service..."
Transport
.log_channel

View File

@ -44,12 +44,12 @@ describe SessionToken do
.unencoded[:os_update_server]
end
expect(test_case["0.0.0"]).to eq(SessionToken::OLD_OS_URL)
expect(test_case["5.0.5"]).to eq(SessionToken::OLD_OS_URL)
expect(test_case["5.0.6"]).to eq(SessionToken::OLD_OS_URL)
expect(test_case["5.0.7"]).to eq(SessionToken::OS_RELEASE)
expect(test_case["5.0.8"]).to eq(SessionToken::OS_RELEASE)
expect(test_case["5.1.0"]).to eq(SessionToken::OS_RELEASE)
expect(test_case["0.0.0"]).to eq(CalculateUpgrade::OLD_OS_URL)
expect(test_case["5.0.5"]).to eq(CalculateUpgrade::OLD_OS_URL)
expect(test_case["5.0.6"]).to eq(CalculateUpgrade::OLD_OS_URL)
expect(test_case["5.0.7"]).to eq(CalculateUpgrade::OS_RELEASE)
expect(test_case["5.0.8"]).to eq(CalculateUpgrade::OS_RELEASE)
expect(test_case["5.1.0"]).to eq(CalculateUpgrade::OS_RELEASE)
end
it "doesn't honor expired tokens" do

View File

@ -28,7 +28,7 @@ describe Auth::FromJWT do
.run!(credentials: creds, fbos_version: Gem::Version.new("999.9.9"))
expect(results[:token]).to be_kind_of(SessionToken)
expect(results[:user]).to eq(user)
expect(results[:token].unencoded[:os_update_server]).to eq(SessionToken::OS_RELEASE)
expect(results[:token].unencoded[:os_update_server]).to eq(CalculateUpgrade::OS_RELEASE)
end
it 'sometimes renders the legacy URL' do
@ -41,6 +41,6 @@ describe Auth::FromJWT do
expect(results[:token]).to be_kind_of(SessionToken)
expect(results[:user]).to eq(user)
expect(results[:token].unencoded[:os_update_server])
.to eq(SessionToken::OLD_OS_URL)
.to eq(CalculateUpgrade::OLD_OS_URL)
end
end