Skip to content

Commit

Permalink
Update the way the client works with a working directory and fix mino…
Browse files Browse the repository at this point in the history
…r bugs
  • Loading branch information
porunov committed Mar 24, 2017
1 parent 1834872 commit f900cd5
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 76 deletions.
30 changes: 27 additions & 3 deletions src/main/java/com/jblur/acme_client/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,33 @@
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;
import com.beust.jcommander.JCommander;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Application {

private static final Logger LOG = LoggerFactory.getLogger(Application.class);

private static final String LOGBACK_CONF = "logback_pattern.xml";

private static final String RESULT_ERROR;

static {
Gson gson = new Gson();
JsonObject jsonObject = new JsonObject();
jsonObject.add("status", gson.toJsonTree("error"));
RESULT_ERROR = gson.toJson(jsonObject);
}

private static void configureLogger(String logDir, String logLevel, String logbackConf) {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
try {
Expand Down Expand Up @@ -56,7 +69,16 @@ private static boolean checkLogLevel(String logLevel) {

public static void main(String[] args) throws URISyntaxException, IOException {
Parameters parameters = new Parameters();
JCommander jCommander = new JCommander(parameters, args);

JCommander jCommander;
try{
jCommander = new JCommander(parameters, args);
}catch (Exception e){
LOG.error("An error occurred during parameters parsing.", e);
System.out.print(RESULT_ERROR);
return;
}

jCommander.setProgramName("java -jar acme_client.jar --command <command>");

if (parameters.isHelp()) {
Expand All @@ -68,13 +90,14 @@ public static void main(String[] args) throws URISyntaxException, IOException {
return;
}

if (!IOManager.isDirectoryExists(parameters.getLogDir())) {
if (!Files.isDirectory(Paths.get(parameters.getLogDir()))) {
LOG.info("Your log dir isn't exists: " + parameters.getLogDir() +
"\nTrying to create the directory for log files");
try {
IOManager.createDirectories(parameters.getLogDir());
Files.createDirectories(Paths.get(parameters.getLogDir()));
} catch (IOException e) {
LOG.error("Can not create log dir: " + parameters.getLogDir() + "\n . Please check permissions", e);
System.out.print(RESULT_ERROR);
return;
}
LOG.info("Log directory " + parameters.getLogDir() + " is successfully created");
Expand All @@ -87,6 +110,7 @@ public static void main(String[] args) throws URISyntaxException, IOException {


if (!parameters.verifyRequirements()) {
System.out.print(RESULT_ERROR);
return;
}

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/jblur/acme_client/CommandExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ private RegistrationManager getRegistrationManager() {
RegistrationCommand registrationCommand = new RegistrationCommand(parameters);
result = executeACMECommand(registrationCommand);
registrationManager = registrationCommand.getRegistrationManager();
if (registrationManager == null) {
LOG.error("Can not get account information.");
}
} catch (AccountKeyNotFoundException e) {
LOG.error("Key not found exception", e);
}
Expand All @@ -53,7 +56,7 @@ private void automaticallyUpdateAgreement(RegistrationManager registrationManage
if (registrationManager != null) {
new UpdateAgreementCommand(parameters, registrationManager).execute();
} else {
LOG.warn("Can not create Registration. Can not update agreement");
LOG.warn("Can not create Registration. Can not update agreement.");
}
} catch (AccountKeyNotFoundException e) {
LOG.warn("Can not update agreement.", e);
Expand All @@ -77,13 +80,20 @@ public void execute() {
case Parameters.COMMAND_RENEW_CERTIFICATE:
registrationManager = getRegistrationManager();
if (registrationManager == null) {
LOG.error("Can not get account information");
System.out.println(result);
return;
}
}

if (parameters.isWithAgreementUpdate()) {
//Strange. After the registration LE return not workable info. You need get registration one more time.
if(parameters.getCommand().equals(Parameters.COMMAND_REGISTER)){
registrationManager = getRegistrationManager();
if (registrationManager == null) {
System.out.println(result);
return;
}
}
automaticallyUpdateAgreement(registrationManager);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jblur/acme_client/IOManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void writeString(String path, String str) throws IOException {
}

public static String readString(String path) throws IOException {
return Files.readAllLines(Paths.get(path)).get(0);
return String.join("", Files.readAllLines(Paths.get(path)));
}

public static boolean isDirectoryExists(String path) {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/jblur/acme_client/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

Expand All @@ -19,7 +21,7 @@ public class Parameters {
public final static String COMMAND_DOWNLOAD_CHALLENGES = "download-challenges";
public final static String COMMAND_VERIFY_DOMAINS = "verify-domains";
public final static String COMMAND_GENERATE_CERTIFICATE = "generate-certificate";
public final static String COMMAND_DOWNLOAD_CERTIFICATES = "download-certificate";
public final static String COMMAND_DOWNLOAD_CERTIFICATES = "download-certificates";
public final static String COMMAND_REVOKE_CERTIFICATE = "revoke-certificate";
public final static String COMMAND_RENEW_CERTIFICATE = "renew-certificate";
public final static String AUTHORIZATION_URI_LIST = "authorization_uri_list";
Expand Down Expand Up @@ -218,15 +220,15 @@ public class Parameters {
private String command;

private boolean checkFile(String path, String errMsg) {
if (!IOManager.isFileExists(path)) {
if (!new File(path).isFile()) {
LOG.error(errMsg);
return false;
}
return true;
}

private boolean checkDir(String path, String errMsg) {
if (!IOManager.isDirectoryExists(path)) {
if (!Files.isDirectory(Paths.get(path))) {
LOG.error(errMsg);
return false;
}
Expand Down Expand Up @@ -254,7 +256,7 @@ private boolean checkCsr() {
}

private boolean checkDomains() {
if (domains.size() == 0) {
if (domains == null || domains.size() == 0) {
LOG.error("You haven't provided any domain name");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.jblur.acme_client.command.authorization;

import com.google.gson.reflect.TypeToken;
import com.jblur.acme_client.IOManager;
import com.jblur.acme_client.Parameters;
import com.jblur.acme_client.command.ACMECommand;
import com.jblur.acme_client.command.AccountKeyNotFoundException;
import com.jblur.acme_client.manager.AuthorizationManager;
import org.shredzone.acme4j.Authorization;
import org.shredzone.acme4j.Certificate;
import org.shredzone.acme4j.challenge.Dns01Challenge;
import org.shredzone.acme4j.challenge.Http01Challenge;
import org.shredzone.acme4j.exception.AcmeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -20,38 +26,75 @@ public abstract class AuthorizationCommand extends ACMECommand {

private static final Logger LOG = LoggerFactory.getLogger(AuthorizationCommand.class);

private static Type listOfAuthorizationLocationsObject = new TypeToken<List<String>>(){}.getType();

public final String AUTHORIZATION_FILE_PATH;

public AuthorizationCommand(Parameters parameters) throws AccountKeyNotFoundException {
super(parameters);
AUTHORIZATION_FILE_PATH = Paths.get(getParameters().getWorkDir(), Parameters.AUTHORIZATION_URI_LIST).toString();
}

public List<Authorization> getNotExpiredAuthorizations() {
List<Authorization> authorizationList = null;
if (IOManager.isFileExists(Paths.get(getParameters().getWorkDir(), Parameters.AUTHORIZATION_URI_LIST).toString())) {
List<Authorization> oldAuthorizationList = new LinkedList<>();

if (!IOManager.isFileExists(AUTHORIZATION_FILE_PATH)) {
return null;
}

List<String> authorizationLocationList;

try {
authorizationLocationList = getGson().fromJson(
IOManager.readString(AUTHORIZATION_FILE_PATH),
listOfAuthorizationLocationsObject);
} catch (Exception e) {
LOG.warn("Your file can not be read. It has a bad structure", e);
return null;
}

for(String authorizationLocation : authorizationLocationList){
try {
List<Authorization> oldAuthorizations = (List<Authorization>) IOManager.deserialize(
Paths.get(getParameters().getWorkDir(), Parameters.AUTHORIZATION_URI_LIST).toString());
authorizationList = new LinkedList<>();
for (Authorization authorization : oldAuthorizations) {
if (authorization.getExpires().getTime() > System.currentTimeMillis()) {
authorization.rebind(getSession());
authorizationList.add(authorization);
}
oldAuthorizationList.add(Authorization.bind(getSession(), new URI(authorizationLocation)));
} catch (URISyntaxException e) {
LOG.warn("URI isn't correct: "+authorizationLocation, e);
}
}

List<Authorization> authorizationList = new LinkedList<>();

for (Authorization authorization : oldAuthorizationList) {
try {
if (authorization.getExpires().getTime() > System.currentTimeMillis()) {
authorizationList.add(authorization);
}
} catch (ClassNotFoundException | IOException e) {
LOG.warn("Your file can not be read. It has a bad structure", e);
} catch (NullPointerException e){
LOG.warn("Found NULL authorization in the file " +
AUTHORIZATION_FILE_PATH, e);
} catch (Exception e) {
LOG.warn("Authorization "+authorization.getLocation().toString()+" can not be rebinded. " +
"Please check internet connectivity or authorization existence.", e);
authorizationList.add(authorization);
}
}

return authorizationList;
}


public boolean writeAuthorizationList(List<Authorization> authorizationList) {
try {
IOManager.serialize(authorizationList,
Paths.get(getParameters().getWorkDir(), Parameters.AUTHORIZATION_URI_LIST).toString());
List<String> authorizationLocationList = new LinkedList<>();

for(Authorization authorization : authorizationList){
authorizationLocationList.add(authorization.getLocation().toString());
}

IOManager.writeString(AUTHORIZATION_FILE_PATH,
getGson().toJson(authorizationLocationList, listOfAuthorizationLocationsObject));
} catch (IOException e) {
LOG.error("Can not write authorization list to file: " + Paths.get(getParameters().getWorkDir(),
Parameters.AUTHORIZATION_URI_LIST).toString() + "\n Please check permissions of the file.", e);
LOG.error("Can not write authorization list to file: " + AUTHORIZATION_FILE_PATH
+ "\n Please check permissions of the file.", e);
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void commandExecution() {
List<Authorization> allAuthrizationsList = getNotExpiredAuthorizations();
if (allAuthrizationsList == null) {
LOG.error("Can not read file: " +
Paths.get(getParameters().getWorkDir(), Parameters.AUTHORIZATION_URI_LIST).toString());
AUTHORIZATION_FILE_PATH);
error = true;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.LoggerFactory;

import java.nio.file.Paths;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -22,12 +23,13 @@ public DownloadChallengesCommand(Parameters parameters) throws AccountKeyNotFoun

@Override
public void commandExecution() {
List<String> failedAuthorizations = new LinkedList<>();
HashSet<String> succeedDomains = new HashSet<>();
HashSet<String> failedDomains = new HashSet<>();

List<Authorization> authorizationList = getNotExpiredAuthorizations();
if (authorizationList == null) {
LOG.error("Can not read file: " +
Paths.get(getParameters().getWorkDir(), Parameters.AUTHORIZATION_URI_LIST).toString());
AUTHORIZATION_FILE_PATH);
error = true;
return;
}
Expand All @@ -36,20 +38,28 @@ public void commandExecution() {
if (getParameters().getDomains() == null || getParameters().getDomains().contains(authorization.getDomain())) {
try {
writeChallengeByAuthorization(new AuthorizationManager(authorization));
if (!succeedDomains.contains(authorization.getDomain()))
succeedDomains.add(authorization.getDomain());
} catch (Exception e) {
LOG.error("Can not get challenges for authorization: " + authorization.getLocation()
+ "\nDomain: " + authorization.getDomain(), e);
failedAuthorizations.add(authorization.getLocation().toString());
if (!failedDomains.contains(authorization.getDomain()))
failedDomains.add(authorization.getDomain());
}
}
}

error = error || !writeAuthorizationList(authorizationList);

if (failedAuthorizations.size() > 0) {
JsonElement failedAuthorizationsJsonElement = getGson().toJsonTree(failedAuthorizations,
new TypeToken<List<String>>() {}.getType());
result.add("failed_authorizations_to_download", failedAuthorizationsJsonElement);
for(String failedDomain:failedDomains)
if(succeedDomains.contains(failedDomain))
failedDomains.remove(failedDomain);

if (failedDomains.size() > 0) {
JsonElement failedDomainsJsonElement = getGson().toJsonTree(failedDomains,
new TypeToken<HashSet<String>>() {}.getType());
result.add("failed_domains_to_download_authorizations", failedDomainsJsonElement);
error=true;
}
}
}
Loading

0 comments on commit f900cd5

Please sign in to comment.