Skip to content

Commit

Permalink
Merge pull request #14889 from mkanoor/bad_credentials
Browse files Browse the repository at this point in the history
Use credential callback to set credentials
(cherry picked from commit 2dd64f8)

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1568603
Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1568605
  • Loading branch information
gmcculloug authored and simaishi committed Apr 17, 2018
1 parent 3d38541 commit 6835b92
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/models/git_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def repo_params
params = {:path => directory_name}
params[:certificate_check] = method(:self_signed_cert_cb) if verify_ssl == OpenSSL::SSL::VERIFY_NONE
if authentications.any?
params[:username] = authentications.first.userid
params[:password] = authentications.first.password
params[:username] = default_authentication.userid
params[:password] = default_authentication.password
end
params
end
Expand Down
16 changes: 13 additions & 3 deletions lib/git_worktree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def initialize(options = {})
@remote_name = 'origin'
@cred = Rugged::Credentials::UserPassword.new(:username => @username,
:password => @password)
@credentials_set = false
@base_name = File.basename(@path)
@certificate_check_cb = options[:certificate_check]
process_repo(options)
Expand Down Expand Up @@ -166,6 +167,12 @@ def mv_dir(old_dir, new_dir)
current_index.remove_dir(old_dir)
end

def credentials_cb(url, _username, _types)
raise GitWorktreeException::InvalidCredentials, "Invalid credentials for URL #{url}" if @credentials_set
@credentials_set = true
@cred
end

private

def current_branch
Expand All @@ -187,7 +194,8 @@ def fetch_and_merge
end

def fetch
options = {:credentials => @cred, :certificate_check => @certificate_check_cb}
@credentials_set = false
options = {:credentials => method(:credentials_cb), :certificate_check => @certificate_check_cb}
@repo.fetch(@remote_name, options)
end

Expand All @@ -201,7 +209,8 @@ def merge_and_push(commit)
@saved_cid = @repo.ref(local_ref).target.oid
merge(commit, rebase)
rebase = true
@repo.push(@remote_name, [local_ref], :credentials => @cred)
@credentials_set = false
@repo.push(@remote_name, [local_ref], :credentials => method(:credentials_cb))
end
end

Expand Down Expand Up @@ -254,7 +263,8 @@ def open_repo
end

def clone(url)
options = {:credentials => @cred, :bare => true, :remote => @remote_name, :certificate_check => @certificate_check_cb}
@credentials_set = false
options = {:credentials => method(:credentials_cb), :bare => true, :remote => @remote_name, :certificate_check => @certificate_check_cb}
@repo = Rugged::Repository.clone_at(url, @path, options)
end

Expand Down
1 change: 1 addition & 0 deletions lib/git_worktree_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ class GitRepositoryMissing < RuntimeError; end
class DirectoryAlreadyExists < RuntimeError; end
class BranchMissing < RuntimeError; end
class TagMissing < RuntimeError; end
class InvalidCredentials < RuntimeError; end
end
12 changes: 12 additions & 0 deletions spec/lib/git_worktree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,16 @@ def open_existing_repo
end
end
end

describe 'credentials_cb' do
let(:git_repo_path) { Rails.root.join("spec", "fixtures", "git_repos", "branch_and_tag.git") }
let(:test_repo) { GitWorktree.new(:path => git_repo_path.to_s) }

describe "#credentials_cb" do
it "call the credentials callback" do
expect(test_repo.credentials_cb("url", nil, nil).class).to eq(Rugged::Credentials::UserPassword)
expect { test_repo.credentials_cb("url", nil, nil) }.to raise_exception(GitWorktreeException::InvalidCredentials)
end
end
end
end
3 changes: 3 additions & 0 deletions spec/models/git_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@
it "userid and password is set" do
repo.update_authentication(:default => {:userid => userid, :password => password})
expect(GitWorktree).to receive(:new).with(args).and_return(gwt)

repo.refresh
expect(repo.default_authentication.userid).to eq(userid)
expect(repo.default_authentication.password).to eq(password)
end

context "self signed certifcate" do
Expand Down

0 comments on commit 6835b92

Please sign in to comment.