-
Notifications
You must be signed in to change notification settings - Fork 293
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is being used for |
||
|
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also add the value of |
||
} | ||
} | ||
|
||
} |
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); | ||
} | ||
} |
There was a problem hiding this comment.
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?