use of com.yahoo.athenz.common.server.dns.HostnameResolver in project athenz by yahoo.
the class InstanceZTSProviderTest method testValidateHostname.
@Test
public void testValidateHostname() {
HostnameResolver hostnameResolver = Mockito.mock(HostnameResolver.class);
Mockito.when(hostnameResolver.getAllByName("abc.athenz.com")).thenReturn(new HashSet<>(Arrays.asList("10.1.1.1", "2001:db8:a0b:12f0:0:0:0:1")));
InstanceZTSProvider provider = new InstanceZTSProvider();
provider.initialize("provider", "com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider", null, null);
provider.setHostnameResolver(hostnameResolver);
assertTrue(provider.validateHostname("abc.athenz.com", new String[] { "10.1.1.1" }));
assertTrue(provider.validateHostname("abc.athenz.com", new String[] { "10.1.1.1", "2001:db8:a0b:12f0:0:0:0:1" }));
assertFalse(provider.validateHostname("abc.athenz.com", new String[] { "10.1.1.2" }));
assertFalse(provider.validateHostname("abc.athenz.com", new String[] { "10.1.1.1", "1:2:3:4:5:6:7:8" }));
assertFalse(provider.validateHostname("abc.athenz.com", new String[] { "10.1.1.2", "1:2:3:4:5:6:7:8" }));
// If hostname is passed, sanIp must be non empty
assertFalse(provider.validateHostname("abc.athenz.com", null));
assertFalse(provider.validateHostname("abc.athenz.com", new String[] {}));
assertFalse(provider.validateHostname("abc.athenz.com", new String[] { "" }));
// It's possible client didn't set Hostname payload. One sanIp be optionally set, and would have been matched with clientIp upstream
assertTrue(provider.validateHostname("", new String[] { "10.1.1.1" }));
assertTrue(provider.validateHostname(null, new String[] { "10.1.1.1" }));
// If more than one sanIp is passed, hostname must be non empty
assertFalse(provider.validateHostname(null, new String[] { "10.1.1.1", "2001:db8:a0b:12f0:0:0:0:1" }));
assertFalse(provider.validateHostname("", new String[] { "10.1.1.1", "2001:db8:a0b:12f0:0:0:0:1" }));
provider.close();
}
use of com.yahoo.athenz.common.server.dns.HostnameResolver in project athenz by yahoo.
the class CertFailedRefreshNotificationTaskTest method testSeveralProviders.
@Test
public void testSeveralProviders() {
Date currentDate = new Date();
List<X509CertRecord> records = new ArrayList<>();
Mockito.when(hostnameResolver.isValidHostname(anyString())).thenReturn(true);
// Configure 3 providers in property
System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider1, provider2, provider3");
// Create 7 records, each in it's own domain (domain0, domain1... domain6)
for (int i = 0; i < 7; ++i) {
X509CertRecord record = getMockX509CertRecord(currentDate, i);
records.add(record);
NotificationTestsCommon.mockDomainData(i, dataStore);
}
// Set one record in provider1, two records in provider2 and three records in provider3
records.get(0).setProvider("provider1");
records.get(1).setProvider("provider2");
records.get(2).setProvider("provider2");
records.get(3).setProvider("provider3");
records.get(4).setProvider("provider3");
records.get(5).setProvider("provider3");
// Set one record in a provider not configured in properties (shouldn't be retrieved)
records.get(6).setProvider("providerNotInProperty");
Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), eq("provider1"))).thenReturn(records.subList(0, 1));
Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), eq("provider2"))).thenReturn(records.subList(1, 3));
Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), eq("provider3"))).thenReturn(records.subList(3, 6));
Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), eq("providerNotInProperty"))).thenReturn(records.subList(6, 7));
CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask(instanceCertManager, dataStore, hostnameResolver, userDomainPrefix, serverName, httpsPort, notificationToEmailConverterCommon);
List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications();
assertEquals(6, notifications.size());
notifications.sort(Comparator.comparing(notif -> notif.getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS)));
// Assert one records for provider1:
String expectedDetail = "service0;provider1;instanceID0;" + Timestamp.fromMillis(currentDate.getTime()) + ";;hostName0";
assertEquals(expectedDetail, notifications.get(0).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
// Assert two records for provider2:
expectedDetail = "service1;provider2;instanceID1;" + Timestamp.fromMillis(currentDate.getTime()) + ";;hostName1";
assertEquals(expectedDetail, notifications.get(1).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
expectedDetail = "service2;provider2;instanceID2;" + Timestamp.fromMillis(currentDate.getTime()) + ";;hostName2";
assertEquals(expectedDetail, notifications.get(2).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
// Assert three records for provider3:
expectedDetail = "service3;provider3;instanceID3;" + Timestamp.fromMillis(currentDate.getTime()) + ";;hostName3";
assertEquals(expectedDetail, notifications.get(3).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
expectedDetail = "service4;provider3;instanceID4;" + Timestamp.fromMillis(currentDate.getTime()) + ";;hostName4";
assertEquals(expectedDetail, notifications.get(4).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
expectedDetail = "service5;provider3;instanceID5;" + Timestamp.fromMillis(currentDate.getTime()) + ";;hostName5";
assertEquals(expectedDetail, notifications.get(5).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST);
}
use of com.yahoo.athenz.common.server.dns.HostnameResolver in project athenz by yahoo.
the class CertFailedRefreshNotificationTaskTest method setup.
@BeforeClass
public void setup() {
instanceCertManager = Mockito.mock(InstanceCertManager.class);
dataStore = Mockito.mock(DataStore.class);
hostnameResolver = Mockito.mock(HostnameResolver.class);
notificationToEmailConverterCommon = new NotificationToEmailConverterCommon(null);
}
use of com.yahoo.athenz.common.server.dns.HostnameResolver in project athenz by yahoo.
the class ZTSImplTest method testPostInstanceRefreshInformationWithHostnameCnames.
@Test
public void testPostInstanceRefreshInformationWithHostnameCnames() throws IOException {
ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
DataStore store = new DataStore(structStore, null, ztsMetric);
ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store);
SignedDomain providerDomain = signedAuthorizedProviderDomain();
store.processSignedDomain(providerDomain, false);
SignedDomain tenantDomain = signedBootstrapTenantDomain("athenz.provider", "athenz", "production");
store.processSignedDomain(tenantDomain, false);
Path path = Paths.get("src/test/resources/athenz.instanceid.cname.csr");
String certCsr = new String(Files.readAllBytes(path));
List<String> cnames = new ArrayList<>();
cnames.add("cname1.athenz.info");
cnames.add("cname2.athenz.info");
String service = "athenz.production";
HostnameResolver resolver = Mockito.mock(HostnameResolver.class);
Mockito.when(resolver.isValidHostCnameList(service, "host1.athenz.cloud", cnames, CertType.X509)).thenReturn(true);
Mockito.when(resolver.isValidHostname("host1.athenz.cloud")).thenReturn(true);
InstanceProviderManager instanceProviderManager = Mockito.mock(InstanceProviderManager.class);
InstanceProvider providerClient = Mockito.mock(InstanceProvider.class);
Mockito.when(providerClient.getProviderScheme()).thenReturn(InstanceProvider.Scheme.CLASS);
InstanceConfirmation confirmation = new InstanceConfirmation().setDomain("athenz").setService("production").setProvider("athenz.provider");
InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);
Mockito.when(instanceProviderManager.getProvider("athenz.provider", resolver)).thenReturn(providerClient);
Mockito.when(providerClient.refreshInstance(Mockito.any())).thenReturn(confirmation);
Mockito.when(instanceManager.insertX509CertRecord(Mockito.any())).thenReturn(true);
X509CertRecord certRecord = new X509CertRecord();
certRecord.setInstanceId("1001");
certRecord.setProvider("athenz.provider");
certRecord.setService("athenz.production");
certRecord.setCurrentSerial("16503746516960996918");
certRecord.setPrevSerial("16503746516960996918");
Mockito.when(instanceManager.getX509CertRecord("athenz.provider", "1001", "athenz.production")).thenReturn(certRecord);
Mockito.when(instanceManager.updateX509CertRecord(Mockito.any())).thenReturn(true);
path = Paths.get("src/test/resources/athenz.instanceid.pem");
String pem = new String(Files.readAllBytes(path));
InstanceIdentity identity = new InstanceIdentity().setName("athenz.production").setX509Certificate(pem);
Mockito.doReturn(identity).when(instanceManager).generateIdentity(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyInt(), Mockito.any());
ztsImpl.instanceProviderManager = instanceProviderManager;
ztsImpl.instanceCertManager = instanceManager;
InstanceRefreshInformation info = new InstanceRefreshInformation().setCsr(certCsr).setToken(true).setHostname("host1.athenz.cloud").setHostCnames(cnames);
CertificateAuthority certAuthority = new CertificateAuthority();
SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("athenz", "production", "v=S1;d=athenz;n=production;s=signature", 0, certAuthority);
assertNotNull(principal);
X509Certificate cert = Crypto.loadX509Certificate(pem);
principal.setX509Certificate(cert);
ResourceContext context = createResourceContext(principal);
// setup the hostname resolver for our request
ztsImpl.hostnameResolver = resolver;
InstanceIdentity instanceIdentity = ztsImpl.postInstanceRefreshInformation(context, "athenz.provider", "athenz", "production", "1001", info);
assertNotNull(instanceIdentity);
}
use of com.yahoo.athenz.common.server.dns.HostnameResolver in project athenz by yahoo.
the class ZTSImplTest method testPostInstanceRegisterInformationWithHostnameCnames.
@Test
public void testPostInstanceRegisterInformationWithHostnameCnames() throws IOException {
ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
DataStore store = new DataStore(structStore, null, ztsMetric);
ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store);
SignedDomain providerDomain = signedAuthorizedProviderDomain();
store.processSignedDomain(providerDomain, false);
SignedDomain tenantDomain = signedBootstrapTenantDomain("athenz.provider", "athenz", "production");
store.processSignedDomain(tenantDomain, false);
Path path = Paths.get("src/test/resources/athenz.instanceid.cname.csr");
String certCsr = new String(Files.readAllBytes(path));
InstanceProviderManager instanceProviderManager = Mockito.mock(InstanceProviderManager.class);
InstanceProvider providerClient = Mockito.mock(InstanceProvider.class);
Mockito.when(providerClient.getProviderScheme()).thenReturn(InstanceProvider.Scheme.CLASS);
Map<String, String> attrs = new HashMap<>();
attrs.put("certSSH", "true");
// setup the hostname resolver for our request
List<String> cnames = new ArrayList<>();
cnames.add("cname1.athenz.info");
cnames.add("cname2.athenz.info");
String service = "athenz.production";
HostnameResolver resolver = Mockito.mock(HostnameResolver.class);
Mockito.when(resolver.isValidHostCnameList(service, "host1.athenz.cloud", cnames, CertType.X509)).thenReturn(true);
Mockito.when(resolver.isValidHostname("host1.athenz.cloud")).thenReturn(true);
InstanceConfirmation confirmation = new InstanceConfirmation().setDomain("athenz").setService("production").setProvider("athenz.provider").setAttributes(attrs);
InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);
Mockito.when(instanceProviderManager.getProvider("athenz.provider", resolver)).thenReturn(providerClient);
Mockito.when(providerClient.confirmInstance(Mockito.any())).thenReturn(confirmation);
Mockito.when(instanceManager.insertX509CertRecord(Mockito.any())).thenReturn(true);
path = Paths.get("src/test/resources/athenz.instanceid.pem");
String pem = new String(Files.readAllBytes(path));
InstanceIdentity identity = new InstanceIdentity().setName("athenz.production").setX509Certificate(pem);
Mockito.doReturn(identity).when(instanceManager).generateIdentity(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyInt(), Mockito.any());
ztsImpl.instanceProviderManager = instanceProviderManager;
ztsImpl.instanceCertManager = instanceManager;
ztsImpl.hostnameResolver = resolver;
InstanceRegisterInformation info = new InstanceRegisterInformation().setAttestationData("attestationData").setCsr(certCsr).setDomain("athenz").setService("production").setProvider("athenz.provider").setToken(true).setHostname("host1.athenz.cloud").setHostCnames(cnames);
ResourceContext context = createResourceContext(null);
Response response = ztsImpl.postInstanceRegisterInformation(context, info);
assertEquals(response.getStatus(), 201);
InstanceIdentity resIdentity = (InstanceIdentity) response.getEntity();
assertNotNull(resIdentity.getX509Certificate());
}
Aggregations