@@ -49,36 +49,40 @@ public WASAML2ClientKeystoreGenerator(final WARestClient waRestClient, final SAM
4949
5050 @ Override
5151 public boolean shouldGenerate () {
52- return true ;
52+ try {
53+ SAML2SPEntityTO spEntity = waRestClient .getService (SAML2SPEntityService .class ).get (saml2Client .getName ());
54+ return spEntity .getKeystore () == null ;
55+ } catch (Exception e ) {
56+ LOG .error ("While attempting to read if keystore is available for SP Entity {}" , saml2Client .getName (), e );
57+ return true ;
58+ }
5359 }
5460
5561 @ Override
5662 protected void store (final KeyStore ks , final X509Certificate certificate , final PrivateKey privateKey )
5763 throws Exception {
5864
65+ String encodedKeystore ;
5966 try (ByteArrayOutputStream out = new ByteArrayOutputStream ()) {
6067 char [] password = saml2Configuration .getKeystorePassword ().toCharArray ();
6168 ks .store (out , password );
6269 out .flush ();
63- String encodedKeystore = Base64 .getEncoder ().encodeToString (out .toByteArray ());
70+ encodedKeystore = Base64 .getEncoder ().encodeToString (out .toByteArray ());
6471 LOG .debug ("Encoded keystore {}" , encodedKeystore );
72+ }
6573
66- SAML2SPEntityTO entityTO ;
67- try {
68- entityTO = waRestClient .getService (SAML2SPEntityService .class ).get (saml2Client .getName ());
69- entityTO .setKeystore (encodedKeystore );
70- } catch (Exception e ) {
71- LOG .debug ("SP Entity {} not found, creating new" , saml2Client .getName (), e );
72-
73- entityTO = new SAML2SPEntityTO .Builder ().
74- key (saml2Client .getName ()).
75- keystore (encodedKeystore ).
76- build ();
77- }
74+ SAML2SPEntityTO entityTO ;
75+ try {
76+ entityTO = waRestClient .getService (SAML2SPEntityService .class ).get (saml2Client .getName ());
77+ entityTO .setKeystore (encodedKeystore );
78+ } catch (Exception e ) {
79+ LOG .debug ("SP Entity {} keystore not found, creating new" , saml2Client .getName (), e );
7880
79- LOG .debug ("Storing SP Entity {}" , entityTO );
80- waRestClient .getService (SAML2SPEntityService .class ).set (entityTO );
81+ entityTO = new SAML2SPEntityTO .Builder ().key (saml2Client .getName ()).keystore (encodedKeystore ).build ();
8182 }
83+
84+ LOG .debug ("Storing SP Entity {}" , entityTO );
85+ waRestClient .getService (SAML2SPEntityService .class ).set (entityTO );
8286 }
8387
8488 @ Override
@@ -91,7 +95,7 @@ public InputStream retrieve() throws Exception {
9195 } catch (Exception e ) {
9296 String message = "Unable to fetch SAML2 SP keystore for " + saml2Client .getName ();
9397 LOG .error (message , e );
94- throw new Exception (message );
98+ throw new Exception (message , e );
9599 }
96100 }
97101}
0 commit comments