Compare commits

...

4 Commits

Author SHA1 Message Date
guoshoujing d0e9aa92f5 update api version 2021-07-19 16:47:08 +08:00
guoshoujing 86ef5ff967 improve code 2021-07-19 15:30:01 +08:00
guoshoujing 7e66bae080 add listByIds api in GroupAPI 2021-07-19 15:29:49 +08:00
guoshoujing 652cbdb418 create adminGroup and opGroup when initStore 2021-07-19 15:27:24 +08:00
11 changed files with 152 additions and 13 deletions

View File

@ -153,7 +153,7 @@
</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Implementation-Version>0.64.0.0</Implementation-Version>
<Implementation-Version>0.65.0.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>

View File

@ -20,6 +20,7 @@
package com.baidu.hugegraph.api.auth;
import java.util.List;
import java.util.stream.Collectors;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
@ -40,6 +41,7 @@ import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.api.API;
import com.baidu.hugegraph.api.filter.StatusFilter.Status;
import com.baidu.hugegraph.auth.HugeGroup;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.id.IdGenerator;
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.define.Checkable;
@ -111,6 +113,23 @@ public class GroupAPI extends API {
return manager.serializer(g).writeAuthElements("groups", groups);
}
@POST
@Timed
@Path("/ids")
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String listByIds(@Context GraphManager manager,
@PathParam("graph") String graph,
List<String> groupIds) {
LOG.debug("Graph [{}] list groups", graph);
HugeGraph g = graph(manager, graph);
List<Id> ids = groupIds.stream()
.map(UserAPI::parseId)
.collect(Collectors.toList());
List<HugeGroup> groups = manager.authManager().listGroups(ids);
return manager.serializer(g).writeAuthElements("groups", groups);
}
@GET
@Timed
@Path("{id}")

View File

@ -176,6 +176,7 @@ public class ProjectAPI extends API {
throw new IllegalArgumentException("Invalid project id: " + id);
}
}
public static boolean isAddGraph(String action) {
return ACTION_ADD_GRAPH.equals(action);
}

View File

@ -327,7 +327,7 @@ public interface HugeAuthenticator extends Authenticator {
for (Map.Entry<HugePermission, Object> e : perms.entrySet()) {
HugePermission permission = e.getKey();
// May be required = ANY
if (action.match(permission)) {
if (permission.match(action)) {
// Return matched resource of corresponding action
return e.getValue();
}

View File

@ -114,10 +114,11 @@ public final class ApiVersion {
* [0.62] Issue-1378: Add compact api for rocksdb/cassandra/hbase backend
* [0.63] Issue-1500: Add user-login RESTful API
* [0.64] Issue-1504: Add auth-project RESTful API
* [0.65] Issue-1505: Create admin-group and op-group when initStore
*/
// The second parameter of Version.of() is for IDE running without JAR
public static final Version VERSION = Version.of(ApiVersion.class, "0.64");
public static final Version VERSION = Version.of(ApiVersion.class, "0.65");
public static final void check() {
// Check version of hugegraph-core. Firstly do check from version 0.3

View File

@ -32,6 +32,8 @@ import com.baidu.hugegraph.HugeGraphParams;
import com.baidu.hugegraph.auth.SchemaDefine.Entity;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.schema.VertexLabel;
import com.baidu.hugegraph.type.define.DataType;
import com.baidu.hugegraph.type.define.HugeGroupTag;
import com.baidu.hugegraph.util.E;
public class HugeGroup extends Entity {
@ -40,6 +42,7 @@ public class HugeGroup extends Entity {
private String name;
private String description;
private HugeGroupTag tag;
public HugeGroup(String name) {
this(null, name);
@ -78,6 +81,14 @@ public class HugeGroup extends Entity {
this.description = description;
}
public HugeGroupTag tag() {
return this.tag;
}
public void tag(HugeGroupTag tag) {
this.tag = tag;
}
@Override
public String toString() {
return String.format("HugeGroup(%s)%s", this.id, this.asMap());
@ -95,6 +106,9 @@ public class HugeGroup extends Entity {
case P.DESCRIPTION:
this.description = (String) value;
break;
case P.TAG:
this.tag = HugeGroupTag.fromCode((Byte) value);
break;
default:
throw new AssertionError("Unsupported key: " + key);
}
@ -118,6 +132,11 @@ public class HugeGroup extends Entity {
list.add(this.description);
}
if (this.tag != null) {
list.add(P.TAG);
list.add(this.tag.code());
}
return super.asArray(list);
}
@ -131,6 +150,9 @@ public class HugeGroup extends Entity {
if (this.description != null) {
map.put(Hidden.unHide(P.DESCRIPTION), this.description);
}
if (this.tag != null) {
map.put(Hidden.unHide(P.TAG), this.tag);
}
return super.asMap(map);
}
@ -153,6 +175,7 @@ public class HugeGroup extends Entity {
public static final String NAME = "~group_name";
public static final String DESCRIPTION = "~group_description";
public static final String TAG = "~group_tag";
public static String unhide(String key) {
final String prefix = Hidden.hide("group_");
@ -182,7 +205,7 @@ public class HugeGroup extends Entity {
.properties(properties)
.usePrimaryKeyId()
.primaryKeys(P.NAME)
.nullableKeys(P.DESCRIPTION)
.nullableKeys(P.DESCRIPTION, P.TAG)
.enableLabelIndex(true)
.build();
this.graph.schemaTransaction().addVertexLabel(label);
@ -193,6 +216,7 @@ public class HugeGroup extends Entity {
props.add(createPropertyKey(P.NAME));
props.add(createPropertyKey(P.DESCRIPTION));
props.add(createPropertyKey(P.TAG, DataType.BYTE));
return super.initProperties(props);
}

View File

@ -32,8 +32,8 @@ public enum HugePermission implements SerialEnum {
ANY(0x7f, "any");
private byte code;
private String name;
private final byte code;
private final String name;
static {
SerialEnum.register(HugePermission.class);

View File

@ -40,7 +40,6 @@ import org.apache.tinkerpop.shaded.jackson.databind.ser.std.StdSerializer;
import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.auth.SchemaDefine.AuthElement;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.structure.HugeElement;
import com.baidu.hugegraph.traversal.optimize.TraversalUtil;
import com.baidu.hugegraph.type.Namifiable;
@ -59,6 +58,11 @@ public class HugeResource {
private static final Set<ResourceType> CHECK_NAME_RESS = ImmutableSet.of(
ResourceType.META);
public static final List<HugeResource> SUPER_ADMIN_RES = ImmutableList.of(
new HugeResource(ResourceType.ROOT, ANY, null));
public static final List<HugeResource> OP_ADMIN_RES = ImmutableList.of(
new HugeResource(ResourceType.METRICS, ANY, null),
new HugeResource(ResourceType.STATUS, ANY, null));
static {
SimpleModule module = new SimpleModule();

View File

@ -46,6 +46,7 @@ import com.baidu.hugegraph.config.AuthOptions;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.event.EventListener;
import com.baidu.hugegraph.type.define.Directions;
import com.baidu.hugegraph.type.define.HugeGroupTag;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Events;
import com.baidu.hugegraph.util.LockUtil;
@ -132,6 +133,7 @@ public class StandardAuthManager implements AuthManager {
if (storeEvents.contains(event.name())) {
try {
this.initSchemaIfNeeded();
this.initAdminAndOpGroupIfNeeded();
} finally {
this.graph.closeTx();
}
@ -153,6 +155,16 @@ public class StandardAuthManager implements AuthManager {
return true;
}
private void invalidateUserCache() {
this.usersCache.clear();
}
private void invalidatePasswdCache(Id id) {
this.pwdCache.invalidate(id);
// Clear all tokenCache because can't get userId in it
this.tokenCache.clear();
}
private void initSchemaIfNeeded() {
this.invalidateUserCache();
HugeUser.schema(this.graph).initSchemaIfNeeded();
@ -163,14 +175,36 @@ public class StandardAuthManager implements AuthManager {
HugeProject.schema(this.graph).initSchemaIfNeeded();
}
private void invalidateUserCache() {
this.usersCache.clear();
private void initAdminAndOpGroupIfNeeded() {
HugeConfig config = this.graph.configuration();
String authStore = config.get(AuthOptions.AUTH_GRAPH_STORE);
this.initGroup(authStore, HugeGroupTag.SUPER_ADMIN,
HugeResource.SUPER_ADMIN_RES, HugePermission.ANY);
this.initGroup(authStore, HugeGroupTag.OP_SUPER_ADMIN,
HugeResource.OP_ADMIN_RES, HugePermission.ANY);
}
private void invalidatePasswdCache(Id id) {
this.pwdCache.invalidate(id);
// Clear all tokenCache because can't get userId in it
this.tokenCache.clear();
private void initGroup(String authStore, HugeGroupTag tag,
List<HugeResource> res, HugePermission permission) {
List<HugeGroup> groups = this.groups.query(HugeGroup.P.NAME, tag.name(), -1);
if (CollectionUtils.isNotEmpty(groups)) {
return;
}
HugeGroup adminGroup = new HugeGroup(tag.name());
adminGroup.tag(tag);
adminGroup.creator("admin");
Id adminGroupId = this.createGroup(adminGroup);
HugeTarget adminTarget = new HugeTarget(tag.name(), authStore, "", res);
adminTarget.creator("admin");
Id adminTargetId = this.createTarget(adminTarget);
HugeAccess access = new HugeAccess(adminGroupId, adminTargetId,
permission);
access.creator("admin");
this.createAccess(access);
}
@Override

View File

@ -20,6 +20,7 @@
package com.baidu.hugegraph.auth;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Date;
import java.util.Map;
@ -47,6 +48,7 @@ public class TokenGenerator {
}
public String create(Map<String, ?> payload, long expire) {
expire = Duration.ofSeconds(expire).toMillis();
return Jwts.builder()
.setClaims(payload)
.setExpiration(new Date(System.currentTimeMillis() + expire))

View File

@ -0,0 +1,54 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package com.baidu.hugegraph.type.define;
public enum HugeGroupTag implements SerialEnum {
SUPER_ADMIN(1, "Super administrator"),
OP_SUPER_ADMIN(2, "Cluster operation and maintenance administrator"),
PROJECT_ADMIN(3, "Project manager"),
OP_PROJECT_ADMIN(4, "Project operation and maintenance administrator");
private final byte code;
private final String name;
HugeGroupTag(int code, String name) {
assert code < 256;
this.code = (byte) code;
this.name = name;
}
static {
SerialEnum.register(HugeGroupTag.class);
}
@Override
public byte code() {
return this.code;
}
public String string() {
return this.name;
}
public static HugeGroupTag fromCode(byte code) {
return SerialEnum.fromCode(HugeGroupTag.class, code);
}
}