Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add Credentials::SshKeyMemory
  • Loading branch information
tjk committed Mar 23, 2023
commit e67ea2dd96cdd037a15e4c742d4e19011b1d4698
37 changes: 37 additions & 0 deletions ext/rugged/rugged_cred.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ VALUE rb_mRuggedCred;
VALUE rb_cRuggedCredUserPassword;
VALUE rb_cRuggedCredSshKey;
VALUE rb_cRuggedCredSshKeyFromAgent;
VALUE rb_cRuggedCredSshKeyMemory;
VALUE rb_cRuggedCredDefault;

static void rugged_cred_extract_userpass(git_cred **cred, VALUE rb_credential)
Expand Down Expand Up @@ -66,6 +67,31 @@ static void rugged_credential_extract_ssh_key_from_agent(git_cred **cred, VALUE
);
}

static void rugged_credential_extract_ssh_key_memory(git_cred **cred, VALUE rb_credential)
{
VALUE rb_username = rb_iv_get(rb_credential, "@username");
VALUE rb_publickey = rb_iv_get(rb_credential, "@publickey");
VALUE rb_privatekey = rb_iv_get(rb_credential, "@privatekey");
VALUE rb_passphrase = rb_iv_get(rb_credential, "@passphrase");

Check_Type(rb_username, T_STRING);
Check_Type(rb_privatekey, T_STRING);

if (!NIL_P(rb_publickey))
Check_Type(rb_publickey, T_STRING);
if (!NIL_P(rb_passphrase))
Check_Type(rb_passphrase, T_STRING);

rugged_exception_check(
git_cred_ssh_key_memory_new(cred,
StringValueCStr(rb_username),
NIL_P(rb_publickey) ? NULL : StringValueCStr(rb_publickey),
StringValueCStr(rb_privatekey),
NIL_P(rb_passphrase) ? NULL : StringValueCStr(rb_passphrase)
)
);
}

static void rugged_cred_extract_default(git_cred **cred, VALUE rb_credential)
{
rugged_exception_check(git_cred_default_new(cred));
Expand Down Expand Up @@ -111,6 +137,16 @@ void rugged_cred_extract(git_cred **cred, int allowed_types, VALUE rb_credential
rb_raise(rb_eArgError, "Invalid credential type");

rugged_credential_extract_ssh_key_from_agent(cred, rb_credential);
} else if (rb_obj_is_kind_of(rb_credential, rb_cRuggedCredSshKeyMemory)) {
if (allowed_types & GIT_CREDTYPE_USERNAME) {
rugged_cred_extract_username(cred, rb_credential);
return;
}

if (!(allowed_types & GIT_CREDTYPE_SSH_KEY))
rb_raise(rb_eArgError, "Invalid credential type");

rugged_credential_extract_ssh_key_memory(cred, rb_credential);
} else if (rb_obj_is_kind_of(rb_credential, rb_cRuggedCredDefault)) {
if (!(allowed_types & GIT_CREDTYPE_DEFAULT))
rb_raise(rb_eArgError, "Invalid credential type");
Expand All @@ -127,5 +163,6 @@ void Init_rugged_cred(void)
rb_cRuggedCredUserPassword = rb_define_class_under(rb_mRuggedCred, "UserPassword", rb_cObject);
rb_cRuggedCredSshKey = rb_define_class_under(rb_mRuggedCred, "SshKey", rb_cObject);
rb_cRuggedCredSshKeyFromAgent = rb_define_class_under(rb_mRuggedCred, "SshKeyFromAgent", rb_cObject);
rb_cRuggedCredSshKeyMemory = rb_define_class_under(rb_mRuggedCred, "SshKeyMemory", rb_cObject);
rb_cRuggedCredDefault = rb_define_class_under(rb_mRuggedCred, "Default", rb_cObject);
}
11 changes: 11 additions & 0 deletions lib/rugged/credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ def call(url, username_from_url, allowed_types)
end
end

# A ssh key credential object that can optionally be passphrase-protected (from memory)
class SshKeyMemory
def initialize(options)
@username, @publickey, @privatekey, @passphrase = options[:username], options[:publickey], options[:privatekey], options[:passphrase]
end

def call(url, username_from_url, allowed_types)
self
end
end

# A "default" credential usable for Negotiate mechanisms like NTLM or
# Kerberos authentication
class Default
Expand Down
10 changes: 10 additions & 0 deletions test/online/fetch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ def test_fetch_over_ssh_with_credentials
})
end

def test_fetch_over_ssh_with_credentials_memory
skip unless Rugged.features.include?(:ssh) && ssh_creds?

@repo.remotes.create("origin", ENV['GITTEST_REMOTE_SSH_URL'])

@repo.fetch("origin", **{
credentials: ssh_key_credential_memory
})
end

def test_fetch_over_ssh_with_credentials_from_agent
skip unless Rugged.features.include?(:ssh) && ssh_creds?

Expand Down
9 changes: 9 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ def ssh_key_credential
})
end

def ssh_key_credential_memory
Rugged::Credentials::SshKeyMemory.new({
username: ENV["GITTEST_REMOTE_SSH_USER"],
publickey: File.read(ENV["GITTEST_REMOTE_SSH_PUBKEY"]),
privatekey: File.read(ENV["GITTEST_REMOTE_SSH_KEY"]),
passphrase: ENV["GITTEST_REMOTE_SSH_PASSPHASE"],
})
end

def ssh_key_credential_from_agent
Rugged::Credentials::SshKeyFromAgent.new({
username: ENV["GITTEST_REMOTE_SSH_USER"]
Expand Down