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

DefaultContextResolver should respect the defaultRealm configuration #309

Merged
merged 10 commits into from
Oct 19, 2024
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 @@ -91,6 +91,7 @@ public void setPolarisAuthenticator(
}

public RealmContextResolver getRealmContextResolver() {
realmContextResolver.setDefaultRealm(this.defaultRealm);
return realmContextResolver;
Comment on lines +105 to 106
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we combine these two fields(defaultRealm, realmContextResolver) to one? I don't see a reason to keep both of them. I'd prefer to keep the resolver, which may have different implementations.

What we can do is to mimic the rate limiter. The default realm name can be picked up by the implementation itself, instead of setting a string field then pass it to the resolver.

  @JsonProperty("rateLimiter")
  public RateLimiter getRateLimiter() {
    return rateLimiter;
  }

  @JsonProperty("rateLimiter")
  public void setRateLimiter(@Nullable RateLimiter rateLimiter) {
    this.rateLimiter = rateLimiter;
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm confused by this suggestion; the linked issue is about users not being able to pass an explicit string default via the config. Are you suggesting that we remove the config?

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, I wasn't clear in the first place. I think we can combine these two configs to one.

realmContextResolver:
  type: default

defaultRealms:
  - default-realm

To

realmContextResolver:
  type: default
  defaultRealms: default-realm

With that, we don't have to keep two fields in the config class, and I think it is a nice way to manage the lifecycle of realmContextResolver. It could be immutable object without any setting methods.

Copy link
Contributor

@flyrain flyrain Oct 9, 2024

Choose a reason for hiding this comment

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

If we made the above change, I think we don't have to make the interface changes in RealmContextResolver. We just need to add a constructor in the class DefaultContextResolver like this

public DefaultContextResolver( @JsonProperty("defaultRealms") final String defaultRealm) {
    this.defaultRealm = defaultRealm;
}

Not setter and getter are needed in this way. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing the default constructor is still a breaking change, so is this materially better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, to be clear, we are not just changing how this works for DefaultContextResolver but for all RealmContextResolver implementations

Copy link
Contributor

Choose a reason for hiding this comment

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

It is cleaner solution unless the config defaultRealm would be used by other places than RealmContextResolver. Do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The defaultRealm is just the default realm used by RealmContextResolver when the realm cannot be parsed from the request

Copy link
Contributor

Choose a reason for hiding this comment

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

Synced with @eric-maynard offline, we agreed on pushing this break change to another PR.

}

Expand Down Expand Up @@ -125,6 +126,7 @@ public String getDefaultRealm() {
@JsonProperty("defaultRealm")
public void setDefaultRealm(String defaultRealm) {
this.defaultRealm = defaultRealm;
realmContextResolver.setDefaultRealm(defaultRealm);
}

@JsonProperty("cors")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@
* <p>Example: principal:data-engineer;password:test;realm:acct123
*/
@JsonTypeName("default")
public class DefaultContextResolver
implements RealmContextResolver, CallContextResolver, ConfigurationStoreAware {
public class DefaultContextResolver extends RealmContextResolver
implements CallContextResolver, ConfigurationStoreAware {
flyrain marked this conversation as resolved.
Show resolved Hide resolved
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultContextResolver.class);

public static final String REALM_PROPERTY_KEY = "realm";
public static final String REALM_PROPERTY_DEFAULT_VALUE = "default-realm";

public static final String PRINCIPAL_PROPERTY_KEY = "principal";
public static final String PRINCIPAL_PROPERTY_DEFAULT_VALUE = "default-principal";
Expand Down Expand Up @@ -92,10 +91,8 @@ public RealmContext resolveRealmContext(

if (!parsedProperties.containsKey(REALM_PROPERTY_KEY)) {
LOGGER.warn(
"Failed to parse {} from headers; using {}",
REALM_PROPERTY_KEY,
REALM_PROPERTY_DEFAULT_VALUE);
parsedProperties.put(REALM_PROPERTY_KEY, REALM_PROPERTY_DEFAULT_VALUE);
"Failed to parse {} from headers; using {}", REALM_PROPERTY_KEY, getDefaultRealm());
parsedProperties.put(REALM_PROPERTY_KEY, getDefaultRealm());
}
return () -> parsedProperties.get(REALM_PROPERTY_KEY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@
import org.apache.polaris.service.config.HasEntityManagerFactory;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
public interface RealmContextResolver extends Discoverable, HasEntityManagerFactory {
RealmContext resolveRealmContext(
public abstract class RealmContextResolver implements Discoverable, HasEntityManagerFactory {
eric-maynard marked this conversation as resolved.
Show resolved Hide resolved
private String defaultRealm = "default-realm";

public abstract RealmContext resolveRealmContext(
String requestURL,
String method,
String path,
Map<String, String> queryParams,
Map<String, String> headers);

public final void setDefaultRealm(String defaultRealm) {
this.defaultRealm = defaultRealm;
}

public final String getDefaultRealm() {
return this.defaultRealm;
}
}