|
| 1 | +package com.uid2.shared.secure; |
| 2 | + |
| 3 | +import com.uid2.shared.secure.azurecc.IMaaTokenSignatureValidator; |
| 4 | +import com.uid2.shared.secure.azurecc.IPolicyValidator; |
| 5 | +import com.uid2.shared.secure.azurecc.MaaTokenPayload; |
| 6 | +import com.uid2.shared.secure.azurecc.RuntimeData; |
| 7 | +import io.vertx.core.AsyncResult; |
| 8 | +import io.vertx.core.Handler; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 12 | +import org.mockito.Mock; |
| 13 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 14 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 15 | +import org.mockito.quality.Strictness; |
| 16 | + |
| 17 | +import java.nio.ByteBuffer; |
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Base64; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | +import static org.mockito.ArgumentMatchers.any; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +@ExtendWith(MockitoExtension.class) |
| 27 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 28 | +class AzureCCAksCoreAttestationServiceTest { |
| 29 | + private static byte[] encodeStringUnicodeAttestationEndpoint(String data) { |
| 30 | + // buffer.array() may include extra empty bytes at the end. This returns only the bytes that have data |
| 31 | + ByteBuffer buffer = StandardCharsets.UTF_8.encode(data); |
| 32 | + return Arrays.copyOf(buffer.array(), buffer.limit()); |
| 33 | + } |
| 34 | + private static final String ATTESTATION_REQUEST = "test-attestation-request"; |
| 35 | + |
| 36 | + private static final String PUBLIC_KEY = "test-public-key"; |
| 37 | + |
| 38 | + private static final String ENCLAVE_ID = "test-enclave"; |
| 39 | + private static final String ATTESTATION_URL = "https://example.com"; |
| 40 | + private static final String ENCODED_ATTESTATION_URL = Base64.getEncoder().encodeToString(encodeStringUnicodeAttestationEndpoint(ATTESTATION_URL)); |
| 41 | + private static final RuntimeData RUNTIME_DATA = RuntimeData.builder().attestationUrl(ENCODED_ATTESTATION_URL).build(); |
| 42 | + |
| 43 | + private static final MaaTokenPayload VALID_TOKEN_PAYLOAD = MaaTokenPayload.builder().runtimeData(RUNTIME_DATA).build(); |
| 44 | + @Mock |
| 45 | + private IMaaTokenSignatureValidator alwaysPassTokenValidator; |
| 46 | + |
| 47 | + @Mock |
| 48 | + private IMaaTokenSignatureValidator alwaysFailTokenValidator; |
| 49 | + |
| 50 | + @Mock |
| 51 | + private IPolicyValidator alwaysPassPolicyValidator; |
| 52 | + |
| 53 | + @Mock |
| 54 | + private IPolicyValidator alwaysFailPolicyValidator; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + public void setup() throws AttestationException { |
| 58 | + when(alwaysPassTokenValidator.validate(any(), any())).thenReturn(VALID_TOKEN_PAYLOAD); |
| 59 | + when(alwaysPassPolicyValidator.validate(any(), any())).thenReturn(ENCLAVE_ID); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testHappyPath() throws AttestationException { |
| 64 | + var provider = new AzureCCAksCoreAttestationService(alwaysPassTokenValidator, alwaysPassPolicyValidator); |
| 65 | + provider.registerEnclave(ENCLAVE_ID); |
| 66 | + attest(provider, ar -> { |
| 67 | + assertTrue(ar.succeeded()); |
| 68 | + assertTrue(ar.result().isSuccess()); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testSignatureCheckFailed_ClientError() throws AttestationException { |
| 74 | + var errorStr = "token signature validation failed"; |
| 75 | + when(alwaysFailTokenValidator.validate(any(), any())).thenThrow(new AttestationClientException(errorStr, AttestationFailure.BAD_PAYLOAD)); |
| 76 | + var provider = new AzureCCAksCoreAttestationService(alwaysFailTokenValidator, alwaysPassPolicyValidator); |
| 77 | + provider.registerEnclave(ENCLAVE_ID); |
| 78 | + attest(provider, ar -> { |
| 79 | + assertTrue(ar.succeeded()); |
| 80 | + assertFalse(ar.result().isSuccess()); |
| 81 | + assertEquals(errorStr, ar.result().getReason()); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testSignatureCheckFailed_ServerError() throws AttestationException { |
| 87 | + when(alwaysFailTokenValidator.validate(any(), any())).thenThrow(new AttestationException("unknown server error")); |
| 88 | + var provider = new AzureCCAksCoreAttestationService(alwaysFailTokenValidator, alwaysPassPolicyValidator); |
| 89 | + provider.registerEnclave(ENCLAVE_ID); |
| 90 | + attest(provider, ar -> { |
| 91 | + assertFalse(ar.succeeded()); |
| 92 | + assertTrue(ar.cause() instanceof AttestationException); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testPolicyCheckSuccess_ClientError() throws AttestationException { |
| 98 | + var errorStr = "policy validation failed"; |
| 99 | + when(alwaysFailPolicyValidator.validate(any(), any())).thenThrow(new AttestationClientException(errorStr, AttestationFailure.BAD_PAYLOAD)); |
| 100 | + var provider = new AzureCCAksCoreAttestationService(alwaysFailTokenValidator, alwaysFailPolicyValidator); |
| 101 | + provider.registerEnclave(ENCLAVE_ID); |
| 102 | + attest(provider, ar -> { |
| 103 | + assertTrue(ar.succeeded()); |
| 104 | + assertFalse(ar.result().isSuccess()); |
| 105 | + assertEquals(errorStr, ar.result().getReason()); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testPolicyCheckFailed_ServerError() throws AttestationException { |
| 111 | + when(alwaysFailPolicyValidator.validate(any(), any())).thenThrow(new AttestationException("unknown server error")); |
| 112 | + var provider = new AzureCCAksCoreAttestationService(alwaysFailTokenValidator, alwaysFailPolicyValidator); |
| 113 | + provider.registerEnclave(ENCLAVE_ID); |
| 114 | + attest(provider, ar -> { |
| 115 | + assertFalse(ar.succeeded()); |
| 116 | + assertTrue(ar.cause() instanceof AttestationException); |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testEnclaveNotRegistered() throws AttestationException { |
| 122 | + var provider = new AzureCCAksCoreAttestationService(alwaysFailTokenValidator, alwaysPassPolicyValidator); |
| 123 | + attest(provider, ar -> { |
| 124 | + assertTrue(ar.succeeded()); |
| 125 | + assertFalse(ar.result().isSuccess()); |
| 126 | + assertEquals(AttestationFailure.FORBIDDEN_ENCLAVE, ar.result().getFailure()); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + private static void attest(ICoreAttestationService provider, Handler<AsyncResult<AttestationResult>> handler) { |
| 131 | + provider.attest( |
| 132 | + ATTESTATION_REQUEST.getBytes(StandardCharsets.UTF_8), |
| 133 | + PUBLIC_KEY.getBytes(StandardCharsets.UTF_8), |
| 134 | + handler); |
| 135 | + } |
| 136 | +} |
0 commit comments