Skip to content

Commit

Permalink
Merge pull request #15976 from gsmet/1.13.0-backports-3
Browse files Browse the repository at this point in the history
1.13.0 backports 3
  • Loading branch information
gsmet authored Mar 24, 2021
2 parents bdedac0 + eea3210 commit e51b06d
Show file tree
Hide file tree
Showing 16 changed files with 375 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ ReadResult run() {
nameBuilder.setLength(len);
}
// sweep-up
for (String propertyName : getAllProperties()) {
for (String propertyName : config.getPropertyNames()) {
if (propertyName.equals(ConfigSource.CONFIG_ORDINAL)) {
continue;
}
Expand Down Expand Up @@ -718,19 +718,6 @@ private Converter<?> getConverter(SmallRyeConfig config, Field field, ConverterT
convByType.put(valueType, converter);
return converter;
}

/**
* We collect all properties from ConfigSources, because Config#getPropertyNames exclude the active profiled
* properties, meaning that the property is written in the default config source unprofiled. This may cause
* issues if we run with a different profile and fallback to defaults.
*/
private Set<String> getAllProperties() {
Set<String> properties = new HashSet<>();
for (ConfigSource configSource : config.getConfigSources()) {
properties.addAll(configSource.getPropertyNames());
}
return properties;
}
}

public static final class ReadResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@

import io.quarkus.test.QuarkusUnitTest;

public class ConfigDefaultValuesTest {
public class ConfigDefaultValues {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addAsResource(new StringAsset(
"config_ordinal=1000\n" +
"my.prop=1234\n" +
"%prod.my.prop=1234\n" +
"%dev.my.prop=5678\n" +
"%test.my.prop=1234"),
"application.properties"));
.addAsResource(new StringAsset("config_ordinal=1000\n" +
"my.prop=1234\n"), "application.properties"));
@Inject
Config config;

Expand All @@ -44,17 +39,6 @@ void configDefaultValues() {
assertEquals("1234", applicationProperties.getValue("my.prop"));
}

@Test
void profileDefaultValues() {
ConfigSource defaultValues = getConfigSourceByName("PropertiesConfigSource[source=Specified default values]");
assertNotNull(defaultValues);
assertEquals("1234", defaultValues.getValue("my.prop"));
assertEquals("1234", defaultValues.getValue("%prod.my.prop"));
assertEquals("5678", defaultValues.getValue("%dev.my.prop"));
assertEquals("1234", defaultValues.getValue("%test.my.prop"));
assertEquals("1234", config.getValue("my.prop", String.class));
}

private ConfigSource getConfigSourceByName(String name) {
for (ConfigSource configSource : config.getConfigSources()) {
if (configSource.getName().contains(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ public class QuarkusSecurityIdentity implements SecurityIdentity {
private final Set<Credential> credentials;
private final Map<String, Object> attributes;
private final List<Function<Permission, Uni<Boolean>>> permissionCheckers;
private final boolean anonymous;

private QuarkusSecurityIdentity(Builder builder) {
this.principal = builder.principal;
this.roles = Collections.unmodifiableSet(builder.roles);
this.credentials = Collections.unmodifiableSet(builder.credentials);
this.attributes = Collections.unmodifiableMap(builder.attributes);
this.permissionCheckers = Collections.unmodifiableList(builder.permissionCheckers);
this.anonymous = builder.anonymous;
}

@Override
Expand All @@ -38,7 +40,7 @@ public Principal getPrincipal() {

@Override
public boolean isAnonymous() {
return false;
return anonymous;
}

@Override
Expand Down Expand Up @@ -121,11 +123,13 @@ public static Builder builder() {
}

public static Builder builder(SecurityIdentity identity) {
return new Builder()
Builder builder = new Builder()
.addAttributes(identity.getAttributes())
.addCredentials(identity.getCredentials())
.addRoles(identity.getRoles())
.setPrincipal(identity.getPrincipal());
.setPrincipal(identity.getPrincipal())
.setAnonymous(identity.isAnonymous());
return builder;
}

public static class Builder {
Expand All @@ -135,6 +139,7 @@ public static class Builder {
Set<Credential> credentials = new HashSet<>();
Map<String, Object> attributes = new HashMap<>();
List<Function<Permission, Uni<Boolean>>> permissionCheckers = new ArrayList<>();
private boolean anonymous;
boolean built = false;

public Builder setPrincipal(Principal principal) {
Expand Down Expand Up @@ -211,7 +216,25 @@ public Builder addPermissionChecker(Function<Permission, Uni<Boolean>> function)
return this;
}

/**
* Sets an anonymous identity status.
*
* @param anonymous the anonymous status
* @return This builder
*/
public Builder setAnonymous(boolean anonymous) {
if (built) {
throw new IllegalStateException();
}
this.anonymous = anonymous;
return this;
}

public QuarkusSecurityIdentity build() {
if (principal == null && !anonymous) {
throw new IllegalStateException("Principal is null but anonymous status is false");
}

built = true;
return new QuarkusSecurityIdentity(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package io.quarkus.security.runtime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.security.Permission;
import java.security.Principal;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Test;

import io.quarkus.security.credential.Credential;
import io.quarkus.security.credential.PasswordCredential;
import io.quarkus.security.identity.SecurityIdentity;
import io.smallrye.mutiny.Uni;

public class QuarkusSecurityIdentityTest {

Expand All @@ -18,11 +30,132 @@ public void testCopyIdentity() throws Exception {
.addAttribute("key", "value")
.build();

assertFalse(identity1.isAnonymous());

SecurityIdentity identity2 = QuarkusSecurityIdentity.builder(identity1).build();
assertFalse(identity1.isAnonymous());

assertEquals(identity1.getAttributes(), identity2.getAttributes());
assertEquals(identity1.getPrincipal(), identity2.getPrincipal());
assertEquals(identity1.getCredentials(), identity2.getCredentials());
assertEquals(identity1.getRoles(), identity2.getRoles());
}

@Test
public void testAnonymousPrincipalWithCustomIdentity() throws Exception {
SecurityIdentity identity1 = new TestSecurityIdentityAnonymousPrincipal();
assertTrue(identity1.isAnonymous());
assertEquals("anonymous-principal", identity1.getPrincipal().getName());

SecurityIdentity identity2 = QuarkusSecurityIdentity.builder(identity1).build();
assertTrue(identity2.isAnonymous());
assertEquals("anonymous-principal", identity2.getPrincipal().getName());
}

@Test
public void testPrincipalNullAnonymousFalseWithBuilder() throws Exception {
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder()
.addRole("admin")
.addCredential(new PasswordCredential("password".toCharArray()))
.addAttribute("key", "value");
;

assertThrows(IllegalStateException.class, () -> builder.build());
}

@Test
public void testPrincipalNullAnonymousFalseWithCustomIdentity() throws Exception {
SecurityIdentity identity1 = new TestSecurityIdentityPrincipalNullAnonymousFalse();
assertFalse(identity1.isAnonymous());
assertNull(identity1.getPrincipal());

assertThrows(IllegalStateException.class, () -> QuarkusSecurityIdentity.builder(identity1).build());
}

@Test
public void testPrincipalNullAnonymousFalseWithCustomIdentityFixed() throws Exception {
SecurityIdentity identity1 = new TestSecurityIdentityPrincipalNullAnonymousFalse();
assertFalse(identity1.isAnonymous());
assertNull(identity1.getPrincipal());

SecurityIdentity identity2 = QuarkusSecurityIdentity.builder(identity1).setAnonymous(true).build();
assertTrue(identity2.isAnonymous());
assertNull(identity2.getPrincipal());
}

static class TestSecurityIdentityAnonymousPrincipal extends AbstractTestSecurityIdentity {

@Override
public Principal getPrincipal() {
return new Principal() {
@Override
public String getName() {
return "anonymous-principal";
}
};
}

@Override
public boolean isAnonymous() {
return true;
}

}

static class TestSecurityIdentityPrincipalNullAnonymousFalse extends AbstractTestSecurityIdentity {

@Override
public Principal getPrincipal() {
return null;
}

@Override
public boolean isAnonymous() {
return false;
}

}

static abstract class AbstractTestSecurityIdentity implements SecurityIdentity {

@Override
public Set<String> getRoles() {
return Collections.emptySet();
}

@Override
public boolean hasRole(String role) {
// TODO Auto-generated method stub
return false;
}

@Override
public <T extends Credential> T getCredential(Class<T> credentialType) {
// TODO Auto-generated method stub
return null;
}

@Override
public Set<Credential> getCredentials() {
return Collections.emptySet();
}

@Override
public <T> T getAttribute(String name) {
// TODO Auto-generated method stub
return null;
}

@Override
public Map<String, Object> getAttributes() {
return Collections.emptyMap();
}

@Override
public Uni<Boolean> checkPermission(Permission permission) {
// TODO Auto-generated method stub
return null;
}

}
}
Loading

0 comments on commit e51b06d

Please sign in to comment.