use of com.yahoo.athenz.zms.SignedDomain in project athenz by yahoo.
the class ZTSImplTest method testPostInstanceRefreshInformationPrevSerialMatch.
@Test
public void testPostInstanceRefreshInformationPrevSerialMatch() throws IOException {
ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
DataStore store = new DataStore(structStore, null);
ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store);
SignedDomain providerDomain = signedAuthorizedProviderDomain();
store.processDomain(providerDomain, false);
SignedDomain tenantDomain = signedBootstrapTenantDomain("athenz.provider", "athenz", "production");
store.processDomain(tenantDomain, false);
Path path = Paths.get("src/test/resources/athenz.instanceid.csr");
String certCsr = new String(Files.readAllBytes(path));
InstanceProviderManager instanceProviderManager = Mockito.mock(InstanceProviderManager.class);
InstanceProvider providerClient = Mockito.mock(InstanceProvider.class);
InstanceConfirmation confirmation = new InstanceConfirmation().setDomain("athenz").setService("production").setProvider("athenz.provider");
InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);
Mockito.when(instanceProviderManager.getProvider("athenz.provider")).thenReturn(providerClient);
Mockito.when(providerClient.refreshInstance(Mockito.any())).thenReturn(confirmation);
X509CertRecord certRecord = new X509CertRecord();
certRecord.setInstanceId("1001");
certRecord.setProvider("athenz.provider");
certRecord.setService("athenz.production");
certRecord.setCurrentSerial("101");
certRecord.setPrevSerial("16503746516960996918");
Mockito.when(instanceManager.getX509CertRecord("athenz.provider", "1001")).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.anyInt());
ztsImpl.instanceProviderManager = instanceProviderManager;
ztsImpl.instanceCertManager = instanceManager;
InstanceRefreshInformation info = new InstanceRefreshInformation().setCsr(certCsr);
CertificateAuthority certAuthority = new CertificateAuthority();
SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("athenz", "production", "v=S1;d=athenz;n=production;s=signature", 0, certAuthority);
X509Certificate cert = Crypto.loadX509Certificate(pem);
principal.setX509Certificate(cert);
ResourceContext context = createResourceContext(principal);
InstanceIdentity instanceIdentity = ztsImpl.postInstanceRefreshInformation(context, "athenz.provider", "athenz", "production", "1001", info);
assertNotNull(instanceIdentity);
}
use of com.yahoo.athenz.zms.SignedDomain in project athenz by yahoo.
the class ZTSImplTest method testGetAWSTemporaryCredentialsNoAwsAccount.
@Test
public void testGetAWSTemporaryCredentialsNoAwsAccount() {
Principal principal = SimplePrincipal.create("user_domain", "user101", "v=U1;d=user_domain;n=user101;s=signature", 0, null);
CloudStore cloudStore = new MockCloudStore();
store.setCloudStore(cloudStore);
zts.cloudStore = cloudStore;
SignedDomain signedDomain = createAwsSignedDomain("athenz.product", null);
store.processDomain(signedDomain, false);
try {
zts.getAWSTemporaryCredentials(createResourceContext(principal), "athenz.product", "aws_role_name");
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 400);
}
}
use of com.yahoo.athenz.zms.SignedDomain in project athenz by yahoo.
the class ZTSImplTest method testVerifyAWSAssumeRole.
@Test
public void testVerifyAWSAssumeRole() {
SignedDomain signedDomain = createAwsSignedDomain("athenz.product", "1234");
store.processDomain(signedDomain, false);
// our group includes user100 and user101
assertTrue(zts.verifyAWSAssumeRole("athenz.product", "athenz.product:aws_role_name", "user_domain.user100"));
assertTrue(zts.verifyAWSAssumeRole("athenz.product", "athenz.product:aws_role_name", "user_domain.user101"));
assertFalse(zts.verifyAWSAssumeRole("athenz.product", "athenz.product:aws_role_name", "user_domain.user102"));
}
use of com.yahoo.athenz.zms.SignedDomain in project athenz by yahoo.
the class ZTSImplTest method createSignedDomainExpiration.
private SignedDomain createSignedDomainExpiration(String domainName, String serviceName, Boolean enabled) {
SignedDomain signedDomain = new SignedDomain();
List<Role> roles = new ArrayList<>();
String memberName = "user_domain.user1";
Role role = new Role();
role.setName(generateRoleName(domainName, "admin"));
List<RoleMember> members = new ArrayList<RoleMember>();
RoleMember roleMember = new RoleMember();
roleMember.setMemberName("user_domain.adminuser");
members.add(roleMember);
role.setRoleMembers(members);
roles.add(role);
role = new Role();
role.setName(generateRoleName(domainName, "role1"));
members = new ArrayList<RoleMember>();
roleMember = new RoleMember();
roleMember.setMemberName(memberName);
roleMember.setExpiration(Timestamp.fromMillis(System.currentTimeMillis() - 100));
members.add(roleMember);
role.setRoleMembers(members);
roles.add(role);
role = new Role();
role.setName(generateRoleName(domainName, "role2"));
members = new ArrayList<RoleMember>();
roleMember = new RoleMember();
roleMember.setMemberName(memberName);
roleMember.setExpiration(Timestamp.fromMillis(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1)));
members.add(roleMember);
role.setRoleMembers(members);
roles.add(role);
List<ServiceIdentity> services = new ArrayList<>();
ServiceIdentity service = new ServiceIdentity();
service.setName(generateServiceIdentityName(domainName, serviceName));
setServicePublicKey(service, "0", ZTS_Y64_CERT0);
services.add(service);
DomainData domain = new DomainData();
domain.setName(domainName);
domain.setRoles(roles);
domain.setServices(services);
domain.setModified(Timestamp.fromCurrentTime());
domain.setEnabled(enabled);
signedDomain.setDomain(domain);
signedDomain.setSignature(Crypto.sign(SignUtils.asCanonicalString(domain), privateKey));
signedDomain.setKeyId("0");
return signedDomain;
}
use of com.yahoo.athenz.zms.SignedDomain in project athenz by yahoo.
the class ZTSImplTest method testPostDomainMetrics.
@Test
public void testPostDomainMetrics() {
SignedDomain signedDomain = createSignedDomain("coretech", "weather", "storage", false);
store.processDomain(signedDomain, false);
Principal principal = SimplePrincipal.create("user_domain", "user", "v=U1;d=user_domain;n=user;s=signature", 0, null);
ResourceContext context = createResourceContext(principal);
// create zts with a metric we can verify
CloudStore cloudStore = new CloudStore(null);
cloudStore.setHttpClient(null);
ZtsMetricTester metric = new ZtsMetricTester();
ZTSImpl ztsImpl = new ZTSImpl(cloudStore, store);
ZTSImpl.serverHostName = "localhost";
ztsImpl.metric = metric;
String testDomain = "coretech";
// create some metrics
List<DomainMetric> metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(99));
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED).setMetricVal(27));
DomainMetrics req = new DomainMetrics().setDomainName(testDomain).setMetricList(metricList);
// send the metrics
ztsImpl.postDomainMetrics(context, testDomain, req);
// verify metrics were recorded
Map<String, Integer> metrixMap = metric.getMap();
String key = "dom_metric_" + DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH.toString().toLowerCase() + testDomain;
Integer val = metrixMap.get(key);
assertNotNull(val);
assertEquals(val.intValue(), 99);
key = "dom_metric_" + DomainMetricType.ACCESS_ALLOWED.toString().toLowerCase() + testDomain;
val = metrixMap.get(key);
assertNotNull(val);
assertEquals(val.intValue(), 27);
// test - failure case - invalid domain
testDomain = "not_coretech";
// create some metrics
metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(999));
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED).setMetricVal(277));
req = new DomainMetrics().setDomainName(testDomain).setMetricList(metricList);
// send the metrics
metrixMap.clear();
String errMsg = "No such domain";
try {
ztsImpl.postDomainMetrics(context, testDomain, req);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 404);
assertTrue(ex.getMessage().contains(errMsg));
}
// verify no metrics were recorded
assertEquals(metrixMap.size(), 0);
// test - failure case - missing domain name in metric data
testDomain = "coretech";
// create some metrics
metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(999));
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED).setMetricVal(277));
req = new DomainMetrics().setMetricList(metricList);
errMsg = "Missing required field: domainName";
// send the metrics
metrixMap.clear();
try {
ztsImpl.postDomainMetrics(context, testDomain, req);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 400);
assertTrue(ex.getMessage().contains(errMsg), ex.getMessage());
}
// verify no metrics were recorded
assertEquals(metrixMap.size(), 0);
// test - failure case - mismatch domain in uri and metric data
testDomain = "coretech";
// create some metrics
metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(999));
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED).setMetricVal(277));
req = new DomainMetrics().setDomainName("not_coretech").setMetricList(metricList);
errMsg = "mismatched domain names";
// send the metrics
metrixMap.clear();
try {
ztsImpl.postDomainMetrics(context, testDomain, req);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 400);
assertTrue(ex.getMessage().contains(errMsg), ex.getMessage());
}
// verify no metrics were recorded
assertEquals(metrixMap.size(), 0);
// test - failure case - empty metric list
testDomain = "coretech";
// create some metrics
metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(999));
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED).setMetricVal(277));
req = new DomainMetrics().setDomainName(testDomain);
errMsg = "Missing required field: metricList";
// send the metrics
metrixMap.clear();
try {
ztsImpl.postDomainMetrics(context, testDomain, req);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 400);
assertTrue(ex.getMessage().contains(errMsg), ex.getMessage());
}
// verify no metrics were recorded
assertEquals(metrixMap.size(), 0);
// test - failure case - metric count is missing
testDomain = "coretech";
// create a single metric without a count
metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(-1));
req = new DomainMetrics().setDomainName(testDomain).setMetricList(metricList);
// verify no metrics were recorded
metrixMap.clear();
ztsImpl.postDomainMetrics(context, testDomain, req);
assertEquals(metrixMap.size(), 0);
}
Aggregations