Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.bundle
*.so
*.o
*.a
mkmf.log

spec/examples.txt
spec/known_hosts.valid
spec/known_hosts.invalid
/spec/docker.log
/spec/examples.txt
/spec/known_hosts.valid
/spec/known_hosts.invalid
/spec/reports/
2 changes: 1 addition & 1 deletion ext/libssh_ruby/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
if ENV['LIBSSH_CFLAGS']
$CFLAGS = ENV['LIBSSH_CFLAGS']
else
$CFLAGS << ' -Wall -W -Wno-deprecated-declarations -Wno-missing-field-initializers'
$CFLAGS << ' -Wall -W -Wno-deprecated-declarations -Wno-missing-field-initializers -Wno-unused-parameter'
end

unless have_header('libssh/libssh.h')
Expand Down
2 changes: 2 additions & 0 deletions ext/libssh_ruby/libssh_ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <libssh/callbacks.h>

VALUE rb_mLibSSH;
VALUE rb_mLibSSHC;

/*
* @overload version(req_version = 0)
Expand Down Expand Up @@ -31,6 +32,7 @@ void Init_libssh_ruby(void) {
ssh_init();

rb_mLibSSH = rb_define_module("LibSSH");
rb_mLibSSHC = rb_define_module_under(rb_mLibSSH, "C");

/* @see Session#server_known */
rb_define_const(rb_mLibSSH, "SERVER_KNOWN_OK", INT2FIX(SSH_SERVER_KNOWN_OK));
Expand Down
1 change: 1 addition & 0 deletions ext/libssh_ruby/libssh_ruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <libssh/libssh.h>

extern VALUE rb_mLibSSH;
extern VALUE rb_mLibSSHC;
extern VALUE rb_cLibSSHKey;

void Init_libssh_ruby(void);
Expand Down
51 changes: 33 additions & 18 deletions ext/libssh_ruby/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ SessionHolder *libssh_ruby_session_holder(VALUE session) {
}

static VALUE session_alloc(VALUE klass) {
SessionHolder *holder = ALLOC(SessionHolder);
holder->session = NULL;
return TypedData_Wrap_Struct(klass, &session_type, holder);
SessionHolder *holder;
VALUE object = TypedData_Make_Struct(klass, SessionHolder, &session_type, holder);
holder->session = ssh_new();
return object;
}

static void session_mark(RB_UNUSED_VAR(void *arg)) {}
Expand All @@ -49,19 +50,6 @@ static size_t session_memsize(RB_UNUSED_VAR(const void *arg)) {
return sizeof(SessionHolder);
}

/*
* @overload initialize
* Create a new SSH session.
* @see http://api.libssh.org/stable/group__libssh__session.html ssh_new
*/
static VALUE m_initialize(VALUE self) {
SessionHolder *holder;

TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
holder->session = ssh_new();
return self;
}

/*
* @overload log_verbosity=(verbosity)
* Set the session logging verbosity.
Expand Down Expand Up @@ -468,6 +456,27 @@ static VALUE m_add_identity(VALUE self, VALUE path) {
return Qnil;
}

static VALUE c_ssh_options_set(VALUE module, VALUE session, VALUE type, VALUE value) {
SessionHolder *holder;
TypedData_Get_Struct(session, SessionHolder, &session_type, holder);

int c_type = NUM2INT(type);
const void *c_value;

switch (c_type) {
// const char*
case SSH_OPTIONS_PROXYJUMP:
c_value = NIL_P(value) ? NULL : StringValueCStr(value);
break;

default:
rb_raise(rb_eTypeError, "unsupported option");
}

RAISE_IF_ERROR(ssh_options_set(holder->session, c_type, c_value));
return Qnil;
}

struct nogvl_session_args {
ssh_session session;
int rc;
Expand Down Expand Up @@ -773,8 +782,6 @@ void Init_libssh_session() {
I(gssapi_mic);
#undef I

rb_define_method(rb_cLibSSHSession, "initialize", m_initialize, 0);

rb_define_method(rb_cLibSSHSession, "log_verbosity=", m_set_log_verbosity, 1);
rb_define_method(rb_cLibSSHSession, "host=", m_set_host, 1);
rb_define_method(rb_cLibSSHSession, "user=", m_set_user, 1);
Expand Down Expand Up @@ -815,4 +822,12 @@ void Init_libssh_session() {
rb_define_method(rb_cLibSSHSession, "userauth_kbdint_setanswer", m_userauth_kbdint_setanswer, 2);
rb_define_method(rb_cLibSSHSession, "get_publickey", m_get_publickey, 0);
rb_define_method(rb_cLibSSHSession, "write_knownhost", m_write_knownhost, 0);

/*
* LibSSH::C constants and low-level functions.
*/

rb_define_const(rb_mLibSSHC, "SSH_OPTIONS_PROXYJUMP", INT2FIX(SSH_OPTIONS_PROXYJUMP));

rb_define_module_function(rb_mLibSSHC, "ssh_options_set", c_ssh_options_set, 3);
}
1 change: 1 addition & 0 deletions lib/libssh.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'libssh/version'
require 'libssh/libssh_ruby'
require 'libssh/key'
require 'libssh/session'
23 changes: 23 additions & 0 deletions lib/libssh/session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "libssh/libssh_ruby"

module LibSSH
class Session
def initialize
@proxyjump_hosts = []
end

def add_proxy_jump(host)
raise ArgumentError, "Jump host must not contain commas: #{host.inspect}" if host.include?(",")
@proxyjump_hosts << host
set_option("proxy jump", C::SSH_OPTIONS_PROXYJUMP, @proxyjump_hosts.join(","))
end

private

def set_option(name, type, value)
C::ssh_options_set(self, type, value)
rescue Error
raise ArgumentError, "Invalid #{name}: #{value.inspect}"
end
end
end
18 changes: 18 additions & 0 deletions spec/integration/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,22 @@ def kbdint(password)
end
end
end

describe "proxy jump" do
before do
session.host = SshHelper.host
session.port = DockerHelper.port
session.user = SshHelper.user
session.add_proxy_jump "#{SshHelper.user}@#{SshHelper.host}:#{DockerHelper.port}"
end

specify "when lacking authentication" do
expect { session.connect }.to raise_error LibSSH::Error
end

specify "with a bad host" do
expect { session.add_proxy_jump ":" }.to raise_error ArgumentError, /Invalid proxy jump/
expect { session.add_proxy_jump "," }.to raise_error ArgumentError, /must not contain commas/
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module DockerHelper
-e PASSWORD_ACCESS=true \
-e USER_NAME=alice \
-e USER_PASSWORD=alice \
-e LOG_STDOUT=true \
-v ./spec:/spec:ro \
-v ./spec/sshd_config:/config/sshd/sshd_config.d/99-override.conf:ro \
--publish-all \
Expand All @@ -33,6 +34,7 @@ def start
end

def stop
system("docker", "logs", @container_id, out: "spec/docker.log", err: [:child, :out])
unless system('docker', 'stop', '-t', '0', @container_id, out: File::NULL)
warn "Cannot stop Docker container #{@container_id}"
end
Expand Down