Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
Wiring up allow-clients-to-create-secrets permission check (#1119)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmontgomery-square committed Aug 10, 2022
1 parent aa71dd6 commit 098cec6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Expand Up @@ -8,13 +8,31 @@
public class PermissionCheckModule extends AbstractModule {

@Override
protected void configure() {}
protected void configure() {
bindToDefaultConstructor(AllowClientSecretCreationPermissionCheck.class);
}

@Provides
public PermissionCheck createPermissionCheck(MetricRegistry metricRegistry,
AutomationClientPermissionCheck automationClientCheck,
OwnershipPermissionCheck ownershipCheck) {
PermissionCheck anyPermissionCheck = new AnyPermissionCheck(metricRegistry, List.of(ownershipCheck, automationClientCheck));
OwnershipPermissionCheck ownershipCheck,
AllowClientSecretCreationPermissionCheck clientSecretCreationCheck) {

List<PermissionCheck> permissionChecks = List.of(
ownershipCheck,
clientSecretCreationCheck,
automationClientCheck
);

PermissionCheck anyPermissionCheck = new AnyPermissionCheck(metricRegistry, permissionChecks);
return anyPermissionCheck;
}

private <T> void bindToDefaultConstructor(Class<T> clazz) {
try {
bind(clazz).toConstructor(clazz.getConstructor());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}
@@ -1,10 +1,13 @@
package keywhiz.service.permissions;

import javax.inject.Inject;
import keywhiz.api.model.Client;
import keywhiz.api.model.Secret;
import org.junit.Test;

import static keywhiz.test.KeywhizTests.createInjector;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class PermissionCheckModuleTest {
@Test
Expand All @@ -21,4 +24,27 @@ class Holder {
createInjector().injectMembers(holder);
assertNotNull(holder.permissionCheck);
}

@Test
public void injectedPermissionCheckAllowsClientSecretCreation() {
PermissionCheck permissionCheck = createInjector().getInstance(PermissionCheck.class);
assertTrue(permissionCheck.isAllowedForTargetType(newClient(), Action.CREATE, Secret.class));
}

private static Client newClient() {
return new Client(
0L,
"name",
null,
null,
null,
null,
null,
null,
null,
null,
false,
false);
}

}

0 comments on commit 098cec6

Please sign in to comment.