use of com.thoughtworks.go.config.policy.Policy in project gocd by gocd.
the class PluginRoleConfigTest method shouldAnswerWhetherItHasPermissionsForGivenEntityOfTypeAndName.
@Test
public void shouldAnswerWhetherItHasPermissionsForGivenEntityOfTypeAndName() {
final Policy directives = new Policy();
directives.add(new Allow("view", ENVIRONMENT.getType(), "env_1"));
RoleConfig role = new RoleConfig(new CaseInsensitiveString(""), new Users(), directives);
assertTrue(role.hasPermissionsFor(SupportedAction.VIEW, EnvironmentConfig.class, "env_1"));
assertFalse(role.hasPermissionsFor(SupportedAction.VIEW, EnvironmentConfig.class, "env_2"));
assertFalse(role.hasPermissionsFor(SupportedAction.VIEW, PipelineConfig.class, "*"));
}
use of com.thoughtworks.go.config.policy.Policy in project gocd by gocd.
the class RoleConfigTest method validatePolicyIsInvalid.
private void validatePolicyIsInvalid(Validator validator) {
SecurityConfig securityConfig = new SecurityConfig();
ValidationContext validationContext = ValidationContextMother.validationContext(securityConfig);
Policy policy = new Policy();
policy.add(new Allow("*", ENVIRONMENT.getType(), "env_1"));
RoleConfig role = new RoleConfig(new CaseInsensitiveString("role"), new Users(), policy);
securityConfig.getRoles().add(role);
validator.validate(role, validationContext);
assertThat(role.getPolicy().hasErrors()).isTrue();
assertThat(role.getPolicy().get(0).errors().on("action")).isEqualTo("Invalid action, must be one of [view, administer].");
}
use of com.thoughtworks.go.config.policy.Policy in project gocd by gocd.
the class RoleConfigCommandTest method shouldNotPassValidationIfPolicyHasAnError.
@Test
public void shouldNotPassValidationIfPolicyHasAnError() {
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
Policy policy = new Policy();
policy.add(new Allow("*", "*", "*"));
RoleConfig roleConfig = new RoleConfig(new CaseInsensitiveString("foo"), new Users(), policy);
cruiseConfig.server().security().addRole(roleConfig);
RoleConfigCommand command = new StubCommand(goConfigService, roleConfig, extension, currentUser, result);
boolean isValid = command.isValid(cruiseConfig);
assertFalse(isValid);
}
use of com.thoughtworks.go.config.policy.Policy in project gocd by gocd.
the class RoleRepresenter method fromJSON.
public static Role fromJSON(JsonReader jsonReader) {
Role model;
String type = jsonReader.optString("type").orElse("");
if ("gocd".equals(type)) {
model = GoCDRoleConfigRepresenter.fromJSON(jsonReader.readJsonObject("attributes"));
} else if ("plugin".equals(type)) {
model = PluginRoleConfigRepresenter.fromJSON(jsonReader.readJsonObject("attributes"));
} else {
throw new JsonParseException("Invalid role type '%s'. It has to be one of 'gocd' or 'plugin'");
}
model.setName(new CaseInsensitiveString(jsonReader.optString("name").orElse(null)));
Policy directives = new Policy();
jsonReader.readArrayIfPresent("policy", policy -> {
policy.forEach(directive -> directives.add(DirectiveRepresenter.fromJSON(new JsonReader(directive.getAsJsonObject()))));
});
model.setPolicy(directives);
return model;
}
use of com.thoughtworks.go.config.policy.Policy in project gocd by gocd.
the class RoleConfigTest method shouldAnswerWhetherItHasPermissionsForGivenEntityOfTypeAndName.
@Test
public void shouldAnswerWhetherItHasPermissionsForGivenEntityOfTypeAndName() {
final Policy directives = new Policy();
directives.add(new Allow("view", ENVIRONMENT.getType(), "env_1"));
RoleConfig role = new RoleConfig(new CaseInsensitiveString(""), new Users(), directives);
assertThat(role.hasPermissionsFor(SupportedAction.VIEW, EnvironmentConfig.class, "env_1")).isTrue();
assertThat(role.hasPermissionsFor(SupportedAction.VIEW, EnvironmentConfig.class, "env_2")).isFalse();
assertThat(role.hasPermissionsFor(SupportedAction.VIEW, PipelineConfig.class, "*")).isFalse();
}
Aggregations