5454
5555import org .apache .knox .gateway .config .GatewayConfig ;
5656import org .apache .knox .gateway .dto .HomePageProfile ;
57+ import org .apache .knox .gateway .fips .FipsUtils ;
5758import org .apache .knox .gateway .i18n .messages .MessagesFactory ;
5859import org .apache .knox .gateway .service .definition .Metadata ;
5960import org .apache .knox .gateway .service .definition .ServiceDefinitionPair ;
@@ -98,39 +99,49 @@ public class KnoxMetadataResource {
9899 @ Produces ({ APPLICATION_JSON , APPLICATION_XML })
99100 @ Path ("info" )
100101 public GeneralProxyInformation getGeneralProxyInformation () {
101- final GeneralProxyInformation proxyInfo = new GeneralProxyInformation ();
102102 final GatewayServices gatewayServices = (GatewayServices ) request .getServletContext ().getAttribute (GatewayServices .GATEWAY_SERVICES_ATTRIBUTE );
103- if (gatewayServices != null ) {
104- final ServerInfoService serviceInfoService = gatewayServices .getService (ServiceType .SERVER_INFO_SERVICE );
105- final String versionInfo = serviceInfoService .getBuildVersion () + " (hash=" + serviceInfoService .getBuildHash () + ")" ;
106- proxyInfo .setVersion (versionInfo );
107- proxyInfo .setHostname (Hostname .getHostname ());
108- proxyInfo .setAdminApiBookUrl (
109- String .format (Locale .ROOT , "https://knox.apache.org/books/knox-%s/user-guide.html#Admin+API" , getAdminApiBookVersion (serviceInfoService .getBuildVersion ())));
110- final GatewayConfig config = (GatewayConfig ) request .getServletContext ().getAttribute (GatewayConfig .GATEWAY_CONFIG_ATTRIBUTE );
111- proxyInfo .setAdminUiUrl (getBaseGatewayUrl (config ) + "/manager/admin-ui/" );
112- proxyInfo .setWebShellUrl (getBaseGatewayUrl (config ) + "/homepage/webshell-ui/index.html" );
113- setTokenManagementEnabledFlag (proxyInfo , gatewayServices );
114- proxyInfo .setEnableWebshell (String .valueOf (config .isWebShellEnabled ()));
103+ if (gatewayServices == null ) {
104+ return GeneralProxyInformation .builder ().build ();
115105 }
106+ final GatewayConfig config = (GatewayConfig ) request .getServletContext ().getAttribute (GatewayConfig .GATEWAY_CONFIG_ATTRIBUTE );
107+ final ServerInfoService serverInfo = gatewayServices .getService (ServiceType .SERVER_INFO_SERVICE );
108+ final String baseGatewayUrl = getBaseGatewayUrl (config );
116109
117- return proxyInfo ;
110+ return GeneralProxyInformation .builder ()
111+ .version (serverInfo .getBuildVersion () + " (hash=" + serverInfo .getBuildHash () + ")" )
112+ .hostname (Hostname .getHostname ())
113+ .adminUiUrl (baseGatewayUrl + "/manager/admin-ui/" )
114+ .webShellUrl (baseGatewayUrl + "/homepage/webshell-ui/index.html" )
115+ .adminApiBookUrl (buildAdminApiBookUrl (serverInfo .getBuildVersion ()))
116+ .enableTokenManagement (isTokenManagementEnabled (gatewayServices ))
117+ .enableWebshell (config .isWebShellEnabled ())
118+ .truststoreType (preferredTruststoreType ())
119+ .build ();
118120 }
119121
120- private void setTokenManagementEnabledFlag ( final GeneralProxyInformation proxyInfo , final GatewayServices gatewayServices ) {
122+ private boolean isTokenManagementEnabled ( final GatewayServices gatewayServices ) {
121123 try {
122124 final AliasService aliasService = gatewayServices .getService (ServiceType .ALIAS_SERVICE );
123125 final List <String > aliases = aliasService .getAliasesForCluster (AliasService .NO_CLUSTER_NAME );
124126 final boolean tokenManagementEnabled = aliases .contains (TokenMAC .KNOX_TOKEN_HASH_KEY_ALIAS_NAME );
125- proxyInfo .setEnableTokenManagement (Boolean .toString (tokenManagementEnabled ));
126127 if (!tokenManagementEnabled ) {
127128 LOG .tokenManagementDisabled ();
128129 }
130+ return tokenManagementEnabled ;
129131 } catch (AliasServiceException e ) {
130132 LOG .failedToFetchGatewayAliasList (e .getMessage (), e );
133+ return false ;
131134 }
132135 }
133136
137+ private String buildAdminApiBookUrl (String buildVersion ) {
138+ return String .format (Locale .ROOT , "https://knox.apache.org/books/knox-%s/user-guide.html#Admin+API" , getAdminApiBookVersion (buildVersion ));
139+ }
140+
141+ private String preferredTruststoreType () {
142+ return FipsUtils .isFipsEnabledWithBCProvider () ? "bcfks" : "jks" ;
143+ }
144+
134145 private String getAdminApiBookVersion (String buildVersion ) {
135146 return buildVersion .replaceAll (SNAPSHOT_VERSION_POSTFIX , "" ).replaceAll ("\\ ." , "-" );
136147 }
@@ -142,18 +153,21 @@ public Response getPublicCertification(@QueryParam("type") @DefaultValue("pem")
142153 final GatewayConfig config = (GatewayConfig ) request .getServletContext ().getAttribute (GatewayConfig .GATEWAY_CONFIG_ATTRIBUTE );
143154 final Certificate [] certificateChain = config .isSSLEnabled () ? getPublicCertificates () : getSigningkeyCerts (config );
144155 if (certificateChain != null ) {
156+ final java .nio .file .Path certFilePath ;
145157 if ("pem" .equals (certType )) {
146- generateCertificatePem (certificateChain , config );
147- return generateSuccessFileDownloadResponse (pemFilePath );
158+ certFilePath = generateCertificatePem (certificateChain , config );
148159 } else if ("jks" .equals (certType )) {
149- generateCertificateJks (certificateChain , config );
150- return generateSuccessFileDownloadResponse (jksFilePath );
160+ certFilePath = generateCertificateJks (certificateChain , config );
151161 } else if ("bcfks" .equals (certType )) {
152- generateCertificateBcfks (certificateChain , config );
153- return generateSuccessFileDownloadResponse (bcfksFilePath );
162+ certFilePath = generateCertificateBcfks (certificateChain , config );
154163 } else {
155164 return generateFailureFileDownloadResponse (Status .BAD_REQUEST , "Invalid certification type provided!" );
156165 }
166+ if (certFilePath != null && certFilePath .toFile ().exists ()) {
167+ return generateSuccessFileDownloadResponse (certFilePath );
168+ }
169+ return generateFailureFileDownloadResponse (Status .SERVICE_UNAVAILABLE ,
170+ "Could not generate " + certType .toUpperCase (Locale .ROOT ) + " public certificate" );
157171 }
158172 return generateFailureFileDownloadResponse (Status .SERVICE_UNAVAILABLE , "Could not generate public certificate" );
159173 }
@@ -190,36 +204,48 @@ private Certificate[] getPublicCertificates() {
190204 return null ;
191205 }
192206
193- private void generateCertificatePem (Certificate [] certificateChain , GatewayConfig gatewayConfig ) {
207+ private java .nio .file .Path generateCertificatePem (Certificate [] certificateChain , GatewayConfig gatewayConfig ) {
208+ if (pemFilePath != null && pemFilePath .toFile ().exists ()) {
209+ return pemFilePath ;
210+ }
211+ final java .nio .file .Path candidate = Paths .get (gatewayConfig .getGatewaySecurityDir (), "gateway-client-trust.pem" );
194212 try {
195- if (pemFilePath == null || !pemFilePath .toFile ().exists ()) {
196- pemFilePath = Paths .get (gatewayConfig .getGatewaySecurityDir (), "gateway-client-trust.pem" );
197- X509CertificateUtil .writeCertificatesToFile (certificateChain , pemFilePath .toFile ());
198- }
213+ X509CertificateUtil .writeCertificatesToFile (certificateChain , candidate .toFile ());
214+ pemFilePath = candidate ;
215+ return pemFilePath ;
199216 } catch (CertificateEncodingException | IOException e ) {
200217 LOG .failedToGeneratePublicCert ("PEM" , e .getMessage (), e );
218+ return null ;
201219 }
202220 }
203221
204- private void generateCertificateJks (Certificate [] certificateChain , GatewayConfig gatewayConfig ) {
222+ private java .nio .file .Path generateCertificateJks (Certificate [] certificateChain , GatewayConfig gatewayConfig ) {
223+ if (jksFilePath != null && jksFilePath .toFile ().exists ()) {
224+ return jksFilePath ;
225+ }
226+ final java .nio .file .Path candidate = Paths .get (gatewayConfig .getGatewaySecurityDir (), "gateway-client-trust.jks" );
205227 try {
206- if (jksFilePath == null || !jksFilePath .toFile ().exists ()) {
207- jksFilePath = Paths .get (gatewayConfig .getGatewaySecurityDir (), "gateway-client-trust.jks" );
208- X509CertificateUtil .writeCertificatesToJks (certificateChain , jksFilePath .toFile (), null );
209- }
228+ X509CertificateUtil .writeCertificatesToJks (certificateChain , candidate .toFile (), null );
229+ jksFilePath = candidate ;
230+ return jksFilePath ;
210231 } catch (IOException | KeyStoreException | NoSuchAlgorithmException | CertificateException e ) {
211232 LOG .failedToGeneratePublicCert ("JKS" , e .getMessage (), e );
233+ return null ;
212234 }
213235 }
214236
215- private void generateCertificateBcfks (Certificate [] certificateChain , GatewayConfig gatewayConfig ) {
237+ private java .nio .file .Path generateCertificateBcfks (Certificate [] certificateChain , GatewayConfig gatewayConfig ) {
238+ if (bcfksFilePath != null && bcfksFilePath .toFile ().exists ()) {
239+ return bcfksFilePath ;
240+ }
241+ final java .nio .file .Path candidate = Paths .get (gatewayConfig .getGatewaySecurityDir (), "gateway-client-trust.bcfks" );
216242 try {
217- if (bcfksFilePath == null || !bcfksFilePath .toFile ().exists ()) {
218- bcfksFilePath = Paths .get (gatewayConfig .getGatewaySecurityDir (), "gateway-client-trust.bcfks" );
219- X509CertificateUtil .writeCertificatesToBcfks (certificateChain , bcfksFilePath .toFile (), null );
220- }
243+ X509CertificateUtil .writeCertificatesToBcfks (certificateChain , candidate .toFile (), null );
244+ bcfksFilePath = candidate ;
245+ return bcfksFilePath ;
221246 } catch (IOException | KeyStoreException | NoSuchAlgorithmException | CertificateException e ) {
222247 LOG .failedToGeneratePublicCert ("BCFKS" , e .getMessage (), e );
248+ return null ;
223249 }
224250 }
225251
0 commit comments