Skip to content

fix(datafile-parsing): Prevent newer versions datafile #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 5, 2018
Merged
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
14 changes: 5 additions & 9 deletions lib/optimizely.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,13 @@ def initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = n

begin
@config = ProjectConfig.new(datafile, @logger, @error_handler)
rescue
rescue StandardError => e
@is_valid = false
@logger = SimpleLogger.new
@logger.log(Logger::ERROR, InvalidInputError.new('datafile').message)
return
end

unless @config.parsing_succeeded?
@is_valid = false
@logger = SimpleLogger.new
@logger.log(Logger::ERROR, InvalidDatafileVersionError.new.message)
error_msg = e.class == InvalidDatafileVersionError ? e.message : InvalidInputError.new('datafile').message
error_to_handle = e.class == InvalidDatafileVersionError ? InvalidDatafileVersionError : InvalidInputError
@logger.log(Logger::ERROR, error_msg)
@error_handler.handle_error error_to_handle
return
end

Expand Down
7 changes: 3 additions & 4 deletions lib/optimizely/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2016-2017, Optimizely and contributors
# Copyright 2016-2018, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,9 +85,8 @@ def initialize(aborted_method)
class InvalidDatafileVersionError < Error
# Raised when a datafile with an unsupported version is provided

def initialize(msg = 'Provided datafile is an unsupported version. Please use SDK version 1.1.2 or earlier '\
'for datafile version 1.')
super
def initialize(version)
super("This version of the Ruby SDK does not support the given datafile version: #{version}.")
end
end

Expand Down
6 changes: 6 additions & 0 deletions lib/optimizely/helpers/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ module Constants
'BUCKETING_ID' => '$opt_bucketing_id',
'USER_AGENT' => '$opt_user_agent'
}.freeze

SUPPORTED_VERSIONS = {
'v2' => '2',
'v3' => '3',
'v4' => '4'
}.freeze
end
end
end
17 changes: 1 addition & 16 deletions lib/optimizely/project_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
require_relative 'helpers/validator'

module Optimizely
V1_CONFIG_VERSION = '1'

UNSUPPORTED_VERSIONS = [V1_CONFIG_VERSION].freeze

class ProjectConfig
# Representation of the Optimizely project config.
RUNNING_EXPERIMENT_STATUS = ['Running'].freeze
Expand All @@ -39,7 +35,6 @@ class ProjectConfig
attr_reader :experiments
attr_reader :feature_flags
attr_reader :groups
attr_reader :parsing_succeeded
attr_reader :project_id
# Boolean - denotes if Optimizely should remove the last block of visitors' IP address before storing event data
attr_reader :anonymize_ip
Expand Down Expand Up @@ -75,12 +70,11 @@ def initialize(datafile, logger, error_handler)

config = JSON.parse(datafile)

@parsing_succeeded = false
@error_handler = error_handler
@logger = logger
@version = config['version']

return if UNSUPPORTED_VERSIONS.include?(@version)
raise InvalidDatafileVersionError, @version unless Helpers::Constants::SUPPORTED_VERSIONS.value?(@version)

@account_id = config['accountId']
@attributes = config.fetch('attributes', [])
Expand Down Expand Up @@ -147,7 +141,6 @@ def initialize(datafile, logger, error_handler)
@feature_flag_key_map.each do |key, feature_flag|
@feature_variable_key_map[key] = generate_key_map(feature_flag['variables'], 'key')
end
@parsing_succeeded = true
end

def experiment_running?(experiment)
Expand Down Expand Up @@ -389,14 +382,6 @@ def get_attribute_id(attribute_key)
nil
end

def parsing_succeeded?
# Helper method to determine if parsing the datafile was successful.
#
# Returns Boolean depending on whether parsing the datafile succeeded or not.

@parsing_succeeded
end

def variation_id_exists?(experiment_id, variation_id)
# Determines if a given experiment ID / variation ID pair exists in the datafile
#
Expand Down
11 changes: 0 additions & 11 deletions spec/project_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
expect(project_config.groups).to eq(config_body['groups'])
expect(project_config.project_id).to eq(config_body['projectId'])
expect(project_config.revision).to eq(config_body['revision'])
expect(project_config.parsing_succeeded).to be(true)

expected_attribute_key_map = {
'browser_type' => config_body['attributes'][0]
Expand Down Expand Up @@ -659,16 +658,6 @@
end
end

describe 'parsing_succeeded?' do
let(:config_body_v2) { OptimizelySpec::VALID_CONFIG_BODY }
let(:config_body_v2_JSON) { OptimizelySpec::VALID_CONFIG_BODY_JSON }

it 'should be true for version 2' do
project_config_v2 = Optimizely::ProjectConfig.new(config_body_v2_JSON, logger, error_handler)
expect(project_config_v2.parsing_succeeded?).to be(true)
end
end

describe '@logger' do
let(:spy_logger) { spy('logger') }
let(:config) { Optimizely::ProjectConfig.new(config_body_JSON, spy_logger, error_handler) }
Expand Down
25 changes: 19 additions & 6 deletions spec/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ def handle_error(error)
expect(instance_with_error_handler.error_handler.handle_error('test_message')). to eq('test_message')
end

it 'should log an error when datafile is null' do
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.')
Optimizely::Project.new(nil)
end

it 'should log an error when datafile is empty' do
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.')
Optimizely::Project.new('')
end

it 'should log an error when given a datafile that does not conform to the schema' do
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.')
Optimizely::Project.new('{"foo": "bar"}')
Expand Down Expand Up @@ -100,22 +110,25 @@ class InvalidErrorHandler; end
Optimizely::Project.new(config_body_JSON, nil, nil, nil, true)
end

it 'should log an error when provided a datafile that is not JSON and skip_json_validation is true' do
it 'should log and raise an error when provided a datafile that is not JSON and skip_json_validation is true' do
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.')
expect_any_instance_of(Optimizely::RaiseErrorHandler).to receive(:handle_error).once.with(Optimizely::InvalidInputError)

Optimizely::Project.new('this is not JSON', nil, nil, nil, true)
Optimizely::Project.new('this is not JSON', nil, nil, Optimizely::RaiseErrorHandler.new, true)
end

it 'should log an error when provided an invalid JSON datafile and skip_json_validation is true' do
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.')

Optimizely::Project.new('{"foo": "bar"}', nil, nil, nil, true)
Optimizely::Project.new('{"version": "2", "foo": "bar"}', nil, nil, nil, true)
end

it 'should log an error when provided a datafile of unsupported version' do
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is an unsupported version. Please use SDK version 1.1.2 or earlier for datafile version 1.')
it 'should log and raise an error when provided a datafile of unsupported version' do
config_body_invalid_json = JSON.parse(config_body_invalid_JSON)
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, "This version of the Ruby SDK does not support the given datafile version: #{config_body_invalid_json['version']}.")
expect_any_instance_of(Optimizely::RaiseErrorHandler).to receive(:handle_error).once.with(Optimizely::InvalidDatafileVersionError)

Optimizely::Project.new(config_body_invalid_JSON, nil, nil, nil, true)
Optimizely::Project.new(config_body_invalid_JSON, nil, nil, Optimizely::RaiseErrorHandler.new, true)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,6 @@ module OptimizelySpec
VALID_CONFIG_BODY_JSON = JSON.dump(VALID_CONFIG_BODY)

INVALID_CONFIG_BODY = VALID_CONFIG_BODY.dup
INVALID_CONFIG_BODY['version'] = '1'
INVALID_CONFIG_BODY['version'] = '5'
INVALID_CONFIG_BODY_JSON = JSON.dump(INVALID_CONFIG_BODY)
end