Skip to content

Commit

Permalink
do not throw generic Exception (AthenZ#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesk40 authored and havetisyan committed May 24, 2019
1 parent 33e0d44 commit 3592df0
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.oath.auth;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;

Expand All @@ -31,9 +33,11 @@ public CaCertKeyStoreProvider(final String caCertFilePath) {
}

@Override
public KeyStore provide() throws Exception {
public KeyStore provide() throws KeyRefresherException, FileNotFoundException, IOException {
KeyStore keyStore = null;
try (InputStream inputStream = new FileInputStream(caCertFilePath)) {
return Utils.generateTrustStore(inputStream);
keyStore = Utils.generateTrustStore(inputStream);
}
return keyStore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.oath.auth;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;

/**
Expand All @@ -32,7 +34,7 @@ public JavaKeyStoreProvider(final String jksFilePath, final char[] password) {
}

@Override
public KeyStore provide() throws Exception {
public KeyStore provide() throws FileNotFoundException, IOException, KeyRefresherException {
return Utils.getKeyStore(jksFilePath, password);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 Oath Holdings Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.oath.auth;

public class KeyRefresherException extends Exception {

private static final long serialVersionUID = 1L;

public KeyRefresherException() {
super();
}

public KeyRefresherException(String message, Throwable cause) {
super(message, cause);
}

public KeyRefresherException(String message) {
super(message);
}

public KeyRefresherException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.oath.auth;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;

/**
Expand All @@ -24,6 +26,6 @@
*/
public interface KeyStoreProvider {

KeyStore provide() throws Exception;
KeyStore provide() throws KeyRefresherException, FileNotFoundException, IOException;
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
*/
package com.oath.auth;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

Expand All @@ -30,12 +35,19 @@ public TrustStore(final String filePath, final KeyStoreProvider keyStoreProvider
this.keyStoreProvider = keyStoreProvider;
}

public TrustManager[] getTrustManagers() throws Exception {
public TrustManager[] getTrustManagers() throws FileNotFoundException, KeyRefresherException, IOException {
final KeyStore keystore = keyStoreProvider.provide();

final TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keystore);
TrustManagerFactory trustManagerFactory;
try {
trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keystore);
} catch (NoSuchAlgorithmException e) {
throw new KeyRefresherException("No Provider supports a TrustManagerFactorySpi implementation for the specified algorithm.", e);
} catch (KeyStoreException e) {
throw new KeyRefresherException("Unable to generate TrustManagerFactory.", e);
}
return trustManagerFactory.getTrustManagers();
}

Expand Down
Loading

0 comments on commit 3592df0

Please sign in to comment.