Skip to content

Commit

Permalink
Add feature to hide sensitive data in logs (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangarha authored Dec 12, 2023
1 parent d5b1ae2 commit 6f5acc3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/core/controlplane.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ def profile_exists?(profile)
end

def profile_create(profile, token)
sensitive_data_pattern = /(?<=--token )(\S+)/
cmd = "cpln profile create #{profile} --token #{token}"
cmd += " > /dev/null" if Shell.should_hide_output?
perform!(cmd)
perform!(cmd, sensitive_data_pattern: sensitive_data_pattern)
end

def profile_delete(profile)
Expand Down Expand Up @@ -346,8 +347,8 @@ def perform(cmd)
system(cmd)
end

def perform!(cmd)
Shell.debug("CMD", cmd)
def perform!(cmd, sensitive_data_pattern: nil)
Shell.debug("CMD", cmd, sensitive_data_pattern: sensitive_data_pattern)

system(cmd) || exit(false)
end
Expand Down
26 changes: 24 additions & 2 deletions lib/core/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,33 @@ def self.verbose_mode(verbose)
@verbose = verbose
end

def self.debug(prefix, message)
stderr.puts("\n[#{color(prefix, :red)}] #{message}") if verbose
def self.debug(prefix, message, sensitive_data_pattern: nil)
return unless verbose

filtered_message = hide_sensitive_data(message, sensitive_data_pattern)
stderr.puts("\n[#{color(prefix, :red)}] #{filtered_message}")
end

def self.should_hide_output?
tmp_stderr && !verbose
end

#
# Hide sensitive data based on the passed pattern
#
# @param [String] message
# The message to get processed.
# @param [Regexp, nil] pattern
# The regular expression to be used. If not provided, no filter gets applied.
#
# @return [String]
# Filtered message.
#
# @example
# hide_sensitive_data("--token abcd", /(?<=--token )(\S+)/)
def self.hide_sensitive_data(message, pattern = nil)
return message unless pattern.is_a?(Regexp)

message.gsub(pattern, "XXXXXXX")
end
end

0 comments on commit 6f5acc3

Please sign in to comment.