11package io .scalecube .security .environment ;
22
3+ import static org .testcontainers .shaded .org .apache .commons .lang3 .RandomStringUtils .randomAlphabetic ;
4+ import static org .testcontainers .shaded .org .apache .commons .lang3 .RandomStringUtils .randomAlphanumeric ;
5+
36import com .bettercloud .vault .json .Json ;
47import com .bettercloud .vault .rest .Rest ;
58import com .bettercloud .vault .rest .RestException ;
1215
1316public class VaultEnvironment implements AutoCloseable {
1417
15- private static final String VAULT_TOKEN = "test" ;
18+ private static final String VAULT_TOKEN = UUID . randomUUID (). toString () ;
1619 private static final String VAULT_TOKEN_HEADER = "X-Vault-Token" ;
1720 private static final int PORT = 8200 ;
1821
@@ -37,11 +40,20 @@ public static VaultEnvironment start() {
3740 return environment ;
3841 }
3942
40- public String generateIdentityToken (String clientToken , String roleName ) throws RestException {
41- RestResponse restResponse =
42- new Rest ().header (VAULT_TOKEN_HEADER , clientToken ).url (oidcToken (roleName )).get ();
43- int status = restResponse .getStatus ();
43+ public String vaultAddr () {
44+ return vaultAddr ;
45+ }
46+
47+ public String generateIdentityToken (String clientToken , String roleName ) {
48+ RestResponse restResponse ;
49+ try {
50+ restResponse =
51+ new Rest ().header (VAULT_TOKEN_HEADER , clientToken ).url (oidcToken (roleName )).get ();
52+ } catch (RestException e ) {
53+ throw new RuntimeException (e );
54+ }
4455
56+ int status = restResponse .getStatus ();
4557 if (status != 200 && status != 204 ) {
4658 throw new IllegalStateException (
4759 "Unexpected status code on identity token creation: " + status );
@@ -55,42 +67,61 @@ public String generateIdentityToken(String clientToken, String roleName) throws
5567 .asString ();
5668 }
5769
58- public void createIdentityTokenPolicy (String roleName ) throws RestException {
59- int status =
60- new Rest ()
61- .header (VAULT_TOKEN_HEADER , VAULT_TOKEN )
62- .url (policiesAclUri (roleName ))
63- .body (
64- ("{\" policy\" :\" path \\ \" identity/oidc/token/"
65- + roleName
66- + "\\ \" {capabilities=[\\ \" create\\ \" , \\ \" read\\ \" ]}\" }" )
67- .getBytes ())
68- .post ()
69- .getStatus ();
70+ public void createIdentityTokenPolicy (String roleName ) {
71+ int status ;
72+ try {
73+ status =
74+ new Rest ()
75+ .header (VAULT_TOKEN_HEADER , VAULT_TOKEN )
76+ .url (policiesAclUri (roleName ))
77+ .body (
78+ ("{\" policy\" :\" path \\ \" identity/oidc/*"
79+ + "\\ \" {capabilities=[\\ \" create\\ \" , \\ \" read\\ \" ]}\" }" )
80+ .getBytes ())
81+ .post ()
82+ .getStatus ();
83+ } catch (RestException e ) {
84+ throw new RuntimeException (e );
85+ }
7086
7187 if (status != 200 && status != 204 ) {
7288 throw new IllegalStateException (
7389 "Unexpected status code on identity token policy creation: " + status );
7490 }
7591 }
7692
77- public String createEntity (final String roleName ) throws Exception {
78- checkSuccess (
79- vault
80- .execInContainer (
81- ("vault write auth/userpass/users/abc password=abc policies=" + roleName )
82- .split ("\\ s" ))
83- .getExitCode ());
84- ExecResult loginExecResult =
85- vault .execInContainer (
86- "vault login -format json -method=userpass username=abc password=abc" .split ("\\ s" ));
87- checkSuccess (loginExecResult .getExitCode ());
88- return Json .parse (loginExecResult .getStdout ().replaceAll ("\\ r?\\ n" , "" ))
89- .asObject ()
90- .get ("auth" )
91- .asObject ()
92- .get ("client_token" )
93- .asString ();
93+ public String login () {
94+ try {
95+ String username = randomAlphabetic (5 );
96+ String policy = randomAlphanumeric (10 );
97+
98+ // add policy
99+ createIdentityTokenPolicy (policy );
100+
101+ // create user and login
102+ checkSuccess (
103+ vault
104+ .execInContainer (
105+ ("vault write auth/userpass/users/"
106+ + username
107+ + " password=abc policies="
108+ + policy )
109+ .split ("\\ s" ))
110+ .getExitCode ());
111+ ExecResult loginExecResult =
112+ vault .execInContainer (
113+ ("vault login -format json -method=userpass username=" + username + " password=abc" )
114+ .split ("\\ s" ));
115+ checkSuccess (loginExecResult .getExitCode ());
116+ return Json .parse (loginExecResult .getStdout ().replaceAll ("\\ r?\\ n" , "" ))
117+ .asObject ()
118+ .get ("auth" )
119+ .asObject ()
120+ .get ("client_token" )
121+ .asString ();
122+ } catch (Exception ex ) {
123+ throw new RuntimeException (ex );
124+ }
94125 }
95126
96127 public static void checkSuccess (int exitCode ) {
@@ -99,40 +130,52 @@ public static void checkSuccess(int exitCode) {
99130 }
100131 }
101132
102- public String createIdentityKey () throws RestException {
103- String keyName = UUID .randomUUID ().toString ();
104- int status =
105- new Rest ()
106- .header (VAULT_TOKEN_HEADER , VAULT_TOKEN )
107- .url (oidcKeyUrl (keyName ))
108- .body (
109- ("{\" rotation_period\" :\" "
110- + "1m"
111- + "\" , "
112- + "\" verification_ttl\" : \" "
113- + "1m"
114- + "\" , "
115- + "\" allowed_client_ids\" : \" *\" , "
116- + "\" algorithm\" : \" RS256\" }" )
117- .getBytes ())
118- .post ()
119- .getStatus ();
133+ public String createIdentityKey () {
134+ String keyName = randomAlphanumeric (10 );
135+
136+ int status ;
137+ try {
138+ status =
139+ new Rest ()
140+ .header (VAULT_TOKEN_HEADER , VAULT_TOKEN )
141+ .url (oidcKeyUrl (keyName ))
142+ .body (
143+ ("{\" rotation_period\" :\" "
144+ + "1m"
145+ + "\" , "
146+ + "\" verification_ttl\" : \" "
147+ + "1m"
148+ + "\" , "
149+ + "\" allowed_client_ids\" : \" *\" , "
150+ + "\" algorithm\" : \" RS256\" }" )
151+ .getBytes ())
152+ .post ()
153+ .getStatus ();
154+ } catch (RestException e ) {
155+ throw new RuntimeException (e );
156+ }
120157
121158 if (status != 200 && status != 204 ) {
122159 throw new IllegalStateException ("Unexpected status code on oidc/key creation: " + status );
123160 }
124161 return keyName ;
125162 }
126163
127- public String createIdentityRole (String keyName ) throws RestException {
128- String roleName = UUID .randomUUID ().toString ();
129- int status =
130- new Rest ()
131- .header (VAULT_TOKEN_HEADER , VAULT_TOKEN )
132- .url (oidcRoleUrl (roleName ))
133- .body (("{\" key\" :\" " + keyName + "\" ,\" ttl\" : \" " + "1h" + "\" }" ).getBytes ())
134- .post ()
135- .getStatus ();
164+ public String createIdentityRole (String keyName ) {
165+ String roleName = randomAlphanumeric (10 );
166+
167+ int status ;
168+ try {
169+ status =
170+ new Rest ()
171+ .header (VAULT_TOKEN_HEADER , VAULT_TOKEN )
172+ .url (oidcRoleUrl (roleName ))
173+ .body (("{\" key\" :\" " + keyName + "\" ,\" ttl\" : \" " + "1h" + "\" }" ).getBytes ())
174+ .post ()
175+ .getStatus ();
176+ } catch (RestException e ) {
177+ throw new RuntimeException (e );
178+ }
136179
137180 if (status != 200 && status != 204 ) {
138181 throw new IllegalStateException ("Unexpected status code on oidc/role creation: " + status );
0 commit comments