-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82bfa66
commit 4f63e10
Showing
4 changed files
with
158 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...t/trino-client/src/main/java/io/trino/client/auth/kerberos/LoginBasedSubjectProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* 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.client.auth.kerberos; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.sun.security.auth.module.Krb5LoginModule; | ||
import org.ietf.jgss.GSSException; | ||
|
||
import javax.annotation.concurrent.GuardedBy; | ||
import javax.security.auth.Subject; | ||
import javax.security.auth.login.AppConfigurationEntry; | ||
import javax.security.auth.login.Configuration; | ||
import javax.security.auth.login.LoginContext; | ||
import javax.security.auth.login.LoginException; | ||
|
||
import java.io.File; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
import static java.lang.Boolean.getBoolean; | ||
import static java.util.Objects.requireNonNull; | ||
import static javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag.REQUIRED; | ||
|
||
public class LoginBasedSubjectProvider | ||
implements SubjectProvider | ||
{ | ||
private final Optional<String> principal; | ||
private final Optional<File> keytab; | ||
private final Optional<File> credentialCache; | ||
|
||
@GuardedBy("this") | ||
private LoginContext loginContext; | ||
|
||
public LoginBasedSubjectProvider( | ||
Optional<String> principal, | ||
Optional<File> kerberosConfig, | ||
Optional<File> keytab, | ||
Optional<File> credentialCache) | ||
{ | ||
this.principal = requireNonNull(principal, "principal is null"); | ||
this.keytab = requireNonNull(keytab, "keytab is null"); | ||
this.credentialCache = requireNonNull(credentialCache, "credentialCache is null"); | ||
|
||
kerberosConfig.ifPresent(file -> { | ||
String newValue = file.getAbsolutePath(); | ||
String currentValue = System.getProperty("java.security.krb5.conf"); | ||
checkState( | ||
currentValue == null || Objects.equals(currentValue, newValue), | ||
"Refusing to set system property 'java.security.krb5.conf' to '%s', it is already set to '%s'", | ||
newValue, | ||
currentValue); | ||
System.setProperty("java.security.krb5.conf", newValue); | ||
}); | ||
} | ||
|
||
@Override | ||
public Subject getSubject() | ||
{ | ||
return loginContext.getSubject(); | ||
} | ||
|
||
@Override | ||
public void refresh() | ||
throws LoginException, GSSException | ||
{ | ||
// TODO: do we need to call logout() on the LoginContext? | ||
|
||
loginContext = new LoginContext("", null, null, new Configuration() | ||
{ | ||
@Override | ||
public AppConfigurationEntry[] getAppConfigurationEntry(String name) | ||
{ | ||
ImmutableMap.Builder<String, String> options = ImmutableMap.builder(); | ||
options.put("refreshKrb5Config", "true"); | ||
options.put("doNotPrompt", "true"); | ||
options.put("useKeyTab", "true"); | ||
|
||
if (getBoolean("trino.client.debugKerberos")) { | ||
options.put("debug", "true"); | ||
} | ||
|
||
keytab.ifPresent(file -> options.put("keyTab", file.getAbsolutePath())); | ||
|
||
credentialCache.ifPresent(file -> { | ||
options.put("ticketCache", file.getAbsolutePath()); | ||
options.put("useTicketCache", "true"); | ||
options.put("renewTGT", "true"); | ||
}); | ||
|
||
principal.ifPresent(value -> options.put("principal", value)); | ||
|
||
return new AppConfigurationEntry[] { | ||
new AppConfigurationEntry(Krb5LoginModule.class.getName(), REQUIRED, options.build()) | ||
}; | ||
} | ||
}); | ||
|
||
loginContext.login(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
client/trino-client/src/main/java/io/trino/client/auth/kerberos/SubjectProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.client.auth.kerberos; | ||
|
||
import org.ietf.jgss.GSSException; | ||
|
||
import javax.security.auth.Subject; | ||
import javax.security.auth.login.LoginException; | ||
|
||
public interface SubjectProvider | ||
{ | ||
Subject getSubject(); | ||
|
||
void refresh() | ||
throws LoginException, GSSException; | ||
} |