1616import org .yaml .snakeyaml .Yaml ;
1717import org .yaml .snakeyaml .constructor .Constructor ;
1818import reactor .core .Exceptions ;
19+ import reactor .core .publisher .Mono ;
20+ import reactor .core .scheduler .Schedulers ;
1921
2022public final class VaultServiceRolesInstaller {
2123
@@ -24,7 +26,7 @@ public final class VaultServiceRolesInstaller {
2426 private static final String VAULT_TOKEN_HEADER = "X-Vault-Token" ;
2527
2628 private String vaultAddress ;
27- private Supplier <String > vaultTokenSupplier ;
29+ private Mono <String > vaultTokenSupplier ;
2830 private Supplier <String > keyNameSupplier ;
2931 private Function <String , String > roleNameBuilder ;
3032 private String inputFileName = "service-roles.yaml" ;
@@ -69,7 +71,7 @@ public VaultServiceRolesInstaller vaultAddress(String vaultAddress) {
6971 * @param vaultTokenSupplier vaultTokenSupplier
7072 * @return new instance with applied setting
7173 */
72- public VaultServiceRolesInstaller vaultTokenSupplier (Supplier <String > vaultTokenSupplier ) {
74+ public VaultServiceRolesInstaller vaultTokenSupplier (Mono <String > vaultTokenSupplier ) {
7375 final VaultServiceRolesInstaller c = copy ();
7476 c .vaultTokenSupplier = vaultTokenSupplier ;
7577 return c ;
@@ -163,31 +165,47 @@ public VaultServiceRolesInstaller roleTtl(String roleTtl) {
163165 * Reads {@code inputFileName} and builds vault oidc micro-infrastructure (identity roles and
164166 * keys) to use it for machine-to-machine authentication.
165167 */
166- public void install () {
168+ public Mono <Void > install () {
169+ return Mono .fromRunnable (this ::install0 )
170+ .subscribeOn (Schedulers .boundedElastic ())
171+ .doOnSubscribe (s -> LOGGER .debug ("[install] Installing vault service roles" ))
172+ .doOnSuccess (s -> LOGGER .debug ("[install][success] Installed vault service roles" ))
173+ .doOnError (
174+ th ->
175+ LOGGER .error (
176+ "[install][error] Failed to install vault service roles, cause: {}" ,
177+ th .toString ()))
178+ .then ();
179+ }
180+
181+ private Mono <Void > install0 () {
167182 if (isNullOrNoneOrEmpty (vaultAddress )) {
168- return ;
183+ return Mono . empty () ;
169184 }
170185
171186 final ServiceRoles serviceRoles = loadServiceRoles ();
172- if (serviceRoles == null ) {
173- return ;
187+ if (serviceRoles == null || serviceRoles . roles . isEmpty () ) {
188+ return Mono . empty () ;
174189 }
175190
176- final Rest rest = new Rest ().header (VAULT_TOKEN_HEADER , vaultTokenSupplier .get ());
177-
178- if (!serviceRoles .roles .isEmpty ()) {
179- String keyName = keyNameSupplier .get ();
180- createVaultIdentityKey (keyName , () -> rest .url (buildVaultIdentityKeyUri (keyName )));
181-
182- for (Role role : serviceRoles .roles ) {
183- String roleName = roleNameBuilder .apply (role .role );
184- createVaultIdentityRole (
185- keyName ,
186- roleName ,
187- role .permissions ,
188- () -> rest .url (buildVaultIdentityRoleUri (roleName )));
189- }
190- }
191+ return Mono .defer (() -> vaultTokenSupplier )
192+ .doOnSuccess (
193+ token -> {
194+ final Rest rest = new Rest ().header (VAULT_TOKEN_HEADER , token );
195+
196+ String keyName = keyNameSupplier .get ();
197+ createVaultIdentityKey (rest .url (buildVaultIdentityKeyUri (keyName )), keyName );
198+
199+ for (Role role : serviceRoles .roles ) {
200+ String roleName = roleNameBuilder .apply (role .role );
201+ createVaultIdentityRole (
202+ rest .url (buildVaultIdentityRoleUri (roleName )),
203+ keyName ,
204+ roleName ,
205+ role .permissions );
206+ }
207+ })
208+ .then ();
191209 }
192210
193211 private ServiceRoles loadServiceRoles () {
@@ -205,7 +223,7 @@ private static void verifyOk(int status, String operation) {
205223 }
206224 }
207225
208- private void createVaultIdentityKey (String keyName , Supplier < Rest > restSupplier ) {
226+ private void createVaultIdentityKey (Rest rest , String keyName ) {
209227 LOGGER .debug ("[createVaultIdentityKey] {}" , keyName );
210228
211229 byte [] body =
@@ -218,14 +236,14 @@ private void createVaultIdentityKey(String keyName, Supplier<Rest> restSupplier)
218236 .getBytes ();
219237
220238 try {
221- verifyOk (restSupplier . get () .body (body ).post ().getStatus (), "createVaultIdentityKey" );
239+ verifyOk (rest .body (body ).post ().getStatus (), "createVaultIdentityKey" );
222240 } catch (RestException e ) {
223241 throw Exceptions .propagate (e );
224242 }
225243 }
226244
227245 private void createVaultIdentityRole (
228- String keyName , String roleName , List <String > permissions , Supplier < Rest > restSupplier ) {
246+ Rest rest , String keyName , String roleName , List <String > permissions ) {
229247 LOGGER .debug ("[createVaultIdentityRole] {}" , roleName );
230248
231249 byte [] body =
@@ -237,7 +255,7 @@ private void createVaultIdentityRole(
237255 .getBytes ();
238256
239257 try {
240- verifyOk (restSupplier . get () .body (body ).post ().getStatus (), "createVaultIdentityRole" );
258+ verifyOk (rest .body (body ).post ().getStatus (), "createVaultIdentityRole" );
241259 } catch (RestException e ) {
242260 throw Exceptions .propagate (e );
243261 }
0 commit comments