Skip to content
Open
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 @@ -379,24 +379,33 @@ private Iterator<? extends OzoneBucket> iterateBuckets(
OzoneVolume volume = getVolume();
ownerSetter.accept(volume);
return query.apply(volume);
} catch (OMException e) {
if (e.getResult() == ResultCodes.VOLUME_NOT_FOUND) {
return Collections.emptyIterator();
} else if (e.getResult() == ResultCodes.PERMISSION_DENIED) {
throw newError(S3ErrorTable.ACCESS_DENIED,
"listBuckets", e);
} else if (isExpiredToken(e)) {
throw newError(S3ErrorTable.EXPIRED_TOKEN, s3Auth.getAccessID(), e);
} else if (e.getResult() == ResultCodes.INVALID_TOKEN) {
throw newError(S3ErrorTable.ACCESS_DENIED,
s3Auth.getAccessID(), e);
} else if (e.getResult() == ResultCodes.TIMEOUT ||
e.getResult() == ResultCodes.INTERNAL_ERROR) {
throw newError(S3ErrorTable.INTERNAL_ERROR,
"listBuckets", e);
} else {
throw e;
} catch (RuntimeException e) {
if (e.getCause() instanceof OMException) {
return handleOMException((OMException) e.getCause());
}
throw e;
} catch (OMException e) {
return handleOMException(e);
}
}

private Iterator<OzoneBucket> handleOMException(OMException e) throws OMException {
if (e.getResult() == ResultCodes.VOLUME_NOT_FOUND) {
return Collections.emptyIterator();
} else if (e.getResult() == ResultCodes.PERMISSION_DENIED) {
throw newError(S3ErrorTable.ACCESS_DENIED,
"listBuckets", e);
} else if (isExpiredToken(e)) {
throw newError(S3ErrorTable.EXPIRED_TOKEN, s3Auth.getAccessID(), e);
} else if (e.getResult() == ResultCodes.INVALID_TOKEN) {
throw newError(S3ErrorTable.ACCESS_DENIED,
s3Auth.getAccessID(), e);
} else if (e.getResult() == ResultCodes.TIMEOUT ||
e.getResult() == ResultCodes.INTERNAL_ERROR) {
throw newError(S3ErrorTable.INTERNAL_ERROR,
"listBuckets", e);
} else {
throw e;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import org.apache.hadoop.ozone.OzoneConsts;
import org.apache.hadoop.ozone.client.OzoneVolume;
import org.apache.hadoop.ozone.om.exceptions.OMException;
import org.apache.hadoop.ozone.s3.exception.OS3Exception;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -137,4 +141,45 @@ public void init() { }
assertFalse(endpointBase.isExpiredToken(new OMException(ResultCodes.INVALID_TOKEN)));
}

@Test
public void testListS3BucketsHandlesRuntimeExceptionWrappingOMException() throws Exception {
final EndpointBase endpointBase = new EndpointBase() {
@Override
public void init() { }

@Override
protected OzoneVolume getVolume() {
final OzoneVolume volume = mock(OzoneVolume.class);
when(volume.listBuckets(anyString())).thenThrow(
new RuntimeException(new OMException("Permission Denied", ResultCodes.PERMISSION_DENIED)));
return volume;
}
};

final OS3Exception e = assertThrows(
OS3Exception.class, () -> endpointBase.listS3Buckets(
"prefix", volume -> { }), "listS3Buckets should fail.");

// Ensure we get the correct code
assertEquals("AccessDenied", e.getCode());
}

@Test
public void testListS3BucketsHandlesRuntimeExceptionWrappingOMExceptionVolumeNotFound() throws Exception {
final EndpointBase endpointBase = new EndpointBase() {
@Override
public void init() { }

@Override
protected OzoneVolume getVolume() {
final OzoneVolume volume = mock(OzoneVolume.class);
when(volume.listBuckets(anyString())).thenThrow(
new RuntimeException(new OMException("Volume Not Found", ResultCodes.VOLUME_NOT_FOUND)));
return volume;
}
};

// Ensure we get an empty iterator
assertFalse(endpointBase.listS3Buckets("prefix", volume -> { }).hasNext());
}
}