Search in sources :

Example 1 with Policy

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, "*"));
}
Also used : Policy(com.thoughtworks.go.config.policy.Policy) Allow(com.thoughtworks.go.config.policy.Allow) Test(org.junit.jupiter.api.Test)

Example 2 with Policy

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].");
}
Also used : Policy(com.thoughtworks.go.config.policy.Policy) Allow(com.thoughtworks.go.config.policy.Allow)

Example 3 with Policy

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);
}
Also used : Policy(com.thoughtworks.go.config.policy.Policy) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Allow(com.thoughtworks.go.config.policy.Allow) Test(org.junit.jupiter.api.Test)

Example 4 with Policy

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;
}
Also used : Role(com.thoughtworks.go.config.Role) Policy(com.thoughtworks.go.config.policy.Policy) JsonReader(com.thoughtworks.go.api.representers.JsonReader) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) JsonParseException(com.google.gson.JsonParseException) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Example 5 with Policy

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();
}
Also used : Policy(com.thoughtworks.go.config.policy.Policy) Allow(com.thoughtworks.go.config.policy.Allow) Test(org.junit.jupiter.api.Test)

Aggregations

Policy (com.thoughtworks.go.config.policy.Policy)6 Allow (com.thoughtworks.go.config.policy.Allow)5 Test (org.junit.jupiter.api.Test)3 JsonParseException (com.google.gson.JsonParseException)1 JsonReader (com.thoughtworks.go.api.representers.JsonReader)1 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)1 Role (com.thoughtworks.go.config.Role)1 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)1