@VisibleForTesting void setSelectedProject(CloudProject cloudProject) { if (cloudProject != null && !Strings.isNullOrEmpty(cloudProject.googleUsername())) { Optional<CredentialedUser> loggedInUser = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (loggedInUser.isPresent()) { selectedProjectsByAccount.put(loggedInUser.get(), cloudProject.projectName()); accountComboBox.setSelectedItem(loggedInUser.get()); } } else { // no project set, show active user projects by default. accountComboBox.setSelectedItem(Services.getLoginService().getActiveUser()); } }
private void loadRepositories(@Nullable Runnable onComplete) { if (cloudProject == null) { return; } Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (!user.isPresent()) { return; } if (repositories == null) { repositories = new ProjectRepositoriesModelItem(); } repositories.loadRepositories(cloudProject.projectId(), user.get(), onComplete); }
private Optional<Path> doStageCredentials(String googleUsername) { Optional<CredentialedUser> projectUser = Services.getLoginService().getLoggedInUser(googleUsername); GoogleLoginState googleLoginState; if (projectUser.isPresent()) { googleLoginState = projectUser.get().getGoogleLoginState(); } else { return Optional.empty(); } String clientId = googleLoginState.fetchOAuth2ClientId(); String clientSecret = googleLoginState.fetchOAuth2ClientSecret(); String refreshToken = googleLoginState.fetchOAuth2RefreshToken(); Map<String, String> credentialMap = ImmutableMap.of( CLIENT_ID_LABEL, clientId, CLIENT_SECRET_LABEL, clientSecret, REFRESH_TOKEN_LABEL, refreshToken, GCLOUD_USER_TYPE_LABEL, GCLOUD_USER_TYPE); String jsonCredential = new Gson().toJson(credentialMap); try { credentialsPath = FileUtil.createTempFile( "tmp_google_application_default_credential", "json", true /* deleteOnExit */) .toPath(); Files.write(credentialsPath, jsonCredential.getBytes(Charsets.UTF_8)); return Optional.of(credentialsPath); } catch (IOException ex) { throw new RuntimeException(ex); } }
private RepositorySelector createInitializedSelector() { String mockUserId = "mockUser"; CredentialedUser mockUser = mock(CredentialedUser.class); when(googleLoginService.getLoggedInUser(mockUserId)).thenReturn(Optional.of(mockUser)); return new RepositorySelector( CloudProject.create("my-project", "my-project", mockUserId), false /*canCreateRepository*/); } }
private void mockUserLoggedIn(String username) { when(mockLoginService.isLoggedIn()).thenReturn(true); CredentialedUser mockUser = mock(CredentialedUser.class); when(mockLoginService.getLoggedInUser(TEST_PROJECT.googleUsername())) .thenReturn(Optional.of(mockUser)); when(mockUser.getEmail()).thenReturn(username); } }
@Nullable private Debugger getCloudDebuggerClient() { CloudProject cloudProject = projectSelector.getSelectedProject(); CredentialedUser credentialedUser = cloudProject == null ? null : Services.getLoginService() .getLoggedInUser(cloudProject.googleUsername()) .orElse(null); if (this.credentialedUser == credentialedUser) { return cloudDebuggerClient; } this.credentialedUser = credentialedUser; cloudDebuggerClient = this.credentialedUser != null ? CloudDebuggerClient.getLongTimeoutClient(this.credentialedUser.getEmail()) : null; return cloudDebuggerClient; }
@Nullable private String getCurrentUrlText() { CloudProject selectedProject = projectSelector.getSelectedProject(); Optional<CredentialedUser> selectedUser = selectedProject == null ? Optional.empty() : Services.getLoginService().getLoggedInUser(selectedProject.googleUsername()); if (selectedProject == null || !selectedUser.isPresent() || StringUtil.isEmpty(repositorySelector.getText())) { return null; } return GcpHttpAuthDataProvider.getGcpUrl( selectedProject.projectId(), repositorySelector.getText()); }
/** Mocks list of currently signed in users returned by login service. First user is active. */ private void mockUserList(List<CredentialedUser> userList) { Map<String, CredentialedUser> emailUserMap = userList .stream() .collect(Collectors.toMap(CredentialedUser::getEmail, Function.identity())); when(googleLoginService.getAllUsers()).thenReturn(emailUserMap); when(googleLoginService.getActiveUser()).thenReturn(userList.get(0)); for (CredentialedUser user : userList) { when(googleLoginService.ensureLoggedIn(user.getEmail())).thenReturn(true); when(googleLoginService.getLoggedInUser(user.getEmail())).thenReturn(Optional.of(user)); } }
@Override protected void doOKAction() { CloudProject selectedProject = projectSelector.getSelectedProject(); projectId = selectedProject.projectId(); repositoryId = repositorySelector.getText(); remoteName = remoteNameSelector.getText(); credentialedUser = Services.getLoginService().getLoggedInUser(selectedProject.googleUsername()).orElse(null); super.doOKAction(); } }
/** * Fetches the list of {@link Role} for the supplied {@link CloudProject} by querying the Iam API. */ static List<Role> getServiceAccountRoles(CloudProject cloudProject) { Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (!user.isPresent()) { LOG.error("Cannot fetch service account roles: logged in user not found."); return ImmutableList.of(); } Iam iam = GoogleApiClientFactory.getInstance().getIamClient(user.get().getCredential()); try { return iam.roles().list().execute().getRoles(); } catch (IOException e) { LOG.warn("Exception occurred attempting to fetch service account roles"); return ImmutableList.of(); } }
/** * Updates the text of the panel as follows: if no project is selected, no message is displayed, * if the project represents a valid project, the project details are displayed, if the project * represents an invalid project, an error message is displayed. */ private void refreshApplicationInfoPanel(CloudProject selectedProject) { if (selectedProject == null) { applicationInfoPanel.clearMessage(); return; } Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(selectedProject.googleUsername()); if (user.isPresent()) { applicationInfoPanel.refresh(selectedProject.projectId(), user.get().getCredential()); } else { applicationInfoPanel.setMessage( AppEngineMessageBundle.getString("appengine.infopanel.no.region"), true /* isError*/); } }
void refresh() { showNotificationPanel(); if (!Services.getLoginService().isLoggedIn()) { notificationLabel.setText( GoogleCloudStorageMessageBundle.message("gcs.panel.bucket.listing.not.logged.in")); } else if (projectSelector.getSelectedProject() == null) { notificationLabel.setText( GoogleCloudStorageMessageBundle.message("gcs.panel.bucket.listing.no.project.selected")); } else { CloudProject cloudProject = projectSelector.getSelectedProject(); Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (user.isPresent()) { loadAndDisplayBuckets(cloudProject.projectId(), user.get()); } else { notificationLabel.setText( GoogleCloudStorageMessageBundle.message( "gcs.panel.bucket.listing.error.loading.buckets")); log.warn("Cloud not load credentialed user for GCS operation. User may not be logged."); } } }
private void updateCloudProjectSelection(CloudProject selection) { projectNameLabel.setHyperlinkText(selection.projectName()); projectAccountSeparatorLabel.setVisible(true); // first just show account email, then expand with name/picture if this account is signed in. // if not signed in, hide icon and account name completely and prompt to sign in. accountInfoLabel.setHyperlinkText(selection.googleUsername()); IntegratedGoogleLoginService loginService = Services.getLoginService(); Optional<CredentialedUser> loggedInUser = loginService.getLoggedInUser(selection.googleUsername()); if (!loggedInUser.isPresent()) { accountInfoLabel.setHyperlinkText( GoogleCloudCoreMessageBundle.message( "cloud.project.selector.not.signed.in", selection.googleUsername())); } else if (loggedInUser.isPresent()) { accountInfoLabel.setHyperlinkText( String.format( "%s (%s)", Strings.nullToEmpty(loggedInUser.get().getName()), loggedInUser.get().getEmail())); } accountInfoLabel.setIcon( loginService.isLoggedIn() ? GoogleLoginIcons.getScaledUserIcon(ACCOUNT_ICON_SIZE, loggedInUser.orElse(null)) : null); }
Services.getLoginService().getLoggedInUser(cloudProject.googleUsername());
@Test public void projectSelector_validProjectSelected_infoPanelShowsProjectDetails() { String projectId = "projectId"; String googleUsername = "some-user-id"; CloudProject project = CloudProject.create(projectId, projectId, googleUsername); when(credentialedUser.getCredential()).thenReturn(credential); when(mockGoogleLoginService.getLoggedInUser(googleUsername)) .thenReturn(Optional.of(credentialedUser)); when(projectSelector.getSelectedProject()).thenReturn(project); deploymentConfiguration.setCloudProjectName(projectId); deploymentConfiguration.setGoogleUsername(googleUsername); configurationPanel.resetEditorFrom(deploymentConfiguration); verify(infoPanel).refresh(projectId, credential); } }
@Test public void testStageCredentials_withValidCreds() throws Exception { String username = "jones@gmail.com"; String clientId = "clientId"; String clientSecret = "clientSecret"; String refreshToken = "refreshToken"; when(googleLoginService.ensureLoggedIn(username)).thenReturn(true); when(googleLoginService.getLoggedInUser(username)).thenReturn(Optional.of(credentialedUser)); when(credentialedUser.getGoogleLoginState()).thenReturn(loginState); when(loginState.fetchOAuth2ClientId()).thenReturn(clientId); when(loginState.fetchOAuth2ClientSecret()).thenReturn(clientSecret); when(loginState.fetchOAuth2RefreshToken()).thenReturn(refreshToken); helper.stageCredentials(username); Path credentialFile = helper.getCredentialsPath(); Reader credentialFileReader = Files.newBufferedReader(credentialFile, Charset.defaultCharset()); Map jsonMap = new Gson().fromJson(credentialFileReader, Map.class); credentialFileReader.close(); assertEquals(clientId, jsonMap.get("client_id")); assertEquals(clientSecret, jsonMap.get("client_secret")); assertEquals(refreshToken, jsonMap.get("refresh_token")); assertEquals("authorized_user", jsonMap.get("type")); Files.delete(credentialFile); }
@Before public void setUp() throws IOException { when(googleLoginService.getLoggedInUser("user")).thenReturn(Optional.of(user)); when(progressManager.getProgressIndicator()).thenReturn(progressIndicator); when(apiClientFactory.getIamClient(any())).thenReturn(iam); when(apiClientFactory.getCloudResourceManagerClient(any())).thenReturn(resourceManager); setupFakeServiceAccount(); setupFakeServiceAccountKey(); setupFakePolicy(); setupMockIamClient(); setupMockResourceManagerClient(); testFixture .getProject() .getMessageBus() .connect() .subscribe(Notifications.TOPIC, notifications); // TODO: consider shutting down timer instead when clear what is creating the timer. ThreadTracker.longRunningThreadCreated(ApplicationManager.getApplication(), "Timer-0"); }
@Override public void showPopup(RelativePoint showTarget) { Optional<CredentialedUser> user = cloudProject == null ? Optional.empty() : Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (user.isPresent()) { if (popup == null || popup.isDisposed()) { panel = new RepositoryPanel(); ComponentPopupBuilder popupBuilder = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, null); popup = popupBuilder.createPopup(); } if (!popup.isVisible()) { popup.show(showTarget); } } else { panel = new ProjectNotSelectedPanel(); ComponentPopupBuilder popupBuilder = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, null); popup = popupBuilder.createPopup(); popup.show(showTarget); } }
selectedProject == null ? Optional.empty() : Services.getLoginService().getLoggedInUser(selectedProject.googleUsername());
Services.getLoginService().getLoggedInUser(selectedProject.googleUsername());