11import io .github .easy4j .opencli .OpenCliExecutionTarget ;
22import io .github .easy4j .opencli .OpenCliProperties ;
3+ import io .github .easy4j .opencli .OpenCliRemoteAuthentication ;
34import io .github .easy4j .opencli .core .OpenCliExecutor ;
45import io .github .easy4j .opencli .core .OpenCliRemoteExecutionDetails ;
56import io .github .easy4j .opencli .core .OpenCliResult ;
7+ import io .github .easy4j .opencli .exception .OpenCliExecutableFailureException ;
68import io .github .easy4j .opencli .exception .OpenCliNonZeroExitException ;
9+ import java .io .IOException ;
10+ import java .io .PrintWriter ;
11+ import java .io .StringWriter ;
712import java .net .URI ;
13+ import java .nio .charset .StandardCharsets ;
14+ import java .nio .file .Files ;
15+ import java .nio .file .Paths ;
816import java .util .Objects ;
917
1018public final class OpenCliAdminLoopbackProbe {
1119
12- public static void main (String [] args ) {
20+ public static void main (String [] args ) throws IOException {
1321 URI endpoint = URI .create (args [0 ]);
1422 require ("127.0.0.1" .equals (endpoint .getHost ()), "test Agent must be on IPv4 loopback" );
15- try (OpenCliExecutor executor = executor (endpoint , false )) {
23+ if (args .length == 2 ) {
24+ require ("https" .equals (endpoint .getScheme ()), "TLS proxy requires HTTPS" );
25+ String token = new String (Files .readAllBytes (Paths .get (args [1 ])), StandardCharsets .US_ASCII ).trim ();
26+ verifyTlsProxy (endpoint , token );
27+ return ;
28+ }
29+ try (OpenCliExecutor executor = executor (endpoint , false , null )) {
1630 OpenCliResult help = executor .invoke ("npm" , "search" , "fixture" , "--help=true" , "-f" , "json" );
1731 verify (help , true );
1832 require (help .getStdout ().contains ("opencli npm search" ), "structured CLI help is missing" );
@@ -28,7 +42,7 @@ public static void main(String[] args) {
2842 require (result .getStderr ().contains ("opencli exit" ), "the Agent's business error is missing" );
2943 }
3044 }
31- try (OpenCliExecutor executor = executor (endpoint , true )) {
45+ try (OpenCliExecutor executor = executor (endpoint , true , null )) {
3246 OpenCliResult help = executor .invoke ("npm" , "search" , "fixture" , "--help=true" , "-f" , "json" );
3347 verify (help , true );
3448 require (Objects .nonNull (help .getRemoteRawHttpBody ()) && help .getRemoteRawHttpBody ().contains ("opencli npm search" ),
@@ -37,12 +51,48 @@ public static void main(String[] args) {
3751 System .out .println ("PASS: real Agent help, missing-argument failure, and opt-in raw HTTP capture" );
3852 }
3953
40- private static OpenCliExecutor executor (URI endpoint , boolean capture ) {
54+ private static void verifyTlsProxy (URI endpoint , String token ) {
55+ try (OpenCliExecutor executor = executor (endpoint , false , "Bearer " + token )) {
56+ OpenCliResult help = executor .invoke ("npm" , "search" , "fixture" , "--help=true" , "-f" , "json" );
57+ verify (help , true );
58+ require (help .getStdout ().contains ("opencli npm search" ), "structured CLI help is missing" );
59+ require (Objects .isNull (help .getRemoteRawHttpBody ()), "raw capture must be off by default" );
60+ }
61+ verifyRejected (endpoint , null , 401 );
62+ verifyRejected (endpoint , "Bearer invalid-" + token , 401 );
63+ verifyRejected (URI .create ("https://localhost:" + endpoint .getPort ()), "Bearer " + token , 0 );
64+ System .out .println ("PASS: real Agent through TLS, missing and invalid credentials, and certificate hostname rejection" );
65+ }
66+
67+ private static void verifyRejected (URI endpoint , String credential , int status ) {
68+ try (OpenCliExecutor executor = executor (endpoint , false , credential )) {
69+ executor .invoke ("npm" , "search" , "fixture" , "--help=true" , "-f" , "json" );
70+ throw new AssertionError ("proxy unexpectedly accepted the request" );
71+ } catch (OpenCliExecutableFailureException failure ) {
72+ OpenCliResult result = failure .getPartialResult ();
73+ OpenCliRemoteExecutionDetails details = result .getRemoteExecutionDetails ();
74+ require (details .getObservedHttpStatus ().orElse (0 ) == status , "wrong observed HTTP status" );
75+ require (details .getOutcome () == (status == 0 ? OpenCliRemoteExecutionDetails .Outcome .TRANSPORT_FAILURE
76+ : OpenCliRemoteExecutionDetails .Outcome .HTTP_FAILURE ), "wrong rejection classification" );
77+ require (Objects .isNull (result .getExitCode ()), "rejection must not invent an exit code" );
78+ require (Objects .isNull (result .getRemoteRawHttpBody ()), "raw capture must be off by default" );
79+ if (Objects .nonNull (credential )) {
80+ StringWriter diagnostics = new StringWriter ();
81+ failure .printStackTrace (new PrintWriter (diagnostics ));
82+ require (!diagnostics .toString ().contains (credential ), "exception included the authorization value" );
83+ }
84+ }
85+ }
86+
87+ private static OpenCliExecutor executor (URI endpoint , boolean capture , String credential ) {
4188 OpenCliProperties properties = new OpenCliProperties ();
4289 properties .setExecutionTarget (OpenCliExecutionTarget .REMOTE_AGENT_HTTP );
4390 properties .setRemoteAgentBaseUrl (endpoint .toString ());
4491 properties .setRemoteCollectMode ("bridge" );
4592 properties .setRemoteCaptureRawHttpResponse (capture );
93+ if (Objects .nonNull (credential )) {
94+ properties .setRemoteAuthentication (new OpenCliRemoteAuthentication ("Authorization" , credential ));
95+ }
4696 return new OpenCliExecutor (properties );
4797 }
4898
0 commit comments