Skip to content

Commit

Permalink
External resources repository can also be configured using an environ…
Browse files Browse the repository at this point in the history
…ment variable.
  • Loading branch information
asafgabai committed Nov 2, 2023
1 parent 2001d7f commit 63069d1
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.jfrog.ide.idea.events.ApplicationEvents;
import com.jfrog.ide.idea.log.Logger;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -66,7 +67,7 @@ public GlobalSettings getState() {
serverConfig.setWatches(this.serverConfig.getWatches());
serverConfig.setConnectionRetries(this.serverConfig.getConnectionRetries());
serverConfig.setConnectionTimeout(this.serverConfig.getConnectionTimeout());
serverConfig.setCustomResourcesRepo(this.serverConfig.getCustomResourcesRepo());
serverConfig.setExternalResourcesRepo(this.serverConfig.getExternalResourcesRepo());

GlobalSettings settings = new GlobalSettings();
settings.serverConfig = serverConfig;
Expand All @@ -81,11 +82,12 @@ public GlobalSettings getState() {
@Override
public void loadState(@NotNull GlobalSettings state) {
XmlSerializerUtil.copyBean(state, this);
serverConfig.readMissingConfFromEnv();
}

@Override
public void noStateLoaded() {
reloadXrayCredentials();
reloadMissingConfiguration();
}

/**
Expand Down Expand Up @@ -131,18 +133,19 @@ private void setAdvancedSettings(ServerConfigImpl serverConfig) {
this.serverConfig.setExcludedPaths(serverConfig.getExcludedPaths());
this.serverConfig.setConnectionRetries(serverConfig.getConnectionRetries());
this.serverConfig.setConnectionTimeout(serverConfig.getConnectionTimeout());
this.serverConfig.setCustomResourcesRepo(serverConfig.getCustomResourcesRepo());
this.serverConfig.setExternalResourcesRepo(serverConfig.getExternalResourcesRepo());
this.serverConfig.setPolicyType(serverConfig.getPolicyType());
this.serverConfig.setProject(serverConfig.getProject());
this.serverConfig.setWatches(serverConfig.getWatches());
}

/**
* Reloads Xray credentials.
* Reloads missing configuration from the plugin settings, environment variables or JFrog CLI configuration.
*
* @return true if credentials exist and Xray is configured, false otherwise.
*/
public boolean reloadXrayCredentials() {
public boolean reloadMissingConfiguration() {
serverConfig.readMissingConfFromEnv();
if (serverConfig.isXrayConfigured()) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.jfrog.ide.idea.ui.configuration.ConnectionRetriesSpinner;
import com.jfrog.ide.idea.ui.configuration.ConnectionTimeoutSpinner;
import org.apache.commons.collections4.CollectionUtils;
import org.codehaus.plexus.util.StringUtils;
import org.jfrog.build.client.ProxyConfiguration;

import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -62,14 +63,15 @@ public enum ConnectionType {
}

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";
static final String PLATFORM_URL_ENV = "JFROG_IDE_PLATFORM_URL";
static final String ARTIFACTORY_URL_ENV = "JFROG_IDE_ARTIFACTORY_URL";
static final String XRAY_URL_ENV = "JFROG_IDE_XRAY_URL";
static final String USERNAME_ENV = "JFROG_IDE_USERNAME";
static final String PASSWORD_ENV = "JFROG_IDE_PASSWORD";
static final String ACCESS_TOKEN_ENV = "JFROG_IDE_ACCESS_TOKEN";
static final String PROJECT_ENV = "JFROG_IDE_PROJECT";
static final String EXTERNAL_RESOURCES_REPO_ENV = "JFROG_IDE_RELEASES_REPO";

@OptionTag
private ConnectionType connectionType;
Expand Down Expand Up @@ -101,7 +103,7 @@ public enum ConnectionType {
@Tag
private Integer connectionTimeout;
@Tag
private String customResourcesRepo;
private String externalResourcesRepo;
// The subsystem key of the plugin configuration in the PasswordSafe
@Transient
private String jfrogSettingsCredentialsKey = JFROG_SETTINGS_KEY;
Expand All @@ -123,7 +125,7 @@ public enum ConnectionType {
this.excludedPaths = builder.excludedPaths;
this.connectionRetries = builder.connectionRetries;
this.connectionTimeout = builder.connectionTimeout;
this.customResourcesRepo = builder.customResourcesRepo;
this.externalResourcesRepo = builder.externalResourcesRepo;
this.jfrogSettingsCredentialsKey = builder.jfrogSettingsCredentialsKey;
}

Expand Down Expand Up @@ -159,13 +161,13 @@ public boolean equals(Object o) {
Objects.equals(getExcludedPaths(), other.getExcludedPaths()) &&
getConnectionRetries() == other.getConnectionRetries() &&
getConnectionTimeout() == other.getConnectionTimeout() &&
getCustomResourcesRepo() == other.getCustomResourcesRepo();
getExternalResourcesRepo() == other.getExternalResourcesRepo();
}

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

@Override
Expand Down Expand Up @@ -262,8 +264,8 @@ public int getConnectionTimeout() {
}

@Override
public String getCustomResourcesRepo() {
return this.customResourcesRepo;
public String getExternalResourcesRepo() {
return this.externalResourcesRepo;
}

public String getJFrogSettingsCredentialsKey() {
Expand Down Expand Up @@ -411,8 +413,8 @@ void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}

void setCustomResourcesRepo(String customResourcesRepo) {
this.customResourcesRepo = customResourcesRepo;
void setExternalResourcesRepo(String externalResourcesRepo) {
this.externalResourcesRepo = externalResourcesRepo;
}

public void setJFrogSettingsCredentialsKey(String jfrogSettingsCredentialsKey) {
Expand Down Expand Up @@ -464,6 +466,18 @@ public void readConnectionDetailsFromEnv() {
}
}

/**
* Read missing configuration from environment variables.
*/
public void readMissingConfFromEnv() {
if (isBlank(getExternalResourcesRepo())) {
String externalResourcesRepoEnv = EnvironmentUtil.getValue(EXTERNAL_RESOURCES_REPO_ENV);
if (isNotBlank(externalResourcesRepoEnv)) {
setExternalResourcesRepo(externalResourcesRepoEnv);
}
}
}

/**
* Read the connection details from JFrog CLI's config. The configuration is read by executing JFrog CLI.
* If no JFrog CLI server configuration was found or the config
Expand Down Expand Up @@ -518,7 +532,7 @@ public static class Builder {
private String watches;
private int connectionRetries;
private int connectionTimeout;
private String customResourcesRepo;
private String externalResourcesRepo;

public ServerConfigImpl build() {
return new ServerConfigImpl(this);
Expand Down Expand Up @@ -591,8 +605,8 @@ public Builder setConnectionTimeout(int connectionTimeout) {
return this;
}

public Builder setCustomResourcesRepo(String customResourcesRepo) {
this.customResourcesRepo = customResourcesRepo;
public Builder setExternalResourcesRepo(String externalResourcesRepo) {
this.externalResourcesRepo = externalResourcesRepo;
return this;
}

Expand Down
23 changes: 11 additions & 12 deletions src/main/java/com/jfrog/ide/idea/scan/ScanBinaryExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import static com.jfrog.ide.idea.scan.ScanUtils.getOSAndArc;
import static com.jfrog.ide.idea.utils.Utils.HOME_PATH;
import static java.lang.String.join;
import static org.apache.commons.lang3.StringUtils.defaultIfEmpty;

/**
* @author Tal Arian
Expand Down Expand Up @@ -112,10 +111,10 @@ protected void setOsDistribution() {
}
}

String getBinaryDownloadURL(String customResourcesRepo) {
String getBinaryDownloadURL(String externalResourcesRepo) {
String downloadUrlPrefix = "";
if (!StringUtils.isEmpty(customResourcesRepo)) {
downloadUrlPrefix = String.format("%s/artifactory/", customResourcesRepo);
if (!StringUtils.isEmpty(externalResourcesRepo)) {
downloadUrlPrefix = String.format("%s/artifactory/", externalResourcesRepo);
}
return String.format("%s%s/%s/%s", downloadUrlPrefix, BINARY_DOWNLOAD_URL, getOsDistribution(), DOWNLOAD_SCANNER_NAME);
}
Expand Down Expand Up @@ -197,13 +196,13 @@ private void updateBinaryIfNeeded() throws IOException {
return;
}
ServerConfig server = GlobalSettings.getInstance().getServerConfig();
String customResourcesRepo = server.getCustomResourcesRepo();
ArtifactoryManagerBuilder artifactoryManagerBuilder = createManagerBuilder(StringUtils.isEmpty(customResourcesRepo), server);
String externalResourcesRepo = server.getExternalResourcesRepo();
ArtifactoryManagerBuilder artifactoryManagerBuilder = createManagerBuilder(StringUtils.isEmpty(externalResourcesRepo), server);
try (ArtifactoryManager artifactoryManager = artifactoryManagerBuilder.build()) {
if (Files.exists(binaryTargetPath)) {
// Check for new version of the binary
try (FileInputStream archiveBinaryFile = new FileInputStream(archiveTargetPath.toFile())) {
String latestBinaryChecksum = getFileChecksumFromServer(artifactoryManager, customResourcesRepo);
String latestBinaryChecksum = getFileChecksumFromServer(artifactoryManager, externalResourcesRepo);
String currentBinaryCheckSum = DigestUtils.sha256Hex(archiveBinaryFile);
if (latestBinaryChecksum.equals(currentBinaryCheckSum)) {
nextUpdateCheck = currentTime.plusDays(UPDATE_INTERVAL);
Expand All @@ -215,13 +214,13 @@ private void updateBinaryIfNeeded() throws IOException {
} else {
log.debug(String.format("Resource %s is not found. Downloading it.", binaryTargetPath));
}
downloadBinary(artifactoryManager, customResourcesRepo);
downloadBinary(artifactoryManager, externalResourcesRepo);
}
}
}

public String getFileChecksumFromServer(ArtifactoryManager artifactoryManager, String customResourcesRepo) throws IOException {
String url = getBinaryDownloadURL(customResourcesRepo);
public String getFileChecksumFromServer(ArtifactoryManager artifactoryManager, String externalResourcesRepo) throws IOException {
String url = getBinaryDownloadURL(externalResourcesRepo);
Header[] headers = artifactoryManager.downloadHeaders(url);
for (Header header : headers) {
if (StringUtils.equalsIgnoreCase(header.getName(), "x-checksum-sha256")) {
Expand Down Expand Up @@ -276,8 +275,8 @@ protected Output getOutputObj(Path outputFile) throws IOException {
return om.readValue(outputFile.toFile(), Output.class);
}

protected void downloadBinary(ArtifactoryManager artifactoryManager, String customResourcesRepo) throws IOException {
String downloadUrl = getBinaryDownloadURL(customResourcesRepo);
protected void downloadBinary(ArtifactoryManager artifactoryManager, String externalResourcesRepo) throws IOException {
String downloadUrl = getBinaryDownloadURL(externalResourcesRepo);
File downloadArchive = artifactoryManager.downloadToFile(downloadUrl, archiveTargetPath.toString());
log.debug(String.format("Downloading: %s", downloadUrl));
if (downloadArchive == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jfrog/ide/idea/scan/ScanManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void startScan() {
return;
}

if (!GlobalSettings.getInstance().reloadXrayCredentials()) {
if (!GlobalSettings.getInstance().reloadMissingConfiguration()) {
Logger.getInstance().error("Xray server is not configured.");
return;
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/jfrog/ide/idea/scan/ScannerBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,6 @@ public void run(@NotNull com.intellij.openapi.progress.ProgressIndicator indicat
if (project.isDisposed()) {
return;
}
if (!GlobalSettings.getInstance().reloadXrayCredentials()) {
throw new RuntimeException("Xray server is not configured.");
}
// Prevent multiple simultaneous scans
if (!scanInProgress.compareAndSet(false, true)) {
log.info("Scan already in progress");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ public void run(@NotNull com.intellij.openapi.progress.ProgressIndicator indicat
if (project.isDisposed()) {
return;
}
if (!GlobalSettings.getInstance().reloadXrayCredentials()) {
throw new RuntimeException("Xray server is not configured.");
}
// Prevent multiple simultaneous scans
if (!scanInProgress.compareAndSet(false, true)) {
log.info("Advanced source code scan is already in progress");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private void alertIfCacheExpired() {
private void refreshView(boolean reloadCredentials) {
GlobalSettings globalSettings = GlobalSettings.getInstance();
if ((!reloadCredentials && !globalSettings.areXrayCredentialsSet()) ||
!globalSettings.reloadXrayCredentials()) {
!globalSettings.reloadMissingConfiguration()) {
setLeftPanelContent(ComponentUtils.createNoCredentialsView());
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,12 @@
<text value="Resources"/>
</properties>
</component>
<component id="d3948" class="javax.swing.JCheckBox" binding="useCustomRepositoryCheckBox">
<component id="d3948" class="javax.swing.JCheckBox" binding="useExternalRepositoryCheckBox">
<constraints>
<grid row="7" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="2" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Use custom repository for downloading resources"/>
<text value="Use external repository for downloading resources"/>
</properties>
</component>
<component id="3d960" class="javax.swing.JLabel" binding="repositoryNameJLabel">
Expand Down Expand Up @@ -622,7 +622,7 @@
<properties>
<font size="11"/>
<foreground color="-6513248"/>
<text value="&lt;html&gt;&#10;Specify the name of a custom repository within your Artifactory instance for resource downloads.&#10;&lt;/html&gt;"/>
<text value="&lt;html&gt; Specify the name of an external repository within your Artifactory instance for resource downloads. &lt;/html&gt;"/>
</properties>
</component>
</children>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public class JFrogGlobalConfiguration implements Configurable, Configurable.NoSc
private JBTextField excludedPaths;
private ActionLink scanOptionsRestoreDefaultsActionLink;
private ActionLink connectionOptionsRestoreDefaultsActionLink;
private JCheckBox useCustomRepositoryCheckBox;
private JCheckBox useExternalRepositoryCheckBox;
private JLabel repositoryNameJLabel;
private JBTextField repositoryNameJBTextField;
private JLabel repositoryNameDescJLabel;
Expand All @@ -148,7 +148,7 @@ public JFrogGlobalConfiguration() {
// Advanced
initConnectionOptionsRestoreDefaultsActionLink();
initScanOptionsRestoreDefaultsActionLink();
initUseCustomRepositoryCheckBox();
initUseExternalRepositoryCheckBox();

loadConfig();
}
Expand Down Expand Up @@ -233,8 +233,8 @@ private ServerConfigImpl createServerConfig() {
.setWatches(watches.getText())
.setConnectionRetries(connectionRetries.getNumber())
.setConnectionTimeout(connectionTimeout.getNumber());
if (useCustomRepositoryCheckBox.isSelected()) {
builder.setCustomResourcesRepo(repositoryNameJBTextField.getText());
if (useExternalRepositoryCheckBox.isSelected()) {
builder.setExternalResourcesRepo(repositoryNameJBTextField.getText());
}
return builder.build();
}
Expand Down Expand Up @@ -284,11 +284,11 @@ private void loadConfig() {
watches.setText(serverConfig.getWatches());
connectionRetries.setValue(serverConfig.getConnectionRetries());
connectionTimeout.setValue(serverConfig.getConnectionTimeout());
if (!StringUtils.isEmpty(serverConfig.getCustomResourcesRepo())) {
useCustomRepositoryCheckBox.setSelected(true);
repositoryNameJBTextField.setText(serverConfig.getCustomResourcesRepo());
if (!StringUtils.isEmpty(serverConfig.getExternalResourcesRepo())) {
useExternalRepositoryCheckBox.setSelected(true);
repositoryNameJBTextField.setText(serverConfig.getExternalResourcesRepo());
} else {
useCustomRepositoryCheckBox.setSelected(false);
useExternalRepositoryCheckBox.setSelected(false);
}
} else {
clearText(platformUrl, xrayUrl, artifactoryUrl, username, password);
Expand All @@ -299,9 +299,9 @@ private void loadConfig() {
connectionRetries.setValue(ConnectionRetriesSpinner.RANGE.initial);
connectionTimeout.setValue(ConnectionTimeoutSpinner.RANGE.initial);
ssoLoginSelection.setSelected(true);
useCustomRepositoryCheckBox.setSelected(false);
useExternalRepositoryCheckBox.setSelected(false);
}
updateCustomRepositoryFields();
updateExternalRepositoryFields();
initAuthMethodSelection();
}

Expand Down Expand Up @@ -684,16 +684,16 @@ private void initScanOptionsRestoreDefaultsActionLink() {
}

/**
* Initialize the "Use custom repository" checkbox in the "Advanced" tab.
* Initialize the "Use external repository" checkbox in the "Advanced" tab.
*/
private void initUseCustomRepositoryCheckBox() {
useCustomRepositoryCheckBox.addActionListener(e -> ApplicationManager.getApplication().executeOnPooledThread(() -> {
updateCustomRepositoryFields();
private void initUseExternalRepositoryCheckBox() {
useExternalRepositoryCheckBox.addActionListener(e -> ApplicationManager.getApplication().executeOnPooledThread(() -> {
updateExternalRepositoryFields();
}));
}

private void updateCustomRepositoryFields() {
boolean enabled = useCustomRepositoryCheckBox.isSelected();
private void updateExternalRepositoryFields() {
boolean enabled = useExternalRepositoryCheckBox.isSelected();
repositoryNameJLabel.setEnabled(enabled);
repositoryNameJBTextField.setEnabled(enabled);
repositoryNameDescJLabel.setEnabled(enabled);
Expand Down
Loading

0 comments on commit 63069d1

Please sign in to comment.