Search in sources :

Example 1 with AccessTokenAzureManager

use of com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager in project azure-tools-for-java by Microsoft.

the class SignInDialog method doCreateServicePrincipal.

private void doCreateServicePrincipal() {
    setErrorMessage(null);
    AdAuthManager adAuthManager = null;
    try {
        adAuthManager = AdAuthManager.getInstance();
        if (adAuthManager.isSignedIn()) {
            adAuthManager.signOut();
        }
        signInAsync();
        if (!adAuthManager.isSignedIn()) {
            // canceled by the user
            System.out.println(">> Canceled by the user");
            return;
        }
        AccessTokenAzureManager accessTokenAzureManager = new AccessTokenAzureManager();
        SubscriptionManager subscriptionManager = accessTokenAzureManager.getSubscriptionManager();
        IRunnableWithProgress op = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.beginTask("Getting Subscription List...", IProgressMonitor.UNKNOWN);
                try {
                    subscriptionManager.getSubscriptionDetails();
                } catch (Exception ex) {
                    System.out.println("run@ProgressDialog@doCreateServicePrincipal@SignInDialog: " + ex.getMessage());
                    LOG.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "run@ProgressDialog@doCreateServicePrincipal@SignInDialogg", ex));
                }
            }
        };
        new ProgressMonitorDialog(this.getShell()).run(true, false, op);
        SrvPriSettingsDialog d = SrvPriSettingsDialog.go(this.getShell(), subscriptionManager.getSubscriptionDetails());
        List<SubscriptionDetail> subscriptionDetailsUpdated;
        String destinationFolder;
        if (d != null) {
            subscriptionDetailsUpdated = d.getSubscriptionDetails();
            destinationFolder = d.getDestinationFolder();
        } else {
            System.out.println(">> Canceled by the user");
            return;
        }
        Map<String, List<String>> tidSidsMap = new HashMap<>();
        for (SubscriptionDetail sd : subscriptionDetailsUpdated) {
            if (sd.isSelected()) {
                System.out.format(">> %s\n", sd.getSubscriptionName());
                String tid = sd.getTenantId();
                List<String> sidList;
                if (!tidSidsMap.containsKey(tid)) {
                    sidList = new LinkedList<>();
                } else {
                    sidList = tidSidsMap.get(tid);
                }
                sidList.add(sd.getSubscriptionId());
                tidSidsMap.put(tid, sidList);
            }
        }
        SrvPriCreationStatusDialog d1 = SrvPriCreationStatusDialog.go(this.getShell(), tidSidsMap, destinationFolder);
        if (d1 == null) {
            System.out.println(">> Canceled by the user");
            return;
        }
        String path = d1.getSelectedAuthFilePath();
        if (path == null) {
            System.out.println(">> No file was created");
            return;
        }
        textAuthenticationFilePath.setText(path);
        fileDialog.setFilterPath(destinationFolder);
    } catch (Exception ex) {
        ex.printStackTrace();
        LOG.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "doCreateServicePrincipal@SignInDialog", ex));
    } finally {
        if (adAuthManager != null) {
            adAuthManager.signOut();
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) HashMap(java.util.HashMap) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) AccessTokenAzureManager(com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager) AdAuthManager(com.microsoft.azuretools.authmanage.AdAuthManager) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) PartInitException(org.eclipse.ui.PartInitException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AuthCanceledException(com.microsoft.azuretools.adauth.AuthCanceledException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) LinkedList(java.util.LinkedList) List(java.util.List)

Example 2 with AccessTokenAzureManager

use of com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager in project azure-tools-for-java by Microsoft.

the class SignInWindow method doCreateServicePrincipal.

private void doCreateServicePrincipal() {
    AdAuthManager adAuthManager = null;
    try {
        adAuthManager = AdAuthManager.getInstance();
        if (adAuthManager.isSignedIn()) {
            adAuthManager.signOut();
        }
        signInAsync();
        if (!adAuthManager.isSignedIn()) {
            // canceled by the user
            System.out.println(">> Canceled by the user");
            return;
        }
        AccessTokenAzureManager accessTokenAzureManager = new AccessTokenAzureManager();
        SubscriptionManager subscriptionManager = accessTokenAzureManager.getSubscriptionManager();
        ProgressManager.getInstance().run(new Task.Modal(project, "Load Subscriptions Progress", true) {

            @Override
            public void run(ProgressIndicator progressIndicator) {
                progressIndicator.setText("Loading subscriptions...");
                try {
                    progressIndicator.setIndeterminate(true);
                    subscriptionManager.getSubscriptionDetails();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    //LOGGER.error("doCreateServicePrincipal::Task.Modal", ex);
                    ApplicationManager.getApplication().invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            ErrorWindow.show(project, ex.getMessage(), "Load Subscription Error");
                        }
                    });
                }
            }
        });
        SrvPriSettingsDialog d = SrvPriSettingsDialog.go(subscriptionManager.getSubscriptionDetails(), project);
        List<SubscriptionDetail> subscriptionDetailsUpdated;
        String destinationFolder;
        if (d != null) {
            subscriptionDetailsUpdated = d.getSubscriptionDetails();
            destinationFolder = d.getDestinationFolder();
        } else {
            System.out.println(">> Canceled by the user");
            return;
        }
        Map<String, List<String>> tidSidsMap = new HashMap<>();
        for (SubscriptionDetail sd : subscriptionDetailsUpdated) {
            if (sd.isSelected()) {
                System.out.format(">> %s\n", sd.getSubscriptionName());
                String tid = sd.getTenantId();
                List<String> sidList;
                if (!tidSidsMap.containsKey(tid)) {
                    sidList = new LinkedList<>();
                } else {
                    sidList = tidSidsMap.get(tid);
                }
                sidList.add(sd.getSubscriptionId());
                tidSidsMap.put(tid, sidList);
            }
        }
        SrvPriCreationStatusDialog d1 = SrvPriCreationStatusDialog.go(tidSidsMap, destinationFolder, project);
        if (d1 == null) {
            System.out.println(">> Canceled by the user");
            return;
        }
        String path = d1.getSelectedAuthFilePath();
        if (path == null) {
            System.out.println(">> No file was created");
            return;
        }
        authFileTextField.setText(path);
        fileChooser.setCurrentDirectory(new File(destinationFolder));
    } catch (Exception ex) {
        ex.printStackTrace();
        //LOGGER.error("doCreateServicePrincipal", ex);
        ErrorWindow.show(project, ex.getMessage(), "Get Subscription Error");
    } finally {
        if (adAuthManager != null) {
            try {
                System.out.println(">> Signing out...");
                adAuthManager.signOut();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : Task(com.intellij.openapi.progress.Task) HashMap(java.util.HashMap) AccessTokenAzureManager(com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager) AdAuthManager(com.microsoft.azuretools.authmanage.AdAuthManager) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) IOException(java.io.IOException) AuthCanceledException(com.microsoft.azuretools.adauth.AuthCanceledException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) LinkedList(java.util.LinkedList) List(java.util.List) File(java.io.File)

Example 3 with AccessTokenAzureManager

use of com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager 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);
    }
}
Also used : Path(java.nio.file.Path) AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) IUIFactory(com.microsoft.azuretools.authmanage.interact.IUIFactory) AccessTokenAzureManager(com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) IOException(java.io.IOException) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) File(java.io.File) IOException(java.io.IOException) AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Example 4 with AccessTokenAzureManager

use of com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager in project azure-tools-for-java by Microsoft.

the class AuthMethodManager method getAzureManager.

private AzureManager getAzureManager(AuthMethod authMethod) throws IOException {
    if (azureManager != null)
        return azureManager;
    switch(authMethod) {
        case AD:
            if (StringUtils.isNullOrEmpty(authMethodDetails.getAccountEmail())) {
                return null;
            }
            azureManager = new AccessTokenAzureManager();
            break;
        case SP:
            String credFilePath = authMethodDetails.getCredFilePath();
            if (StringUtils.isNullOrEmpty(credFilePath)) {
                return null;
            }
            Path filePath = Paths.get(credFilePath);
            if (!Files.exists(filePath)) {
                cleanAll();
                INotification nw = CommonSettings.getUiFactory().getNotificationWindow();
                nw.deliver("Credential File Error", "File doesn't exist: " + filePath.toString());
                return null;
            }
            azureManager = new ServicePrincipalAzureManager(new File(credFilePath));
    }
    return azureManager;
}
Also used : Path(java.nio.file.Path) INotification(com.microsoft.azuretools.authmanage.interact.INotification) AccessTokenAzureManager(com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager) ServicePrincipalAzureManager(com.microsoft.azuretools.sdkmanage.ServicePrincipalAzureManager) File(java.io.File)

Aggregations

AccessTokenAzureManager (com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager)4 SubscriptionManager (com.microsoft.azuretools.authmanage.SubscriptionManager)3 SubscriptionDetail (com.microsoft.azuretools.authmanage.models.SubscriptionDetail)3 File (java.io.File)3 IOException (java.io.IOException)3 AuthCanceledException (com.microsoft.azuretools.adauth.AuthCanceledException)2 AdAuthManager (com.microsoft.azuretools.authmanage.AdAuthManager)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 AuthMethodManager (com.microsoft.azuretools.authmanage.AuthMethodManager)1 INotification (com.microsoft.azuretools.authmanage.interact.INotification)1 IUIFactory (com.microsoft.azuretools.authmanage.interact.IUIFactory)1 AuthMethodDetails (com.microsoft.azuretools.authmanage.models.AuthMethodDetails)1 AzureManager (com.microsoft.azuretools.sdkmanage.AzureManager)1 ServicePrincipalAzureManager (com.microsoft.azuretools.sdkmanage.ServicePrincipalAzureManager)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1