Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to hide sensitive data in logs #115

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 23 additions & 2 deletions lib/core/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,32 @@ 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)
filtered_message = hide_sensitive_data(message, sensitive_data_pattern)
ahangarha marked this conversation as resolved.
Show resolved Hide resolved

stderr.puts("\n[#{color(prefix, :red)}] #{filtered_message}") if verbose
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