All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.trino.SessionRepresentation Maven / Gradle / Ivy

There is a newer version: 465
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.airlift.slice.Slice;
import io.opentelemetry.api.trace.Span;
import io.trino.metadata.SessionPropertyManager;
import io.trino.spi.QueryId;
import io.trino.spi.security.BasicPrincipal;
import io.trino.spi.security.Identity;
import io.trino.spi.security.SelectedRole;
import io.trino.spi.session.ResourceEstimates;
import io.trino.spi.type.TimeZoneKey;
import io.trino.sql.SqlPath;
import io.trino.transaction.TransactionId;

import java.time.Instant;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;

import static io.trino.client.ProtocolHeaders.createProtocolHeaders;
import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;

public final class SessionRepresentation
{
    private final String queryId;
    private final Span querySpan;
    private final Optional transactionId;
    private final boolean clientTransactionSupport;
    private final String user;
    private final String originalUser;
    private final Set groups;
    private final Set originalUserGroups;
    private final Optional principal;
    private final Set enabledRoles;
    private final Optional source;
    private final Optional catalog;
    private final Optional schema;
    private final SqlPath path;
    private final Optional traceToken;
    private final TimeZoneKey timeZoneKey;
    private final Locale locale;
    private final Optional remoteUserAddress;
    private final Optional userAgent;
    private final Optional clientInfo;
    private final Set clientTags;
    private final Set clientCapabilities;
    private final Instant start;
    private final ResourceEstimates resourceEstimates;
    private final Map systemProperties;
    private final Map> catalogProperties;
    private final Map catalogRoles;
    private final Map preparedStatements;
    private final String protocolName;

    @JsonCreator
    public SessionRepresentation(
            @JsonProperty("queryId") String queryId,
            @JsonProperty("querySpan") Span querySpan,
            @JsonProperty("transactionId") Optional transactionId,
            @JsonProperty("clientTransactionSupport") boolean clientTransactionSupport,
            @JsonProperty("user") String user,
            @JsonProperty("originalUser") String originalUser,
            @JsonProperty("groups") Set groups,
            @JsonProperty("originalUserGroups") Set originalUserGroups,
            @JsonProperty("principal") Optional principal,
            @JsonProperty("enabledRoles") Set enabledRoles,
            @JsonProperty("source") Optional source,
            @JsonProperty("catalog") Optional catalog,
            @JsonProperty("schema") Optional schema,
            @JsonProperty("path") SqlPath path,
            @JsonProperty("traceToken") Optional traceToken,
            @JsonProperty("timeZoneKey") TimeZoneKey timeZoneKey,
            @JsonProperty("locale") Locale locale,
            @JsonProperty("remoteUserAddress") Optional remoteUserAddress,
            @JsonProperty("userAgent") Optional userAgent,
            @JsonProperty("clientInfo") Optional clientInfo,
            @JsonProperty("clientTags") Set clientTags,
            @JsonProperty("clientCapabilities") Set clientCapabilities,
            @JsonProperty("resourceEstimates") ResourceEstimates resourceEstimates,
            @JsonProperty("start") Instant start,
            @JsonProperty("systemProperties") Map systemProperties,
            @JsonProperty("catalogProperties") Map> catalogProperties,
            @JsonProperty("catalogRoles") Map catalogRoles,
            @JsonProperty("preparedStatements") Map preparedStatements,
            @JsonProperty("protocolName") String protocolName)
    {
        this.queryId = requireNonNull(queryId, "queryId is null");
        this.querySpan = requireNonNull(querySpan, "querySpan is null");
        this.transactionId = requireNonNull(transactionId, "transactionId is null");
        this.clientTransactionSupport = clientTransactionSupport;
        this.user = requireNonNull(user, "user is null");
        this.originalUser = requireNonNull(originalUser, "originalUser is null");
        this.groups = requireNonNull(groups, "groups is null");
        this.originalUserGroups = requireNonNull(originalUserGroups, "originalUserGroups is null");
        this.principal = requireNonNull(principal, "principal is null");
        this.enabledRoles = ImmutableSet.copyOf(requireNonNull(enabledRoles, "enabledRoles is null"));
        this.source = requireNonNull(source, "source is null");
        this.catalog = requireNonNull(catalog, "catalog is null");
        this.schema = requireNonNull(schema, "schema is null");
        this.path = requireNonNull(path, "path is null");
        this.traceToken = requireNonNull(traceToken, "traceToken is null");
        this.timeZoneKey = requireNonNull(timeZoneKey, "timeZoneKey is null");
        this.locale = requireNonNull(locale, "locale is null");
        this.remoteUserAddress = requireNonNull(remoteUserAddress, "remoteUserAddress is null");
        this.userAgent = requireNonNull(userAgent, "userAgent is null");
        this.clientInfo = requireNonNull(clientInfo, "clientInfo is null");
        this.clientTags = requireNonNull(clientTags, "clientTags is null");
        this.clientCapabilities = requireNonNull(clientCapabilities, "clientCapabilities is null");
        this.resourceEstimates = requireNonNull(resourceEstimates, "resourceEstimates is null");
        this.start = start;
        this.systemProperties = ImmutableMap.copyOf(systemProperties);
        this.catalogRoles = ImmutableMap.copyOf(catalogRoles);
        this.preparedStatements = ImmutableMap.copyOf(preparedStatements);
        this.protocolName = requireNonNull(protocolName, "protocolName is null");

        ImmutableMap.Builder> catalogPropertiesBuilder = ImmutableMap.builder();
        for (Entry> entry : catalogProperties.entrySet()) {
            catalogPropertiesBuilder.put(entry.getKey(), ImmutableMap.copyOf(entry.getValue()));
        }
        this.catalogProperties = catalogPropertiesBuilder.buildOrThrow();
    }

    @JsonProperty
    public String getQueryId()
    {
        return queryId;
    }

    @JsonProperty
    public Span getQuerySpan()
    {
        return querySpan;
    }

    @JsonProperty
    public Optional getTransactionId()
    {
        return transactionId;
    }

    @JsonProperty
    public boolean isClientTransactionSupport()
    {
        return clientTransactionSupport;
    }

    @JsonProperty
    public String getUser()
    {
        return user;
    }

    @JsonProperty
    public String getOriginalUser()
    {
        return originalUser;
    }

    @JsonProperty
    public Set getGroups()
    {
        return groups;
    }

    @JsonProperty
    public Set getOriginalUserGroups()
    {
        return originalUserGroups;
    }

    @JsonProperty
    public Optional getPrincipal()
    {
        return principal;
    }

    @JsonProperty
    public Set getEnabledRoles()
    {
        return enabledRoles;
    }

    @JsonProperty
    public Optional getSource()
    {
        return source;
    }

    @JsonProperty
    public Optional getTraceToken()
    {
        return traceToken;
    }

    @JsonProperty
    public Optional getCatalog()
    {
        return catalog;
    }

    @JsonProperty
    public Optional getSchema()
    {
        return schema;
    }

    @JsonProperty
    public SqlPath getPath()
    {
        return path;
    }

    @JsonProperty
    public TimeZoneKey getTimeZoneKey()
    {
        return timeZoneKey;
    }

    @JsonProperty
    public Locale getLocale()
    {
        return locale;
    }

    @JsonProperty
    public Optional getRemoteUserAddress()
    {
        return remoteUserAddress;
    }

    @JsonProperty
    public Optional getUserAgent()
    {
        return userAgent;
    }

    @JsonProperty
    public Optional getClientInfo()
    {
        return clientInfo;
    }

    @JsonProperty
    public Set getClientTags()
    {
        return clientTags;
    }

    @JsonProperty
    public Set getClientCapabilities()
    {
        return clientCapabilities;
    }

    @JsonProperty
    public Instant getStart()
    {
        return start;
    }

    @JsonProperty
    public ResourceEstimates getResourceEstimates()
    {
        return resourceEstimates;
    }

    @JsonProperty
    public Map getSystemProperties()
    {
        return systemProperties;
    }

    @JsonProperty
    public Map> getCatalogProperties()
    {
        return catalogProperties;
    }

    @JsonProperty
    public Map getCatalogRoles()
    {
        return catalogRoles;
    }

    @JsonProperty
    public Map getPreparedStatements()
    {
        return preparedStatements;
    }

    @JsonProperty
    public String getProtocolName()
    {
        return protocolName;
    }

    @JsonProperty
    public String getTimeZone()
    {
        return timeZoneKey.getId();
    }

    public Identity toIdentity()
    {
        return toIdentity(emptyMap());
    }

    public Identity toIdentity(Map extraCredentials)
    {
        return Identity.forUser(user)
                .withGroups(groups)
                .withPrincipal(principal.map(BasicPrincipal::new))
                .withEnabledRoles(enabledRoles)
                .withConnectorRoles(catalogRoles)
                .withExtraCredentials(extraCredentials)
                .build();
    }

    public Identity toOriginalIdentity(Map extraCredentials)
    {
        return Identity.forUser(originalUser)
                .withGroups(originalUserGroups)
                .withPrincipal(principal.map(BasicPrincipal::new))
                .withExtraCredentials(extraCredentials)
                .build();
    }

    public Session toSession(SessionPropertyManager sessionPropertyManager)
    {
        return toSession(sessionPropertyManager, emptyMap(), Optional.empty());
    }

    public Session toSession(SessionPropertyManager sessionPropertyManager, Map extraCredentials, Optional exchangeEncryptionKey)
    {
        return new Session(
                new QueryId(queryId),
                querySpan,
                transactionId,
                clientTransactionSupport,
                toIdentity(extraCredentials),
                toOriginalIdentity(extraCredentials),
                source,
                catalog,
                schema,
                path,
                traceToken,
                timeZoneKey,
                locale,
                remoteUserAddress,
                userAgent,
                clientInfo,
                clientTags,
                clientCapabilities,
                resourceEstimates,
                start,
                systemProperties,
                catalogProperties,
                sessionPropertyManager,
                preparedStatements,
                createProtocolHeaders(protocolName),
                exchangeEncryptionKey);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy