Keep step-up elevation for the signed-in session and stop gating terminals
The proxy forwards an HMAC-derived session id (X-Pipelines-Session) on /connect and POST /files; the server trusts it only on token-authenticated requests and keys elevation by it, so reconnects stay elevated up to a 24 h cap. Terminals rely on their su -l login. The app no longer forgets the elevation on disconnect and asks again only on a fresh refusal.
This commit is contained in:
@@ -37,7 +37,6 @@ class AppSession extends ChangeNotifier {
|
||||
_elevation = MachineElevation(
|
||||
name: endpoint?.host.isNotEmpty == true ? endpoint!.host : 'this server',
|
||||
gateway: _client,
|
||||
states: _client.states,
|
||||
);
|
||||
_gateway = ElevatingGateway(_client, _elevation);
|
||||
}
|
||||
@@ -192,7 +191,6 @@ class AppSession extends ChangeNotifier {
|
||||
_disposed = true;
|
||||
_sendUnsubscribe();
|
||||
unawaited(_states.cancel());
|
||||
unawaited(_elevation.dispose());
|
||||
unawaited(_frames.cancel());
|
||||
unawaited(_client.dispose());
|
||||
super.dispose();
|
||||
|
||||
@@ -80,21 +80,14 @@ Future<ChatMachineConnection> connectChatMachine(
|
||||
);
|
||||
await client.connectAny(endpoints);
|
||||
// Each machine confirms its own password through its own connection.
|
||||
final elevation = MachineElevation(
|
||||
name: server.name,
|
||||
gateway: client,
|
||||
states: client.states,
|
||||
);
|
||||
final elevation = MachineElevation(name: server.name, gateway: client);
|
||||
return ChatMachineConnection(
|
||||
gateway: ElevatingGateway(client, elevation),
|
||||
headers: headers,
|
||||
endpoint: () => client.endpoint?.toString() ?? '',
|
||||
connected: () => client.state.kind == ConnectionKind.connected,
|
||||
states: client.states,
|
||||
close: () async {
|
||||
await elevation.dispose();
|
||||
await client.dispose();
|
||||
},
|
||||
close: client.dispose,
|
||||
retry: () => client.connectAny(endpoints),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -97,9 +97,9 @@ class _ElevationDialogState extends State<ElevationDialog> {
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(
|
||||
'Running commands, opening a terminal and changing files on '
|
||||
'${widget.name} need its password again. This connection stays '
|
||||
'confirmed for 15 minutes.',
|
||||
'Running commands and changing files on ${widget.name} need its '
|
||||
'password. You are asked once; it stays confirmed until you sign '
|
||||
'out (at most a day).',
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
field,
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import 'dart:async';
|
||||
|
||||
import '../../api_gateway.dart';
|
||||
import '../../error_reason.dart';
|
||||
import '../../transport/connection_state.dart';
|
||||
|
||||
/// The server refused a request until this connection re-confirms the
|
||||
/// The server refused a request until this machine's session confirms the
|
||||
/// password (step-up re-authentication).
|
||||
const elevationRequiredCode = 'elevation_required';
|
||||
|
||||
@@ -17,24 +15,21 @@ typedef ElevationPrompt = Future<bool> Function(MachineElevation machine);
|
||||
bool isElevationRequired(Object error) =>
|
||||
error is ProtocolError && error.code == elevationRequiredCode;
|
||||
|
||||
/// One machine connection's step-up state.
|
||||
/// One machine's step-up state for this app run.
|
||||
///
|
||||
/// Elevation belongs to the websocket connection that confirmed the
|
||||
/// password: it survives navigation, ends when the server's window expires
|
||||
/// and is forgotten when the connection drops (the server forgets it too).
|
||||
/// The server keys a confirmation by the signed-in web session, so it
|
||||
/// survives navigation, reconnects and new connections of the same session:
|
||||
/// the operator is asked at most once per machine while that session lasts.
|
||||
/// Only a fresh `elevation_required` refusal (the session ended, the cap
|
||||
/// passed, or an older server that elevates one connection) asks again.
|
||||
/// Each machine elevates separately through its own gateway.
|
||||
class MachineElevation {
|
||||
MachineElevation({
|
||||
required this.name,
|
||||
required this.gateway,
|
||||
Stream<ConnectionState>? states,
|
||||
this.prompt,
|
||||
DateTime Function()? clock,
|
||||
}) : _clock = clock ?? DateTime.now {
|
||||
_states = states?.listen((state) {
|
||||
if (state.kind != ConnectionKind.connected) clear();
|
||||
});
|
||||
}
|
||||
}) : _clock = clock ?? DateTime.now;
|
||||
|
||||
/// The prompt every elevation uses when none is given; the app installs
|
||||
/// its native password dialog here.
|
||||
@@ -47,7 +42,6 @@ class MachineElevation {
|
||||
final ApiGateway gateway;
|
||||
final ElevationPrompt? prompt;
|
||||
final DateTime Function() _clock;
|
||||
StreamSubscription<ConnectionState>? _states;
|
||||
String? _token;
|
||||
DateTime? _expires;
|
||||
Future<bool>? _pending;
|
||||
@@ -89,7 +83,7 @@ class MachineElevation {
|
||||
final expiresAt = DateTime.tryParse('${body['expires_at'] ?? ''}');
|
||||
_expires = seconds is num
|
||||
? _clock().add(Duration(seconds: seconds.toInt()))
|
||||
: expiresAt?.toLocal() ?? _clock().add(const Duration(minutes: 15));
|
||||
: expiresAt?.toLocal() ?? _clock().add(const Duration(hours: 24));
|
||||
final token = body['token'];
|
||||
_token = token is String ? token : null;
|
||||
return null;
|
||||
@@ -101,14 +95,9 @@ class MachineElevation {
|
||||
}
|
||||
}
|
||||
|
||||
/// Forgets the elevation (the connection dropped or the server refused).
|
||||
/// Forgets the elevation (the server refused it).
|
||||
void clear() {
|
||||
_token = null;
|
||||
_expires = null;
|
||||
}
|
||||
|
||||
Future<void> dispose() async {
|
||||
await _states?.cancel();
|
||||
_states = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ void main() {
|
||||
),
|
||||
);
|
||||
await expectLater(
|
||||
gateway.call('terminal_open'),
|
||||
gateway.call('chat_shell_run'),
|
||||
throwsA(
|
||||
isA<ProtocolError>().having(
|
||||
(e) => e.code,
|
||||
@@ -91,7 +91,7 @@ void main() {
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(machine.calls, ['terminal_open']);
|
||||
expect(machine.calls, ['chat_shell_run']);
|
||||
});
|
||||
|
||||
test('other errors pass through untouched and never prompt', () async {
|
||||
@@ -161,35 +161,33 @@ void main() {
|
||||
expect(elevation.token, 'tok-1');
|
||||
});
|
||||
|
||||
test('the token expires with the window and on disconnect', () async {
|
||||
final machine = FakeMachine();
|
||||
var now = DateTime(2026, 11, 10, 12);
|
||||
final states = StreamController<ConnectionState>.broadcast();
|
||||
final elevation = MachineElevation(
|
||||
name: 'm',
|
||||
gateway: machine,
|
||||
states: states.stream,
|
||||
clock: () => now,
|
||||
);
|
||||
await elevation.submit('right');
|
||||
now = now.add(const Duration(minutes: 14));
|
||||
expect(elevation.token, 'tok-1');
|
||||
now = now.add(const Duration(minutes: 2));
|
||||
expect(elevation.token, isNull);
|
||||
now = now.subtract(const Duration(minutes: 16));
|
||||
await elevation.submit('right');
|
||||
states.add(const ConnectionState(ConnectionKind.disconnected));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(elevation.token, isNull);
|
||||
await elevation.dispose();
|
||||
await states.close();
|
||||
});
|
||||
test(
|
||||
'the token expires with the window; only a refusal forgets it',
|
||||
() async {
|
||||
final machine = FakeMachine();
|
||||
var now = DateTime(2026, 11, 10, 12);
|
||||
final elevation = MachineElevation(
|
||||
name: 'm',
|
||||
gateway: machine,
|
||||
clock: () => now,
|
||||
);
|
||||
await elevation.submit('right');
|
||||
now = now.add(const Duration(minutes: 14));
|
||||
expect(elevation.token, 'tok-1');
|
||||
now = now.add(const Duration(minutes: 2));
|
||||
expect(elevation.token, isNull);
|
||||
await elevation.submit('right');
|
||||
expect(elevation.token, 'tok-1');
|
||||
elevation.clear();
|
||||
expect(elevation.token, isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test('the client carries the server error code', () async {
|
||||
final channel = FakeChannel()..readyCompleter.complete();
|
||||
final client = PipelinesClient(channelFactory: (_) => channel);
|
||||
await client.connect(Uri.parse('ws://example.test'));
|
||||
final call = client.call('terminal_open');
|
||||
final call = client.call('chat_shell_run');
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
final id = (jsonDecode(channel.outgoing.single as String) as Map)['id'];
|
||||
channel.incoming.add(
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/servers/elevation/elevating_gateway.dart';
|
||||
import 'package:pipelines_app/core/servers/elevation/machine_elevation.dart';
|
||||
import 'package:pipelines_app/core/transport/pipelines_client.dart';
|
||||
|
||||
import '../../transport/pipelines_client_test.dart' show FakeChannel, FakeSink;
|
||||
|
||||
/// A machine whose elevation belongs to the signed-in web session, as the
|
||||
/// server keeps it: every connection it hands out shares [elevated].
|
||||
class SessionServer {
|
||||
bool elevated = false;
|
||||
final channels = <ScriptedChannel>[];
|
||||
final requests = <String>[];
|
||||
|
||||
ScriptedChannel connect() {
|
||||
final channel = ScriptedChannel(this)..readyCompleter.complete();
|
||||
channels.add(channel);
|
||||
return channel;
|
||||
}
|
||||
|
||||
JsonReply answer(Map<String, dynamic> request) {
|
||||
final type = request['type'] as String;
|
||||
requests.add(type);
|
||||
if (type == 'elevate') {
|
||||
elevated = true;
|
||||
return {
|
||||
'type': 'elevated',
|
||||
'elevation': {'seconds': 86400, 'token': 'tok'},
|
||||
};
|
||||
}
|
||||
if (type == 'chat_shell_run' && !elevated) {
|
||||
return {'type': 'error', 'code': elevationRequiredCode, 'error': 'no'};
|
||||
}
|
||||
return {'type': type};
|
||||
}
|
||||
}
|
||||
|
||||
typedef JsonReply = Map<String, dynamic>;
|
||||
|
||||
class ScriptedChannel extends FakeChannel {
|
||||
ScriptedChannel(this.server);
|
||||
final SessionServer server;
|
||||
|
||||
@override
|
||||
FakeSink get sink => ScriptedSink(this);
|
||||
}
|
||||
|
||||
class ScriptedSink extends FakeSink {
|
||||
ScriptedSink(this.channel) : super(channel.outgoing, channel.incoming);
|
||||
final ScriptedChannel channel;
|
||||
|
||||
@override
|
||||
void add(dynamic event) {
|
||||
super.add(event);
|
||||
final request = jsonDecode(event as String) as Map<String, dynamic>;
|
||||
final reply = channel.server.answer(request)..['id'] = request['id'];
|
||||
scheduleMicrotask(() => channel.incoming.add(jsonEncode(reply)));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> connected(PipelinesClient client) async {
|
||||
if (client.state.kind == ConnectionKind.connected) return;
|
||||
await client.states.firstWhere((s) => s.kind == ConnectionKind.connected);
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('a reconnect re-uses the session elevation without asking', () async {
|
||||
final server = SessionServer();
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (_) => server.connect(),
|
||||
backoffBase: const Duration(milliseconds: 1),
|
||||
);
|
||||
await client.connect(Uri.parse('ws://machine.test'));
|
||||
var prompts = 0;
|
||||
final elevation = MachineElevation(
|
||||
name: 'machine',
|
||||
gateway: client,
|
||||
prompt: (m) async {
|
||||
prompts++;
|
||||
return await m.submit('right') == null;
|
||||
},
|
||||
);
|
||||
final gateway = ElevatingGateway(client, elevation);
|
||||
expect((await gateway.call('chat_shell_run'))['type'], 'chat_shell_run');
|
||||
expect(prompts, 1);
|
||||
// The connection drops; the client reconnects on a new socket.
|
||||
await server.channels.single.incoming.close();
|
||||
await connected(client);
|
||||
expect(server.channels, hasLength(2));
|
||||
expect(elevation.token, 'tok', reason: 'kept across the reconnect');
|
||||
expect((await gateway.call('chat_shell_run'))['type'], 'chat_shell_run');
|
||||
expect((await gateway.call('chat_shell_run'))['type'], 'chat_shell_run');
|
||||
expect(prompts, 1);
|
||||
expect(server.requests.where((t) => t == 'elevate'), hasLength(1));
|
||||
await client.dispose();
|
||||
});
|
||||
|
||||
test(
|
||||
'one prompt per run per machine; a fresh refusal asks once more',
|
||||
() async {
|
||||
final server = SessionServer();
|
||||
final client = PipelinesClient(channelFactory: (_) => server.connect());
|
||||
await client.connect(Uri.parse('ws://machine.test'));
|
||||
var prompts = 0;
|
||||
final gateway = ElevatingGateway(
|
||||
client,
|
||||
MachineElevation(
|
||||
name: 'machine',
|
||||
gateway: client,
|
||||
prompt: (m) async {
|
||||
prompts++;
|
||||
return await m.submit('right') == null;
|
||||
},
|
||||
),
|
||||
);
|
||||
for (var i = 0; i < 5; i++) {
|
||||
await gateway.call('chat_shell_run');
|
||||
}
|
||||
expect(prompts, 1);
|
||||
// The session ended on the server (expired, signed out elsewhere).
|
||||
server.elevated = false;
|
||||
for (var i = 0; i < 3; i++) {
|
||||
await gateway.call('chat_shell_run');
|
||||
}
|
||||
expect(prompts, 2);
|
||||
// Terminals never ask: the server does not gate them.
|
||||
expect((await gateway.call('terminal_open'))['type'], 'terminal_open');
|
||||
expect(prompts, 2);
|
||||
await client.dispose();
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user