Skip to content

Commit

Permalink
fixup! Added CURRENT_USER, 'USER and SESSION_USER` system functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidan committed Jul 5, 2017
1 parent 2d35bdc commit 3ff4b32
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 203 deletions.
6 changes: 6 additions & 0 deletions blackbox/docs/sql/administration/user_management.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ To list all existing users query that table::
The column ``name`` shows the unique name of the user, the column ``superuser``
shows whether the user has superuser privileges or not.

.. note::

We also support retrieving the current connected user using the system
information functions: :ref:`CURRENT_USER <current_user>`,
:ref:`USER <user>` and :ref:`SESSION_USER <session_user>`.

.. warning::

When the :ref:`es_api_setting` is enabled, it is possible to read the users
Expand Down
48 changes: 31 additions & 17 deletions enterprise/users/src/main/java/io/crate/plugin/UsersPlugin.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
/*
* Licensed to Crate under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership. Crate licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
* This file is part of a module with proprietary Enterprise Features.
*
* http://www.apache.org/licenses/LICENSE-2.0
* Licensed to Crate.io Inc. ("Crate.io") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
* Unauthorized copying of this file, via any medium is strictly prohibited.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial
* agreement.
* To use this file, Crate.io must have given you permission to enable and
* use such Enterprise Features and you must have a valid Enterprise or
* Subscription Agreement with Crate.io. If you enable or use the Enterprise
* Features, you represent and warrant that you have a valid Enterprise or
* Subscription Agreement with Crate.io. Your use of the Enterprise Features
* if governed by the terms and conditions of your Enterprise or Subscription
* Agreement with Crate.io.
*/

package io.crate.plugin;

import io.crate.metadata.UsersMetaData;
import io.crate.metadata.UsersPrivilegesMetaData;
import io.crate.scalar.UsersScalarFunctionModule;
import io.crate.settings.SharedSettings;
import org.elasticsearch.cluster.NamedDiff;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.plugins.Plugin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class UsersPlugin extends Plugin {

public UsersPlugin() {
private final Settings settings;

public UsersPlugin(Settings settings) {
this.settings = settings;
}

@Override
Expand Down Expand Up @@ -81,4 +86,13 @@ public List<NamedXContentRegistry.Entry> getNamedXContent() {
));
return entries;
}

@Override
public Collection<Module> createGuiceModules() {
if (SharedSettings.ENTERPRISE_LICENSE_SETTING.setting().get(settings)) {
return Collections.singletonList(new UsersScalarFunctionModule());
} else {
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file is part of a module with proprietary Enterprise Features.
*
* Licensed to Crate.io Inc. ("Crate.io") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Unauthorized copying of this file, via any medium is strictly prohibited.
*
* To use this file, Crate.io must have given you permission to enable and
* use such Enterprise Features and you must have a valid Enterprise or
* Subscription Agreement with Crate.io. If you enable or use the Enterprise
* Features, you represent and warrant that you have a valid Enterprise or
* Subscription Agreement with Crate.io. Your use of the Enterprise Features
* if governed by the terms and conditions of your Enterprise or Subscription
* Agreement with Crate.io.
*/

package io.crate.scalar;

import io.crate.metadata.FunctionIdent;
import io.crate.metadata.FunctionImplementation;
import io.crate.scalar.systeminformation.UserFunction;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.multibindings.MapBinder;

public class UsersScalarFunctionModule extends AbstractModule {

@Override
protected void configure() {
MapBinder<FunctionIdent, FunctionImplementation> functionBinder =
MapBinder.newMapBinder(binder(), FunctionIdent.class, FunctionImplementation.class);
UserFunction currentUserFunction = new UserFunction(UserFunction.CURRENT_USER_FUNCTION_NAME);
functionBinder.addBinding(currentUserFunction.info().ident()).toInstance(currentUserFunction);
UserFunction sessionUserFunction = new UserFunction(UserFunction.SESSION_USER_FUNCTION_NAME);
functionBinder.addBinding(sessionUserFunction.info().ident()).toInstance(sessionUserFunction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,35 @@
* Agreement with Crate.io.
*/

package io.crate.operation.scalar.systeminformation;
package io.crate.scalar.systeminformation;

import com.google.common.collect.ImmutableList;
import io.crate.analyze.symbol.Function;
import io.crate.analyze.symbol.Literal;
import io.crate.analyze.symbol.Symbol;
import io.crate.analyze.symbol.format.FunctionFormatSpec;
import io.crate.data.Input;
import io.crate.exceptions.UnsupportedFeatureException;
import io.crate.metadata.FunctionIdent;
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.Scalar;
import io.crate.metadata.TransactionContext;
import io.crate.operation.scalar.ScalarFunctionModule;
import io.crate.operation.user.User;
import io.crate.settings.SharedSettings;
import io.crate.types.DataTypes;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.settings.Settings;

import javax.annotation.Nullable;
import java.util.Locale;

public class UserFunction extends Scalar<BytesRef, Object> implements FunctionFormatSpec {

static final String CURRENT_USER_FUNCTION_NAME = "current_user";
static final String SESSION_USER_FUNCTION_NAME = "session_user";
public static final String CURRENT_USER_FUNCTION_NAME = "current_user";
public static final String SESSION_USER_FUNCTION_NAME = "session_user";

public static void register(ScalarFunctionModule scalarFunctionModule, Settings settings) {
scalarFunctionModule.register(new UserFunction(CURRENT_USER_FUNCTION_NAME, settings));
scalarFunctionModule.register(new UserFunction(SESSION_USER_FUNCTION_NAME, settings));
}

public final String name;
private final boolean enterpriseEnabled;
public final FunctionInfo functionInfo;
private final String name;
private final FunctionInfo functionInfo;

public UserFunction(String name, Settings settings) {
public UserFunction(String name) {
this.name = name;
this.enterpriseEnabled = SharedSettings.ENTERPRISE_LICENSE_SETTING.setting().get(settings);
this.functionInfo = new FunctionInfo(new FunctionIdent(name, ImmutableList.of()), DataTypes.STRING);
}

Expand All @@ -72,11 +61,6 @@ public BytesRef evaluate(Input<Object>... args) {

@Override
public Symbol normalizeSymbol(Function symbol, @Nullable TransactionContext transactionContext) {
if (enterpriseEnabled == false) {
throw new UnsupportedFeatureException(
String.format(Locale.ENGLISH, "%s function is only supported in enterprise version", name)
);
}
if (transactionContext == null) {
return Literal.NULL;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* This file is part of a module with proprietary Enterprise Features.
*
* Licensed to Crate.io Inc. ("Crate.io") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Unauthorized copying of this file, via any medium is strictly prohibited.
*
* To use this file, Crate.io must have given you permission to enable and
* use such Enterprise Features and you must have a valid Enterprise or
* Subscription Agreement with Crate.io. If you enable or use the Enterprise
* Features, you represent and warrant that you have a valid Enterprise or
* Subscription Agreement with Crate.io. Your use of the Enterprise Features
* if governed by the terms and conditions of your Enterprise or Subscription
* Agreement with Crate.io.
*/

package io.crate.scalar.systeminformation;

import io.crate.action.sql.Option;
import io.crate.action.sql.SessionContext;
import io.crate.analyze.symbol.Symbol;
import io.crate.analyze.symbol.format.SymbolPrinter;
import io.crate.operation.scalar.AbstractScalarFunctionsTest;
import io.crate.operation.user.User;
import io.crate.scalar.UsersScalarFunctionModule;
import io.crate.testing.SqlExpressions;
import org.junit.Test;

import java.util.Collections;

import static io.crate.testing.SymbolMatchers.isLiteral;
import static org.hamcrest.Matchers.is;

public class UserFunctionTest extends AbstractScalarFunctionsTest {

private static final User TEST_USER = new User("testUser", Collections.emptySet(), Collections.emptySet());

private void setupFunctionsForTestUser() {
SessionContext sessionContext = new SessionContext(0, Option.NONE, null, TEST_USER,
s -> {}, t -> {});
sqlExpressions = new SqlExpressions(tableSources, null, null, sessionContext,
new UsersScalarFunctionModule());
functions = sqlExpressions.functions();
}

@Test
public void testNormalizeCurrentUser() {
setupFunctionsForTestUser();
assertNormalize("current_user", isLiteral("testUser"), false);
}

@Test
public void testNormalizeSessionUser() {
setupFunctionsForTestUser();
assertNormalize("session_user", isLiteral("testUser"), false);
}

@Test
public void testNormalizeUser() {
setupFunctionsForTestUser();
assertNormalize("user", isLiteral("testUser"), false);
}

@Test
public void testCurrentUserForMissingUserReturnsNull() {
setupFunctionsWithoutUser();
assertNormalize("current_user", isLiteral(null), false);
}

@Test
public void testUserForMissingUserReturnsNull() {
setupFunctionsWithoutUser();
assertNormalize("user", isLiteral(null), false);
}

@Test
public void testSessionUserForMissingUserReturnsNull() {
setupFunctionsWithoutUser();
assertNormalize("session_user", isLiteral(null), false);
}

@Test
public void testFormatFunctionsWithoutBrackets() {
setupFunctionsForTestUser();
SymbolPrinter printer = new SymbolPrinter(sqlExpressions.functions());
Symbol f = sqlExpressions.asSymbol("current_user");
assertThat(printer.printFullQualified(f), is("current_user"));

f = sqlExpressions.asSymbol("session_user");
assertThat(printer.printFullQualified(f), is("session_user"));

f = sqlExpressions.asSymbol("user");
assertThat(printer.printFullQualified(f), is("current_user"));
}

private void setupFunctionsWithoutUser() {
SessionContext sessionContext = new SessionContext(0, Option.NONE, null, null, null, null);
sqlExpressions = new SqlExpressions(tableSources, null, null, sessionContext, new UsersScalarFunctionModule());
functions = sqlExpressions.functions();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,20 @@
import io.crate.operation.scalar.string.LengthFunction;
import io.crate.operation.scalar.string.StringCaseFunction;
import io.crate.operation.scalar.systeminformation.CurrentSchemaFunction;
import io.crate.operation.scalar.systeminformation.UserFunction;
import io.crate.operation.scalar.timestamp.CurrentTimestampFunction;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.multibindings.MapBinder;
import org.elasticsearch.common.settings.Settings;

import java.util.HashMap;
import java.util.Map;

public class ScalarFunctionModule extends AbstractModule {

private final Settings settings;
private Map<FunctionIdent, FunctionImplementation> functions = new HashMap<>();
private Map<String, FunctionResolver> resolver = new HashMap<>();
private MapBinder<FunctionIdent, FunctionImplementation> functionBinder;
private MapBinder<String, FunctionResolver> resolverBinder;

public ScalarFunctionModule(Settings settings) {
this.settings = settings;
}

public void register(FunctionImplementation impl) {
functions.put(impl.info().ident(), impl);
}
Expand Down Expand Up @@ -129,7 +122,6 @@ protected void configure() {
IfFunction.register(this);

CurrentSchemaFunction.register(this);
UserFunction.register(this, settings);

// bind all registered functions and resolver
// by doing it here instead of the register functions, plugins can also use the
Expand Down
2 changes: 1 addition & 1 deletion sql/src/main/java/io/crate/plugin/SQLPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public Collection<Module> createGuiceModules() {
modules.add(new SysClusterExpressionModule());
modules.add(new SysNodeExpressionModule());
modules.add(new AggregationImplModule());
modules.add(new ScalarFunctionModule(settings));
modules.add(new ScalarFunctionModule());
modules.add(new TableFunctionModule());
modules.add(new BulkModule());
modules.add(new SysChecksModule());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,6 @@ public void testFormatFunctionFullQualifiedInputName() throws Exception {
public void testFormatFunctionsWithoutBrackets() throws Exception {
Symbol f = sqlExpressions.asSymbol("current_schema");
assertPrint(f, "current_schema");

f = sqlExpressions.asSymbol("current_user");
assertPrint(f, "current_user");

f = sqlExpressions.asSymbol("session_user");
assertPrint(f, "session_user");

f = sqlExpressions.asSymbol("user");
assertPrint(f, "current_user");
}

@Test
Expand Down
Loading

0 comments on commit 3ff4b32

Please sign in to comment.