Skip to content

Commit a7f9ab8

Browse files
committed
only delete keys if permanently not accessible, additonal error logs
1 parent 92aafec commit a7f9ab8

File tree

7 files changed

+68
-5
lines changed

7 files changed

+68
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [0.48.3]
5+
6+
### Changes
7+
- Additional logging for KeyStore related problems
8+
- Only invalidate KeyStore when keys are permanently inaccessible
9+
410
## [0.48.2]
511

612
### Fixed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ allprojects {
3131
}
3232

3333
project.ext {
34-
sdkVersion='0.48.2'
34+
sdkVersion='0.48.3'
3535
versionCode=1
3636

3737
compileSdkVersion=31

core/src/main/java/io/snabble/sdk/Events.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public EventType getEventType() {
294294
}
295295
}
296296

297-
public static void logErrorEvent(String projectId, String format, Object... args) {
297+
private static Project getUsableProject(String projectId) {
298298
// since we have no error logging without a project, we try to find the project by id
299299
// and if no project is found we just use the first project to at least log it to something
300300
Project project = Snabble.getInstance().getProjectById(projectId);
@@ -305,6 +305,18 @@ public static void logErrorEvent(String projectId, String format, Object... args
305305
}
306306
}
307307

308+
return project;
309+
}
310+
311+
public static void logErrorEvent(String projectId, String format, Object... args) {
312+
Project project = getUsableProject(projectId);
313+
if (project != null) {
314+
project.logErrorEvent(format, args);
315+
}
316+
}
317+
318+
public static void logWarningEvent(String projectId, String format, Object... args) {
319+
Project project = getUsableProject(projectId);
308320
if (project != null) {
309321
project.logErrorEvent(format, args);
310322
}

core/src/main/java/io/snabble/sdk/Project.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,13 @@ public void logErrorEvent(String format, Object... args) {
578578
}
579579
}
580580

581+
public void logEvent(String format, Object... args) {
582+
if (events != null) {
583+
Logger.e(format, args);
584+
events.log(format, args);
585+
}
586+
}
587+
581588
/**
582589
* Sets the customer card number for user identification with the backend.
583590
*/

core/src/main/java/io/snabble/sdk/Snabble.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public void setup(Application app, Config config, final SetupCompletionListener
8787
this.config = config;
8888

8989
Logger.setErrorEventHandler((message, args) -> Events.logErrorEvent(null, message, args));
90+
Logger.setLogEventHandler((message, args) -> Events.logErrorEvent(null, message, args));
9091

9192
if (config.appId == null || config.secret == null) {
9293
setupCompletionListener.onError(Error.CONFIG_PARAMETER_MISSING);

utils/src/main/java/io/snabble/sdk/utils/Logger.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Logger {
1414

1515
private static boolean isEnabled = BuildConfig.DEBUG;
1616
private static ErrorEventHandler errorEventHandler = null;
17+
private static LogEventHandler logEventHandler = null;
1718

1819
public static void setEnabled(boolean enable) {
1920
isEnabled = enable;
@@ -101,4 +102,19 @@ public static void errorEvent(String message, Object... args) {
101102
e.logErrorEvent(message, args);
102103
}
103104
}
105+
106+
public interface LogEventHandler {
107+
void logEvent(String message, Object... args);
108+
}
109+
110+
public static void setLogEventHandler(LogEventHandler e) {
111+
logEventHandler = e;
112+
}
113+
114+
public static void logEvent(String message, Object... args) {
115+
LogEventHandler e = logEventHandler;
116+
if (e != null) {
117+
e.logEvent(message, args);
118+
}
119+
}
104120
}

utils/src/main/java/io/snabble/sdk/utils/security/KeyStoreCipherMarshmallow.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public class KeyStoreCipherMarshmallow extends KeyStoreCipher {
3030
private KeyStore keyStore;
3131
private String alias;
3232
private boolean requireUserAuthentication;
33-
33+
private boolean wasNotAccessible = false;
34+
private boolean wasPermanentlyInvalidated = false;
35+
3436
KeyStoreCipherMarshmallow(String alias, boolean requireUserAuthentication) {
3537
this.alias = alias + "_M";
3638
this.requireUserAuthentication = requireUserAuthentication;
@@ -89,15 +91,34 @@ private boolean isKeyAccessible() {
8991
IvParameterSpec ivParameterSpec = new IvParameterSpec(FIXED_IV);
9092
Key key = keyStore.getKey(alias, null);
9193
c.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
94+
logWasNotAccessible();
9295
return true;
9396
} catch (UserNotAuthenticatedException e) {
97+
logWasNotAccessible();
9498
return true;
95-
} catch (Exception e) {
96-
Logger.errorEvent("KeyStore inaccessible: " + e.getClass().getName() + ": " + e.getMessage());
99+
} catch (KeyPermanentlyInvalidatedException e) {
100+
wasPermanentlyInvalidated = true;
101+
Logger.errorEvent("KeyStore permanently invalidated " + e.getClass().getName() + ": " + e.getMessage());
97102
return false;
103+
} catch (Exception e) {
104+
wasNotAccessible = true;
105+
Logger.logEvent("KeyStore inaccessible: " + e.getClass().getName() + ": " + e.getMessage());
106+
return true;
98107
}
99108
}
109+
110+
private void logWasNotAccessible() {
111+
if (wasNotAccessible) {
112+
Logger.logEvent("KeyStore was not accessible, but is now accessible again");
113+
wasNotAccessible = false;
114+
}
100115

116+
if (wasPermanentlyInvalidated) {
117+
Logger.logEvent("KeyStore was permanently invalidated, but is now recreated");
118+
wasPermanentlyInvalidated = false;
119+
}
120+
}
121+
101122
@Override
102123
public String id() {
103124
try {

0 commit comments

Comments
 (0)