allow auth library to read settings from disk

This commit is contained in:
Yang Cao (VSNC) 2017-03-23 12:52:04 -04:00
Родитель 15d2088ea7
Коммит d664b74554
4 изменённых файлов: 112 добавлений и 6 удалений

Просмотреть файл

@ -0,0 +1,102 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root.
package com.microsoft.alm.helpers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* Utility class to read properties from a file.
*
* If the property is not found from the setting file, it will fallback to System properties.
*/
public class SettingsHelper {
private static final Logger logger = LoggerFactory.getLogger(SettingsHelper.class);
/**
* Searching for TeamServices/auth_settings.properties file in the following folders.
*/
private static Environment.SpecialFolder[] PARENT_FOLDERS = new Environment.SpecialFolder[] {
Environment.SpecialFolder.LocalApplicationData,
Environment.SpecialFolder.ApplicationData,
Environment.SpecialFolder.UserProfile};
private static String PROGRAM_FOLDER = SystemHelper.isWindows() ? "VSTeamServicesAuthPlugin" : ".VSTeamServicesAuthPlugin";
private static String FILE_NAME = "settings.properties";
private final Properties properties = new Properties();
private static SettingsHelper instance;
public static synchronized SettingsHelper getInstance() {
if (instance == null) {
instance = new SettingsHelper();
}
return instance;
}
private SettingsHelper() {
// Trying to locate the settings file one by one
for (final Environment.SpecialFolder candidate : PARENT_FOLDERS) {
final String path = Environment.getFolderPath(candidate);
final File folder = new File(path, PROGRAM_FOLDER);
logger.info("Searching for {}", Path.combine(folder.getAbsolutePath(), FILE_NAME));
if (folder.exists()) {
final File potential = new File(folder, FILE_NAME);
if (potential.exists() && potential.isFile() && potential.canRead()) {
logger.info("Found setting file, trying to load properties from {}", potential.getAbsolutePath());
try {
properties.load(new FileReader(potential));
logger.info("Properties loaded.");
} catch (Throwable t) {
logger.warn("Failed to load properties.", t);
properties.clear();
}
// Do not look further when we found a potential file
break;
}
}
}
}
/**
* Get named property. Values from the setting files take precedence over System properties.
* The method returns {@code null} if the property is not defined.
*
* @param name Property name
* @return value of the property. {@code null} if property is not defined.
*/
public synchronized String getProperty(final String name) {
String result = properties.getProperty(name);
if (result == null) {
result = System.getProperty(name);
}
return result;
}
/**
* Get named property. Values from the setting files take precedence over System properties.
*
* If the property is not defined, return the default value.
*
* @param name Property name
* @param defaultValue This value will be returned if property is not defined
* @return value of the property, or the defaultValue.
*/
public synchronized String getProperty(final String name, final String defaultValue) {
final String result = getProperty(name);
return result == null ? defaultValue : result;
}
}

Просмотреть файл

@ -11,6 +11,7 @@ import com.microsoft.alm.helpers.Action;
import com.microsoft.alm.helpers.Debug;
import com.microsoft.alm.helpers.HttpClient;
import com.microsoft.alm.helpers.HttpClientImpl;
import com.microsoft.alm.helpers.SettingsHelper;
import com.microsoft.alm.helpers.StringHelper;
import com.microsoft.alm.oauth2.useragent.AuthorizationException;
import com.microsoft.alm.secret.Token;
@ -220,7 +221,7 @@ public class OAuth2Authenticator extends BaseAuthenticator {
final AtomicReference<File> swtRuntime = new AtomicReference<File>();
final String defaultProviderName
= System.getProperty(USER_AGENT_PROVIDER_PROPERTY_NAME, JAVAFX_PROVIDER_NAME);
= SettingsHelper.getInstance().getProperty(USER_AGENT_PROVIDER_PROPERTY_NAME, JAVAFX_PROVIDER_NAME);
logger.info("Attempt to use oauth2-useragent provider: {}", defaultProviderName);

Просмотреть файл

@ -5,6 +5,7 @@ package com.microsoft.alm.provider;
import com.microsoft.alm.auth.Authenticator;
import com.microsoft.alm.auth.PromptBehavior;
import com.microsoft.alm.helpers.SettingsHelper;
import com.microsoft.alm.secret.Credential;
import com.microsoft.alm.secret.Token;
import com.microsoft.alm.secret.TokenPair;
@ -249,8 +250,9 @@ public class JaxrsClientProvider {
private void addProxySettings(final ClientConfig clientConfig) {
// favor http proxyHost
final String proxyHost = System.getProperty("http.proxyHost");
final String proxyPort = ObjectExtensions.<String>coalesce(System.getProperty("http.proxyPort"), "8080");
final String proxyHost = SettingsHelper.getInstance().getProperty("http.proxyHost");
final String proxyPort = ObjectExtensions.<String>coalesce(
SettingsHelper.getInstance().getProperty("http.proxyPort"), "8080");
if (proxyHost != null) {
final String proxyUrl = String.format("http://%s:%s", proxyHost, proxyPort);
@ -267,8 +269,8 @@ public class JaxrsClientProvider {
}
private SslConfigurator getSslConfigurator() {
final String trustStore = System.getProperty("javax.net.ssl.trustStore");
final String trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
final String trustStore = SettingsHelper.getInstance().getProperty("javax.net.ssl.trustStore");
final String trustStorePassword = SettingsHelper.getInstance().getProperty("javax.net.ssl.trustStorePassword");
final SslConfigurator sslConfigurator;
if (trustStore != null && trustStorePassword != null) {

Просмотреть файл

@ -4,6 +4,7 @@
package com.microsoft.alm.storage.posix.internal;
import com.microsoft.alm.helpers.Debug;
import com.microsoft.alm.helpers.SettingsHelper;
import com.microsoft.alm.helpers.SystemHelper;
import com.microsoft.alm.secret.Secret;
import com.microsoft.alm.storage.SecretStore;
@ -192,7 +193,7 @@ public abstract class GnomeKeyringBackedSecureStore<E extends Secret> implements
logger.info("Keyring is locked, most likely due to UI is unavailable or user logged in " +
"automatically without supplying a password.");
final boolean allowUnlock = Boolean.valueOf(System.getProperty(ALLOW_UNLOCK_KEYRING));
final boolean allowUnlock = Boolean.valueOf(SettingsHelper.getInstance().getProperty(ALLOW_UNLOCK_KEYRING));
if (allowUnlock) {
final int ret = INSTANCE.gnome_keyring_unlock_sync(GnomeKeyringLibrary.GNOME_KEYRING_DEFAULT, null);
return checkResult(ret, "Could not unlock keyring. GNOME Keyring is not available.");