Skip to content

Commit

Permalink
use URI to parse git repository url, rename filter_sensitive_info to …
Browse files Browse the repository at this point in the history
…filter_basic_auth
  • Loading branch information
anmarchenko committed Feb 15, 2024
1 parent 98ec040 commit 8fdb976
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/core/environment/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Git
def git_repository_url
return @git_repository_url if defined?(@git_repository_url)

@git_repository_url = Utils::Url.filter_sensitive_info(ENV[Datadog::Core::Git::Ext::ENV_REPOSITORY_URL])
@git_repository_url = Utils::Url.filter_basic_auth(ENV[Datadog::Core::Git::Ext::ENV_REPOSITORY_URL])
end

def git_commit_sha
Expand Down
12 changes: 10 additions & 2 deletions lib/datadog/core/utils/url.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
# frozen_string_literal: true

require 'uri'

module Datadog
module Core
module Utils
# Helpers class that provides methods to process URLs
# such as filtering sensitive information.
module Url
def self.filter_sensitive_info(url)
def self.filter_basic_auth(url)
return nil if url.nil?

url.gsub(%r{((https?|ssh)://)[^/]*@}, '\1')
URI(url).tap do |u|
u.user = nil
u.password = nil
end.to_s
# Git scheme: git@github.com:DataDog/dd-trace-rb.git
rescue URI::InvalidURIError
url
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/core/utils/url.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Datadog
module Core
module Utils
module Url
def self?.filter_sensitive_info: (::String? url) -> ::String?
def self?.filter_basic_auth: (::String? url) -> ::String?
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/datadog/core/utils/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require 'datadog/core/utils/url'

RSpec.describe Datadog::Core::Utils::Url do
describe '.filter_sensitive_info' do
subject(:filtered_url) { described_class.filter_sensitive_info(url) }
describe '.filter_basic_auth' do
subject(:filtered_url) { described_class.filter_basic_auth(url) }

context 'with https' do
context 'with username and password' do
Expand Down

0 comments on commit 8fdb976

Please sign in to comment.