|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with this |
| 4 | + * work for additional information regarding copyright ownership. The ASF |
| 5 | + * licenses this file to You under the Apache License, Version 2.0 (the |
| 6 | + * "License"); you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hugegraph.api.profile; |
| 19 | + |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.regex.Matcher; |
| 26 | +import java.util.regex.Pattern; |
| 27 | + |
| 28 | +import org.apache.commons.lang.StringUtils; |
| 29 | +import org.apache.hugegraph.api.API; |
| 30 | +import org.apache.hugegraph.api.filter.StatusFilter; |
| 31 | +import org.apache.hugegraph.auth.AuthManager; |
| 32 | +import org.apache.hugegraph.core.GraphManager; |
| 33 | +import org.apache.hugegraph.util.E; |
| 34 | +import org.apache.hugegraph.util.Log; |
| 35 | +import org.slf4j.Logger; |
| 36 | + |
| 37 | +import com.codahale.metrics.annotation.Timed; |
| 38 | +import com.google.common.collect.ImmutableMap; |
| 39 | + |
| 40 | +import jakarta.annotation.security.RolesAllowed; |
| 41 | +import jakarta.inject.Singleton; |
| 42 | +import jakarta.ws.rs.Consumes; |
| 43 | +import jakarta.ws.rs.GET; |
| 44 | +import jakarta.ws.rs.POST; |
| 45 | +import jakarta.ws.rs.PUT; |
| 46 | +import jakarta.ws.rs.Path; |
| 47 | +import jakarta.ws.rs.Produces; |
| 48 | +import jakarta.ws.rs.QueryParam; |
| 49 | +import jakarta.ws.rs.core.Context; |
| 50 | + |
| 51 | +@Path("whiteiplist") |
| 52 | +@Singleton |
| 53 | +public class WhiteIpListAPI extends API { |
| 54 | + |
| 55 | + private static final Logger LOG = Log.logger(WhiteIpListAPI.class); |
| 56 | + |
| 57 | + @GET |
| 58 | + @Timed |
| 59 | + @Produces(APPLICATION_JSON_WITH_CHARSET) |
| 60 | + @RolesAllowed("admin") |
| 61 | + public Map<String, Object> list(@Context GraphManager manager) { |
| 62 | + LOG.debug("List white ips"); |
| 63 | + AuthManager authManager = manager.authManager(); |
| 64 | + Set<String> whiteIpList = authManager.listWhiteIPs(); |
| 65 | + return ImmutableMap.of("whiteIpList", whiteIpList); |
| 66 | + } |
| 67 | + |
| 68 | + @POST |
| 69 | + @Timed |
| 70 | + @StatusFilter.Status(StatusFilter.Status.ACCEPTED) |
| 71 | + @Consumes(APPLICATION_JSON) |
| 72 | + @Produces(APPLICATION_JSON_WITH_CHARSET) |
| 73 | + @RolesAllowed("admin") |
| 74 | + public Map<String, Object> updateWhiteIPs(@Context GraphManager manager, Map<String, Object> actionMap) { |
| 75 | + E.checkArgument(actionMap != null, |
| 76 | + "Missing argument: actionMap"); |
| 77 | + Set<String> whiteIpList = manager.authManager().listWhiteIPs(); |
| 78 | + Object ipListRaw = actionMap.get("ips"); |
| 79 | + E.checkArgument(ipListRaw instanceof List, |
| 80 | + "Invalid ips type '%s', must be list", ipListRaw.getClass()); |
| 81 | + List<String> ipList = (List<String>) ipListRaw; |
| 82 | + Object actionRaw = actionMap.get("action"); |
| 83 | + E.checkArgument(actionRaw != null, |
| 84 | + "Missing argument: action"); |
| 85 | + E.checkArgument(actionRaw instanceof String, |
| 86 | + "Invalid action type '%s', must be string", |
| 87 | + actionRaw.getClass()); |
| 88 | + String action = (String) actionRaw; |
| 89 | + E.checkArgument(StringUtils.isNotEmpty(action), |
| 90 | + "Missing argument: action"); |
| 91 | + Set<String> existedIPs = new HashSet<>(); |
| 92 | + Set<String> loadedIPs = new HashSet<>(); |
| 93 | + Set<String> illegalIPs = new HashSet<>(); |
| 94 | + Map<String, Object> result = new HashMap<>(); |
| 95 | + for (String ip : ipList) { |
| 96 | + if (whiteIpList.contains(ip)) { |
| 97 | + existedIPs.add(ip); |
| 98 | + continue; |
| 99 | + } |
| 100 | + if ("load".equals(action)) { |
| 101 | + boolean rightIp = checkIp(ip) ? loadedIPs.add(ip) : illegalIPs.add(ip); |
| 102 | + } |
| 103 | + } |
| 104 | + switch (action) { |
| 105 | + case "load": |
| 106 | + LOG.debug("Load to white ip list"); |
| 107 | + result.put("existed_ips", existedIPs); |
| 108 | + result.put("added_ips", loadedIPs); |
| 109 | + if (!illegalIPs.isEmpty()) { |
| 110 | + result.put("illegal_ips", illegalIPs); |
| 111 | + } |
| 112 | + whiteIpList.addAll(loadedIPs); |
| 113 | + break; |
| 114 | + case "remove": |
| 115 | + LOG.debug("Remove from white ip list"); |
| 116 | + result.put("removed_ips", existedIPs); |
| 117 | + result.put("non_existed_ips", loadedIPs); |
| 118 | + whiteIpList.removeAll(existedIPs); |
| 119 | + break; |
| 120 | + default: |
| 121 | + throw new AssertionError(String.format("Invalid action '%s', " + |
| 122 | + "supported action is " + |
| 123 | + "'load' or 'remove'", |
| 124 | + action)); |
| 125 | + } |
| 126 | + manager.authManager().setWhiteIPs(whiteIpList); |
| 127 | + return result; |
| 128 | + } |
| 129 | + |
| 130 | + @PUT |
| 131 | + @Timed |
| 132 | + @Produces(APPLICATION_JSON_WITH_CHARSET) |
| 133 | + @RolesAllowed("admin") |
| 134 | + public Map<String, Object> updateStatus(@Context GraphManager manager, @QueryParam("status") String status) { |
| 135 | + LOG.debug("Enable or disable white ip list"); |
| 136 | + E.checkArgument("true".equals(status) || |
| 137 | + "false".equals(status), |
| 138 | + "Invalid status, valid status is 'true' or 'false'"); |
| 139 | + boolean open = Boolean.parseBoolean(status); |
| 140 | + manager.authManager().enabledWhiteIpList(open); |
| 141 | + Map<String, Object> map = new HashMap<>(); |
| 142 | + map.put("WhiteIpListOpen", open); |
| 143 | + return map; |
| 144 | + } |
| 145 | + |
| 146 | + private boolean checkIp(String ipStr) { |
| 147 | + String ip = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." |
| 148 | + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." |
| 149 | + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." |
| 150 | + + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; |
| 151 | + Pattern pattern = Pattern.compile(ip); |
| 152 | + Matcher matcher = pattern.matcher(ipStr); |
| 153 | + return matcher.matches(); |
| 154 | + } |
| 155 | +} |
0 commit comments