22
33import com .bettercloud .vault .Vault ;
44import com .bettercloud .vault .VaultConfig ;
5+ import com .bettercloud .vault .VaultException ;
56import com .bettercloud .vault .json .Json ;
67import com .bettercloud .vault .json .JsonObject ;
78import com .bettercloud .vault .vault .VaultTestUtils ;
89import com .bettercloud .vault .vault .mock .AuthRequestValidatingMockVault ;
910import org .apache .commons .io .IOUtils ;
1011import org .eclipse .jetty .server .Server ;
11- import org .junit .Ignore ;
1212import org .junit .Test ;
1313
1414import javax .servlet .http .HttpServletRequest ;
15- import java .util .HashSet ;
1615import java .util .function .Predicate ;
1716
1817import static org .junit .Assert .assertEquals ;
1918import static org .junit .Assert .assertNotNull ;
2019
2120public class AuthBackendAwsTests {
2221
23- @ Ignore
2422 @ Test
25- public void testLoginByAwsEc2 () throws Exception {
26- final Predicate <HttpServletRequest > isValidEc2pkcs7Request = (request ) -> {
27- JsonObject requestBody = readRequestBody (request );
28- return requestBody != null && request .getRequestURI ().endsWith ("/auth/aws/login" ) &&
29- requestBody .getString ("pkcs7" , "" ) == "pkcs7" ;
30- };
31-
23+ public void testLoginByAwsEc2Id () throws Exception {
3224 final Predicate <HttpServletRequest > isValidEc2IdRequest = (request ) -> {
33- JsonObject requestBody = readRequestBody (request );
34- return requestBody != null && request .getRequestURI ().endsWith ("/auth/aws/login" ) &&
35- requestBody .getString ("identity" , "" ) == "identity" &&
36- requestBody .getString ("signature" , "" ) == "signature" ;
25+ try {
26+ JsonObject requestBody = readRequestBody (request );
27+ return requestBody != null && request .getRequestURI ().endsWith ("/auth/aws/login" ) &&
28+ requestBody .getString ("identity" , "" ).equals ("identity" ) &&
29+ requestBody .getString ("signature" , "" ).equals ("signature" );
30+ } catch (Exception e ) {
31+ return false ;
32+ }
3733 };
34+ final AuthRequestValidatingMockVault mockVault = new AuthRequestValidatingMockVault (isValidEc2IdRequest );
3835
39- final Predicate <HttpServletRequest > isValidEc2IamRequest = (request ) -> {
40- JsonObject requestBody = readRequestBody (request );
41- return requestBody != null && request .getRequestURI ().endsWith ("/auth/aws/login" ) &&
42- requestBody .getString ("iam_http_request_method" , "" ) == "POST" &&
43- requestBody .getString ("iam_http_request_url" , "" ) == "url" &&
44- requestBody .getString ("iam_http_request_body" , "" ) == "body" &&
45- requestBody .getString ("iam_http_request_headers" , "" ) == "headers" ;
46- };
36+ final Server server = VaultTestUtils .initHttpMockVault (mockVault );
37+ server .start ();
4738
48- final AuthRequestValidatingMockVault mockVault = new AuthRequestValidatingMockVault (new HashSet <Predicate <HttpServletRequest >>() {{
49- add (isValidEc2pkcs7Request );
50- add (isValidEc2IdRequest );
51- }});
39+ final VaultConfig vaultConfig = new VaultConfig ()
40+ .address ("http://127.0.0.1:8999" )
41+ .build ();
42+ final Vault vault = new Vault (vaultConfig );
43+
44+ String token = null ;
45+ try {
46+ token = vault .auth ()
47+ .loginByAwsEc2 ("role" ,"identity" ,"signature" , null , null )
48+ .getAuthClientToken ();
49+ } catch (VaultException e ) { }
50+
51+ server .stop ();
52+
53+ assertNotNull (token );
54+ assertEquals ("c9368254-3f21-aded-8a6f-7c818e81b17a" , token .trim ());
55+
56+ }
57+
58+ @ Test
59+ public void testLoginByAwsEc2Pkcs7 () throws Exception {
60+ final Predicate <HttpServletRequest > isValidEc2pkcs7Request = (request ) -> {
61+ try {
62+ JsonObject requestBody = readRequestBody (request );
63+ return requestBody != null && request .getRequestURI ().endsWith ("/auth/aws/login" ) &&
64+ requestBody .getString ("pkcs7" , "" ).equals ("pkcs7" );
65+ } catch (Exception e ) {
66+ e .printStackTrace (System .out );
67+ return false ;
68+ }
69+ };
70+ final AuthRequestValidatingMockVault mockVault = new AuthRequestValidatingMockVault (isValidEc2pkcs7Request );
5271
5372 final Server server = VaultTestUtils .initHttpMockVault (mockVault );
5473 server .start ();
@@ -58,36 +77,33 @@ public void testLoginByAwsEc2() throws Exception {
5877 .build ();
5978 final Vault vault = new Vault (vaultConfig );
6079
61- final String token1 = vault .auth ()
62- .loginByAwsEc2 ("role" ,"pkcs7" ,null ,null )
63- .getAuthClientToken ();
80+ System .out .println ("Running Aws EC2 test" );
6481
65- assertNotNull (token1 );
66- assertEquals ("c9368254-3f21-aded-8a6f-7c818e81b17a" , token1 .trim ());
82+ String token = null ;
83+ try {
84+ token = vault .auth ()
85+ .loginByAwsEc2 ("role" ,"pkcs7" ,null ,null )
86+ .getAuthClientToken ();
87+ } catch (VaultException e ) { }
6788
68- final String token2 = vault .auth ()
69- .loginByAwsEc2 ("role" ,"identity" ,"signature" , null , null )
70- .getAuthClientToken ();
89+ server .stop ();
7190
72- assertNotNull (token2 );
73- assertEquals ("c9368254-3f21-aded-8a6f-7c818e81b17a" , token2 .trim ());
91+ assertNotNull (token );
92+ assertEquals ("c9368254-3f21-aded-8a6f-7c818e81b17a" , token .trim ());
7493 }
7594
76- @ Ignore
7795 @ Test
7896 public void testLoginByAwsIam () throws Exception {
7997 final Predicate <HttpServletRequest > isValidEc2IamRequest = (request ) -> {
8098 JsonObject requestBody = readRequestBody (request );
8199 return requestBody != null && request .getRequestURI ().endsWith ("/auth/aws/login" ) &&
82- requestBody .getString ("iam_http_request_method" , "" ) == "POST" &&
83- requestBody .getString ("iam_http_request_url " , "" ) == "url" &&
84- requestBody .getString ("iam_http_request_body " , "" ) == "body" &&
85- requestBody .getString ("iam_http_request_headers " , "" ) == "headers" ;
100+ requestBody .getString ("iam_http_request_method" , "" ). equals ( "POST" ) &&
101+ requestBody .getString ("iam_request_url " , "" ). equals ( "url" ) &&
102+ requestBody .getString ("iam_request_body " , "" ). equals ( "body" ) &&
103+ requestBody .getString ("iam_request_headers " , "" ). equals ( "headers" ) ;
86104 };
87105
88- final AuthRequestValidatingMockVault mockVault = new AuthRequestValidatingMockVault (new HashSet <Predicate <HttpServletRequest >>() {{
89- add (isValidEc2IamRequest );
90- }});
106+ final AuthRequestValidatingMockVault mockVault = new AuthRequestValidatingMockVault (isValidEc2IamRequest );
91107
92108 final Server server = VaultTestUtils .initHttpMockVault (mockVault );
93109 server .start ();
@@ -101,6 +117,8 @@ public void testLoginByAwsIam() throws Exception {
101117 .loginByAwsIam ("role" ,"url" ,"body" ,"headers" ,null )
102118 .getAuthClientToken ();
103119
120+ server .stop ();
121+
104122 assertNotNull (token );
105123 assertEquals ("c9368254-3f21-aded-8a6f-7c818e81b17a" , token .trim ());
106124 }
0 commit comments