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 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
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.MutliTenancyRestHandler;
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 MutliTenancyRestHandler());
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 = "cluster:feature/tenancy/multitenancy_enabled/read";

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 = "cluster:feature/tenancy/multitenancy_enabled/update";

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