Skip to content

Commit

Permalink
closes #38 ChainLP: S3Client only: Change default behavior to always …
Browse files Browse the repository at this point in the history
…presign URLs
  • Loading branch information
anshooarora committed Jan 17, 2025
1 parent 4715398 commit 7f18409
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void update(final Secret secret) {
},
() -> log.debug("Creating new secret")
);
System.setProperty(secret.getK(), secret.getV());
decode(secret);
System.setProperty(secret.getK(), secret.getDecoded());
repository.save(secret);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.aventstack.chainlp.api.test;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString(exclude = { "test" })
@Table(name = "embed")
public class Embed {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@ManyToOne
@JsonIgnore
private Test test;

@Column
private String url;

@Transient
private String presigned;

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,15 @@ public void setChildren(List<Test> children) {
@Column(columnDefinition = "TEXT")
private String error;

@ElementCollection
@CollectionTable(name = "screenshot", joinColumns = @JoinColumn(name="id"))
@Column
private List<String> screenshotURL;
@OneToMany(mappedBy = "test", cascade = CascadeType.ALL)
private List<Embed> embeds;

public void setEmbeds(final List<Embed> embeds) {
this.embeds = embeds;
if (embeds != null) {
embeds.forEach(x -> x.setTest(this));
}
}

@ElementCollection
@CollectionTable(name = "log", joinColumns = @JoinColumn(name="id"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public Page<Test> findAll(final Test test, final String op, final Pageable pagea
}

private Test resolveEmbeds(final Test test) {
for (final String s : test.getScreenshotURL()) {
test.getScreenshotURL().set(test.getScreenshotURL().indexOf(s),
embedResolver.getResolver(s).resolve(s));
for (final Embed embed : test.getEmbeds()) {
final String presigned = embedResolver.getResolver(embed.getUrl())
.resolve(embed.getUrl());
embed.setPresigned(presigned);
}
test.getChildren().forEach(this::resolveEmbeds);
return test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ public class AwsS3SignedUrlResolver implements SignedUrlResolver {

@Override
public String resolve(final String path) {
log.debug("Resolving path: [{}]", path);
final URI uri = URI.create(path);
final S3Client s3Client = S3Client.create();
final S3Uri s3URI = s3Client.utilities().parseUri(uri);
final String bucket = s3URI.bucket()
.orElseThrow(() -> new IllegalArgumentException("Bucket not found"));
final String key = s3URI.key()
.orElseThrow(() -> new IllegalArgumentException("Key not found"));
return createPresignedGetUrl(bucket, key);
try {
log.debug("Resolving path: [{}]", path);
final URI uri = URI.create(path);
final S3Client s3Client = S3Client.create();
final S3Uri s3URI = s3Client.utilities().parseUri(uri);
final String bucket = s3URI.bucket()
.orElseThrow(() -> new IllegalArgumentException("Bucket not found"));
final String key = s3URI.key()
.orElseThrow(() -> new IllegalArgumentException("Key not found"));
return createPresignedGetUrl(bucket, key);
} catch (final Exception e) {
log.error("Failed to resolve path: [{}]", path, e);
return path;
}
}

public String createPresignedGetUrl(final String bucket, final String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class SignedEmbedResolverFactory {
public SignedEmbedResolverFactory() { }

public SignedUrlResolver getResolver(final String path) {
final boolean useSignedUrls = Boolean.parseBoolean(System.getenv(CHAINTEST_USE_SIGNED_URLS))
|| Boolean.parseBoolean(System.getProperty(CHAINTEST_USE_SIGNED_URLS, "false"));
if (!useSignedUrls) {
if (!path.toLowerCase().contains(".amazonaws.")
&& !path.toLowerCase().contains(".windows.")
&& !path.toLowerCase().contains(".azure.")) {
return defaultResolver;
}

Expand Down

0 comments on commit 7f18409

Please sign in to comment.