1818 */
1919package com .here .platform .artifact .gradle ;
2020
21+ import org .slf4j .Logger ;
22+ import org .slf4j .LoggerFactory ;
23+
2124import java .io .*;
2225import java .util .Properties ;
2326
2427import 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
3537 */
3638public 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}
0 commit comments