1919import java .io .IOException ;
2020import java .io .InputStreamReader ;
2121import java .io .Reader ;
22+ import java .nio .file .Files ;
23+ import java .nio .file .Path ;
2224import java .util .ArrayList ;
2325import java .util .Arrays ;
2426import java .util .List ;
27+ import java .util .UUID ;
2528import java .util .concurrent .ExecutorService ;
2629import java .util .concurrent .Executors ;
2730import java .util .concurrent .TimeUnit ;
5053 })
5154public class EtcdTestSuite {
5255
53- static Process etcdProcess , etcdTlsProcess , etcdTlsCaProcess ;
56+ static Process etcdProcess , etcdTlsProcess , etcdTlsCaProcess , etcdJwtProcess ;
5457
5558 static final String etcdCommand ;
59+ static final String etcdctlCommand ;
5660 static {
5761 String etcd = System .getenv ("ETCD_CMD" );
5862 etcdCommand = etcd != null ? etcd : "etcd" ;
63+ String etcdctl = System .getenv ("ETCDCTL_CMD" );
64+ etcdctlCommand = etcdctl != null ? etcdctl : "etcdctl" ;
5965 }
6066
6167 static final String clientKey = EtcdTestSuite .class .getResource ("/client.key" ).getFile ();
6268 static final String clientCert = EtcdTestSuite .class .getResource ("/client.crt" ).getFile ();
6369 static final String serverKey = EtcdTestSuite .class .getResource ("/server.key" ).getFile ();
6470 static final String serverCert = EtcdTestSuite .class .getResource ("/server.crt" ).getFile ();
71+ static final String jwtKey = EtcdTestSuite .class .getResource ("/jwt_RS256.key" ).getFile (); // openssl genrsa -out jwt_RS256.key 4096
72+ static final String jwtPub = EtcdTestSuite .class .getResource ("/jwt_RS256.pub" ).getFile (); // openssl rsa -in jwt_RS256.key -pubout > jwt_RS256.pub
73+
74+ static final String userName = "root" ;
75+ static final String userPwd = UUID .randomUUID ().toString ();
6576
6677 @ BeforeClass
6778 public static void setUp () throws Exception {
6879 etcdProcess = startProcess ();
80+
6981 etcdTlsProcess = startProcess ("--cert-file=" + serverCert ,
7082 "--key-file=" + serverKey , "--listen-client-urls=https://localhost:2360" ,
7183 "--listen-peer-urls=http://localhost:2361" ,
7284 "--advertise-client-urls=https://localhost:2360" , "--name=tls" );
73- etcdTlsCaProcess = null ; startProcess ("--cert-file=" + serverCert ,
85+
86+ etcdTlsCaProcess = startProcess ("--cert-file=" + serverCert ,
7487 "--key-file=" + serverKey , "--listen-client-urls=https://localhost:2362" ,
7588 "--listen-peer-urls=http://localhost:2363" ,
7689 "--advertise-client-urls=https://localhost:2362" , "--name=tls-ca" ,
7790 "--trusted-ca-file=" + clientCert , "--client-cert-auth" );
91+
92+ Path tmpDir = Files .createTempDirectory (null );
93+ tmpDir .toFile ().deleteOnExit ();
94+ etcdJwtProcess = startProcess ("--auth-token=jwt,pub-key=" + jwtPub + ",priv-key=" + jwtKey + ",sign-method=RS256,ttl=4s" ,
95+ "--listen-client-urls=http://localhost:2365" ,
96+ "--listen-peer-urls=http://localhost:2364" ,
97+ "--advertise-client-urls=http://localhost:2365" ,
98+ "--data-dir=" + tmpDir );
99+ executeCtlCommand ("--endpoints=localhost:2365" ,
100+ "user" , "add" , userName , "--new-user-password" , userPwd );
101+ executeCtlCommand ("--endpoints=localhost:2365" ,
102+ "user" , "grant-role" , userName , "root" );
103+ executeCtlCommand ("--endpoints=localhost:2365" ,
104+ "auth" , "enable" );
78105 }
79106
80107 private static Process startProcess (String ... cmdline ) throws Exception {
@@ -98,11 +125,34 @@ private static Process startProcess(String... cmdline) throws Exception {
98125 }
99126 }
100127
128+ private static void executeCtlCommand (String ... cmdline ) throws Exception {
129+ boolean ok = false ;
130+ Process p = null ;
131+ try {
132+ List <String > cmd = new ArrayList <>();
133+ cmd .add (etcdctlCommand );
134+ cmd .addAll (Arrays .asList (cmdline ));
135+ p = new ProcessBuilder (cmd )
136+ .redirectErrorStream (true ).start ();
137+ waitForStartup (p );
138+ p .waitFor (30L , TimeUnit .SECONDS );
139+ System .out .println ("etcdctl exit value:" + p .exitValue ());
140+ ok = true ;
141+ } catch (IOException e ) {
142+ System .out .println ("Failed to execute etcdctl: " + e );
143+ } finally {
144+ if (!ok ) {
145+ tearDown (p );
146+ }
147+ }
148+ }
149+
101150 @ AfterClass
102151 public static void tearDown () {
103152 tearDown (etcdProcess );
104153 tearDown (etcdTlsProcess );
105154 tearDown (etcdTlsCaProcess );
155+ tearDown (etcdJwtProcess );
106156 }
107157
108158 public static void tearDown (Process process ) {
0 commit comments