Skip to content

Commit 2e0f841

Browse files
committed
fix pmd AvoidUncheckedExceptionsInSignatures
1 parent bcf629a commit 2e0f841

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

core/src/main/java/cloud/stackit/sdk/core/KeyFlowAuthenticator.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public String getAccessToken() throws IOException, ApiException, InvalidKeySpecE
215215
* @throws JsonSyntaxException parsing of the created access token failed
216216
*/
217217
protected void createAccessToken()
218-
throws InvalidKeySpecException, IOException, JsonSyntaxException, ApiException {
218+
throws InvalidKeySpecException, IOException, ApiException {
219219
String assertion;
220220
try {
221221
assertion = generateSelfSignedJWT();
@@ -239,15 +239,22 @@ protected void createAccessToken()
239239
* @throws JsonSyntaxException can not parse new access token
240240
*/
241241
protected synchronized void createAccessTokenWithRefreshToken()
242-
throws IOException, JsonSyntaxException, ApiException {
242+
throws IOException, ApiException {
243243
String refreshToken = token.refreshToken;
244244
try (Response response = requestToken(REFRESH_TOKEN, refreshToken).execute()) {
245245
parseTokenResponse(response);
246246
}
247247
}
248248

249+
/**
250+
* Parses the token response from the server
251+
*
252+
* @param response HTTP response containing the token
253+
* @throws ApiException if the response has a bad status code
254+
* @throws JsonSyntaxException if the response body cannot be parsed
255+
*/
249256
private synchronized void parseTokenResponse(Response response)
250-
throws ApiException, JsonSyntaxException {
257+
throws ApiException {
251258
if (response.code() != HttpURLConnection.HTTP_OK) {
252259
String body = null;
253260
if (response.body() != null) {

core/src/main/java/cloud/stackit/sdk/core/auth/SetupAuth.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,22 @@ private static String getDefaultCredentialsFilePath() {
126126
* @throws IOException thrown when a file can not be found
127127
*/
128128
public static ServiceAccountKey setupKeyFlow(CoreConfiguration cfg)
129-
throws CredentialsInFileNotFoundException, IOException {
129+
throws IOException {
130130
return setupKeyFlow(cfg, new EnvironmentVariables());
131131
}
132132

133+
/**
134+
* Sets up the KeyFlow Authentication
135+
*
136+
* @param cfg Configuration
137+
* @param env Environment variables
138+
* @return Service account key
139+
* @throws CredentialsInFileNotFoundException thrown when no service account key or private key
140+
* can be found
141+
* @throws IOException thrown when a file can not be found
142+
*/
133143
protected static ServiceAccountKey setupKeyFlow(CoreConfiguration cfg, EnvironmentVariables env)
134-
throws CredentialsInFileNotFoundException, IOException {
144+
throws IOException {
135145
// Explicit config in code
136146
if (Utils.isStringSet(cfg.getServiceAccountKey())) {
137147
ServiceAccountKey saKey = ServiceAccountKey.loadFromJson(cfg.getServiceAccountKey());
@@ -184,13 +194,21 @@ protected static ServiceAccountKey setupKeyFlow(CoreConfiguration cfg, Environme
184194
return saKey;
185195
}
186196

197+
/**
198+
* Loads the private key into the service account key
199+
*
200+
* @param cfg Configuration
201+
* @param env Environment variables
202+
* @param saKey Service account key
203+
* @throws PrivateKeyNotFoundException if the private key could not be found
204+
*/
187205
protected static void loadPrivateKey(
188-
CoreConfiguration cfg, EnvironmentVariables env, ServiceAccountKey saKey)
189-
throws PrivateKeyNotFoundException {
190-
if (!saKey.getCredentials().isPrivateKeySet()) {
206+
CoreConfiguration cfg, EnvironmentVariables env, ServiceAccountKey saKey) {
207+
ServiceAccountCredentials credentials = saKey.getCredentials();
208+
if (!credentials.isPrivateKeySet()) {
191209
try {
192210
String privateKey = getPrivateKey(cfg, env);
193-
saKey.getCredentials().setPrivateKey(privateKey);
211+
credentials.setPrivateKey(privateKey);
194212
} catch (CredentialsInFileNotFoundException | IOException e) {
195213
throw new PrivateKeyNotFoundException("could not find private key", e);
196214
}
@@ -228,7 +246,7 @@ protected static void loadPrivateKey(
228246
* pathKey can not be found
229247
*/
230248
private static String getPrivateKey(CoreConfiguration cfg, EnvironmentVariables env)
231-
throws CredentialsInFileNotFoundException, IOException {
249+
throws IOException {
232250
// Explicit code config
233251
// Get private key
234252
if (Utils.isStringSet(cfg.getPrivateKey())) {
@@ -286,7 +304,7 @@ private static String getPrivateKey(CoreConfiguration cfg, EnvironmentVariables
286304
*/
287305
protected static String readValueFromCredentialsFile(
288306
String path, String valueKey, String pathKey)
289-
throws IOException, CredentialsInFileNotFoundException {
307+
throws IOException {
290308
// Read credentials file
291309
String fileContent =
292310
new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);

0 commit comments

Comments
 (0)