Skip to content

Commit

Permalink
Add disable external url option (#97)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Jacomb <[email protected]>
  • Loading branch information
WenjunMao and timja authored Mar 12, 2024
1 parent 068a70c commit 658454b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class AzureArtifactConfig extends AbstractDescribableImpl<AzureArtifactCo
private String storageCredentialId;
private String container;
private String prefix;
private boolean disableExternalUrl;

public AzureArtifactConfig() {
}
Expand Down Expand Up @@ -61,6 +62,15 @@ public void setPrefix(String prefix) {
this.prefix = prefix;
}

public boolean getDisableExternalUrl() {
return disableExternalUrl;
}

@DataBoundSetter
public void setDisableExternalUrl(boolean disableExternalUrl) {
this.disableExternalUrl = disableExternalUrl;
}

Check warning on line 72 in src/main/java/com/microsoft/jenkins/artifactmanager/AzureArtifactConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 66-72 are not covered by tests

public String getStorageCredentialId() {
return this.storageCredentialId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ private BlobContainerClient getContainer() throws IOException,

@Override
public VirtualFile root() {
return new AzureBlobVirtualFile(this.actualContainerName, getVirtualPath("artifacts"), build);
return new AzureBlobVirtualFile(this.actualContainerName, getVirtualPath("artifacts"),
this.config.getDisableExternalUrl(), build);

Check warning on line 401 in src/main/java/com/microsoft/jenkins/artifactmanager/AzureArtifactManager.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 400-401 are not covered by tests
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ public class AzureBlobVirtualFile extends AzureAbstractVirtualFile {

private final String container;
private final String key;

private Boolean disableExternalUrl;
private final transient Run<?, ?> build;

public AzureBlobVirtualFile(String container, String key, Run<?, ?> build) {
public AzureBlobVirtualFile(String container, String key, boolean disableExternalUrl, Run<?, ?> build) {
this.container = container;
this.key = key;
this.build = build;
this.disableExternalUrl = disableExternalUrl;
}

public String getContainer() {
Expand Down Expand Up @@ -185,20 +188,24 @@ public URI toURI() {
@CheckForNull
@Override
public URL toExternalURL() throws IOException {
StorageAccountInfo accountInfo = Utils.getStorageAccount(build.getParent());
String sas;
try {
sas = Utils.generateBlobSASURL(accountInfo, this.container, this.key);
} catch (Exception e) {
throw new IOException(e);
if (!this.disableExternalUrl) {
StorageAccountInfo accountInfo = Utils.getStorageAccount(build.getParent());
String sas;
try {
sas = Utils.generateBlobSASURL(accountInfo, this.container, this.key);
} catch (Exception e) {
throw new IOException(e);
}
return new URL(toURI() + "?" + sas);
} else {
return null;
}
return new URL(toURI() + "?" + sas);
}

@Override
public VirtualFile getParent() {
return new AzureBlobVirtualFile(this.container, this.key.replaceFirst("/[^/]+$", Constants.EMPTY_STRING),
this.build);
this.disableExternalUrl, this.build);
}

@Override
Expand Down Expand Up @@ -283,7 +290,8 @@ public VirtualFile[] list() throws IOException {
.map(f -> f.substring(relSlash.length()).replaceFirst("/.+", ""))
.distinct() // ignore duplicates if have multiple files under one direct subdir
// direct children
.map(simple -> new AzureBlobVirtualFile(this.container, keyS + simple, this.build))
.map(simple -> new AzureBlobVirtualFile(this.container, keyS + simple,
this.disableExternalUrl, this.build))
.toArray(VirtualFile[]::new);
return virtualFiles;
}
Expand All @@ -306,7 +314,8 @@ private List<VirtualFile> listBlobsFromPrefix(String keys, BlobContainerClient b
PagedIterable<BlobItem> blobItems = blobContainerReference.listBlobsByHierarchy(keys);
List<VirtualFile> files = new ArrayList<>();
for (BlobItem blobItem : blobItems) {
files.add(new AzureBlobVirtualFile(this.container, stripTrailingSlash(blobItem.getName()), this.build));
files.add(new AzureBlobVirtualFile(this.container, stripTrailingSlash(blobItem.getName()),
this.disableExternalUrl, this.build));
}
return files;
}
Expand All @@ -315,7 +324,7 @@ private List<VirtualFile> listBlobsFromPrefix(String keys, BlobContainerClient b
@Override
public VirtualFile child(@NonNull String name) {
String joinedKey = stripTrailingSlash(this.key) + Constants.FORWARD_SLASH + name;
return new AzureBlobVirtualFile(this.container, joinedKey, build);
return new AzureBlobVirtualFile(this.container, joinedKey, this.disableExternalUrl, build);

Check warning on line 327 in src/main/java/com/microsoft/jenkins/artifactmanager/AzureBlobVirtualFile.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 56-327 are not covered by tests
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<f:entry title="${%Prefix_title}" field="prefix">
<f:textbox/>
</f:entry>
<f:entry title="${%DisableExternalUrl_title}" field="disableExternalUrl">
<f:checkbox/>
</f:entry>
</f:section>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Credentials_setting_title=Azure Storage Credentials Settings
Access_setting_title=Azure Storage Access Settings
Container_name_title=Azure Container Name
Prefix_title=Base Prefix (Optional)
DisableExternalUrl_title=Disable External URL (Optional)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>
If checked, artifacts will be accessed under a Jenkins URL and not an Azure Storage URL.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public void setUp() throws Exception {
uploadFile(tempFile, TEMP_FILE_RELATIVE_PATH);
uploadFile(subTempFile, SUB_TEMP_FILE_RELATIVE_PATH);

root = new AzureBlobVirtualFile(containerName, ROOT_FOLDER_NAME, run);
subDir = new AzureBlobVirtualFile(containerName, SUB_FOLDER_RELATIVE_PATH, run);
temp = new AzureBlobVirtualFile(containerName, TEMP_FILE_RELATIVE_PATH, run);
subTemp = new AzureBlobVirtualFile(containerName, SUB_TEMP_FILE_RELATIVE_PATH, run);
missing = new AzureBlobVirtualFile(containerName, "missing", run);
root = new AzureBlobVirtualFile(containerName, ROOT_FOLDER_NAME, false, run);
subDir = new AzureBlobVirtualFile(containerName, SUB_FOLDER_RELATIVE_PATH, false, run);
temp = new AzureBlobVirtualFile(containerName, TEMP_FILE_RELATIVE_PATH, false, run);
subTemp = new AzureBlobVirtualFile(containerName, SUB_TEMP_FILE_RELATIVE_PATH, false, run);
missing = new AzureBlobVirtualFile(containerName, "missing", false, run);
}

private void uploadFile(File file, String cloudFileName) {
Expand Down

0 comments on commit 658454b

Please sign in to comment.