travis-api/lib/travis/model/build/config/obfuscate.rb
Aakriti Gupta 65f1a29d86 Move travis-core files from /vendor to /lib.
- Re-factor
- Remove code for notifications
- Remove addons
- Remove travis-core gem.
- Ignore logs directory only
- Move core tests to spec/lib
2016-07-20 11:22:25 +02:00

54 lines
1.3 KiB
Ruby

require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/array/wrap'
require 'travis/secure_config'
class Build
class Config
class Obfuscate < Struct.new(:config, :options)
ENV_VAR_PATTERN = /(?<=\=)(?:(?<q>['"]).*?[^\\]\k<q>|(.*?)(?= \w+=|$))/
def run
config = self.config.except(:source_key)
config[:env] = obfuscate(config[:env]) if config[:env]
config
end
private
def obfuscate(env)
Array.wrap(env).map do |value|
obfuscate_values(value).join(' ')
end
end
def obfuscate_values(values)
Array.wrap(values).compact.map do |value|
obfuscate_value(value)
end
end
def obfuscate_value(value)
secure.decrypt(value) do |decrypted|
obfuscate_env_vars(decrypted)
end
end
def obfuscate_env_vars(line)
if line.respond_to?(:gsub)
line.gsub(ENV_VAR_PATTERN) { |val| '[secure]' }
else
'[One of the secure variables in your .travis.yml has an invalid format.]'
end
end
def secure
@secure ||= Travis::SecureConfig.new(key)
end
def key
@key ||= options[:key_fetcher].call
end
end
end
end