Skip to content

Commit

Permalink
better code
Browse files Browse the repository at this point in the history
  • Loading branch information
SunnyBoy-WYH committed Sep 20, 2023
1 parent 5470e17 commit 2b04f68
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected User authenticate(ContainerRequestContext context) {
path = request.getRequestURI();

Check warning on line 126 in hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AuthenticationFilter.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AuthenticationFilter.java#L125-L126

Added lines #L125 - L126 were not covered by tests

String remoteIp = request.getRemoteAddr();
List<String> whiteIpList = manager.authManager().listWhiteIp();
List<String> whiteIpList = manager.authManager().listWhiteIPs();
boolean whiteIpEnabled = manager.authManager().getWhiteIpStatus();

Check warning on line 130 in hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AuthenticationFilter.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AuthenticationFilter.java#L128-L130

Added lines #L128 - L130 were not covered by tests
if (!path.contains(STRING_WHITE_IP_LIST) && whiteIpEnabled &&
!whiteIpList.contains(remoteIp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

@Path("whiteiplist")
@Singleton
public class WhiteIpAPI extends API {
public class WhiteIpListAPI extends API {

Check warning on line 53 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L53

Added line #L53 was not covered by tests

private static final Logger LOG = Log.logger(RestServer.class);

Check warning on line 55 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L55

Added line #L55 was not covered by tests

Expand All @@ -61,7 +61,7 @@ public class WhiteIpAPI extends API {
public Map<String, Object> list(@Context GraphManager manager) {
LOG.debug("List white ips");
AuthManager authManager = manager.authManager();
List<String> whiteIpList = authManager.listWhiteIp();
List<String> whiteIpList = authManager.listWhiteIPs();
return ImmutableMap.of("whiteIpList", whiteIpList);

Check warning on line 65 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L62-L65

Added lines #L62 - L65 were not covered by tests
}

Expand All @@ -71,75 +71,75 @@ public Map<String, Object> list(@Context GraphManager manager) {
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed("admin")
public Map<String, Object> batch(@Context GraphManager manager,
public Map<String, Object> updateWhiteIPs(@Context GraphManager manager,
Map<String, Object> actionMap) {
E.checkArgument(actionMap != null,
"Missing argument: actionMap");
List<String> whiteIpList = manager.authManager().listWhiteIp();
Object ips = actionMap.get("ips");
E.checkArgument(ips instanceof List,
"Invalid ips type '%s', must be list", ips.getClass());
List<String> ipList = (List<String>) ips;
Object value = actionMap.get("action");
E.checkArgument(value != null,
List<String> whiteIpList = manager.authManager().listWhiteIPs();
Object ipListRaw = actionMap.get("ips");
E.checkArgument(ipListRaw instanceof List,
"Invalid ips type '%s', must be list", ipListRaw.getClass());
List<String> ipList = (List<String>) ipListRaw;
Object actionRaw = actionMap.get("action");

Check warning on line 83 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L78-L83

Added lines #L78 - L83 were not covered by tests
E.checkArgument(actionRaw != null,
"Missing argument: action");
E.checkArgument(value instanceof String,
E.checkArgument(actionRaw instanceof String,

Check warning on line 86 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L86

Added line #L86 was not covered by tests
"Invalid action type '%s', must be string",
value.getClass());
String action = (String) value;
actionRaw.getClass());
String action = (String) actionRaw;
E.checkArgument(StringUtils.isNotEmpty(action),

Check warning on line 90 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L88-L90

Added lines #L88 - L90 were not covered by tests
"Missing argument: action");
List<String> existed = new ArrayList<>();
List<String> loaded = new ArrayList<>();
List<String> illegalIps = new ArrayList<>();
List<String> existedIPs = new ArrayList<>();
List<String> loadedIPs = new ArrayList<>();
List<String> illegalIPs = new ArrayList<>();
Map<String, Object> result = new HashMap<>();

Check warning on line 95 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L92-L95

Added lines #L92 - L95 were not covered by tests
for (String ip : ipList) {
if (whiteIpList.contains(ip)) {
existed.add(ip);
existedIPs.add(ip);
continue;

Check warning on line 99 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L98-L99

Added lines #L98 - L99 were not covered by tests
}
if ("load".equals(action)) {
boolean rightIp = checkIp(ip) ? loaded.add(ip) : illegalIps.add(ip);
boolean rightIp = checkIp(ip) ? loadedIPs.add(ip) : illegalIPs.add(ip);
}
}

Check warning on line 104 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L104

Added line #L104 was not covered by tests
switch (action) {
case "load":
LOG.debug("Load to white ip list");
result.put("existed", existed);
result.put("loaded", loaded);
if (!illegalIps.isEmpty()) {
result.put("illegalIps", illegalIps);
result.put("existed_ips", existedIPs);
result.put("loaded_ips", loadedIPs);

Check warning on line 109 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L107-L109

Added lines #L107 - L109 were not covered by tests
if (!illegalIPs.isEmpty()) {
result.put("illegal_ips", illegalIPs);

Check warning on line 111 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L111

Added line #L111 was not covered by tests
}
whiteIpList.addAll(loaded);
whiteIpList.addAll(loadedIPs);
break;

Check warning on line 114 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L113-L114

Added lines #L113 - L114 were not covered by tests
case "remove":
LOG.debug("Remove from white ip list");
result.put("removed", existed);
result.put("nonexistent", loaded);
whiteIpList.removeAll(existed);
result.put("removed", existedIPs);
result.put("nonexistent", loadedIPs);
whiteIpList.removeAll(existedIPs);
break;

Check warning on line 120 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L116-L120

Added lines #L116 - L120 were not covered by tests
default:
throw new AssertionError(String.format("Invalid action '%s', " +

Check warning on line 122 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L122

Added line #L122 was not covered by tests
"supported action is " +
"'load' or 'remove'",
action));
}
manager.authManager().setWhiteIpList(whiteIpList);
manager.authManager().setWhiteIPs(whiteIpList);
return result;

Check warning on line 128 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L127-L128

Added lines #L127 - L128 were not covered by tests
}

@PUT
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed("admin")
public Map<String, Object> update(@Context GraphManager manager,
public Map<String, Object> updateStatus(@Context GraphManager manager,
@QueryParam("status") String status) {
LOG.debug("Enable or disable white ip list");

Check warning on line 137 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L137

Added line #L137 was not covered by tests
E.checkArgument("true".equals(status) ||
"false".equals(status),
"Invalid status, valid status is 'true' or 'false'");
boolean open = Boolean.parseBoolean(status);
manager.authManager().setWhiteIpStatus(open);
manager.authManager().enabledWhiteIpList(open);
Map<String, Object> map = new HashMap<>();
map.put("WhiteIpListOpen", open);
return map;

Check warning on line 145 in hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/WhiteIpListAPI.java#L141-L145

Added lines #L141 - L145 were not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1569,13 +1569,13 @@ public UserWithRole validateUser(String token) {
}

@Override
public List<String> listWhiteIp() {
return this.authManager.listWhiteIp();
public List<String> listWhiteIPs() {
return this.authManager.listWhiteIPs();

Check warning on line 1573 in hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java#L1573

Added line #L1573 was not covered by tests
}

@Override
public void setWhiteIpList(List<String> whiteIpList) {
this.authManager.setWhiteIpList(whiteIpList);
public void setWhiteIPs(List<String> whiteIpList) {
this.authManager.setWhiteIPs(whiteIpList);
}

Check warning on line 1579 in hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java#L1578-L1579

Added lines #L1578 - L1579 were not covered by tests

@Override
Expand All @@ -1584,8 +1584,8 @@ public boolean getWhiteIpStatus() {
}

@Override
public void setWhiteIpStatus(boolean status) {
this.authManager.setWhiteIpStatus(status);
public void enabledWhiteIpList(boolean status) {
this.authManager.enabledWhiteIpList(status);
}

Check warning on line 1589 in hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java#L1588-L1589

Added lines #L1588 - L1589 were not covered by tests

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ public interface AuthManager {

UserWithRole validateUser(String token);

List<String> listWhiteIp();
List<String> listWhiteIPs();

void setWhiteIpList(List<String> whiteIpList);
void setWhiteIPs(List<String> whiteIpList);

boolean getWhiteIpStatus();

void setWhiteIpStatus(boolean status);
void enabledWhiteIpList(boolean status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class StandardAuthManager implements AuthManager {

private List<String> ipWhiteList;

private Boolean whiteIpStatus;
private Boolean ipWhiteListEnabled;

public StandardAuthManager(HugeGraphParams graph) {
E.checkNotNull(graph, "graph");
Expand Down Expand Up @@ -110,7 +110,7 @@ public StandardAuthManager(HugeGraphParams graph) {

this.ipWhiteList = new ArrayList<>();

this.whiteIpStatus = false;
this.ipWhiteListEnabled = false;
}

private <V> Cache<Id, V> cache(String prefix, long capacity,
Expand Down Expand Up @@ -697,23 +697,23 @@ public UserWithRole validateUser(String token) {
}

@Override
public List<String> listWhiteIp() {
public List<String> listWhiteIPs() {
return ipWhiteList;

Check warning on line 701 in hugegraph-core/src/main/java/org/apache/hugegraph/auth/StandardAuthManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/auth/StandardAuthManager.java#L701

Added line #L701 was not covered by tests
}

@Override
public void setWhiteIpList(List<String> ipWhiteList) {
public void setWhiteIPs(List<String> ipWhiteList) {
this.ipWhiteList = ipWhiteList;
}

Check warning on line 707 in hugegraph-core/src/main/java/org/apache/hugegraph/auth/StandardAuthManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/auth/StandardAuthManager.java#L706-L707

Added lines #L706 - L707 were not covered by tests

@Override
public boolean getWhiteIpStatus() {
return this.whiteIpStatus;
return this.ipWhiteListEnabled;

Check warning on line 711 in hugegraph-core/src/main/java/org/apache/hugegraph/auth/StandardAuthManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/auth/StandardAuthManager.java#L711

Added line #L711 was not covered by tests
}

@Override
public void setWhiteIpStatus(boolean status) {
this.whiteIpStatus = status;
public void enabledWhiteIpList(boolean status) {
this.ipWhiteListEnabled = status;
}

Check warning on line 717 in hugegraph-core/src/main/java/org/apache/hugegraph/auth/StandardAuthManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/auth/StandardAuthManager.java#L716-L717

Added lines #L716 - L717 were not covered by tests

/**
Expand Down

0 comments on commit 2b04f68

Please sign in to comment.