Skip to content

Commit

Permalink
Support login by SSO (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi authored Aug 6, 2023
1 parent ac7af1f commit 24800b2
Show file tree
Hide file tree
Showing 11 changed files with 699 additions and 508 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
id "java"
id "maven-publish"
id "de.undercouch.download" version "5.3.0"
id "io.freefair.lombok" version "8.0.1"
}

group 'com.jfrog.ide'
Expand Down Expand Up @@ -56,7 +57,7 @@ dependencies {
implementation group: 'com.jfrog.xray.client', name: 'xray-client-java', version: '0.14.1'
implementation group: 'org.apache.commons', name: 'commons-collections4', version: '4.4'
implementation group: 'org.jfrog.filespecs', name: 'file-specs-java', version: '1.1.2'
implementation group: 'com.jfrog.ide', name: 'ide-plugins-common', version: '2.2.x-20230801.111526-1'
implementation group: 'com.jfrog.ide', name: 'ide-plugins-common', version: '2.2.x-20230806.132528-2'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
implementation group: 'com.google.guava', name: 'guava', version: '32.0.1-jre'
testImplementation group: 'org.mockito', name: 'mockito-inline', version: '4.2.0'
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/jfrog/ide/idea/ci/CiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ private void loadFirstBuild() {

private boolean scanPreconditionsMet() {
if (!GlobalSettings.getInstance().areArtifactoryCredentialsSet()) {
Logger.getInstance().info("CI integration disabled - Artifactory server is not configured.");
Logger.getInstance().debug("CI integration disabled - Artifactory server is not configured.");
return false;
}
if (StringUtils.isBlank(propertiesComponent.getValue(BUILDS_PATTERN_KEY))) {
Logger.getInstance().info("CI integration disabled - build name pattern is not set. " +
Logger.getInstance().debug("CI integration disabled - build name pattern is not set. " +
"Configure it under the JFrog CI Integration page in the configuration.");
return false;
}
Expand Down
33 changes: 7 additions & 26 deletions src/main/java/com/jfrog/ide/idea/configuration/GlobalSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,11 @@ public GlobalSettings getState() {
serverConfig.setPolicyType(this.serverConfig.getPolicyType());
serverConfig.setProject(this.serverConfig.getProject());
serverConfig.setWatches(this.serverConfig.getWatches());
serverConfig.setConnectionDetailsFromEnv(this.serverConfig.isConnectionDetailsFromEnv());
serverConfig.setConnectionRetries(this.serverConfig.getConnectionRetries());
serverConfig.setConnectionTimeout(this.serverConfig.getConnectionTimeout());

GlobalSettings settings = new GlobalSettings();
settings.serverConfig = serverConfig;
if (this.serverConfig.isConnectionDetailsFromEnv()) {
return settings;
}

settings.serverConfig.setPassword(null);
settings.serverConfig.setUsername(null);
settings.serverConfig.setUrl(this.serverConfig.getUrl());
Expand All @@ -96,19 +92,10 @@ public ServerConfigImpl getServerConfig() {
/**
* Method is called by Idea IS for reading the previously saved config file 'jfrogConfig.xml' from the disk.
* Check if previous configurations contain credentials, perform migration if necessary.
* If connection details loaded from environment, don't override them.
*
* @param serverConfig - configurations read from file.
*/
public void setServerConfig(@NotNull ServerConfigImpl serverConfig) {
if (serverConfig.isConnectionDetailsFromEnv()) {
// Load connection details from environment variables.
setAdvancedSettings(serverConfig);
this.serverConfig.setConnectionDetailsFromEnv(this.serverConfig.readConnectionDetailsFromEnv());
return;
}

// Load configuration from state.
setCommonConfigFields(serverConfig);
this.serverConfig.setCredentials(serverConfig.getCredentialsFromPasswordSafe());
}
Expand All @@ -119,16 +106,6 @@ public void setServerConfig(@NotNull ServerConfigImpl serverConfig) {
* @param serverConfig - the new configurations to update.
*/
public void updateConfig(ServerConfigImpl serverConfig) {
if (serverConfig.isConnectionDetailsFromEnv()) {
if (this.serverConfig.getUrl() != null) {
this.serverConfig.removeCredentialsFromPasswordSafe();
}
this.serverConfig.setConnectionDetailsFromEnv(true);
this.serverConfig.readConnectionDetailsFromEnv();
setAdvancedSettings(serverConfig);
return;
}

if (this.serverConfig.getUrl() != null && !this.serverConfig.getUrl().equals(serverConfig.getUrl())) {
this.serverConfig.removeCredentialsFromPasswordSafe();
}
Expand All @@ -143,10 +120,10 @@ public void updateConfig(ServerConfigImpl serverConfig) {
}

public void setCommonConfigFields(ServerConfigImpl serverConfig) {
this.serverConfig.setConnectionType(serverConfig.getConnectionType());
this.serverConfig.setUrl(serverConfig.getUrl());
this.serverConfig.setXrayUrl(serverConfig.getXrayUrl());
this.serverConfig.setArtifactoryUrl(serverConfig.getArtifactoryUrl());
this.serverConfig.setConnectionDetailsFromEnv(serverConfig.isConnectionDetailsFromEnv());
this.serverConfig.setJFrogSettingsCredentialsKey(serverConfig.getJFrogSettingsCredentialsKey());
setAdvancedSettings(serverConfig);
}
Expand All @@ -169,14 +146,18 @@ public boolean reloadXrayCredentials() {
if (serverConfig.isXrayConfigured()) {
return true;
}
serverConfig.setConnectionDetailsFromEnv(serverConfig.readConnectionDetailsFromEnv());
serverConfig.readConnectionDetailsFromEnv();
if (serverConfig.isXrayConfigured()) {
return true;
}
loadConnectionDetailsFromJfrogCli();
return serverConfig.isXrayConfigured();
}

public boolean areXrayCredentialsSet() {
return serverConfig != null && serverConfig.isXrayConfigured();
}

public boolean areArtifactoryCredentialsSet() {
return serverConfig != null && serverConfig.isArtifactoryConfigured();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
*/
@Immutable
public class ServerConfigImpl implements ServerConfig {
public enum ConnectionType {
SSO, CONNECTION_DETAILS
}

private static final String JFROG_SETTINGS_CREDENTIALS_KEY = "credentials";
static final String ARTIFACTORY_URL_ENV = "JFROG_IDE_ARTIFACTORY_URL";
public static final String JFROG_SETTINGS_KEY = "com.jfrog.idea";
Expand All @@ -67,6 +71,8 @@ public class ServerConfigImpl implements ServerConfig {
static final String ACCESS_TOKEN_ENV = "JFROG_IDE_ACCESS_TOKEN";
static final String PROJECT_ENV = "JFROG_IDE_PROJECT";

@OptionTag
private ConnectionType connectionType;
@OptionTag
private String url;
@OptionTag
Expand All @@ -91,8 +97,6 @@ public class ServerConfigImpl implements ServerConfig {
@Tag
private String excludedPaths;
@Tag
private boolean connectionDetailsFromEnv;
@Tag
private Integer connectionRetries;
@Tag
private Integer connectionTimeout;
Expand All @@ -104,6 +108,7 @@ public class ServerConfigImpl implements ServerConfig {
}

ServerConfigImpl(Builder builder) {
this.connectionType = builder.connectionType;
this.url = builder.url;
this.xrayUrl = builder.xrayUrl;
this.artifactoryUrl = builder.artifactoryUrl;
Expand All @@ -114,18 +119,17 @@ public class ServerConfigImpl implements ServerConfig {
this.project = builder.project;
this.watches = builder.watches;
this.excludedPaths = builder.excludedPaths;
this.connectionDetailsFromEnv = builder.connectionDetailsFromEnv;
this.connectionRetries = builder.connectionRetries;
this.connectionTimeout = builder.connectionTimeout;
this.jfrogSettingsCredentialsKey = builder.jfrogSettingsCredentialsKey;
}

public boolean isXrayConfigured() {
return isNotBlank(xrayUrl) && isAuthenticationConfigured();
return !isAllBlank(url, xrayUrl) && isAuthenticationConfigured();
}

public boolean isArtifactoryConfigured() {
return isNotBlank(artifactoryUrl) && isAuthenticationConfigured();
return !isAllBlank(url, xrayUrl) && isAuthenticationConfigured();
}

private boolean isAuthenticationConfigured() {
Expand All @@ -139,7 +143,8 @@ public boolean equals(Object o) {
}
ServerConfigImpl other = (ServerConfigImpl) o;

return Objects.equals(getUrl(), other.getUrl()) &&
return Objects.equals(getConnectionType(), other.getConnectionType()) &&
Objects.equals(getUrl(), other.getUrl()) &&
Objects.equals(getXrayUrl(), other.getXrayUrl()) &&
Objects.equals(getArtifactoryUrl(), other.getArtifactoryUrl()) &&
Objects.equals(getPassword(), other.getPassword()) &&
Expand All @@ -149,15 +154,14 @@ public boolean equals(Object o) {
Objects.equals(getProject(), other.getProject()) &&
Objects.equals(getWatches(), other.getWatches()) &&
Objects.equals(getExcludedPaths(), other.getExcludedPaths()) &&
isConnectionDetailsFromEnv() == other.isConnectionDetailsFromEnv() &&
getConnectionRetries() == other.getConnectionRetries() &&
getConnectionTimeout() == other.getConnectionTimeout();
}

@Override
public int hashCode() {
return Objects.hash(getUrl(), getXrayUrl(), getArtifactoryUrl(), getPassword(), getAccessToken(), getUsername(), getProject(),
getExcludedPaths(), isConnectionDetailsFromEnv(), getConnectionRetries(), getConnectionTimeout());
return Objects.hash(getConnectionType(), getUrl(), getXrayUrl(), getArtifactoryUrl(), getPassword(), getAccessToken(),
getUsername(), getProject(), getExcludedPaths(), getConnectionRetries(), getConnectionTimeout());
}

@Override
Expand Down Expand Up @@ -191,6 +195,14 @@ public String getAccessToken() {
return accessToken;
}

public void setConnectionType(ConnectionType connectionType) {
this.connectionType = connectionType;
}

public ConnectionType getConnectionType() {
return connectionType;
}

public Credentials getCredentialsFromPasswordSafe() {
return retrieveCredentialsFromPasswordSafe(jfrogSettingsCredentialsKey, JFROG_SETTINGS_CREDENTIALS_KEY);
}
Expand Down Expand Up @@ -268,7 +280,7 @@ void setWatches(String watches) {
* Get proxy configuration as configured under 'Appearance & Behavior' -> 'System Settings' -> 'HTTP Proxy'
*
* @param targetUrl - The target URL. The URL is necessary to determine whether to bypass proxy or to pick the relevant
* proxy configuration for the target URL as configured in *.pac file.
* proxy configuration for the target URL as configured in *.pac file.
* @return the proxy configuration as configured in IDEA settings.
*/
@Override
Expand Down Expand Up @@ -380,14 +392,6 @@ void setCredentials(Credentials credentials) {
}
}

void setConnectionDetailsFromEnv(boolean connectionDetailsFromEnv) {
this.connectionDetailsFromEnv = connectionDetailsFromEnv;
}

public boolean isConnectionDetailsFromEnv() {
return connectionDetailsFromEnv;
}

void setConnectionRetries(int connectionRetries) {
this.connectionRetries = connectionRetries;
}
Expand All @@ -403,10 +407,8 @@ public void setJFrogSettingsCredentialsKey(String jfrogSettingsCredentialsKey) {
/**
* Read connection details from environment variables.
* All connection details must be provided from env, otherwise don't use them.
*
* @return true if connection details loaded from env.
*/
public boolean readConnectionDetailsFromEnv() {
public void readConnectionDetailsFromEnv() {
String platformUrlEnv = EnvironmentUtil.getValue(PLATFORM_URL_ENV);
String xrayUrlEnv = EnvironmentUtil.getValue(XRAY_URL_ENV);
String artifactoryUrlEnv = EnvironmentUtil.getValue(ARTIFACTORY_URL_ENV);
Expand All @@ -421,7 +423,7 @@ public boolean readConnectionDetailsFromEnv() {
setUsername("");
setPassword("");
setAccessToken("");
return false;
return;
}

setUrl(platformUrlEnv);
Expand All @@ -445,7 +447,6 @@ public boolean readConnectionDetailsFromEnv() {
setUsername(usernameEnv);
setPassword(passwordEnv);
}
return true;
}

/**
Expand Down Expand Up @@ -486,6 +487,7 @@ public String toString() {
}

public static class Builder {
private ConnectionType connectionType;
private String jfrogSettingsCredentialsKey = JFROG_SETTINGS_KEY;
private String url;
private String xrayUrl;
Expand All @@ -497,14 +499,18 @@ public static class Builder {
private PolicyType policyType;
private String project;
private String watches;
private boolean connectionDetailsFromEnv;
private int connectionRetries;
private int connectionTimeout;

public ServerConfigImpl build() {
return new ServerConfigImpl(this);
}

public Builder setConnectionType(ConnectionType connectionType) {
this.connectionType = connectionType;
return this;
}

public Builder setUrl(String url) {
this.url = url;
return this;
Expand Down Expand Up @@ -555,11 +561,6 @@ public Builder setWatches(@Nullable String watches) {
return this;
}

public Builder setConnectionDetailsFromEnv(boolean connectionDetailsFromEnv) {
this.connectionDetailsFromEnv = connectionDetailsFromEnv;
return this;
}

public Builder setConnectionRetries(int connectionRetries) {
this.connectionRetries = connectionRetries;
return this;
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/jfrog/ide/idea/ui/JFrogLocalToolWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public JFrogLocalToolWindow(@NotNull Project project) {
compTreeView = createComponentsTreeView();

alertIfCacheExpired();
refreshView();
refreshView(true);
registerListeners(jbCefBrowser.getComponent());
isInitialized = true;
}
Expand Down Expand Up @@ -144,8 +144,10 @@ private void alertIfCacheExpired() {
}
}

private void refreshView() {
if (!GlobalSettings.getInstance().reloadXrayCredentials()) {
private void refreshView(boolean reloadCredentials) {
GlobalSettings globalSettings = GlobalSettings.getInstance();
if ((!reloadCredentials && !globalSettings.areXrayCredentialsSet()) ||
!globalSettings.reloadXrayCredentials()) {
setLeftPanelContent(ComponentUtils.createNoCredentialsView());
return;
}
Expand Down Expand Up @@ -287,14 +289,14 @@ public void onConfigurationChange() {
}
}
super.onConfigurationChange();
refreshView();
refreshView(false);
}

@Override
public void updateUI() {
super.updateUI();
if (isInitialized) {
refreshView();
refreshView(true);
}
}
}
Loading

0 comments on commit 24800b2

Please sign in to comment.