use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.
the class Program method main.
@SuppressWarnings("unused")
public static void main(String[] args) {
LogManager.getLogManager().reset();
try {
try {
if (CommonSettings.getUiFactory() == null)
CommonSettings.setUiFactory(new UIFactory());
String wd = "AuthManageWorkingDir";
Path dirPath = Paths.get(System.getProperty("user.home"), wd);
if (!Files.exists(dirPath)) {
Files.createDirectory(dirPath);
}
CommonSettings.settingsBaseDir = dirPath.toString();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("--- Service Principals section ---");
// create with sp
//final File credFile = new File(System.getProperty("user.home") + "/ServicePrincipals/srv-pri.azureauth-778");
String path = "C:\\Users\\shch\\_ServicePrincipalsTest\\sp5\\sp-9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef_20161202161437.azureauth";
final File credFile = new File("C:\\Users\\shch\\_ServicePrincipalsTest\\sp5\\sp-9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef_20161202161437.azureauth");
AuthMethodDetails amd = new AuthMethodDetails();
amd.setAuthMethod(AuthMethod.SP);
amd.setCredFilePath(path);
AuthMethodManager amm = AuthMethodManager.getInstance();
amm.setAuthMethodDetails(amd);
AzureManager am = amm.getAzureManager();
SubscriptionManager subscriptionManager = am.getSubscriptionManager();
List<SubscriptionDetail> sdl = subscriptionManager.getSubscriptionDetails();
//AzureManager spManager = new ServicePrincipalAzureManager(credFile);
//printResourceGroups(spManager);
//printSubscriptions(spManager);
// create with token
//AzureManager atManager = new AccessTokenAzureManager(Constants.tenant);
//AzureManager atManager = new AccessTokenAzureManager();
//SubscriptionManager.getInstance().showSubscriptionDialog();
// AdAuthManager adAuthManager = AdAuthManager.getInstance();
System.out.println("=== Access token section ---");
// AuthenticationResult res = AdAuthManager.getInstance().signIn();
// UserInfo ui = res.getUserInfo();
// Setup working dir
String wd = "AuthManageWorkingDir";
Path dirPath = Paths.get(System.getProperty("user.home"), wd);
if (!Files.exists(dirPath)) {
Files.createDirectory(dirPath);
}
CommonSettings.settingsBaseDir = dirPath.toString();
// Setup uiFactory
if (CommonSettings.getUiFactory() == null)
CommonSettings.setUiFactory(new UIFactory());
//testReporter(dirPath.toString());
//graphApiAction();
//createSp();
// AzureManager am = AuthMethodManager.getInstance().getAzureManager(AuthMethod.AD);
// if (am == null) {
// return;
// }
//
// System.out.println("-- Subscription list ---");
// printSubscriptions(am);
// sidList = AdAuthManager.getInstance().getAccountSidList();
// for (String sid : AdAuthManager.getInstance().getAccountSidList()) {
// System.out.println(" - " + sid);
// }
//printResourceGroups(am);
//printTenants(atManager);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(1);
}
}
use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.
the class SignOutCommandHandler method getSignOutWarningMessage.
public static String getSignOutWarningMessage(@Nonnull AuthMethodManager authMethodManager) {
final AuthMethodDetails authMethodDetails = authMethodManager.getAuthMethodDetails();
if (authMethodDetails == null || authMethodDetails.getAuthType() == null) {
return "Do you really want to sign out?";
}
final AuthType authType = authMethodDetails.getAuthType();
final String warningMessage;
switch(authType) {
case SERVICE_PRINCIPAL:
warningMessage = String.format("Signed in using service principal \"%s\"", authMethodDetails.getClientId());
break;
case OAUTH2:
case DEVICE_CODE:
warningMessage = String.format("Signed in as %s(%s)", authMethodDetails.getAccountEmail(), authType.toString());
break;
case AZURE_CLI:
warningMessage = "Signed in with Azure CLI";
break;
default:
warningMessage = "Signed in by unknown authentication method.";
break;
}
return String.format("%s\nDo you really want to sign out? %s", warningMessage, authType == AuthType.AZURE_CLI ? "(This will not sign you out from Azure CLI)" : "");
}
use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.
the class AuthMethodManager method loadSettings.
@AzureOperation(name = "account|auth_setting.load", type = AzureOperation.Type.TASK)
private static AuthMethodDetails loadSettings() {
System.out.println("loading authMethodDetails...");
try {
String json = AzureStoreManager.getInstance().getIdeStore().getProperty(ACCOUNT, AUTH_METHOD_DETAIL, "");
if (StringUtils.isBlank(json)) {
FileStorage fs = new FileStorage(FILE_NAME_AUTH_METHOD_DETAILS, CommonSettings.getSettingsBaseDir());
byte[] data = fs.read();
json = new String(data);
AzureStoreManager.getInstance().getIdeStore().setProperty(ACCOUNT, AUTH_METHOD_DETAIL, json);
fs.removeFile();
}
if (StringUtils.isBlank(json)) {
System.out.println("No auth method details are saved.");
return new AuthMethodDetails();
}
return JsonHelper.deserialize(AuthMethodDetails.class, json);
} catch (IOException ignored) {
System.out.println("Failed to loading authMethodDetails settings. Use defaults.");
return new AuthMethodDetails();
}
}
use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.
the class AuthMethodManager method initAuthMethodManagerFromSettings.
private void initAuthMethodManagerFromSettings() {
EventUtil.executeWithLog(ACCOUNT, RESIGNIN, operation -> {
try {
AuthMethodDetails targetAuthMethodDetails = loadSettings();
if (targetAuthMethodDetails == null || targetAuthMethodDetails.getAuthMethod() == null) {
targetAuthMethodDetails = new AuthMethodDetails();
targetAuthMethodDetails.setAuthMethod(AuthMethod.IDENTITY);
} else {
// convert old auth method to new ones
switch(targetAuthMethodDetails.getAuthMethod()) {
case AZ:
{
targetAuthMethodDetails.setAuthType(AuthType.AZURE_CLI);
break;
}
case DC:
{
targetAuthMethodDetails.setAuthType(AuthType.DEVICE_CODE);
break;
}
case AD:
// we don't support it now
LOGGER.warn("The AD auth method is not supported now, ignore the credential.");
break;
case SP:
targetAuthMethodDetails.setAuthType(AuthType.SERVICE_PRINCIPAL);
break;
default:
break;
}
targetAuthMethodDetails.setAuthMethod(AuthMethod.IDENTITY);
}
authMethodDetails = this.identityAzureManager.restoreSignIn(targetAuthMethodDetails).block();
List<SubscriptionDetail> persistSubscriptions = identityAzureManager.getSubscriptionManager().loadSubscriptions();
if (CollectionUtils.isNotEmpty(persistSubscriptions)) {
List<String> savedSubscriptionList = persistSubscriptions.stream().filter(SubscriptionDetail::isSelected).map(SubscriptionDetail::getSubscriptionId).distinct().collect(Collectors.toList());
identityAzureManager.selectSubscriptionByIds(savedSubscriptionList);
}
initFuture.complete(true);
// pre-load regions
AzureAccount az = com.microsoft.azure.toolkit.lib.Azure.az(AzureAccount.class);
Optional.of(identityAzureManager.getSelectedSubscriptionIds()).ifPresent(sids -> sids.stream().limit(5).forEach(az::listRegions));
final String authMethod = authMethodDetails.getAuthMethod() == null ? "Empty" : authMethodDetails.getAuthMethod().name();
final Map<String, String> telemetryProperties = new HashMap<String, String>() {
{
put(SIGNIN_METHOD, authMethod);
put(AZURE_ENVIRONMENT, CommonSettings.getEnvironment().getName());
}
};
EventUtil.logEvent(EventType.info, operation, telemetryProperties);
notifySignInEventListener();
} catch (RuntimeException exception) {
initFuture.complete(true);
EventUtil.logError(operation, ErrorType.systemError, exception, null, null);
this.authMethodDetails = new AuthMethodDetails();
this.authMethodDetails.setAuthMethod(AuthMethod.IDENTITY);
notifySignOutEventListener();
}
return this;
});
}
use of com.microsoft.azuretools.authmanage.models.AuthMethodDetails in project azure-tools-for-java by Microsoft.
the class SignInCommandHandler method loginNonDeviceCodeSingle.
private static Single<AuthMethodDetails> loginNonDeviceCodeSingle(AuthConfiguration auth) {
final AzureString title = AzureOperationBundle.title("account.sign_in");
final AzureTask<AuthMethodDetails> task = new AzureTask<>(null, title, true, () -> doLogin(AzureTaskContext.current().getTask().getMonitor(), auth));
return AzureTaskManager.getInstance().runInBackgroundAsObservable(task).toSingle();
}
Aggregations