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

Feature activation for multi-tenancy #2568

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -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.MultiTenancyRetrieveAction;
import org.opensearch.security.action.tenancy.MultiTenancyRetrieveTransportAction;
import org.opensearch.security.action.tenancy.MultiTenancyUpdateAction;
import org.opensearch.security.action.tenancy.MultiTenancyUpdateTransportAction;
import org.opensearch.security.action.tenancy.MutliTenancyRetrieveRestHandler;
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 MutliTenancyRetrieveRestHandler());
handlers.addAll(
SecurityRestApiActions.getHandler(
settings,
Expand Down Expand Up @@ -505,6 +511,9 @@ 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<>(MultiTenancyRetrieveAction.INSTANCE, MultiTenancyRetrieveTransportAction.class));
actions.add(new ActionHandler<>(MultiTenancyUpdateAction.INSTANCE, MultiTenancyUpdateTransportAction.class));
}
return actions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 BooleanSettingRetrieveResponse extends ActionResponse implements ToXContentObject {
Copy link
Member Author

Choose a reason for hiding this comment

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

If there are additional actions that reuse this pattern { "value": {VALUE} } these names might need to be made more generic and moved outside of the tenancy package. The same is true for the Request types as well.


private Boolean value;

public BooleanSettingRetrieveResponse(final StreamInput in) throws IOException {
super(in);
this.value = in.readBoolean();
}

public BooleanSettingRetrieveResponse(final Boolean value) {
this.value = value;
}

public Boolean getValue() {
return value;
}

@Override
public void writeTo(final StreamOutput out) throws IOException {
out.writeBoolean(getValue());
}

@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("value", getValue());
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.io.IOException;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.core.ParseField;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.XContentParser;

public class BooleanSettingUpdateRequest extends ActionRequest {

private Boolean value;

public BooleanSettingUpdateRequest(final StreamInput in) throws IOException {
super(in);
in.readBoolean();
}

public BooleanSettingUpdateRequest(final Boolean value) {
super();
this.value = value;
}

public Boolean getValue() {
return value;
}

@Override
public ActionRequestValidationException validate() {
if (getValue() == null) {
final ActionRequestValidationException validationException = new ActionRequestValidationException();
validationException.addValidationError("Missing boolean value");
return validationException;
}
return null;
}

private static final ConstructingObjectParser<BooleanSettingUpdateRequest, Void> PARSER = new ConstructingObjectParser<>(
BooleanSettingUpdateRequest.class.getName(),
args -> new BooleanSettingUpdateRequest((Boolean) args[0])
);

static {
PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), new ParseField("value"));
}

public static BooleanSettingUpdateRequest fromXContent(final XContentParser parser) {
return PARSER.apply(parser, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 {

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,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 MultiTenancyRetrieveAction extends ActionType<BooleanSettingRetrieveResponse> {

public static final MultiTenancyRetrieveAction INSTANCE = new MultiTenancyRetrieveAction();
public static final String NAME = "securityconfig:admin/config/tenancy/multitenancy_enabled/read";
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about using cluster:admin/config/tenancy/multitenancy_enabled/read?
This is currently being used in ConfigUpdateAction.java

public static final String NAME = "cluster:admin/opendistro_security/config/update";

Copy link
Member Author

@peternied peternied Mar 20, 2023

Choose a reason for hiding this comment

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

I was thinking about making a new prefix instead of 'cluster'. What would you think about feature:cluster/tenancy/multitenancy_enabled/(read|update) instead?

I'd like it to be unassociated from where the setting is stored, it should be abstract enough it could move to another plugin, it should indicate the scope (e.g. cluster wide!), and then the local namespace tenancy followed by feature name.

What do you think, other recommendations?

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about swapping the order of feature:cluster to be cluster:feature? Unless there is a reason to put feature first, I am generally in favor of broadest-->granlur(est) ordering for the route. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I like the idea of cluster:feature/... This should be clear in reading and will identify the scopes in the decreasing order.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated it to cluster:feature/tenancy/multitenancy_enabled/update - but it seems like we should also update the rest request url to be inline with this, otherwise the coupling is still in place.


protected MultiTenancyRetrieveAction() {
super(NAME, BooleanSettingRetrieveResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 MultiTenancyRetrieveTransportAction
extends HandledTransportAction<EmptyRequest, BooleanSettingRetrieveResponse> {

private final ConfigurationRepository config;

@Inject
public MultiTenancyRetrieveTransportAction(final Settings settings,
final TransportService transportService,
final ActionFilters actionFilters,
final ConfigurationRepository config) {
super(MultiTenancyRetrieveAction.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<BooleanSettingRetrieveResponse> listener) {
// Get the security configuration and lookup the config setting state
final SecurityDynamicConfiguration<?> dynamicConfig = load();
ConfigV7 config = (ConfigV7)dynamicConfig.getCEntry("config");
final Boolean value = config.dynamic.kibana.multitenancy_enabled;

listener.onResponse(new BooleanSettingRetrieveResponse(value));
}
}
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 MultiTenancyUpdateAction extends ActionType<BooleanSettingRetrieveResponse> {

public static final MultiTenancyUpdateAction INSTANCE = new MultiTenancyUpdateAction();
public static final String NAME = "securityconfig:admin/config/tenancy/multitenancy_enabled/update";
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about using cluster:admin/config/tenancy/multitenancy_enabled/update?
This is currently being used in ConfigUpdateAction.java

public static final String NAME = "cluster:admin/opendistro_security/config/update";

Copy link
Member Author

Choose a reason for hiding this comment

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

The point of these new actions is they can be permissioned separately from the security config, if I make them the same this removes its benefit, see the related issue for details


protected MultiTenancyUpdateAction() {
super(NAME, BooleanSettingRetrieveResponse::new);
}
}
Loading