Skip to content

Commit 6a2ad7e

Browse files
authored
Add logging and credentials validation (#3)
Signed-off-by: Oleksandr Vyshniak <ext-oleksandr.vyshniak@here.com>
1 parent 3235f0a commit 6a2ad7e

2 files changed

Lines changed: 99 additions & 3 deletions

File tree

src/main/java/com/here/platform/artifact/gradle/CredentialsResolver.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
*/
1919
package com.here.platform.artifact.gradle;
2020

21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
2124
import java.io.*;
2225
import java.util.Properties;
2326

2427
import static org.apache.http.util.TextUtils.isEmpty;
2528

2629
/**
2730
* Resolves credentials based on system configuration. Credentials precedence:
28-
* <p>
2931
* <ol>
3032
* <li>-DhereCredentialsFile system property
3133
* <li>HERE_CREDENTIALS_FILE environment variable
@@ -35,14 +37,20 @@
3537
*/
3638
public class CredentialsResolver {
3739

40+
public static final String ACCESS_KEY_ID = "here.access.key.id";
41+
public static final String ACCESS_KEY_SECRET = "here.access.key.secret";
42+
public static final String TOKEN_ENDPOINT_URL = "here.token.endpoint.url";
43+
public static final String CLIENT_ID = "here.client.id";
44+
45+
private static final Logger LOG = LoggerFactory.getLogger(CredentialsResolver.class);
46+
3847
private static final String HERE_CREDENTIALS_PROPERTY = "hereCredentialsFile";
3948
private static final String HERE_CREDENTIALS_STRING_ENV = "HERE_CREDENTIALS_STRING";
4049
private static final String HERE_CREDENTIALS_PATH = ".here/credentials.properties";
4150
private static final String HERE_CREDENTIALS_ENV = "HERE_CREDENTIALS_FILE";
4251

4352
/**
4453
* Resolve credentials based on a precedence:
45-
* <p>
4654
* <ol>
4755
* <li>-DhereCredentialsFile system property
4856
* <li>HERE_CREDENTIALS_FILE environment variable
@@ -64,14 +72,24 @@ public Properties resolveCredentials() {
6472
} else {
6573
loadCredentialsFromFile(properties, new File(System.getProperty("user.home"), HERE_CREDENTIALS_PATH));
6674
}
75+
validateCredentials(properties);
6776
return properties;
6877
}
6978

7079
private File resolveFile() {
7180
File file = null;
7281
String systemPropertyFile = System.getProperty(HERE_CREDENTIALS_PROPERTY);
73-
if (isEmpty(systemPropertyFile)) {
82+
if (!isEmpty(systemPropertyFile)) {
83+
LOG
84+
.debug("Found property file value at System Property {}: {}", HERE_CREDENTIALS_PROPERTY,
85+
systemPropertyFile);
86+
} else {
7487
systemPropertyFile = System.getenv(HERE_CREDENTIALS_ENV);
88+
if (!isEmpty(systemPropertyFile)) {
89+
LOG
90+
.debug("Found property file at Environment Property {}: {}", HERE_CREDENTIALS_ENV,
91+
systemPropertyFile);
92+
}
7593
}
7694
if (!isEmpty(systemPropertyFile)) {
7795
file = new File(systemPropertyFile);
@@ -80,22 +98,44 @@ private File resolveFile() {
8098
}
8199

82100
private void loadCredentialsFromFile(Properties properties, File file) {
101+
LOG.debug("Using here credentials file: {}", file.getAbsolutePath());
83102
if (file.exists() && file.canRead()) {
103+
LOG.debug("Attempting to read credentials file at: {}", file.getAbsolutePath());
84104
try (InputStream in = new FileInputStream(file)) {
85105
properties.load(in);
86106
} catch (IOException exp) {
107+
LOG.error("Unable to read client credentials at {}", file.getAbsolutePath(), exp);
87108
throw new RuntimeException("Unable to read client credentials at " + file.getAbsolutePath(), exp);
88109
}
110+
} else {
111+
LOG.warn("Unable to read configured file: {}", file.getAbsolutePath());
89112
}
90113
}
91114

92115
private void loadCredentialsFromString(Properties properties, String credentialsString) {
116+
LOG.debug("Attempting to create credentials from environment variable");
93117
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(credentialsString.getBytes());
94118
try {
95119
properties.load(byteArrayInputStream);
96120
} catch (IOException exp) {
121+
LOG
122+
.error("Unable to create client credentials from environment variable {}", HERE_CREDENTIALS_STRING_ENV,
123+
exp);
97124
throw new RuntimeException(
98125
"Unable to create client credentials from environment variable " + HERE_CREDENTIALS_STRING_ENV, exp);
99126
}
100127
}
128+
129+
void validateCredentials(Properties properties) {
130+
propertyMustExists(properties, ACCESS_KEY_ID);
131+
propertyMustExists(properties, ACCESS_KEY_SECRET);
132+
propertyMustExists(properties, TOKEN_ENDPOINT_URL);
133+
propertyMustExists(properties, CLIENT_ID);
134+
}
135+
136+
private void propertyMustExists(Properties properties, String propertyName) {
137+
if (properties.getProperty(propertyName) == null) {
138+
throw new RuntimeException("Credentials don't contain the property: " + propertyName);
139+
}
140+
}
101141
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2019-2025 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
package com.here.platform.artifact.gradle;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.Mockito;
23+
24+
import java.util.Properties;
25+
26+
import static org.mockito.ArgumentMatchers.eq;
27+
import static org.mockito.Mockito.mock;
28+
import static org.junit.jupiter.api.Assertions.assertThrows;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
31+
class CredentialsResolverTest {
32+
33+
private CredentialsResolver credentialsResolver = new CredentialsResolver();
34+
35+
@Test
36+
public void testValidateCredentials() {
37+
Properties hereCredentialsMock = mock(Properties.class);
38+
Mockito.when(hereCredentialsMock.getProperty(eq("here.token.endpoint.url"))).thenReturn("someUrl");
39+
Mockito.when(hereCredentialsMock.getProperty(eq("here.access.key.id"))).thenReturn("accessKey");
40+
Mockito.when(hereCredentialsMock.getProperty(eq("here.access.key.secret"))).thenReturn("accessKeySecret");
41+
Mockito.when(hereCredentialsMock.getProperty(eq("here.client.id"))).thenReturn("clientId");
42+
credentialsResolver.validateCredentials(hereCredentialsMock);
43+
}
44+
45+
@Test
46+
public void testValidateCredentialsWithoutEndpointUrl() {
47+
Properties hereCredentialsMock = mock(Properties.class);
48+
Mockito.when(hereCredentialsMock.getProperty(eq("here.access.key.id"))).thenReturn("accessKey");
49+
Mockito.when(hereCredentialsMock.getProperty(eq("here.access.key.secret"))).thenReturn("accessKeySecret");
50+
Mockito.when(hereCredentialsMock.getProperty(eq("here.client.id"))).thenReturn("clientId");
51+
RuntimeException exception = assertThrows(RuntimeException.class,
52+
() -> credentialsResolver.validateCredentials(hereCredentialsMock));
53+
assertEquals("Credentials don't contain the property: here.token.endpoint.url", exception.getMessage());
54+
}
55+
56+
}

0 commit comments

Comments
 (0)