Skip to content

Commit d7c6d4e

Browse files
committed
Move impersonation authorization to SessionSupplier
1 parent 5a6542c commit d7c6d4e

File tree

6 files changed

+37
-60
lines changed

6 files changed

+37
-60
lines changed

presto-main/src/main/java/com/facebook/presto/dispatcher/DispatchManager.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@
3131
import com.facebook.presto.server.SessionContext;
3232
import com.facebook.presto.server.SessionPropertyDefaults;
3333
import com.facebook.presto.server.SessionSupplier;
34-
import com.facebook.presto.server.security.SecurityConfig;
3534
import com.facebook.presto.spi.PrestoException;
3635
import com.facebook.presto.spi.QueryId;
3736
import com.facebook.presto.spi.analyzer.AnalyzerOptions;
3837
import com.facebook.presto.spi.analyzer.QueryPreparerProvider;
3938
import com.facebook.presto.spi.resourceGroups.SelectionContext;
4039
import com.facebook.presto.spi.resourceGroups.SelectionCriteria;
4140
import com.facebook.presto.spi.security.AccessControl;
42-
import com.facebook.presto.spi.security.AuthorizedIdentity;
4341
import com.facebook.presto.sql.analyzer.QueryPreparerProviderManager;
4442
import com.facebook.presto.transaction.TransactionManager;
4543
import com.google.common.util.concurrent.AbstractFuture;
@@ -57,8 +55,6 @@
5755
import java.util.concurrent.Executor;
5856

5957
import static com.facebook.presto.SystemSessionProperties.getAnalyzerType;
60-
import static com.facebook.presto.security.AccessControlUtils.checkPermissions;
61-
import static com.facebook.presto.security.AccessControlUtils.getAuthorizedIdentity;
6258
import static com.facebook.presto.spi.StandardErrorCode.QUERY_TEXT_TOO_LARGE;
6359
import static com.facebook.presto.util.AnalyzerUtil.createAnalyzerOptions;
6460
import static com.google.common.base.Preconditions.checkArgument;
@@ -93,7 +89,6 @@ public class DispatchManager
9389

9490
private final QueryManagerStats stats = new QueryManagerStats();
9591

96-
private final SecurityConfig securityConfig;
9792
private final QueryPreparerProviderManager queryPreparerProviderManager;
9893

9994
/**
@@ -130,7 +125,6 @@ public DispatchManager(
130125
QueryManagerConfig queryManagerConfig,
131126
DispatchExecutor dispatchExecutor,
132127
ClusterStatusSender clusterStatusSender,
133-
SecurityConfig securityConfig,
134128
Optional<ClusterQueryTrackerService> clusterQueryTrackerService)
135129
{
136130
this.queryIdGenerator = requireNonNull(queryIdGenerator, "queryIdGenerator is null");
@@ -152,8 +146,6 @@ public DispatchManager(
152146
this.clusterStatusSender = requireNonNull(clusterStatusSender, "clusterStatusSender is null");
153147

154148
this.queryTracker = new QueryTracker<>(queryManagerConfig, dispatchExecutor.getScheduledExecutor(), clusterQueryTrackerService);
155-
156-
this.securityConfig = requireNonNull(securityConfig, "securityConfig is null");
157149
}
158150

159151
/**
@@ -275,14 +267,8 @@ private <C> void createQueryInternal(QueryId queryId, String slug, int retryCoun
275267
throw new PrestoException(QUERY_TEXT_TOO_LARGE, format("Query text length (%s) exceeds the maximum length (%s)", queryLength, maxQueryLength));
276268
}
277269

278-
// check permissions if needed
279-
checkPermissions(accessControl, securityConfig, queryId, sessionContext);
280-
281-
// get authorized identity if possible
282-
Optional<AuthorizedIdentity> authorizedIdentity = getAuthorizedIdentity(accessControl, securityConfig, queryId, sessionContext);
283-
284270
// decode session
285-
session = sessionSupplier.createSession(queryId, sessionContext, warningCollectorFactory, authorizedIdentity);
271+
session = sessionSupplier.createSession(queryId, sessionContext, warningCollectorFactory);
286272

287273
// prepare query
288274
AnalyzerOptions analyzerOptions = createAnalyzerOptions(session, session.getWarningCollector());

presto-main/src/main/java/com/facebook/presto/server/NoOpSessionSupplier.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
import com.facebook.presto.Session;
1717
import com.facebook.presto.execution.warnings.WarningCollectorFactory;
1818
import com.facebook.presto.spi.QueryId;
19-
import com.facebook.presto.spi.security.AuthorizedIdentity;
20-
21-
import java.util.Optional;
2219

2320
/**
2421
* Used on workers.
@@ -27,7 +24,7 @@ public class NoOpSessionSupplier
2724
implements SessionSupplier
2825
{
2926
@Override
30-
public Session createSession(QueryId queryId, SessionContext context, WarningCollectorFactory warningCollectorFactory, Optional<AuthorizedIdentity> authorizedIdentity)
27+
public Session createSession(QueryId queryId, SessionContext context, WarningCollectorFactory warningCollectorFactory)
3128
{
3229
throw new UnsupportedOperationException();
3330
}

presto-main/src/main/java/com/facebook/presto/server/QuerySessionSupplier.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.facebook.presto.common.type.TimeZoneKey;
2020
import com.facebook.presto.execution.warnings.WarningCollectorFactory;
2121
import com.facebook.presto.metadata.SessionPropertyManager;
22+
import com.facebook.presto.server.security.SecurityConfig;
2223
import com.facebook.presto.spi.QueryId;
2324
import com.facebook.presto.spi.WarningCollector;
2425
import com.facebook.presto.spi.function.SqlFunctionId;
@@ -39,6 +40,8 @@
3940
import static com.facebook.presto.Session.SessionBuilder;
4041
import static com.facebook.presto.SystemSessionProperties.WARNING_HANDLING;
4142
import static com.facebook.presto.common.type.TimeZoneKey.getTimeZoneKey;
43+
import static com.facebook.presto.security.AccessControlUtils.checkPermissions;
44+
import static com.facebook.presto.security.AccessControlUtils.getAuthorizedIdentity;
4245
import static java.util.Map.Entry;
4346
import static java.util.Objects.requireNonNull;
4447

@@ -52,44 +55,30 @@ public class QuerySessionSupplier
5255
private final AccessControl accessControl;
5356
private final SessionPropertyManager sessionPropertyManager;
5457
private final Optional<TimeZoneKey> forcedSessionTimeZone;
58+
private final SecurityConfig securityConfig;
5559

5660
@Inject
5761
public QuerySessionSupplier(
5862
TransactionManager transactionManager,
5963
AccessControl accessControl,
6064
SessionPropertyManager sessionPropertyManager,
61-
SqlEnvironmentConfig config)
65+
SqlEnvironmentConfig sqlEnvironmentConfig,
66+
SecurityConfig securityConfig)
6267
{
6368
this.transactionManager = requireNonNull(transactionManager, "transactionManager is null");
6469
this.accessControl = requireNonNull(accessControl, "accessControl is null");
6570
this.sessionPropertyManager = requireNonNull(sessionPropertyManager, "sessionPropertyManager is null");
66-
requireNonNull(config, "config is null");
67-
this.forcedSessionTimeZone = requireNonNull(config.getForcedSessionTimeZone(), "forcedSessionTimeZone is null");
71+
requireNonNull(sqlEnvironmentConfig, "sqlEnvironmentConfig is null");
72+
this.forcedSessionTimeZone = requireNonNull(sqlEnvironmentConfig.getForcedSessionTimeZone(), "forcedSessionTimeZone is null");
73+
this.securityConfig = requireNonNull(securityConfig, "securityConfig is null");
6874
}
6975

7076
@Override
71-
public Session createSession(QueryId queryId, SessionContext context, WarningCollectorFactory warningCollectorFactory, Optional<AuthorizedIdentity> authorizedIdentity)
77+
public Session createSession(QueryId queryId, SessionContext context, WarningCollectorFactory warningCollectorFactory)
7278
{
73-
Identity identity = context.getIdentity();
74-
if (authorizedIdentity.isPresent()) {
75-
identity = new Identity(
76-
identity.getUser(),
77-
identity.getPrincipal(),
78-
identity.getRoles(),
79-
identity.getExtraCredentials(),
80-
identity.getExtraAuthenticators(),
81-
Optional.of(authorizedIdentity.get().getUserName()),
82-
authorizedIdentity.get().getReasonForSelect());
83-
log.info(String.format(
84-
"For query %s, given user is %s, authorized user is %s",
85-
queryId.getId(),
86-
identity.getUser(),
87-
authorizedIdentity.get().getUserName()));
88-
}
89-
9079
SessionBuilder sessionBuilder = Session.builder(sessionPropertyManager)
9180
.setQueryId(queryId)
92-
.setIdentity(identity)
81+
.setIdentity(authenticateIdentity(queryId, context))
9382
.setSource(context.getSource())
9483
.setCatalog(context.getCatalog())
9584
.setSchema(context.getSchema())
@@ -145,4 +134,19 @@ else if (context.getTimeZoneId() != null) {
145134
}
146135
return session;
147136
}
137+
138+
private Identity authenticateIdentity(QueryId queryId, SessionContext context)
139+
{
140+
checkPermissions(accessControl, securityConfig, queryId, context);
141+
Optional<AuthorizedIdentity> authorizedIdentity = getAuthorizedIdentity(accessControl, securityConfig, queryId, context);
142+
143+
return authorizedIdentity.map(identity -> new Identity(
144+
context.getIdentity().getUser(),
145+
context.getIdentity().getPrincipal(),
146+
context.getIdentity().getRoles(),
147+
context.getIdentity().getExtraCredentials(),
148+
context.getIdentity().getExtraAuthenticators(),
149+
Optional.of(identity.getUserName()),
150+
identity.getReasonForSelect())).orElseGet(context::getIdentity);
151+
}
148152
}

presto-main/src/main/java/com/facebook/presto/server/SessionSupplier.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
import com.facebook.presto.Session;
1717
import com.facebook.presto.execution.warnings.WarningCollectorFactory;
1818
import com.facebook.presto.spi.QueryId;
19-
import com.facebook.presto.spi.security.AuthorizedIdentity;
20-
21-
import java.util.Optional;
2219

2320
public interface SessionSupplier
2421
{
25-
Session createSession(QueryId queryId, SessionContext context, WarningCollectorFactory warningCollectorFactory, Optional<AuthorizedIdentity> authorizedIdentity);
22+
Session createSession(QueryId queryId, SessionContext context, WarningCollectorFactory warningCollectorFactory);
2623
}

presto-main/src/test/java/com/facebook/presto/server/TestQuerySessionSupplier.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.facebook.presto.common.type.TimeZoneNotSupportedException;
1919
import com.facebook.presto.execution.warnings.WarningCollectorFactory;
2020
import com.facebook.presto.metadata.SessionPropertyManager;
21+
import com.facebook.presto.server.security.SecurityConfig;
2122
import com.facebook.presto.spi.QueryId;
2223
import com.facebook.presto.spi.WarningCollector;
2324
import com.facebook.presto.spi.function.SqlFunctionId;
@@ -33,7 +34,6 @@
3334
import javax.servlet.http.HttpServletRequest;
3435

3536
import java.util.Locale;
36-
import java.util.Optional;
3737

3838
import static com.facebook.airlift.json.JsonCodec.jsonCodec;
3939
import static com.facebook.presto.SystemSessionProperties.HASH_PARTITION_COUNT;
@@ -91,7 +91,8 @@ public void testCreateSession()
9191
createTestTransactionManager(),
9292
new AllowAllAccessControl(),
9393
new SessionPropertyManager(),
94-
new SqlEnvironmentConfig());
94+
new SqlEnvironmentConfig(),
95+
new SecurityConfig());
9596
WarningCollectorFactory warningCollectorFactory = new WarningCollectorFactory()
9697
{
9798
@Override
@@ -100,7 +101,7 @@ public WarningCollector create(WarningHandlingLevel warningHandlingLevel)
100101
return WarningCollector.NOOP;
101102
}
102103
};
103-
Session session = sessionSupplier.createSession(new QueryId("test_query_id"), context, warningCollectorFactory, Optional.empty());
104+
Session session = sessionSupplier.createSession(new QueryId("test_query_id"), context, warningCollectorFactory);
104105

105106
assertEquals(session.getQueryId(), new QueryId("test_query_id"));
106107
assertEquals(session.getUser(), "testUser");
@@ -162,7 +163,8 @@ public void testInvalidTimeZone()
162163
createTestTransactionManager(),
163164
new AllowAllAccessControl(),
164165
new SessionPropertyManager(),
165-
new SqlEnvironmentConfig());
166+
new SqlEnvironmentConfig(),
167+
new SecurityConfig());
166168
WarningCollectorFactory warningCollectorFactory = new WarningCollectorFactory()
167169
{
168170
@Override
@@ -171,6 +173,6 @@ public WarningCollector create(WarningHandlingLevel warningHandlingLevel)
171173
return WarningCollector.NOOP;
172174
}
173175
};
174-
sessionSupplier.createSession(new QueryId("test_query_id"), context, warningCollectorFactory, Optional.empty());
176+
sessionSupplier.createSession(new QueryId("test_query_id"), context, warningCollectorFactory);
175177
}
176178
}

presto-spark-base/src/main/java/com/facebook/presto/spark/PrestoSparkQueryExecutionFactory.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
import com.facebook.presto.spi.relation.VariableReferenceExpression;
9292
import com.facebook.presto.spi.resourceGroups.ResourceGroupId;
9393
import com.facebook.presto.spi.security.AccessControl;
94-
import com.facebook.presto.spi.security.AuthorizedIdentity;
9594
import com.facebook.presto.spi.storage.TempStorage;
9695
import com.facebook.presto.sql.analyzer.BuiltInQueryPreparer;
9796
import com.facebook.presto.sql.analyzer.BuiltInQueryPreparer.BuiltInPreparedQuery;
@@ -139,8 +138,6 @@
139138
import static com.facebook.presto.execution.QueryState.FAILED;
140139
import static com.facebook.presto.execution.QueryState.PLANNING;
141140
import static com.facebook.presto.execution.StageInfo.getAllStages;
142-
import static com.facebook.presto.security.AccessControlUtils.checkPermissions;
143-
import static com.facebook.presto.security.AccessControlUtils.getAuthorizedIdentity;
144141
import static com.facebook.presto.server.protocol.QueryResourceUtil.toStatementStats;
145142
import static com.facebook.presto.spark.PrestoSparkSessionProperties.isAdaptiveQueryExecutionEnabled;
146143
import static com.facebook.presto.spark.SparkErrorCode.MALFORMED_QUERY_FILE;
@@ -612,13 +609,7 @@ public IPrestoSparkQueryExecution create(
612609
credentialsProviders,
613610
authenticatorProviders);
614611

615-
// check permissions if needed
616-
checkPermissions(accessControl, securityConfig, queryId, sessionContext);
617-
618-
// get authorized identity if possible
619-
Optional<AuthorizedIdentity> authorizedIdentity = getAuthorizedIdentity(accessControl, securityConfig, queryId, sessionContext);
620-
621-
Session session = sessionSupplier.createSession(queryId, sessionContext, warningCollectorFactory, authorizedIdentity);
612+
Session session = sessionSupplier.createSession(queryId, sessionContext, warningCollectorFactory);
622613
session = sessionPropertyDefaults.newSessionWithDefaultProperties(session, Optional.empty(), Optional.empty());
623614

624615
if (!executionStrategies.isEmpty()) {

0 commit comments

Comments
 (0)