Search in sources :

Example 71 with EntityTag

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());
}
Also used : Response(javax.ws.rs.core.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) EntityTag(javax.ws.rs.core.EntityTag) Timestamp(com.yahoo.rdl.Timestamp)

Example 72 with EntityTag

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();
}
Also used : DomainData(com.yahoo.athenz.zms.DomainData) EntityTag(javax.ws.rs.core.EntityTag)

Example 73 with EntityTag

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();
    }
}
Also used : DomainData(com.yahoo.athenz.zms.DomainData) EntityTag(javax.ws.rs.core.EntityTag)

Aggregations

EntityTag (javax.ws.rs.core.EntityTag)73 GET (javax.ws.rs.GET)24 Response (javax.ws.rs.core.Response)24 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)23 Path (javax.ws.rs.Path)21 CacheControl (javax.ws.rs.core.CacheControl)20 Test (org.junit.Test)18 Produces (javax.ws.rs.Produces)13 Date (java.util.Date)10 Test (org.testng.annotations.Test)9 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)8 Timestamp (com.yahoo.rdl.Timestamp)5 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)5 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)5 List (java.util.List)5 ContainerResponse (org.everrest.core.impl.ContainerResponse)5 Principal (com.yahoo.athenz.auth.Principal)4 EnvironmentUser (fi.otavanopisto.muikku.model.users.EnvironmentUser)4 ArrayList (java.util.ArrayList)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4