Fixed package path.

This commit is contained in:
Christian P. MOMON 2021-04-22 18:11:27 +02:00
parent 2ff492fc83
commit 90dc98fbc5
9 changed files with 85 additions and 26 deletions

View File

@ -39,7 +39,7 @@ import org.april.logar.util.LineIterator;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import anonymizer.Anonymizer; import fr.devinsy.logar.app.anonymizer.Anonymizer;
import fr.devinsy.logar.app.log.LogUtils; import fr.devinsy.logar.app.log.LogUtils;
/** /**
@ -102,7 +102,7 @@ public final class Logar
{ {
if (file.getName().contains("access")) if (file.getName().contains("access"))
{ {
anonymizer.anonymize(file); anonymizer.anonymizeAccessFile(file);
} }
else if (file.getName().contains("error")) else if (file.getName().contains("error"))
{ {

View File

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see <http://www.gnu.org/licenses/>. * along with Logar. If not, see <http://www.gnu.org/licenses/>.
*/ */
package anonymizer; package fr.devinsy.logar.app.anonymizer;
import java.util.HashMap; import java.util.HashMap;

View File

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see <http://www.gnu.org/licenses/>. * along with Logar. If not, see <http://www.gnu.org/licenses/>.
*/ */
package anonymizer; package fr.devinsy.logar.app.anonymizer;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;

View File

@ -16,16 +16,18 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see <http://www.gnu.org/licenses/>. * along with Logar. If not, see <http://www.gnu.org/licenses/>.
*/ */
package anonymizer; package fr.devinsy.logar.app.anonymizer;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.april.logar.util.LineIterator; import org.april.logar.util.LineIterator;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -36,10 +38,17 @@ import fr.devinsy.logar.app.log.LogUtils;
/** /**
* The Class Anonymizer. * The Class Anonymizer.
*/ */
/**
* @author cpm
*
*/
public final class Anonymizer public final class Anonymizer
{ {
private static Logger logger = LoggerFactory.getLogger(Anonymizer.class); private static Logger logger = LoggerFactory.getLogger(Anonymizer.class);
public static final Pattern IPV4_PATTERN = Pattern.compile("\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}");
public static final Pattern IPV6_PATTERN = Pattern.compile("([0-9a-f]{1,4}:{1,2}){4,7}([0-9a-f]){1,4}", Pattern.CASE_INSENSITIVE);
private AnonMap map; private AnonMap map;
/** /**
@ -50,6 +59,32 @@ public final class Anonymizer
this.map = new AnonMap(); this.map = new AnonMap();
} }
/**
* Anonymize.
*
* @param log
* the log
* @return the log
*/
public Log anonymizeAccess(final Log log)
{
Log result;
String anonIp = this.map.anonymizeIp(log.getIp());
String anonUser = this.map.anonymizeUser(log.getUser());
String line = log.getLine().replace(log.getIp(), anonIp);
if (!log.getUser().equals("-"))
{
line = line.replace(log.getUser(), anonUser);
}
result = new Log(line, log.getDatetime(), anonIp, anonUser);
//
return result;
}
/** /**
* Anonymize. * Anonymize.
* *
@ -58,7 +93,7 @@ public final class Anonymizer
* @param target * @param target
* the target * the target
*/ */
public void anonymize(final File source) public void anonymizeAccessFile(final File source)
{ {
if (source == null) if (source == null)
{ {
@ -68,10 +103,24 @@ public final class Anonymizer
{ {
throw new IllegalArgumentException("Parameter is not a file."); throw new IllegalArgumentException("Parameter is not a file.");
} }
else if (!StringUtils.containsAny(source.getName(), "access", "error"))
{
throw new IllegalArgumentException("File name does not contain 'access' or 'error'.");
}
else else
{ {
System.out.println("== Anonymize log for [" + source.getName() + "]"); System.out.println("== Anonymize log for [" + source.getName() + "]");
boolean isAccessFile;
if (source.getName().contains("access"))
{
isAccessFile = true;
}
else
{
isAccessFile = false;
}
File target; File target;
if (source.getName().endsWith(".log.gz")) if (source.getName().endsWith(".log.gz"))
{ {
@ -92,16 +141,26 @@ public final class Anonymizer
String line = iterator.next(); String line = iterator.next();
try try
{
Log anon;
if (isAccessFile)
{ {
Log log = LogUtils.parseAccessLog(line); Log log = LogUtils.parseAccessLog(line);
// logger.info("line={}", line); // logger.info("line={}", line);
// logger.info("log =[{}][{}][{}]", log.getIp(), // logger.info("log =[{}][{}][{}]", log.getIp(),
// log.getUser(), log.getDatetime()); // log.getUser(), log.getDatetime());
Log anon = anonymize(log); anon = anonymizeAccess(log);
// logger.info("anon=[{}][{}][{}]", anon.getIp(), // logger.info("anon=[{}][{}][{}]", anon.getIp(),
// anon.getUser(), anon.getDatetime()); // anon.getUser(), anon.getDatetime());
// logger.info("anon={}", anon); // logger.info("anon={}", anon);
}
else
{
Log log = LogUtils.parseErrorLog(line);
anon = anonymizeError(log);
}
out.println(anon); out.println(anon);
} }
@ -129,13 +188,13 @@ public final class Anonymizer
} }
/** /**
* Anonymize. * Anonymize error.
* *
* @param log * @param log
* the log * the log
* @return the log * @return the log
*/ */
public Log anonymize(final Log log) public Log anonymizeError(final Log log)
{ {
Log result; Log result;

View File

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see <http://www.gnu.org/licenses/>. * along with Logar. If not, see <http://www.gnu.org/licenses/>.
*/ */
package anonymizer; package fr.devinsy.logar.app.anonymizer;
import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see <http://www.gnu.org/licenses/>. * along with Logar. If not, see <http://www.gnu.org/licenses/>.
*/ */
package anonymizer; package fr.devinsy.logar.app.anonymizer;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see <http://www.gnu.org/licenses/>. * along with Logar. If not, see <http://www.gnu.org/licenses/>.
*/ */
package anonymizer; package fr.devinsy.logar.app.anonymizer;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -54,7 +54,7 @@ public final class LogarCLI
message.appendln("Usage:"); message.appendln("Usage:");
message.appendln(" logar [ -h | -help | --help ]"); message.appendln(" logar [ -h | -help | --help ]");
message.appendln(" logar [ -v | -version | --version ]"); message.appendln(" logar [ -v | -version | --version ]");
message.appendln(" logar anonymize source target anonymize ip and login"); message.appendln(" logar anonymize fileordirectory [maptable] anonymize ip and login");
message.appendln(" logar archive source target archive previous month"); message.appendln(" logar archive source target archive previous month");
message.appendln(" logar checksort fileordirectory check sort of an access log file"); message.appendln(" logar checksort fileordirectory check sort of an access log file");
message.appendln(" logar check fileordirectory census bad format line in log files"); message.appendln(" logar check fileordirectory census bad format line in log files");

View File

@ -26,8 +26,8 @@ import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import anonymizer.Ipv4Generator;
import fr.devinsy.logar.app.LogarException; import fr.devinsy.logar.app.LogarException;
import fr.devinsy.logar.app.anonymizer.Ipv4Generator;
/** /**
* The Class VisitCountersTest. * The Class VisitCountersTest.