Skip to content

Commit

Permalink
Merge pull request #412 from /issues/411-logger
Browse files Browse the repository at this point in the history
Fix #411: Logging of steps create invalid JSON
  • Loading branch information
banterCZ authored Feb 21, 2024
2 parents 109870a + d050581 commit 0ce7714
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public ResultStatusObject execute(PowerAuthStep stepId, PowerAuthVersion version
throw new PowerAuthCmdException();
}

BaseStep step = stepProvider.getStep(stepId, version);
final BaseStep step = stepProvider.getStep(stepId, version);

ResultStatusObject result = step.execute(model.toMap());
final ResultStatusObject result = step.execute(stepLogger, model.toMap());
if (result == null) {
throw new PowerAuthCmdException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,6 @@ public AbstractBaseStep(PowerAuthStep step,
*/
protected abstract ParameterizedTypeReference<R> getResponseTypeReference();

/**
* Executes this step with a given context
*
* @param context Provided context
* @return Result status object, null in case of failure.
* @throws Exception In case of any error.
*/
@Override
public ResultStatusObject execute(Map<String, Object> context) throws Exception {
StepLogger stepLogger = stepLoggerFactory.createStepLogger();
stepLogger.start();
JSONObject jsonObject = execute(stepLogger, context);
stepLogger.close();
if (jsonObject == null) {
return null;
} else {
return ResultStatusObject.fromJsonObject(jsonObject);
}
}

/**
* Execute this step with given logger and context objects.
*
Expand All @@ -155,7 +135,7 @@ public ResultStatusObject execute(Map<String, Object> context) throws Exception
* @return Result status object (with current activation status), null in case of failure.
* @throws Exception In case of a failure.
*/
public final JSONObject execute(StepLogger stepLogger, Map<String, Object> context) throws Exception {
public final ResultStatusObject execute(StepLogger stepLogger, Map<String, Object> context) throws Exception {
if (stepLogger == null) {
stepLogger = DisabledStepLogger.INSTANCE;
}
Expand Down Expand Up @@ -187,7 +167,12 @@ public final JSONObject execute(StepLogger stepLogger, Map<String, Object> conte
return null;
}

return stepContext.getModel().getResultStatusObject();
final JSONObject resultStatusObject = stepContext.getModel().getResultStatusObject();
if (resultStatusObject == null) {
return null;
} else {
return ResultStatusObject.fromJsonObject(resultStatusObject);
}
}

/**
Expand Down Expand Up @@ -231,7 +216,6 @@ public void addEncryptedRequest(StepContext<M, R> stepContext, String applicatio
* @throws Exception when an error during encryption of the request data occurred
*/
public void addEncryptedRequest(StepContext<M, R> stepContext, ClientEncryptor encryptor, byte[] data) throws Exception {
M model = stepContext.getModel();
SimpleSecurityContext securityContext = (SimpleSecurityContext) stepContext.getSecurityContext();
if (securityContext == null) {
stepContext.setSecurityContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.getlime.security.powerauth.lib.cmd.consts.PowerAuthStep;
import io.getlime.security.powerauth.lib.cmd.consts.PowerAuthVersion;
import io.getlime.security.powerauth.lib.cmd.logging.StepLogger;
import io.getlime.security.powerauth.lib.cmd.steps.pojo.ResultStatusObject;

import java.util.List;
Expand All @@ -34,11 +35,12 @@ public interface BaseStep {
/**
* Execute this step with given context objects.
*
* @param stepLogger Step logger.
* @param context Context objects.
* @return Result status object (with current activation status), null in case of failure.
* @throws Exception In case of a failure.
*/
ResultStatusObject execute(Map<String, Object> context) throws Exception;
ResultStatusObject execute(StepLogger stepLogger, Map<String, Object> context) throws Exception;

/**
* @return Corresponding PowerAuth step
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ public void setServerPublicKey(String serverPublicKey) {
*/
@JsonIgnore
public SecretKey getSignatureBiometryKeyObject() {
String signatureBiometryKey = (String) jsonObject.get("signatureBiometryKey");
final String signatureBiometryKey = (String) jsonObject.get("signatureBiometryKey");
if (signatureBiometryKey == null) {
return null;
}
return KEY_CONVERTOR.convertBytesToSharedSecretKey(Base64.getDecoder().decode(signatureBiometryKey));
}

Expand Down Expand Up @@ -290,7 +293,10 @@ public void setSignatureKnowledgeKeySalt(String signatureKnowledgeKeySalt) {
*/
@JsonIgnore
public SecretKey getSignaturePossessionKeyObject() {
String signaturePossessionKey = (String) jsonObject.get("signaturePossessionKey");
final String signaturePossessionKey = (String) jsonObject.get("signaturePossessionKey");
if (signaturePossessionKey == null) {
return null;
}
return KEY_CONVERTOR.convertBytesToSharedSecretKey(Base64.getDecoder().decode(signaturePossessionKey));
}

Expand Down Expand Up @@ -324,7 +330,10 @@ public void setSignaturePossessionKey(String signaturePossessionKey) {
*/
@JsonIgnore
public SecretKey getTransportMasterKeyObject() {
String transportMasterKey = (String) jsonObject.get("transportMasterKey");
final String transportMasterKey = (String) jsonObject.get("transportMasterKey");
if (transportMasterKey == null) {
return null;
}
return KEY_CONVERTOR.convertBytesToSharedSecretKey(Base64.getDecoder().decode(transportMasterKey));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static byte[] readFileBytes(StepLogger stepLogger,
"Empty " + fileDescription + " file",
"File not provided, assuming empty data.",
"WARNING",
filePath
null
);
return new byte[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,8 @@ public static void main(String[] args) {

stepExecutionService.execute(powerAuthStep, version, model);
}
case ACTIVATION_CREATE, ACTIVATION_PREPARE -> {
if (powerAuthStep.equals(PowerAuthStep.ACTIVATION_PREPARE)) {
System.err.println("The 'prepare' step name is deprecated, use the 'create' step name instead");
powerAuthStep = PowerAuthStep.ACTIVATION_CREATE;
}

String customAttributesFileName = cmd.getOptionValue("C");
Map<String, Object> customAttributes =
FileUtil.readDataFromFile(stepLogger, customAttributesFileName, HashMap.class, "custom-attributes", "custom attributes");
case ACTIVATION_CREATE -> {
final Map<String, Object> customAttributes = getCustomAttributes(cmd, stepLogger);

PrepareActivationStepModel model = new PrepareActivationStepModel();
model.setActivationCode(cmd.getOptionValue("a"));
Expand Down Expand Up @@ -387,9 +380,7 @@ public static void main(String[] args) {
Map<String, String> identityAttributes =
FileUtil.readDataFromFile(stepLogger, identityAttributesFileName, HashMap.class, "identity-attributes", "identity attributes");

String customAttributesFileName = cmd.getOptionValue("C");
Map<String, Object> customAttributes =
FileUtil.readDataFromFile(stepLogger, customAttributesFileName, HashMap.class, "custom-attributes", "custom attributes");
final Map<String, Object> customAttributes = getCustomAttributes(cmd, stepLogger);

CreateActivationStepModel model = new CreateActivationStepModel();
model.setActivationName(ConfigurationUtil.getApplicationName(clientConfigObject));
Expand Down Expand Up @@ -499,9 +490,7 @@ public static void main(String[] args) {
Map<String, String> identityAttributes =
FileUtil.readDataFromFile(stepLogger, identityAttributesFileName, HashMap.class, "identity-attributes", "identity attributes");

String customAttributesFileName = cmd.getOptionValue("C");
Map<String, Object> customAttributes =
FileUtil.readDataFromFile(stepLogger, customAttributesFileName, HashMap.class, "custom-attributes", "custom attributes");
final Map<String, Object> customAttributes = getCustomAttributes(cmd, stepLogger);

ActivationRecoveryStepModel model = new ActivationRecoveryStepModel();
model.setActivationName(ConfigurationUtil.getApplicationName(clientConfigObject));
Expand Down Expand Up @@ -569,6 +558,18 @@ public static void main(String[] args) {

}

@SuppressWarnings("unchecked")
private static Map<String, Object> getCustomAttributes(CommandLine cmd, StepLogger stepLogger) throws Exception {
final String customAttributesFileName = cmd.getOptionValue("C");
final Map<String, Object> customAttributes;
if (customAttributesFileName != null) {
customAttributes = FileUtil.readDataFromFile(stepLogger, customAttributesFileName, HashMap.class, "custom-attributes", "custom attributes");
} else {
customAttributes = Collections.emptyMap();
}
return customAttributes;
}

private static void printPowerAuthStepsHelp(StepProvider stepProvider) {
System.out.println("Available PowerAuth steps and supported versions.\n");
System.out.printf("%-22s%s%n", "PowerAuth step", "Supported versions");
Expand Down

0 comments on commit 0ce7714

Please sign in to comment.