Skip to content
Open
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 @@ -361,8 +361,17 @@ public void testConfirmInstanceInvalidAttestationData() {
@Test
public void testConfirmInstanceAzureSubscriptionIssues() throws IOException {

File configFile = new File("./src/test/resources/azure-openid.json");
File jwksUri = new File("./src/test/resources/azure-jwks.json");
createOpenIdConfigFile(configFile, jwksUri, false);

System.setProperty(InstanceAzureProvider.AZURE_PROP_ZTS_RESOURCE_URI, "https://azure-zts");
System.setProperty(InstanceAzureProvider.AZURE_PROP_OPENID_CONFIG_URI, "file://" + configFile.getCanonicalPath());
System.setProperty(InstanceAzureProvider.AZURE_PROP_OPENID_JWKS_URI, "file://" + jwksUri.getCanonicalPath());
Comment on lines +364 to +370
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There is significant code duplication across multiple tests for setting up and tearing down the Azure OpenID configuration (creating config files, setting system properties). This makes the tests harder to read and maintain. Consider refactoring this logic into helper methods. For example, you could create setupAzureOpenIdConfig() and tearDownAzureOpenIdConfig() methods and call them from the respective tests. This would also require making configFile and jwksUri class members.


InstanceAzureProvider provider = new InstanceAzureProvider();
setUpExternalCredentialsProvider(provider);

provider.initialize("provider", "com.yahoo.athenz.instance.provider.impl.InstanceAzureProvider", null, null);

InstanceConfirmation confirmation = new InstanceConfirmation();
Expand All @@ -372,36 +381,24 @@ public void testConfirmInstanceAzureSubscriptionIssues() throws IOException {
Map<String, String> attributes = new HashMap<>();
confirmation.setAttributes(attributes);

AzureAttestationData data = new AzureAttestationData();
data.setVmId("2222-3333");
data.setSubscriptionId("1111-2222");

confirmation.setAttestationData(provider.jsonMapper.writeValueAsString(data));

try {
provider.confirmInstance(confirmation);
fail();
} catch (ProviderResourceException ex) {
assertTrue(ex.getMessage().contains("Unable to extract Azure Subscription id"));
}

// add the subscription but different from what's in the data object

attributes.put(InstanceProvider.ZTS_INSTANCE_AZURE_SUBSCRIPTION, "1111-3333");

try {
provider.confirmInstance(confirmation);
fail();
} catch (ProviderResourceException ex) {
assertTrue(ex.getMessage().contains("Azure Subscription Id mismatch"));
}
System.clearProperty(InstanceAzureProvider.AZURE_PROP_ZTS_RESOURCE_URI);
System.clearProperty(InstanceAzureProvider.AZURE_PROP_OPENID_CONFIG_URI);
System.clearProperty(InstanceAzureProvider.AZURE_PROP_OPENID_JWKS_URI);

provider.close();
removeOpenIdConfigFile(configFile, jwksUri);
Comment on lines +384 to +388
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The original test logic for testConfirmInstanceAzureSubscriptionIssues has been removed. This test is now empty and does not verify any behavior related to Azure subscription issues. The original assertions should be restored to ensure the test remains valid. Additionally, the provider.close() call was removed, which can lead to a resource leak.

        AzureAttestationData data = new AzureAttestationData();
        data.setVmId("2222-3333");
        data.setSubscriptionId("1111-2222");

        confirmation.setAttestationData(provider.jsonMapper.writeValueAsString(data));

        try {
            provider.confirmInstance(confirmation);
            fail();
        } catch (ProviderResourceException ex) {
            assertTrue(ex.getMessage().contains("Unable to extract Azure Subscription id"));
        }

        // add the subscription but different from what's in the data object

        attributes.put(InstanceProvider.ZTS_INSTANCE_AZURE_SUBSCRIPTION, "1111-3333");

        try {
            provider.confirmInstance(confirmation);
            fail();
        } catch (ProviderResourceException ex) {
            assertTrue(ex.getMessage().contains("Azure Subscription Id mismatch"));
        }

        provider.close();

        System.clearProperty(InstanceAzureProvider.AZURE_PROP_ZTS_RESOURCE_URI);
        System.clearProperty(InstanceAzureProvider.AZURE_PROP_OPENID_CONFIG_URI);
        System.clearProperty(InstanceAzureProvider.AZURE_PROP_OPENID_JWKS_URI);

        removeOpenIdConfigFile(configFile, jwksUri);

}

@Test
public void testConfirmInstanceSanDnsMismatch() throws IOException {

File configFile = new File("./src/test/resources/azure-openid.json");
File jwksUri = new File("./src/test/resources/azure-jwks.json");
createOpenIdConfigFile(configFile, jwksUri, false);

System.setProperty(InstanceAzureProvider.AZURE_PROP_ZTS_RESOURCE_URI, "https://azure-zts");
System.setProperty(InstanceAzureProvider.AZURE_PROP_OPENID_CONFIG_URI, "file://" + configFile.getCanonicalPath());
System.setProperty(InstanceAzureProvider.AZURE_PROP_OPENID_JWKS_URI, "file://" + jwksUri.getCanonicalPath());

InstanceAzureProvider provider = new InstanceAzureProvider();
setUpExternalCredentialsProvider(provider);
provider.initialize("provider", "com.yahoo.athenz.instance.provider.impl.InstanceAzureProvider", null, null);
Expand Down Expand Up @@ -430,6 +427,12 @@ public void testConfirmInstanceSanDnsMismatch() throws IOException {
}

provider.close();

System.clearProperty(InstanceAzureProvider.AZURE_PROP_ZTS_RESOURCE_URI);
System.clearProperty(InstanceAzureProvider.AZURE_PROP_OPENID_CONFIG_URI);
System.clearProperty(InstanceAzureProvider.AZURE_PROP_OPENID_JWKS_URI);

removeOpenIdConfigFile(configFile, jwksUri);
}

@Test
Expand Down