Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ID-1309] add IP address to DRSHub request #114

Merged
merged 21 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ private DrsMetadata fetchObject(
auditEventBuilder,
authorizations,
forceAccessUrl,
ip,
googleProject);
}

Expand All @@ -180,6 +181,7 @@ private void setDrsResponseValues(
AuditLogEvent.Builder auditEventBuilder,
List<DrsHubAuthorization> authorizations,
boolean forceAccessUrl,
String ip,
String googleProject) {

getDrsFileName(drsResponse).ifPresent(drsMetadataBuilder::fileName);
Expand All @@ -197,6 +199,7 @@ private void setDrsResponseValues(
accessMethodType,
authorizations,
auditEventBuilder,
ip,
googleProject);
drsMetadataBuilder.accessUrl(accessUrl);
} catch (RuntimeException e) {
Expand Down Expand Up @@ -257,13 +260,19 @@ AccessURL fetchDrsObjectAccessUrl(
TypeEnum accessMethodType,
List<DrsHubAuthorization> drsHubAuthorizations,
AuditLogEvent.Builder auditLogEventBuilder,
String ip,
String googleProject) {

var drsApi = drsApiFactory.getApiFromUriComponents(uriComponents, drsProvider);
var objectId = getObjectId(uriComponents);

if (ip != null) {
drsApi.setHeader("X-Forwarded-For", ip);
}
if (googleProject != null) {
drsApi.setHeader("x-user-project", googleProject);
}

for (var authorization : drsHubAuthorizations) {
Optional<List<String>> auth =
authorization.getAuthForAccessMethodType().apply(accessMethodType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,25 @@ class DrsResolutionServiceTest {
SupportedTypesEnum.BEARERAUTH, (var e) -> Optional.of(List.of(TOKEN_VALUE)));
private static final DrsObject DRS_OBJECT = new DrsObject().id("drs.id");

private static final String accessId = "foo";

private static URL url;

private static final DrsProvider testDrsProvider =
DrsProvider.create()
.setMetadataAuth(true)
.setName("test")
.setHostRegex(".*")
.setAccessMethodConfigs(
new ArrayList<>(
List.of(
ProviderAccessMethodConfig.create()
.setType(AccessMethodConfigTypeEnum.gs)
.setAuth(AccessUrlAuthEnum.current_request)
.setFetchAccessUrl(true))));

@BeforeEach
void before() {
void before() throws Exception {
DrsApiFactory drsApiFactory = mock(DrsApiFactory.class);

drsResolutionService =
Expand All @@ -85,6 +102,8 @@ void before() {
when(uriComponents.getPath()).thenReturn(PATH);
when(drsApiFactory.getApiFromUriComponents(eq(uriComponents), any(DrsProvider.class)))
.thenReturn(drsApi);

url = new URL("https://storage.cloud.google.com/my-test-bucket/my/test.txt");
}

@Test
Expand Down Expand Up @@ -215,34 +234,64 @@ void fetchObjectInfo_failedPassportFallsBackToBearerToken() {

@Test
void testSignGoogleUrlWithRequesterPays() throws Exception {
var ip = "test.ip";
var googleProject = "test-google-project";
var url = new URL("https://storage.cloud.google.com/my-test-bucket/my/test.txt");
var accessId = "foo";
SignedUrlTestUtils.setupSignedUrlMocks(authService, googleStorageService, googleProject, url);
DrsProvider drsProvider =
DrsProvider.create()
.setMetadataAuth(true)
.setName("test")
.setHostRegex(".*")
.setAccessMethodConfigs(
new ArrayList<>(
List.of(
ProviderAccessMethodConfig.create()
.setType(AccessMethodConfigTypeEnum.gs)
.setAuth(AccessUrlAuthEnum.current_request)
.setFetchAccessUrl(true))));
when(drsApi.getAccessURL(PATH, accessId)).thenReturn(new AccessURL().url(url.toString()));
var response =
drsResolutionService.fetchDrsObjectAccessUrl(
drsProvider,
testDrsProvider,
uriComponents,
accessId,
TypeEnum.GS,
List.of(BEARERAUTH),
new AuditLogEvent.Builder(),
ip,
googleProject);
assertThat(
"google signed url is properly returned", response.getUrl(), equalTo(url.toString()));
verify(drsApi).setHeader("x-user-project", googleProject);
}

@Test
void testDrsResolutionHeadersIncludeIpAddress() throws Exception {
var googleProject = "test-google-project";
var ip = "test.ip";
SignedUrlTestUtils.setupSignedUrlMocks(authService, googleStorageService, googleProject, url);
when(drsApi.getAccessURL(PATH, accessId)).thenReturn(new AccessURL().url(url.toString()));
var response =
drsResolutionService.fetchDrsObjectAccessUrl(
testDrsProvider,
uriComponents,
accessId,
TypeEnum.GS,
List.of(BEARERAUTH),
new AuditLogEvent.Builder(),
ip,
googleProject);
assertThat("signed url is properly returned", response.getUrl(), equalTo(url.toString()));
verify(drsApi).setHeader("X-Forwarded-For", ip);
}

@Test
void testDrsResolutionWithoutOptionalHeaders() throws Exception {
String googleProject = null;
String ip = null;
SignedUrlTestUtils.setupSignedUrlMocks(authService, googleStorageService, googleProject, url);

when(drsApi.getAccessURL(PATH, accessId)).thenReturn(new AccessURL().url(url.toString()));
var response =
drsResolutionService.fetchDrsObjectAccessUrl(
testDrsProvider,
uriComponents,
accessId,
TypeEnum.GS,
List.of(BEARERAUTH),
new AuditLogEvent.Builder(),
ip,
googleProject);
assertThat("signed url is properly returned", response.getUrl(), equalTo(url.toString()));
verify(drsApi, never()).setHeader("X-Forwarded-For", ip);
verify(drsApi, never()).setHeader("x-user-project", googleProject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void testGetSignedUrl() throws MalformedURLException {
var drsUri = "drs://dg.4503:1234/456/2315asd";
var bucketName = "my-test-bucket";
var objectName = "my-test-folder/my-test-object.txt";
var ip = "test.ip";
var googleProject = "test-google-project";
var url = new URL("https", "storage.cloud.google.com", "/" + bucketName + "/" + objectName);

Expand Down Expand Up @@ -65,6 +66,7 @@ void testGetSignedUrlDataObjectUriOnly() throws Exception {
var drsUri = "drs://dg.4503:1234/456/2315asd";
var bucketName = "my-test-bucket";
var objectName = "my-test-folder/my-test-object.txt";
var ip = "test.ip";
var googleProject = "test-google-project";
var url = new URL("https", "storage.cloud.google.com", "/" + bucketName + "/" + objectName);

Expand Down
Loading