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

optionally pass auth client to dbus client #384

Merged

Conversation

jbbjarnason
Copy link
Contributor

Now I am able to connect to remote dbus sessions and authenticating with the default EXTERNAL method.

example:

Future<DBusClient> connectRemoteSystemBus({
  required String remoteHost,
  required String sshUser,
  required String sshPassword,
  int remotePort = 22,
  int localPort = 7272,
}) async {
  // 1) Start the TCP server that will accept a D-Bus client connection
  final server = await ServerSocket.bind('127.0.0.1', localPort);
  print('Listening locally on ${server.address.address}:${server.port}');

  // 2) Set up the SSH session to run systemd-stdio-bridge on the remote
  final sshSocket = await SSHSocket.connect(remoteHost, remotePort);
  final client = SSHClient(
    sshSocket,
    username: sshUser,
    onPasswordRequest: () => sshPassword,
    onVerifyHostKey: (host, key) => true, // WARNING: verify in production!
  );

  // Start an SSH session that runs "systemd-stdio-bridge"
  final uidSession = await client.execute('id -u');
  final uid = String.fromCharCodes(await uidSession.stdout.first).trim();
  final session = await client.execute('systemd-stdio-bridge');
  print('SSH session started: systemd-stdio-bridge on $remoteHost');

  // 3) Create the DBusClient first
  final dbusAddress = DBusAddress.tcp('127.0.0.1', port: localPort);
  final dbusAuth = DBusAuthClient(uid: uid);
  final dbusClient = DBusClient(dbusAddress, authClient: dbusAuth);
  print(
      'DBusClient created. Will connect to tcp:host=127.0.0.1,port=$localPort');

  // 4) Set up a future for the client connection
  final serverSideSocketFuture = server.first;

  // 5) Make the D-Bus call which will trigger the connection
  print('Attempting D-Bus call: ListNames on org.freedesktop.DBus...');
  final dbusCallFuture = dbusClient.callMethod(
    destination: 'org.freedesktop.DBus',
    path: DBusObjectPath('/org/freedesktop/DBus'),
    interface: 'org.freedesktop.DBus',
    name: 'ListNames',
    replySignature: DBusSignature('as'),
  );

  // 6) Wait for the client to connect
  print('Waiting for client socket connection...');
  final serverSideSocket = await serverSideSocketFuture;
  print('Client connected to local port ${serverSideSocket.port}');

  // 7) Set up the pipes between local socket and SSH session
  serverSideSocket.pipe(session.stdin);
  session.stdout.cast<List<int>>().pipe(serverSideSocket);

  // 8) Now wait for the D-Bus call to complete
  await dbusCallFuture;
  print('D-Bus connection established successfully');

  return dbusClient;
}

Copy link

Hey! jbbjarnason has not signed the Canonical CLA which is required to get this contribution merged on this project.

Please head over to https://ubuntu.com/legal/contributors to read more about it.

@jbbjarnason jbbjarnason force-pushed the optionally-pass-auth-client branch from cc89ff3 to 30dfa7e Compare January 17, 2025 07:14
@jbbjarnason
Copy link
Contributor Author

not sure why I still get the cla error, i have already signed the form

image

@jbbjarnason jbbjarnason force-pushed the optionally-pass-auth-client branch from 30dfa7e to 2d58bb4 Compare January 17, 2025 08:22
@jbbjarnason jbbjarnason force-pushed the optionally-pass-auth-client branch from 2d58bb4 to 14a56dd Compare January 17, 2025 08:26
@robert-ancell
Copy link
Contributor

not sure why I still get the cla error, i have already signed the form

Thanks for that, there must be some issue with the data flowing through to the checker.

Copy link
Contributor

@robert-ancell robert-ancell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@robert-ancell robert-ancell merged commit 99ff613 into canonical:main Jan 19, 2025
1 check failed
@jbbjarnason
Copy link
Contributor Author

Thanks for the quick review and release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants