Search in sources :

Example 1 with Transaction

use of co.elastic.apm.api.Transaction in project ARLAS-server by gisaia.

the class AuthorizationFilter method filter.

@Override
public void filter(ContainerRequestContext ctx) {
    Transaction transaction = ElasticApm.currentTransaction();
    boolean isPublic = ctx.getUriInfo().getPath().concat(":").concat(ctx.getMethod()).matches(authConf.getPublicRegex());
    String header = ctx.getHeaderString(HttpHeaders.AUTHORIZATION);
    if (header == null || (header != null && !header.toLowerCase().startsWith("bearer "))) {
        if (isPublic || ctx.getMethod() == "OPTIONS") {
            return;
        } else {
            ctx.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
        }
    }
    try {
        // header presence and format already checked before in AuthenticationFilter
        DecodedJWT jwt = jwtVerifier.verify(header.substring(7));
        // remove it in case it's been set manually
        ctx.getHeaders().remove(authConf.headerUser);
        String userId = jwt.getSubject();
        if (!StringUtil.isNullOrEmpty(userId)) {
            ctx.getHeaders().putSingle(authConf.headerUser, userId);
            transaction.setUser(userId, "", "");
        }
        // remove it in case it's been set manually
        ctx.getHeaders().remove(authConf.headerGroup);
        Claim jwtClaimRoles = jwt.getClaim(authConf.claimRoles);
        if (!jwtClaimRoles.isNull()) {
            List<String> groups = jwtClaimRoles.asList(String.class).stream().filter(r -> r.toLowerCase().startsWith("group")).collect(Collectors.toList());
            ctx.setProperty("groups", groups);
            ctx.getHeaders().put(authConf.headerGroup, groups);
        }
        Claim jwtClaimPermissions = jwt.getClaim(authConf.claimPermissions);
        if (!jwtClaimPermissions.isNull()) {
            ArlasClaims arlasClaims = new ArlasClaims(jwtClaimPermissions.asList(String.class));
            ctx.setProperty("claims", arlasClaims);
            if (arlasClaims.isAllowed(ctx.getMethod(), ctx.getUriInfo().getPath())) {
                arlasClaims.injectHeaders(ctx.getHeaders(), transaction);
                return;
            }
        }
        if (isPublic) {
            return;
        } else {
            ctx.abortWith(Response.status(Response.Status.FORBIDDEN).build());
        }
    } catch (JWTVerificationException e) {
        LOGGER.warn("JWT verification failed.", e);
        if (!isPublic) {
            ctx.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
        }
        return;
    }
    ctx.abortWith(Response.status(Response.Status.FORBIDDEN).build());
}
Also used : X509Certificate(java.security.cert.X509Certificate) JWT(com.auth0.jwt.JWT) Transaction(co.elastic.apm.api.Transaction) StringUtil(io.arlas.server.core.utils.StringUtil) Provider(javax.ws.rs.ext.Provider) CertificateFactory(java.security.cert.CertificateFactory) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) Priorities(javax.ws.rs.Priorities) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Algorithm(com.auth0.jwt.algorithms.Algorithm) RSAPublicKey(java.security.interfaces.RSAPublicKey) JWTVerifier(com.auth0.jwt.interfaces.JWTVerifier) Claim(com.auth0.jwt.interfaces.Claim) JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) ArlasAuthConfiguration(io.arlas.server.core.app.ArlasAuthConfiguration) Logger(org.slf4j.Logger) ElasticApm(co.elastic.apm.api.ElasticApm) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) Priority(javax.annotation.Priority) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) InputStream(java.io.InputStream) JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) Transaction(co.elastic.apm.api.Transaction) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Claim(com.auth0.jwt.interfaces.Claim)

Example 2 with Transaction

use of co.elastic.apm.api.Transaction in project apm-agent-java by elastic.

the class SpanInstrumentationTest method testSampled.

@Test
void testSampled() {
    assertThat(ElasticApm.currentSpan().isSampled()).isFalse();
    assertThat(ElasticApm.currentTransaction().isSampled()).isFalse();
    final Transaction transaction = ElasticApm.startTransaction();
    assertThat(transaction.isSampled()).isTrue();
    Span span = transaction.startSpan();
    assertThat(span.isSampled()).isTrue();
    span.end();
    transaction.end();
}
Also used : Transaction(co.elastic.apm.api.Transaction) Span(co.elastic.apm.api.Span) Test(org.junit.jupiter.api.Test) AbstractApiTest(co.elastic.apm.AbstractApiTest)

Example 3 with Transaction

use of co.elastic.apm.api.Transaction in project apm-agent-java by elastic.

the class TestApiServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
    Transaction transaction = ElasticApm.currentTransaction();
    // set transaction name
    transaction.setName("custom_transaction_name");
    // set transaction type, here default value is 'request'
    transaction.setType("custom_transaction_type");
    // set custom transaction labels
    transaction.addLabel("custom-label1", "label_value1");
    transaction.addLabel("custom-label2", "label_value2");
    // store custom context field
    transaction.addCustomContext("custom-context", "custom-context-value");
    // creating a custom span with annotation
    captureSpanAnnotation();
    doWork();
    createCustomSpan();
}
Also used : Transaction(co.elastic.apm.api.Transaction)

Example 4 with Transaction

use of co.elastic.apm.api.Transaction in project apm-agent-java by elastic.

the class JakartaTestApiServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
    Transaction transaction = ElasticApm.currentTransaction();
    // set transaction name
    transaction.setName("custom_transaction_name");
    // set transaction type, here default value is 'request'
    transaction.setType("custom_transaction_type");
    // set custom transaction labels
    transaction.addLabel("custom-label1", "label_value1");
    transaction.addLabel("custom-label2", "label_value2");
    // store custom context field
    transaction.addCustomContext("custom-context", "custom-context-value");
    // creating a custom span with annotation
    captureSpanAnnotation();
    doWork();
    createCustomSpan();
}
Also used : Transaction(co.elastic.apm.api.Transaction)

Example 5 with Transaction

use of co.elastic.apm.api.Transaction in project apm-agent-java by elastic.

the class SpanInstrumentationTest method testReferenceCounting.

@Test
void testReferenceCounting() {
    final Transaction transaction = ElasticApm.startTransaction();
    Span span = transaction.startSpan();
    try (Scope scope = span.activate()) {
        span.startSpan().end();
    }
    span.end();
    transaction.end();
    BookkeeperObjectPool<co.elastic.apm.agent.impl.transaction.Span> spanPool = objectPoolFactory.getSpanPool();
    assertThat(spanPool.getRecyclablesToReturn().stream().filter(span1 -> span1.getReferenceCount() > 1).collect(Collectors.toList())).hasSize(spanPool.getRequestedObjectCount());
    BookkeeperObjectPool<co.elastic.apm.agent.impl.transaction.Transaction> transactionPool = objectPoolFactory.getTransactionPool();
    assertThat(transactionPool.getRecyclablesToReturn().stream().filter(transaction1 -> transaction1.getReferenceCount() > 1).collect(Collectors.toList())).hasSize(transactionPool.getRequestedObjectCount());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Transaction(co.elastic.apm.api.Transaction) TextHeaderMapAccessor(co.elastic.apm.agent.impl.TextHeaderMapAccessor) BookkeeperObjectPool(co.elastic.apm.agent.objectpool.impl.BookkeeperObjectPool) ElasticApm(co.elastic.apm.api.ElasticApm) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) SecureRandom(java.security.SecureRandom) AfterEach(org.junit.jupiter.api.AfterEach) Assertions.assertThat(co.elastic.apm.agent.testutils.assertions.Assertions.assertThat) Map(java.util.Map) TraceContext(co.elastic.apm.agent.impl.transaction.TraceContext) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) AbstractApiTest(co.elastic.apm.AbstractApiTest) Span(co.elastic.apm.api.Span) Scope(co.elastic.apm.api.Scope) Transaction(co.elastic.apm.api.Transaction) Scope(co.elastic.apm.api.Scope) Span(co.elastic.apm.api.Span) Test(org.junit.jupiter.api.Test) AbstractApiTest(co.elastic.apm.AbstractApiTest)

Aggregations

Transaction (co.elastic.apm.api.Transaction)6 AbstractApiTest (co.elastic.apm.AbstractApiTest)3 Test (org.junit.jupiter.api.Test)3 ElasticApm (co.elastic.apm.api.ElasticApm)2 Span (co.elastic.apm.api.Span)2 Collectors (java.util.stream.Collectors)2 TextHeaderMapAccessor (co.elastic.apm.agent.impl.TextHeaderMapAccessor)1 TraceContext (co.elastic.apm.agent.impl.transaction.TraceContext)1 BookkeeperObjectPool (co.elastic.apm.agent.objectpool.impl.BookkeeperObjectPool)1 Assertions.assertThat (co.elastic.apm.agent.testutils.assertions.Assertions.assertThat)1 Scope (co.elastic.apm.api.Scope)1 JWT (com.auth0.jwt.JWT)1 Algorithm (com.auth0.jwt.algorithms.Algorithm)1 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)1 Claim (com.auth0.jwt.interfaces.Claim)1 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 JWTVerifier (com.auth0.jwt.interfaces.JWTVerifier)1 ArlasAuthConfiguration (io.arlas.server.core.app.ArlasAuthConfiguration)1 StringUtil (io.arlas.server.core.utils.StringUtil)1 FileInputStream (java.io.FileInputStream)1