Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guice bindings for custom authorizer implementations #2251

Merged
merged 7 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void initialize(final Bootstrap<T> bootstrap) {

GuiceBundle.Builder<SingularityConfiguration> guiceBundleBuilder = GuiceBundle
.defaultBuilder(SingularityConfiguration.class)
.modules(getServiceModule(), getObjectMapperModule(), new SingularityAuthModule())
.modules(getServiceModule(), getObjectMapperModule(), getAuthModule())
.modules(additionalModules)
.enableGuiceEnforcer(false)
.stage(getGuiceStage());
Expand Down Expand Up @@ -171,6 +171,10 @@ public Module getObjectMapperModule() {
.in(Scopes.SINGLETON);
}

public Module getAuthModule() {
return new SingularityAuthModule();
}

/**
* Dropwizard bundles used in addition to the bundles required by Singularity. This is an extension point when embedding
* Singularity into a custom service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

public class SingularityAuthModule
extends DropwizardAwareModule<SingularityConfiguration> {
public static final String WEBHOOK_AUTH_HTTP_CLIENT =
"singularity.webhook.auth.http.client";
private Optional<Class<? extends SingularityAuthorizer>> authorizerClass = Optional.empty();

public SingularityAuthModule() {}

public void setAuthorizerClass(Class<? extends SingularityAuthorizer> authorizerClass) {
this.authorizerClass = Optional.of(authorizerClass);
}

@Override
public void configure(Binder binder) {
binder
Expand Down Expand Up @@ -64,27 +70,10 @@ public void configure(Binder binder) {
}
}

switch (getConfiguration().getAuthConfiguration().getAuthMode()) {
case GROUPS_SCOPES:
binder
.bind(SingularityAuthorizer.class)
.to(SingularityGroupsScopesAuthorizer.class)
.in(Scopes.SINGLETON);
break;
case GROUPS_LOG_SCOPES:
binder
.bind(SingularityAuthorizer.class)
.to(SingularityDualAuthorizer.class)
.in(Scopes.SINGLETON);
break;
case GROUPS:
default:
binder
.bind(SingularityAuthorizer.class)
.to(SingularityGroupsAuthorizer.class)
.in(Scopes.SINGLETON);
break;
}
binder
.bind(SingularityAuthorizer.class)
.to(authorizerClass.orElseGet(this::getAuthClassFromConfig))
.in(Scopes.SINGLETON);

switch (getConfiguration().getAuthConfiguration().getAuthResponseParser()) {
case RAW:
Expand All @@ -110,4 +99,16 @@ public void configure(Binder binder) {
getConfiguration().getAuthConfiguration().getDatastore().getAuthDatastoreClass()
);
}

private Class<? extends SingularityAuthorizer> getAuthClassFromConfig() {
switch (getConfiguration().getAuthConfiguration().getAuthMode()) {
case GROUPS_SCOPES:
return SingularityGroupsScopesAuthorizer.class;
case GROUPS_LOG_SCOPES:
return SingularityDualAuthorizer.class;
case GROUPS:
default:
return SingularityGroupsAuthorizer.class;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.common.collect.Sets;
import com.hubspot.singularity.InvalidSingularityTaskIdException;
import com.hubspot.singularity.SingularityAuthorizationScope;
import com.hubspot.singularity.SingularityDeploy;
import com.hubspot.singularity.SingularityRequest;
import com.hubspot.singularity.SingularityRequestWithState;
import com.hubspot.singularity.SingularityTaskId;
Expand All @@ -31,7 +32,7 @@ public SingularityAuthorizer(RequestManager requestManager, boolean authEnabled)
this.authEnabled = authEnabled;
}

static boolean groupsIntersect(Set<String> a, Set<String> b) {
public static boolean groupsIntersect(Set<String> a, Set<String> b) {
return !Sets.intersection(a, b).isEmpty();
}

Expand Down Expand Up @@ -67,6 +68,13 @@ protected abstract void checkForAuthorization(
Optional<SingularityUserFacingAction> action
);

public void checkForAuthorization(
SingularityRequest request,
SingularityDeploy deploy,
SingularityUser user,
SingularityAuthorizationScope scope
) {}

public boolean isAuthorizedForRequest(
SingularityRequest request,
SingularityUser user,
Expand All @@ -91,6 +99,16 @@ protected abstract boolean isAuthorizedForRequest(
Optional<SingularityUserFacingAction> action
);

public void checkForAuthorizedChanges(
SingularityRequest newRequest,
Optional<SingularityRequest> oldRequest,
SingularityUser user
) {
if (oldRequest.isPresent()) {
checkForAuthorizedChanges(newRequest, oldRequest.get(), user);
} else {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, don't need the empty else block

}

public abstract void checkForAuthorizedChanges(
SingularityRequest request,
SingularityRequest oldRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SingularityGroupsScopesAuthorizer extends SingularityAuthorizer {
SingularityGroupsScopesAuthorizer.class
);

private final AuthConfiguration authConfiguration;
protected final AuthConfiguration authConfiguration;
private final ScopesConfiguration scopesConfiguration;
private final SingularityEventListener singularityEventListener;

Expand Down Expand Up @@ -227,7 +227,7 @@ private boolean isJita(SingularityUser user) {
return groupsIntersect(authConfiguration.getJitaGroups(), user.getGroups());
}

private boolean isAdmin(SingularityUser user) {
protected boolean isAdmin(SingularityUser user) {
return hasScope(user, SingularityAuthorizationScope.ADMIN);
}

Expand Down Expand Up @@ -290,7 +290,7 @@ private void checkForbiddenForGroups(
);
}

private Set<String> getGroups(
protected Set<String> getGroups(
SingularityRequest request,
SingularityAuthorizationScope scope
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ public SingularityRequestParent deploy(
);

SingularityRequest request = requestWithState.getRequest();
authorizationHelper.checkForAuthorization(
request,
deploy,
user,
SingularityAuthorizationScope.WRITE
);

final Optional<SingularityRequest> updatedValidatedRequest;
if (deployRequest.getUpdatedRequest().isPresent()) {
authorizationHelper.checkForAuthorizedChanges(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,14 @@ private void submitRequest(
? Optional.of(oldRequestWithState.get().getRequest())
: Optional.<SingularityRequest>empty();

authorizationHelper.checkForAuthorizedChanges(request, oldRequest, user);

if (oldRequest.isPresent()) {
authorizationHelper.checkForAuthorization(
oldRequest.get(),
user,
SingularityAuthorizationScope.WRITE
);
authorizationHelper.checkForAuthorizedChanges(request, oldRequest.get(), user);
validator.checkActionEnabled(SingularityAction.UPDATE_REQUEST);
} else {
validator.checkActionEnabled(SingularityAction.CREATE_REQUEST);
Expand Down