1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.security;
20
21 import fr.cnes.doi.logging.business.JsonMessage;
22 import fr.cnes.doi.settings.Consts;
23 import fr.cnes.doi.settings.DoiSettings;
24 import java.util.Set;
25 import java.util.StringTokenizer;
26 import java.util.concurrent.CopyOnWriteArraySet;
27 import org.apache.logging.log4j.LogManager;
28 import org.apache.logging.log4j.Logger;
29 import org.restlet.Context;
30 import org.restlet.Request;
31 import org.restlet.Response;
32 import org.restlet.data.Status;
33
34
35
36
37
38
39
40
41
42
43
44
45 public class AllowerIP extends org.restlet.routing.Filter {
46
47
48
49
50 private static final Logger LOG = LogManager.getLogger(AllowerIP.class.getName());
51
52
53
54
55 public static final String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";
56
57
58
59 public static final String LOCALHOST_IPV4 = "127.0.0.1";
60
61
62
63
64 private final Set<String> allowedAddresses;
65
66
67
68
69 private final boolean enabled;
70
71
72
73
74
75
76
77
78 public AllowerIP(final Context context, final boolean isEnabledIP) {
79 super(context);
80 LOG.traceEntry();
81 this.enabled = isEnabledIP;
82 this.allowedAddresses = new CopyOnWriteArraySet<>();
83 this.allowedAddresses.add(LOCALHOST_IPV6);
84 this.allowedAddresses.add(LOCALHOST_IPV4);
85 addCustomIP(allowedAddresses);
86 LOG.traceExit();
87 }
88
89
90
91
92
93
94 private void addCustomIP(final Set<String> allowedAddresses) {
95 LOG.traceEntry("Parameter\n\tallowedAddresses: {}", allowedAddresses);
96 final String ips = DoiSettings.getInstance().getString(Consts.ADMIN_IP_ALLOWER);
97 if (ips != null) {
98 final StringTokenizer tokenizer = new StringTokenizer(ips, "|");
99 while (tokenizer.hasMoreTokens()) {
100 final String newIP = tokenizer.nextToken();
101 LOG.info("Adds this IP {} for allowing the access "
102 + "to the amdinistration application", newIP);
103 allowedAddresses.add(newIP);
104 }
105 }
106 LOG.traceExit();
107 }
108
109
110
111
112
113
114
115
116 @Override
117 protected int beforeHandle(final Request request,
118 final Response response) {
119 LOG.traceEntry(new JsonMessage(request));
120 int result = STOP;
121 final String ipClient = request.getClientInfo().getAddress();
122 if (this.enabled && getAllowedAddresses().contains(ipClient)) {
123 result = CONTINUE;
124 } else if (this.enabled && !getAllowedAddresses().contains(ipClient)) {
125 LOG.info("You IP address {} was blocked", ipClient);
126 response.setStatus(Status.CLIENT_ERROR_FORBIDDEN,
127 "Your IP address " + ipClient + " was blocked");
128 } else {
129
130 result = CONTINUE;
131 }
132 return LOG.traceExit(result);
133 }
134
135
136
137
138
139
140 public Set<String> getAllowedAddresses() {
141 LOG.traceEntry();
142 return LOG.traceExit(allowedAddresses);
143 }
144
145
146
147
148
149
150 public boolean isEnabled() {
151 return this.enabled;
152 }
153
154 }