Skip to content

Instantly share code, notes, and snippets.

@hjanuschka
Last active December 7, 2016 14:08
Show Gist options
  • Save hjanuschka/fa7241a2462d36cb35ded66021188604 to your computer and use it in GitHub Desktop.
Save hjanuschka/fa7241a2462d36cb35ded66021188604 to your computer and use it in GitHub Desktop.
fastlane_anomizer.rb
require 'parser'
require 'parser/current'
require "fastlane"
# BOOTSTRAP a fastlane run
#
#
$verbose = true
Helper = FastlaneCore::Helper # you gotta love Ruby: Helper.* should use the Helper class contained in FastlaneCore
UI = FastlaneCore::UI
ROOT = Pathname.new(File.expand_path('../..', __FILE__))
Fastlane::Actions.load_default_actions
Fastlane::Actions.load_helpers
if FastlaneCore::FastlaneFolder.path
actions_path = File.join(FastlaneCore::FastlaneFolder.path, 'actions')
Fastlane::Actions.load_external_actions(actions_path) if File.directory?(actions_path)
end
module FastlaneCore
class FastfileAnonmizer
attr_accessor :original_action
def to_s
"REMOVE_IT"
end
def fake_action(*args)
return if args.length <= 0
return if @original_action.nil?
UI.important("ACTION: #{@original_action.inspect} 1") if $verbose
UI.important("PARAMS: #{args.inspect}") if $verbose
# FIXME: - ALIASES
a = Fastlane::Actions.action_class_ref(@original_action.to_sym)
return unless a
options_avail = a.available_options
return if options_avail.nil?
options_avail.each do |o|
next unless o.sensitive
self.class.secrets << args.first[o.key.to_sym].to_s if args.first[o.key.to_sym] && !self.class.secrets.include?(args.first[o.key.to_sym].to_s)
UI.important("AX - #{@original_action.to_sym}: #{@action_vars.inspect}") if $verbose
@action_vars.each do |e|
self.class.secrets << e if !self.class.secrets.include?(e)
end
end
end
def self.secrets
unless @secrets
@secrets = []
end
@secrets
end
def dummy()
FastlaneCore::FastfileAnonmizer.new("")
end
def method_missing(sym, *args, &block)
UI.important("CHECK #{sym} - #{args.inspect} - #{block.inspect}") if $verbose
return "dummy" if sym.to_s == "to_str"
dummy()
end
def initialize(filename)
@ast = parse(filename)
end
def find_actions
recursive_find_actions(@ast)
end
def find(method_name)
recursive_search_ast(@ast, method_name)
return @method_source
end
private
def parse(data)
Parser::CurrentRuby.parse(data)
end
def recursive_find_actions(ast)
ast.children.each do |child|
next unless child.class.to_s == "Parser::AST::Node"
# FIXME aliases
if (child.type.to_s == "send") and ( child.children[0].to_s == "" && Fastlane::Actions.action_class_ref(child.children[1].to_s))
src_code = child.loc.expression.source
src_code.gsub!(child.children[1].to_s, "fake_action")
#matches = src_code.gsub!(/#\{.*\}/) do |sym|
# self.class.secrets << sym if !self.class.secrets.include?(sym)
# "########"
#end
copy_code = src_code.clone
@action_vars = []
src_code.scan(%r(#\{.*?\})m) { |mtch|
# Remove #{} vars - so that there are now accidentalliy replaced ones
@action_vars << mtch if !self.class.secrets.include?(mtch)
#copy_code.gsub!(mtch.first, "'#######'")
}
src_code = copy_code
# FIXME: - maybe do in a thread to be safe!
@original_action = child.children[1].to_s
dropper = '
'
UI.important(src_code) if $verbose
begin
result = eval(dropper + src_code)
rescue => ex
UI.important("PARSE ERROR") if $verbose
UI.important("Exception: #{ex}") if $verbose
end
else
recursive_find_actions(child)
end
end
end
end
end
fl_content = File.open("fastlane/fastlane/Fastfile", "r").read
fa = FastlaneCore::FastfileAnonmizer.new(fl_content)
fa.find_actions
UI.important "Found secrets: #{FastlaneCore::FastfileAnonmizer.secrets.inspect}" if $verbose
FastlaneCore::FastfileAnonmizer.secrets.compact.each do | secret |
fl_content.gsub!(secret, "##########")
end
UI.message(fl_content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment