use of javax.ws.rs.core.EntityTag in project athenz by yahoo.
the class ZMSImplTest method testGetJWSDomain.
@Test
public void testGetJWSDomain() throws JsonProcessingException, ParseException, JOSEException {
final String domainName = "jws-domain";
TopLevelDomain dom1 = zmsTestInitializer.createTopLevelDomainObject(domainName, "Test Domain1", "testOrg", zmsTestInitializer.getAdminUser());
zmsTestInitializer.getZms().postTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), zmsTestInitializer.getAuditRef(), dom1);
Response response = zmsTestInitializer.getZms().getJWSDomain(zmsTestInitializer.getMockDomRsrcCtx(), domainName, null, null);
JWSDomain jwsDomain = (JWSDomain) response.getEntity();
DomainData domainData = getDomainData(jwsDomain);
assertNotNull(domainData);
assertEquals(domainData.getName(), "jws-domain");
Map<String, String> header = jwsDomain.getHeader();
assertEquals(header.get("kid"), "0");
// now we're going to ask for the same domain with the tag
// and make sure we get back 304
EntityTag tag = response.getEntityTag();
response = zmsTestInitializer.getZms().getJWSDomain(zmsTestInitializer.getMockDomRsrcCtx(), domainName, Boolean.FALSE, tag.toString());
assertEquals(response.getStatus(), ResourceException.NOT_MODIFIED);
// pass a timestamp a minute back and make sure we
// get back the domain
Timestamp tstamp = Timestamp.fromMillis(System.currentTimeMillis() - 3600);
response = zmsTestInitializer.getZms().getJWSDomain(zmsTestInitializer.getMockDomRsrcCtx(), domainName, false, tstamp.toString());
jwsDomain = (JWSDomain) response.getEntity();
domainData = getDomainData(jwsDomain);
assertNotNull(domainData);
assertEquals(domainData.getName(), "jws-domain");
// any invalid data is also treated as no etag
response = zmsTestInitializer.getZms().getJWSDomain(zmsTestInitializer.getMockDomRsrcCtx(), domainName, null, "unknown-date");
jwsDomain = (JWSDomain) response.getEntity();
domainData = getDomainData(jwsDomain);
assertNotNull(domainData);
assertEquals(domainData.getName(), "jws-domain");
zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), domainName, zmsTestInitializer.getAuditRef());
}
use of javax.ws.rs.core.EntityTag in project athenz by yahoo.
the class ZTSImpl method getDomainSignedPolicyData.
@Override
public Response getDomainSignedPolicyData(ResourceContext ctx, String domainName, String matchingTag) {
final String caller = ctx.getApiName();
final String principalDomain = logPrincipalAndGetDomain(ctx);
validateRequest(ctx.request(), principalDomain, caller);
validate(domainName, TYPE_DOMAIN_NAME, principalDomain, caller);
// for consistent handling of all requests, we're going to convert
// all incoming object values into lower case since ZMS Server
// saves all of its object names in lower case
domainName = domainName.toLowerCase();
setRequestDomain(ctx, domainName);
DomainData domainData = dataStore.getDomainData(domainName);
if (domainData == null) {
setRequestDomain(ctx, ZTSConsts.ZTS_UNKNOWN_DOMAIN);
throw notFoundError("Domain not found: '" + domainName + "'", caller, ZTSConsts.ZTS_UNKNOWN_DOMAIN, principalDomain);
}
Timestamp modified = domainData.getModified();
EntityTag eTag = new EntityTag(modified.toString());
String tag = eTag.toString();
if (matchingTag != null && matchingTag.equals(tag)) {
return Response.status(ResourceException.NOT_MODIFIED).header("ETag", tag).build();
}
// first get our PolicyData object
PolicyData policyData = new PolicyData().setDomain(domainName).setPolicies(getPolicyList(domainData, null));
// then get the signed policy data
Timestamp expires = Timestamp.fromMillis(System.currentTimeMillis() + signedPolicyTimeout);
SignedPolicyData signedPolicyData = new SignedPolicyData().setPolicyData(policyData).setExpires(expires).setModified(modified).setZmsKeyId(domainData.getPolicies().getKeyId()).setZmsSignature(domainData.getPolicies().getSignature());
String signature = Crypto.sign(SignUtils.asCanonicalString(signedPolicyData), privateKey.getKey());
DomainSignedPolicyData result = new DomainSignedPolicyData().setSignedPolicyData(signedPolicyData).setSignature(signature).setKeyId(privateKey.getId());
return Response.status(ResourceException.OK).entity(result).header("ETag", tag).build();
}
use of javax.ws.rs.core.EntityTag in project athenz by yahoo.
the class ZTSImpl method postSignedPolicyRequest.
@Override
public Response postSignedPolicyRequest(ResourceContext ctx, String domainName, SignedPolicyRequest signedPolicyRequest, String matchingTag) {
final String caller = ctx.getApiName();
final String principalDomain = logPrincipalAndGetDomain(ctx);
validateRequest(ctx.request(), principalDomain, caller);
validate(domainName, TYPE_DOMAIN_NAME, principalDomain, caller);
validate(signedPolicyRequest, TYPE_SIGNED_POLICY_REQUEST, principalDomain, caller);
// for consistent handling of all requests, we're going to convert
// all incoming object values into lower case since ZMS Server
// saves all of its object names in lower case
domainName = domainName.toLowerCase();
setRequestDomain(ctx, domainName);
DomainData domainData = dataStore.getDomainData(domainName);
if (domainData == null) {
setRequestDomain(ctx, ZTSConsts.ZTS_UNKNOWN_DOMAIN);
throw notFoundError("Domain not found: '" + domainName + "'", caller, ZTSConsts.ZTS_UNKNOWN_DOMAIN, principalDomain);
}
Timestamp modified = domainData.getModified();
EntityTag eTag = new EntityTag(modified.toString());
final String tag = eTag.toString();
if (matchingTag != null && matchingTag.equals(tag)) {
return Response.status(ResourceException.NOT_MODIFIED).header("ETag", tag).build();
}
// generate our list of policy version requests. in this method
// we make sure our policy name are complete (include domain prefix)
// and all are lowercase
Map<String, String> policyVersions = generatePolicyVersions(domainName, signedPolicyRequest);
// first get our PolicyData object
PolicyData policyData = new PolicyData().setDomain(domainName).setPolicies(getPolicyList(domainData, policyVersions));
// then get the signed policy data
Timestamp expires = Timestamp.fromMillis(System.currentTimeMillis() + signedPolicyTimeout);
SignedPolicyData signedPolicyData = new SignedPolicyData().setPolicyData(policyData).setExpires(expires).setModified(modified).setZmsKeyId("").setZmsSignature("");
JWSPolicyData jwsPolicyData = signJwsPolicyData(signedPolicyData, signedPolicyRequest.getSignatureP1363Format());
if (jwsPolicyData == null) {
return Response.status(ResourceException.INTERNAL_SERVER_ERROR).build();
} else {
return Response.status(ResourceException.OK).entity(jwsPolicyData).header("ETag", tag).build();
}
}
Aggregations