Skip to content

Commit

Permalink
#72 Add new option should_infer_types to optionally not infer the t…
Browse files Browse the repository at this point in the history
…ype of the secret
  • Loading branch information
rogerluan authored Aug 14, 2024
2 parents 90c20c3 + a58a397 commit 3a052ed
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 16 deletions.
34 changes: 21 additions & 13 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,35 @@ task default: %i[spec rubocop]

desc "Generates Swift source code and run its unit tests."
task :test_swift do
config_file = File.absolute_path("spec/fixtures/swift-tests.yml")
config_file_default = File.absolute_path("spec/fixtures/swift-tests.yml")
config_file_no_infer_types = File.absolute_path("spec/fixtures/swift-tests-with-no-infer-types.yml")
config_file_array = [config_file_default, config_file_no_infer_types]
dotenv_file = File.absolute_path("spec/fixtures/.env.fruitloops")
with_temp_dir do |temp_dir|
puts "Current working directory: #{temp_dir}"
sh("ARKANA_RUNNING_CI_INTEGRATION_TESTS=true arkana --config-filepath #{config_file} --dotenv-filepath #{dotenv_file} --include-environments dev,staging")
Dir.chdir("tests/MySecrets")
sh("swift test")
config_file_array.each do |config_file|
with_temp_dir do |temp_dir|
puts "Current working directory: #{temp_dir}"
sh("ARKANA_RUNNING_CI_INTEGRATION_TESTS=true arkana --config-filepath #{config_file} --dotenv-filepath #{dotenv_file} --include-environments dev,staging")
Dir.chdir("tests/MySecrets")
sh("swift test")
end
end
end

desc "Generates Kotlin source code and run its unit tests."
task :test_kotlin do
config_file = File.absolute_path("spec/fixtures/kotlin-tests.yml")
config_file_default = File.absolute_path("spec/fixtures/kotlin-tests.yml")
config_file_no_infer_types = File.absolute_path("spec/fixtures/kotlin-tests-with-no-infer-types.yml")
config_file_array = [config_file_default, config_file_no_infer_types]
dotenv_file = File.absolute_path("spec/fixtures/.env.fruitloops")
directory_to_copy = File.absolute_path("spec/fixtures/kotlin")
with_temp_dir do |temp_dir|
puts "Current working directory: #{temp_dir}"
FileUtils.copy_entry(directory_to_copy, "tests")
sh("ARKANA_RUNNING_CI_INTEGRATION_TESTS=true arkana --lang kotlin --config-filepath #{config_file} --dotenv-filepath #{dotenv_file} --include-environments dev,staging")
Dir.chdir("tests")
sh("./gradlew test")
config_file_array.each do |config_file|
with_temp_dir do |temp_dir|
puts "Current working directory: #{temp_dir}"
FileUtils.copy_entry(directory_to_copy, "tests")
sh("ARKANA_RUNNING_CI_INTEGRATION_TESTS=true arkana --lang kotlin --config-filepath #{config_file} --dotenv-filepath #{dotenv_file} --include-environments dev,staging")
Dir.chdir("tests")
sh("./gradlew test")
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/arkana.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ def self.run(arguments)
salt: salt,
current_flavor: config.current_flavor,
environments: config.environments,
should_infer_types: config.should_infer_types,
)
global_secrets = Encoder.encode!(
keys: config.global_secrets,
salt: salt,
current_flavor: config.current_flavor,
environments: config.environments,
should_infer_types: config.should_infer_types,
)
rescue StandardError => e
# TODO: Improve this by creating an Env/Debug helper
Expand Down
4 changes: 2 additions & 2 deletions lib/arkana/encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ module Encoder
# Fetches values of each key from ENV, and encodes them using the given salt.
#
# @return [Secret[]] an array of Secret objects, which contain their keys and encoded values.
def self.encode!(keys:, salt:, current_flavor:, environments:)
def self.encode!(keys:, salt:, current_flavor:, environments:, should_infer_types:)
keys.map do |key|
secret = find_secret!(key: key, current_flavor: current_flavor)
encoded_value = encode(secret, salt.raw)
secret_type = Type.new(string_value: secret)
secret_type = should_infer_types ? Type.new(string_value: secret) : Type.default_type
protocol_key = protocol_key(key: key, environments: environments)
Secret.new(key: key, protocol_key: protocol_key, encoded_value: encoded_value, type: secret_type)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/arkana/models/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Config
attr_reader :kotlin_jvm_toolchain_version
# @returns [boolean]
attr_reader :is_kotlin_multiplatform_module
# @returns [boolean]
attr_reader :should_infer_types

# @returns [string]
attr_accessor :current_flavor
Expand Down Expand Up @@ -68,6 +70,8 @@ def initialize(yaml)
@kotlin_jvm_toolchain_version = yaml["kotlin_jvm_toolchain_version"] || 11
@is_kotlin_multiplatform_module = yaml["is_kotlin_multiplatform_module"]
@is_kotlin_multiplatform_module = false if @should_generate_gradle_build_file.nil?
@should_infer_types = yaml["should_infer_types"]
@should_infer_types = true if @should_infer_types.nil?
end
# rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

Expand Down
5 changes: 5 additions & 0 deletions lib/arkana/models/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ def self.new(string_value:)
STRING
end
end

# The default type to use when we shouldn't be inferring types.
def self.default_type
STRING
end
end
3 changes: 2 additions & 1 deletion spec/encoder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

RSpec.describe Encoder do
subject { described_class.encode!(keys: keys, salt: salt, current_flavor: current_flavor, environments: environments) }
subject { described_class.encode!(keys: keys, salt: salt, current_flavor: current_flavor, environments: environments, should_infer_types: should_infer_types) }

let(:salt) { SaltGenerator.generate }
let(:environments) { [] }
let(:current_flavor) { nil }
let(:should_infer_types) { true }

describe ".encode!" do
context "when keys is empty" do
Expand Down
32 changes: 32 additions & 0 deletions spec/fixtures/kotlin-tests-with-no-infer-types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace: 'MySecrets'
result_path: 'tests'
should_generate_unit_tests: true
should_generate_gradle_build_file: false
global_secrets:
- BoolAsStringTrueKey
- BoolAsStringFalseKey
- BoolAsBoolTrueKey
- BoolAsBoolFalseKey
- IntAsStringKey
- IntAsNumberKey
- IntWithLeadingZeroesAsStringKey
- IntWithLeadingZeroesAsNumberKey
- MassiveIntAsStringKey
- MassiveIntAsNumberKey
- NegativeIntAsStringKey
- NegativeIntAsNumberKey
- FloatAsStringKey
- FloatAsNumberKey
- SecretWithDollarSignEscapedAndAndNoQuotesKey
- SecretWithDollarSignEscapedAndDoubleQuoteKey
- SecretWithDollarSignNotEscapedAndSingleQuoteKey
- SecretWithDollarSignNotEscapedAndDoubleQuotesKey
- SecretWithDollarSignNotEscapedAndNoQuotesKey
- SecretWithWeirdCharactersKey
environments:
- dev
- staging
- prod
environment_secrets:
- ServiceKey
- Server
36 changes: 36 additions & 0 deletions spec/fixtures/swift-tests-with-no-infer-types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import_name: 'MySecrets'
namespace: 'MySecrets'
pod_name: 'MySecrets'
result_path: 'tests'
swift_declaration_strategy: lazy var
should_generate_unit_tests: true
should_inter_types: false
package_manager: spm
global_secrets:
- BoolAsStringTrueKey
- BoolAsStringFalseKey
- BoolAsBoolTrueKey
- BoolAsBoolFalseKey
- IntAsStringKey
- IntAsNumberKey
- IntWithLeadingZeroesAsStringKey
- IntWithLeadingZeroesAsNumberKey
- MassiveIntAsStringKey
- MassiveIntAsNumberKey
- NegativeIntAsStringKey
- NegativeIntAsNumberKey
- FloatAsStringKey
- FloatAsNumberKey
- SecretWithDollarSignEscapedAndAndNoQuotesKey
- SecretWithDollarSignEscapedAndDoubleQuoteKey
- SecretWithDollarSignNotEscapedAndSingleQuoteKey
- SecretWithDollarSignNotEscapedAndDoubleQuotesKey
- SecretWithDollarSignNotEscapedAndNoQuotesKey
- SecretWithWeirdCharactersKey
environments:
- dev
- staging
- prod
environment_secrets:
- ServiceKey
- Server
2 changes: 2 additions & 0 deletions spec/kotlin_code_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
salt: salt,
current_flavor: config.current_flavor,
environments: config.environments,
should_infer_types: config.should_infer_types,
)
end

Expand All @@ -18,6 +19,7 @@
salt: salt,
current_flavor: config.current_flavor,
environments: config.environments,
should_infer_types: config.should_infer_types,
)
end

Expand Down
2 changes: 2 additions & 0 deletions spec/models/template_arguments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
salt: salt,
current_flavor: config.current_flavor,
environments: config.environments,
should_infer_types: config.should_infer_types,
)
end

Expand All @@ -27,6 +28,7 @@
salt: salt,
current_flavor: config.current_flavor,
environments: config.environments,
should_infer_types: config.should_infer_types,
)
end

Expand Down
2 changes: 2 additions & 0 deletions spec/swift_code_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
salt: salt,
current_flavor: config.current_flavor,
environments: config.environments,
should_infer_types: config.should_infer_types,
)
end

Expand All @@ -18,6 +19,7 @@
salt: salt,
current_flavor: config.current_flavor,
environments: config.environments,
should_infer_types: config.should_infer_types,
)
end

Expand Down
1 change: 1 addition & 0 deletions template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ kotlin_sources_path: 'java' # Optional. The path for the generated Kotlin classe
should_generate_gradle_build_file: true # Optional. Whether a build.gradle file should be generated, when running the Kotlin generator. One of: true, false. Defaults to true.
is_kotlin_multiplatform_module: true # Optional. Whether the generated Kotlin module is a multiplatform module. One of: true, false. Defaults to false.
kotlin_jvm_toolchain_version: 11 # Optional. The kotlin JVM toolchain JDK version to be used in the generated build.gradle file. Defaults to 11.
should_infer_types: true # Optional. Whether type inference should be done based on the values of the secrets. When false, the type will always be string for all languages. One of: true, false. Defaults to true.
environments: # Optional. List of environments that will be used to generate secret keys when you have keys that are different between environments (e.g. debug/staging/prod). Defaults to empty.
- Debug
- Release
Expand Down

0 comments on commit 3a052ed

Please sign in to comment.