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

Dynamic tenancy configurations #2607

Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ config:
#kibana:
# Kibana multitenancy
#multitenancy_enabled: true
#private_tenant_enabled: true
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this consistent with the current default settings?

#default_tenant: ""
#server_username: kibanaserver
#index: '.kibana'
http:
Expand Down
1 change: 1 addition & 0 deletions legacy/securityconfig_v6/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ opendistro_security:
#filtered_alias_mode: warn
#kibana:
#multitenancy_enabled: true

#server_username: kibanaserver
#index: '.kibana'
#do_not_fail_on_forbidden: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@
import org.opensearch.search.query.QuerySearchResult;
import org.opensearch.security.action.configupdate.ConfigUpdateAction;
import org.opensearch.security.action.configupdate.TransportConfigUpdateAction;
import org.opensearch.security.action.tenancy.TenancyConfigRestHandler;
import org.opensearch.security.action.tenancy.TenancyConfigRetrieveActions;
import org.opensearch.security.action.tenancy.TenancyConfigRetrieveTransportAction;
import org.opensearch.security.action.tenancy.TenancyConfigUpdateAction;
import org.opensearch.security.action.tenancy.TenancyConfigUpdateTransportAction;
import org.opensearch.security.action.whoami.TransportWhoAmIAction;
import org.opensearch.security.action.whoami.WhoAmIAction;
import org.opensearch.security.auditlog.AuditLog;
Expand Down Expand Up @@ -469,6 +474,7 @@ public List<RestHandler> getRestHandlers(Settings settings, RestController restC
Objects.requireNonNull(cs), Objects.requireNonNull(adminDns), Objects.requireNonNull(cr)));
handlers.add(new SecurityConfigUpdateAction(settings, restController, Objects.requireNonNull(threadPool), adminDns, configPath, principalExtractor));
handlers.add(new SecurityWhoAmIAction(settings, restController, Objects.requireNonNull(threadPool), adminDns, configPath, principalExtractor));
handlers.add(new TenancyConfigRestHandler());
handlers.addAll(
SecurityRestApiActions.getHandler(
settings,
Expand Down Expand Up @@ -505,6 +511,10 @@ public UnaryOperator<RestHandler> getRestHandlerWrapper(final ThreadContext thre
if(!disabled && !SSLConfig.isSslOnlyMode()) {
actions.add(new ActionHandler<>(ConfigUpdateAction.INSTANCE, TransportConfigUpdateAction.class));
actions.add(new ActionHandler<>(WhoAmIAction.INSTANCE, TransportWhoAmIAction.class));

actions.add(new ActionHandler<>(TenancyConfigRetrieveActions.INSTANCE, TenancyConfigRetrieveTransportAction.class));
stephen-crawford marked this conversation as resolved.
Show resolved Hide resolved
actions.add(new ActionHandler<>(TenancyConfigUpdateAction.INSTANCE, TenancyConfigUpdateTransportAction.class));

}
return actions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.security.action.tenancy;

import java.io.IOException;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.common.io.stream.StreamInput;

public class EmptyRequest extends ActionRequest {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you explain what this abstraction is for? I am not familiar with this pattern. Is this proper format and I am unaware? Is it something specific to this scenario? Thank you.

Copy link

Choose a reason for hiding this comment

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

This is used as the generic type for HandledTransportAction as it expects an instance of ActionRequest

Copy link

Choose a reason for hiding this comment

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

It is being used for TenancyConfigRetrieveTransportAction


public EmptyRequest(final StreamInput in) throws IOException {
super(in);
}

public EmptyRequest() throws IOException {
super();
}

@Override
public ActionRequestValidationException validate()
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.security.action.tenancy;

import java.io.IOException;
import java.util.List;

import com.google.common.collect.ImmutableList;

import org.opensearch.client.node.NodeClient;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.action.RestToXContentListener;

import static org.opensearch.rest.RestRequest.Method.GET;
import static org.opensearch.rest.RestRequest.Method.PUT;

public class TenancyConfigRestHandler extends BaseRestHandler {

public TenancyConfigRestHandler() {
super();
}

@Override
public String getName() {
return "Multi Tenancy actions to Retrieve / Update configs.";
}

@Override
public List<Route> routes() {
return ImmutableList.of(
new Route(GET, "/_plugins/_security/api/tenancy/config"),
new Route(PUT, "/_plugins/_security/api/tenancy/config")
);
}

@Override
protected RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient nodeClient) throws IOException {

switch (request.method()) {
case GET:
return channel -> nodeClient.execute(
TenancyConfigRetrieveActions.INSTANCE,
new EmptyRequest(),
new RestToXContentListener<>(channel));
case PUT:
Copy link
Contributor

Choose a reason for hiding this comment

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

Should you overwrite this with PATCH as well? Seems like PATCH would be the expected path for update

return channel -> nodeClient.execute(
TenancyConfigUpdateAction.INSTANCE,
TenancyConfigUpdateRequest.fromXContent(request.contentParser()),
new RestToXContentListener<>(channel));
default:
throw new RuntimeException("Not implemented");
Copy link

Choose a reason for hiding this comment

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

We can also add the value of request.method() if it's not implemented for better context when it's logged.

}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.security.action.tenancy;

import org.opensearch.action.ActionType;

public class TenancyConfigRetrieveActions extends ActionType<TenancyConfigRetrieveResponse> {

public static final TenancyConfigRetrieveActions INSTANCE = new TenancyConfigRetrieveActions();
public static final String NAME = "cluster:feature/tenancy/config/read";

protected TenancyConfigRetrieveActions() {
super(NAME, TenancyConfigRetrieveResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.security.action.tenancy;

import java.io.IOException;

import org.opensearch.action.ActionResponse;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;

public class TenancyConfigRetrieveResponse extends ActionResponse implements ToXContentObject {

public TenancyConfigs tenancyConfigs = new TenancyConfigs();

public TenancyConfigRetrieveResponse(final StreamInput in) throws IOException {
super(in);
this.tenancyConfigs.multitenancy_enabled = in.readOptionalBoolean();
this.tenancyConfigs.private_tenant_enabled = in.readOptionalBoolean();
this.tenancyConfigs.default_tenant = in.readOptionalString();
}

public TenancyConfigRetrieveResponse(final TenancyConfigs tenancyConfigs) {
this.tenancyConfigs = tenancyConfigs;
}

public TenancyConfigs getMultitenancyConfig() {
return tenancyConfigs;
}

public Boolean getMultitenancyEnabled() { return tenancyConfigs.multitenancy_enabled; }

public Boolean getPrivateTenantEnabled() { return tenancyConfigs.private_tenant_enabled; }

public String getDefaultTenant() { return tenancyConfigs.default_tenant; }

@Override
public void writeTo(final StreamOutput out) throws IOException {
out.writeBoolean(getMultitenancyEnabled());
out.writeBoolean(getPrivateTenantEnabled());
out.writeString(getDefaultTenant());
}

@Override
public String toString() {
return Strings.toString(XContentType.JSON, this, true, true);
}

@Override
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
builder.startObject();
builder.field("multitenancy_enabled", getMultitenancyEnabled());
builder.field("private_tenant_enabled", getPrivateTenantEnabled());
builder.field("default_tenant", getDefaultTenant());
builder.endObject();
return builder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.security.action.tenancy;

import java.util.Collections;

import org.opensearch.action.ActionListener;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.settings.Settings;
import org.opensearch.security.configuration.ConfigurationRepository;
import org.opensearch.security.securityconf.impl.CType;
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration;
import org.opensearch.security.securityconf.impl.v7.ConfigV7;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

public class TenancyConfigRetrieveTransportAction
extends HandledTransportAction<EmptyRequest, TenancyConfigRetrieveResponse> {

private final ConfigurationRepository config;

@Inject
public TenancyConfigRetrieveTransportAction(final Settings settings,
final TransportService transportService,
final ActionFilters actionFilters,
final ConfigurationRepository config) {
super(TenancyConfigRetrieveActions.NAME, transportService, actionFilters, EmptyRequest::new);

this.config = config;
}

/** Load the configuration from the security index and return a copy */
protected final SecurityDynamicConfiguration<?> load() {
return config.getConfigurationsFromIndex(Collections.singleton(CType.CONFIG), false).get(CType.CONFIG).deepClone();
}

@Override
protected void doExecute(final Task task, final EmptyRequest request, final ActionListener<TenancyConfigRetrieveResponse> listener) {

// Get the security configuration and lookup the config setting state
final SecurityDynamicConfiguration<?> dynamicConfig = load();
ConfigV7 config = (ConfigV7)dynamicConfig.getCEntry("config");

final TenancyConfigs tenancyConfigs= new TenancyConfigs();

tenancyConfigs.multitenancy_enabled = config.dynamic.kibana.multitenancy_enabled;
tenancyConfigs.private_tenant_enabled = config.dynamic.kibana.private_tenant_enabled;
tenancyConfigs.default_tenant = config.dynamic.kibana.default_tenant;

listener.onResponse(new TenancyConfigRetrieveResponse(tenancyConfigs));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.security.action.tenancy;

import org.opensearch.action.ActionType;

public class TenancyConfigUpdateAction extends ActionType<TenancyConfigRetrieveResponse> {

public static final TenancyConfigUpdateAction INSTANCE = new TenancyConfigUpdateAction();
public static final String NAME = "cluster:feature/tenancy/config/update";


protected TenancyConfigUpdateAction()
{
super(NAME, TenancyConfigRetrieveResponse::new);
}
}
Loading