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

fix: set visibility of ConfigBasedAuthenticatorFactory.createAuthenticator to protected #165

Merged
merged 1 commit into from
Feb 21, 2022
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
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corp. 2019, 2021.
* (C) Copyright IBM Corp. 2019, 2022.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -58,7 +58,7 @@ public static Authenticator getAuthenticator(String serviceName) {
* @param props a Map containing configuration properties
* @return an Authenticator instance
*/
private static Authenticator createAuthenticator(Map<String, String> props) {
protected static Authenticator createAuthenticator(Map<String, String> props) {
Authenticator authenticator = null;

// If auth type was not specified, we'll use "iam" as the default if the "apikey" property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ public void testCtorGoodConfigMap() {

@Test
public void testSingleAuthHeader() {
String username = "good-username";
padamstx marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String> props = new HashMap<>();
props.put(Authenticator.PROPNAME_USERNAME, "good-username");
props.put(Authenticator.PROPNAME_PASSWORD, "good-password");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,43 @@ public void testNoConfig() {
assertNull(auth);
}

/**
* This class is a subclass of our authenticator factory and is
* simply a test to make sure that the subclass usecase works as expected.
*/
public static class TestAuthFactorySubclass extends ConfigBasedAuthenticatorFactory {

public static Authenticator getAuthenticator(String serviceName) {
// For testing purposes, just hard-code the service properties
// so we can simulate an alternate config source.
Map<String, String> authProps = new HashMap<>();
authProps.put(Authenticator.PROPNAME_AUTH_TYPE, "basic");
authProps.put(Authenticator.PROPNAME_USERNAME, "myuser");
authProps.put(Authenticator.PROPNAME_PASSWORD, "mypassword");

Authenticator authenticator = createAuthenticator(authProps);

return authenticator;
}
}

@Test
public void testFactorySubclass() {

Authenticator auth = TestAuthFactorySubclass.getAuthenticator("dont_care");
assertNotNull(auth);
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_BASIC);
}

@Test
public void testFileCredentialsService1() {
PowerMockito.spy(EnvironmentUtils.class);
PowerMockito.when(EnvironmentUtils.getenv("IBM_CREDENTIALS_FILE")).thenReturn(ALTERNATE_CRED_FILENAME);
assertEquals(ALTERNATE_CRED_FILENAME, EnvironmentUtils.getenv("IBM_CREDENTIALS_FILE"));
assertEquals(EnvironmentUtils.getenv("IBM_CREDENTIALS_FILE"), ALTERNATE_CRED_FILENAME);
padamstx marked this conversation as resolved.
Show resolved Hide resolved

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("service-1");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_IAM, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_IAM);
}

@Test
Expand All @@ -151,7 +179,7 @@ public void testFileCredentialsService2() {

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("service2");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_BASIC, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_BASIC);
}

@Test
Expand All @@ -161,7 +189,7 @@ public void testFileCredentialsService3() {

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("service3");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_CP4D, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_CP4D);
}

@Test
Expand All @@ -171,7 +199,7 @@ public void testFileCredentialsService4() {

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("service4");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_NOAUTH, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_NOAUTH);
}

@Test
Expand Down Expand Up @@ -266,7 +294,7 @@ public void testFileCredentialsService5() {

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("service5");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_BEARER_TOKEN, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_BEARER_TOKEN);
}

@Test(expectedExceptions = IllegalArgumentException.class)
Expand Down Expand Up @@ -304,11 +332,11 @@ public void testFileCredentialsError4() {
@Test
public void testFileCredentialsSystemPropService1() {
System.setProperty("IBM_CREDENTIALS_FILE", ALTERNATE_CRED_FILENAME);
assertEquals(ALTERNATE_CRED_FILENAME, System.getProperty("IBM_CREDENTIALS_FILE"));
assertEquals(System.getProperty("IBM_CREDENTIALS_FILE"), ALTERNATE_CRED_FILENAME);

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("service-1");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_IAM, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_IAM);
System.clearProperty("IBM_CREDENTIALS_FILE");
}

Expand All @@ -319,7 +347,7 @@ public void testEnvCredentialsService1() {

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("service-1");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_IAM, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_IAM);
IamAuthenticator iamAuth = (IamAuthenticator) auth;
assertEquals(iamAuth.getApiKey(), "my-api-key");
assertEquals(iamAuth.getClientId(), "my-client-id");
Expand All @@ -335,7 +363,7 @@ public void testEnvCredentialsService6() {

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("service6");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_CP4D_SERVICE, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_CP4D_SERVICE);
CloudPakForDataServiceAuthenticator cp4dAuth = (CloudPakForDataServiceAuthenticator) auth;
assertEquals(cp4dAuth.getURL(), "https://service1/zen-data/internal");
assertEquals(cp4dAuth.getServiceBrokerSecret(), "f8b7czjt701wy6253be5q8ad8f07kd08");
Expand All @@ -349,7 +377,7 @@ public void testEnvCredentialsService7() {

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("service7");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_CONTAINER, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_CONTAINER);
ContainerAuthenticator containerAuth = (ContainerAuthenticator) auth;
assertEquals(containerAuth.getURL(), "https://iam.com/api");
assertEquals(containerAuth.getCrTokenFilename(), "cr-token.txt");
Expand All @@ -368,7 +396,7 @@ public void testEnvCredentialsService8() {

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("service8");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_VPC, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_VPC);
VpcInstanceAuthenticator containerAuth = (VpcInstanceAuthenticator) auth;
assertEquals(containerAuth.getIamProfileCrn(), "crn:iam-profile-1");
assertNull(containerAuth.getIamProfileId());
Expand Down Expand Up @@ -413,7 +441,7 @@ public void testVcapCredentialsDiscovery() {

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("discovery");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_BASIC, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_BASIC);
}

@Test
Expand All @@ -422,6 +450,6 @@ public void testVcapCredentialsLT() {

Authenticator auth = ConfigBasedAuthenticatorFactory.getAuthenticator("language_translator");
assertNotNull(auth);
assertEquals(Authenticator.AUTHTYPE_IAM, auth.authenticationType());
assertEquals(auth.authenticationType(), Authenticator.AUTHTYPE_IAM);
}
}