好吧,看起来很安全,如果你的钥匙不在那里的话。可以肯定的是,您可以使用正则表达式匹配器来替换整个字符串,例如%r<#{keys['s3_key']:.*?=>
. 坏消息:没有 regexp filter_sensitive_data
。好消息:您可以使用更多低级方法来自己实现。
这是当前的实现filter_sensitive_data
# @param placeholder [String] The placeholder string.
# @param tag [Symbol] Set this to apply this only to cassettes
# with a matching tag; otherwise it will apply to every cassette.
# @yield block that determines what string to replace
# @yieldparam interaction [(optional) VCR::HTTPInteraction::HookAware] the HTTP interaction
# @yieldreturn the string to replace
def define_cassette_placeholder(placeholder, tag = nil, &block)
before_record(tag) do |interaction|
orig_text = call_block(block, interaction)
log "before_record: replacing #{orig_text.inspect} with #{placeholder.inspect}"
interaction.filter!(orig_text, placeholder)
end
before_playback(tag) do |interaction|
orig_text = call_block(block, interaction)
log "before_playback: replacing #{placeholder.inspect} with #{orig_text.inspect}"
interaction.filter!(placeholder, orig_text)
end
end
alias filter_sensitive_data define_cassette_placeholder
资源
这导致我们使用这些方法
# Replaces a string in any part of the HTTP interaction (headers, request body,
# response body, etc) with the given replacement text.
#
# @param [#to_s] text the text to replace
# @param [#to_s] replacement_text the text to put in its place
def filter!(text, replacement_text)
text, replacement_text = text.to_s, replacement_text.to_s
return self if [text, replacement_text].any? { |t| t.empty? }
filter_object!(self, text, replacement_text)
end
private
def filter_object!(object, text, replacement_text)
if object.respond_to?(:gsub)
object.gsub!(text, replacement_text) if object.include?(text)
elsif Hash === object
filter_hash!(object, text, replacement_text)
elsif object.respond_to?(:each)
# This handles nested arrays and structs
object.each { |o| filter_object!(o, text, replacement_text) }
end
object
end
资源
哦,好吧,我们可以试试猴子修补这个方法:
在您的 spec_helper 某处:
class VCR::HTTPInteraction::HookAware
def filter!(text, replacement_text)
replacement_text = replacement_text.to_s unless replacement_text.is_a?(Regexp)
text = text.to_s
return self if [text, replacement_text].any? { |t| t.empty? }
filter_object!(self, text, replacement_text)
end
end
当然,您可以选择不搞乱外星人库的深层内部结构,并且知道一些随机的字母数字数据被写入您的令牌附近的磁带(但不包括后者),不要感到太偏执。