[java-opensaml] branch master updated: OSJ-246 - Consider alternate serialization format for client storage
Scott Cantor
cantor.2 at osu.edu
Mon Jun 8 23:15:08 UTC 2020
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch master
in repository java-opensaml.
View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=2f0218ce05014e4ce4b6a0ecf5e163595eea4ea4
The following commit(s) were added to refs/heads/master by this push:
new 2f0218ce0 OSJ-246 - Consider alternate serialization format for client storage
2f0218ce0 is described below
commit 2f0218ce05014e4ce4b6a0ecf5e163595eea4ea4
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Mon Jun 8 19:15:06 2020 -0400
OSJ-246 - Consider alternate serialization format for client storage
https://issues.shibboleth.net/jira/browse/OSJ-246
---
.../client/AbstractClientStorageServiceStore.java | 118 ++++++++++
.../storage/impl/client/ClientStorageService.java | 239 +++------------------
.../impl/client/ClientStorageServiceStore.java | 109 ++++++++++
.../impl/client/JSONClientStorageServiceStore.java | 181 ++++++++++++++++
.../impl/client/XMLClientStorageServiceStore.java | 231 ++++++++++++++++++++
5 files changed, 672 insertions(+), 206 deletions(-)
diff --git a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/AbstractClientStorageServiceStore.java b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/AbstractClientStorageServiceStore.java
new file mode 100644
index 000000000..0d2f2e623
--- /dev/null
+++ b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/AbstractClientStorageServiceStore.java
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements. See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.opensaml.storage.impl.client;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.opensaml.storage.MutableStorageRecord;
+import org.opensaml.storage.impl.client.ClientStorageService.ClientStorageSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.Live;
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+
+/**
+ * Base class for the storage and reconstitution of data for a {@link ClientStorageService}.
+ *
+ * @since 4.1.0
+ */
+public abstract class AbstractClientStorageServiceStore implements ClientStorageServiceStore {
+
+ /** Class logger. */
+ @Nonnull private final Logger log = LoggerFactory.getLogger(AbstractClientStorageServiceStore.class);
+
+ /** The underlying map of data records. */
+ @Nonnull @NonnullElements private final Map<String, Map<String, MutableStorageRecord<?>>> contextMap;
+
+ /** Data source. */
+ @Nonnull private ClientStorageSource source;
+
+ /** Dirty bit. */
+ private boolean dirty;
+
+ /**
+ * Reconstitute stored data.
+ */
+ AbstractClientStorageServiceStore() {
+ contextMap = new HashMap<>();
+ }
+
+ /** {@inheritDoc} */
+ @Nonnull public ClientStorageSource getSource() {
+ return source;
+ }
+
+ /** {@inheritDoc} */
+ public boolean isDirty() {
+ return dirty;
+ }
+
+ /**
+ * Set the dirty bit for the current data.
+ *
+ * @param flag dirty bit to set
+ */
+ public void setDirty(final boolean flag) {
+ dirty = flag;
+ }
+
+ /** {@inheritDoc} */
+ @Nonnull @NonnullElements @Live public Map<String,Map<String,MutableStorageRecord<?>>> getContextMap() {
+ return contextMap;
+ }
+
+ /** {@inheritDoc} */
+ public void load(@Nullable @NotEmpty final String raw, @Nonnull final ClientStorageSource src) {
+
+ contextMap.clear();
+ source = Constraint.isNotNull(src, "ClientStorageSource cannot be null");
+
+ if (raw != null) {
+ try {
+ doLoad(raw);
+ } catch (final IOException e) {
+ contextMap.clear();
+ // Setting this should force corrupt data in the client to be overwritten.
+ setDirty(true);
+ }
+ }
+ }
+
+
+ /**
+ * Reconstitute stored data.
+ *
+ * @param raw serialized data to load
+ *
+ * @throws IOException if an error occurs
+ */
+ public abstract void doLoad(@Nullable @NotEmpty final String raw) throws IOException;
+
+ /** {@inheritDoc} */
+ @Nullable public abstract ClientStorageServiceOperation save(@Nonnull final ClientStorageService storageService)
+ throws IOException;
+
+}
\ No newline at end of file
diff --git a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageService.java b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageService.java
index c243f6107..4781dacdc 100644
--- a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageService.java
+++ b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageService.java
@@ -18,11 +18,8 @@
package org.opensaml.storage.impl.client;
import java.io.IOException;
-import java.io.StringReader;
-import java.io.StringWriter;
import java.security.KeyException;
import java.time.Duration;
-import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;
@@ -32,13 +29,6 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
-import javax.json.Json;
-import javax.json.JsonException;
-import javax.json.JsonObject;
-import javax.json.JsonReader;
-import javax.json.JsonStructure;
-import javax.json.JsonValue;
-import javax.json.stream.JsonGenerator;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
@@ -66,6 +56,8 @@ import net.shibboleth.utilities.java.support.security.DataSealerKeyStrategy;
import org.opensaml.storage.AbstractMapBackedStorageService;
import org.opensaml.storage.MutableStorageRecord;
import org.opensaml.storage.StorageCapabilitiesEx;
+import org.opensaml.storage.impl.client.ClientStorageServiceStore.Factory;
+import org.opensaml.storage.impl.client.JSONClientStorageServiceStore.JSONClientStorageServiceStoreFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -76,7 +68,7 @@ import org.slf4j.LoggerFactory;
* <p>The data for this service is managed in a {@link ClientStorageServiceStore} object, which must
* be created by some operation within the container for this implementation to function. Actual
* load/store of the data to/from that object is driven via companion classes. The serialization
- * of data via JSON is inside the storage object class, but the encryption/decryption is here.</p>
+ * of data is inside the storage object class, but the encryption/decryption is here.</p>
*/
public class ClientStorageService extends AbstractMapBackedStorageService implements Filter, StorageCapabilitiesEx {
@@ -120,6 +112,9 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
/** KeyStrategy enabling us to detect whether data has been sealed with an older key. */
@Nullable private DataSealerKeyStrategy keyStrategy;
+
+ /** Factory for backing store. */
+ @Nonnull private Factory storeFactory;
/** Constructor. */
public ClientStorageService() {
@@ -127,6 +122,7 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
capabilityMap = new HashMap<>(2);
capabilityMap.put(ClientStorageSource.COOKIE, 4096);
capabilityMap.put(ClientStorageSource.HTML_LOCAL_STORAGE, 1024 * 1024);
+ storeFactory = new JSONClientStorageServiceStoreFactory();
}
/** {@inheritDoc} */
@@ -155,15 +151,12 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
}
}
- /** {@inheritDoc} */
// Checkstyle: CyclomaticComplexity ON
/** {@inheritDoc} */
public boolean isServerSide() {
return false;
}
-
- /** {@inheritDoc} */
/** {@inheritDoc} */
public boolean isClustered() {
@@ -222,6 +215,15 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
storageName = Constraint.isNotNull(StringSupport.trimOrNull(name), "Storage name cannot be null or empty");
}
+ /**
+ * Get the {@link DataSealer} to use for data security.
+ *
+ * @return {@link DataSealer} to use for data security
+ */
+ @NonnullAfterInit public DataSealer getDataSealer() {
+ return dataSealer;
+ }
+
/**
* Set the {@link DataSealer} to use for data security.
*
@@ -243,6 +245,17 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
keyStrategy = strategy;
}
+
+ /**
+ * Set the backing store {@link Factory} to use.
+ *
+ * @param factory factory to use
+ */
+ public void setClientStorageServiceStoreFactory(@Nonnull final Factory factory) {
+ ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+
+ storeFactory = Constraint.isNotNull(factory, "Factory cannot be null");
+ }
/** {@inheritDoc} */
@Override
@@ -281,8 +294,6 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
public long getValueSize() {
return getContextSize();
}
-
- // Checkstyle: CyclomaticComplexity ON
/** {@inheritDoc} */
@Override
@@ -431,7 +442,7 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
log.trace("{} Data after decryption: {}", getLogPrefix(), decrypted);
- storageObject = new ClientStorageServiceStore(decrypted, source);
+ storageObject = storeFactory.load(decrypted, source);
if (keyStrategy != null) {
try {
@@ -447,16 +458,16 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
log.debug("{} Successfully decrypted and loaded storage state from client", getLogPrefix());
} catch (final DataExpiredException e) {
log.debug("{} Secured data or key has expired", getLogPrefix());
- storageObject = new ClientStorageServiceStore(null, source);
+ storageObject = storeFactory.load(null, source);
storageObject.setDirty(true);
} catch (final DataSealerException e) {
log.error("{} Exception unwrapping secured data", getLogPrefix(), e);
- storageObject = new ClientStorageServiceStore(null, source);
+ storageObject = storeFactory.load(null, source);
storageObject.setDirty(true);
}
} else {
log.trace("{} Initializing empty storage state into session", getLogPrefix());
- storageObject = new ClientStorageServiceStore(null, source);
+ storageObject = storeFactory.load(null, source);
}
// The object should be loaded, and marked "clean", or in the event of just about any failure
@@ -500,7 +511,7 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
}
try {
- return ((ClientStorageServiceStore) object).save();
+ return ((ClientStorageServiceStore) object).save(this);
} catch (final IOException e) {
log.error("{} Error while serializing storage data", getLogPrefix(), e);
return null;
@@ -515,192 +526,8 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
*
* @return logging prefix
*/
- @Nonnull @NotEmpty private String getLogPrefix() {
+ @Nonnull @NotEmpty String getLogPrefix() {
return "StorageService " + getId() + ":";
}
-
- /**
- * Implements a session-bound backing store and locking mechanism for the {@link ClientStorageService}.
- */
- public class ClientStorageServiceStore {
-
- /** The underlying map of data records. */
- @Nonnull @NonnullElements private final Map<String, Map<String, MutableStorageRecord<?>>> contextMap;
- /** Data source. */
- @Nonnull private final ClientStorageSource source;
-
- /** Dirty bit. */
- private boolean dirty;
-
- /**
- * Reconstitute stored data.
- *
- * <p>The dirty bit is set based on the result. If successful, the bit is cleared,
- * but if an error occurs, it will be set.</p>
- *
- * @param raw serialized data to load
- * @param src data source
- */
- ClientStorageServiceStore(@Nullable @NotEmpty final String raw, @Nonnull final ClientStorageSource src) {
- contextMap = new HashMap<>();
- source = Constraint.isNotNull(src, "Data source cannot be null");
-
- if (raw == null) {
- return;
- }
-
- try {
- final JsonReader reader = Json.createReader(new StringReader(raw));
- final JsonStructure st = reader.read();
- if (!(st instanceof JsonObject)) {
- throw new JsonException("Found invalid data structure while parsing context map");
- }
- final JsonObject obj = (JsonObject) st;
-
- for (final Map.Entry<String,JsonValue> context : obj.entrySet()) {
- if (context.getValue().getValueType() != JsonValue.ValueType.OBJECT) {
- throw new JsonException("Found invalid data structure while parsing context map");
- }
-
- // Create new context if necessary.
- Map<String,MutableStorageRecord<?>> dataMap = contextMap.get(context.getKey());
- if (dataMap == null) {
- dataMap = new HashMap<>();
- contextMap.put(context.getKey(), dataMap);
- }
-
- final JsonObject contextRecords = (JsonObject) context.getValue();
- for (final Map.Entry<String,JsonValue> record : contextRecords.entrySet()) {
-
- final JsonObject fields = (JsonObject) record.getValue();
- Long exp = null;
- if (fields.containsKey("x")) {
- exp = fields.getJsonNumber("x").longValueExact();
- }
-
- dataMap.put(record.getKey(), new MutableStorageRecord<>(fields.getString("v"), exp));
- }
- }
- setDirty(false);
- } catch (final NullPointerException | ClassCastException | ArithmeticException | JsonException e) {
- contextMap.clear();
- // Setting this should force corrupt data in the client to be overwritten.
- setDirty(true);
- log.error("{} Found invalid data structure while parsing context map", getLogPrefix(), e);
- }
- }
-
- /**
- * Get the map of contexts to manipulate during operations.
- *
- * @return map of contexts to manipulate
- */
- @Nonnull @NonnullElements @Live Map<String, Map<String, MutableStorageRecord<?>>> getContextMap() {
- return contextMap;
- }
-
- /**
- * Get the data source.
- *
- * @return data source
- */
- @Nonnull public ClientStorageSource getSource() {
- return source;
- }
-
- /**
- * Get the dirty bit for the current data.
- *
- * @return status of dirty bit
- */
- boolean isDirty() {
- return dirty;
- }
-
- /**
- * Set the dirty bit for the current data.
- *
- * @param flag dirty bit to set
- */
- void setDirty(final boolean flag) {
- dirty = flag;
- }
-
-// Checkstyle: CyclomaticComplexity OFF
- /**
- * Serialize current state of stored data into a storage operation.
- *
- * @return the operation, or a null if the data has not been modified since loading or saving
- *
- * @throws IOException if an error occurs
- */
- @Nullable ClientStorageServiceOperation save() throws IOException {
-
- if (!isDirty()) {
- log.trace("{} Storage state has not been modified, save operation skipped", getLogPrefix());
- return null;
- }
-
- if (contextMap.isEmpty()) {
- log.trace("{} Data is empty", getLogPrefix());
- return new ClientStorageServiceOperation(getId(), getStorageName(), null, source);
- }
-
- long exp = 0L;
- final long now = System.currentTimeMillis();
- boolean empty = true;
-
- try {
- final StringWriter sink = new StringWriter(128);
- final JsonGenerator gen = Json.createGenerator(sink);
-
- gen.writeStartObject();
- for (final Map.Entry<String,Map<String, MutableStorageRecord<?>>> context : contextMap.entrySet()) {
- if (!context.getValue().isEmpty()) {
- gen.writeStartObject(context.getKey());
- for (final Map.Entry<String,MutableStorageRecord<?>> entry : context.getValue().entrySet()) {
- final MutableStorageRecord<?> record = entry.getValue();
- final Long recexp = record.getExpiration();
- if (recexp == null || recexp > now) {
- empty = false;
- gen.writeStartObject(entry.getKey())
- .write("v", record.getValue());
- if (recexp != null) {
- gen.write("x", record.getExpiration());
- exp = Math.max(exp, recexp);
- }
- gen.writeEnd();
- }
- }
- gen.writeEnd();
- }
- }
- gen.writeEnd().close();
-
- if (empty) {
- log.trace("{} Data is empty", getLogPrefix());
- return new ClientStorageServiceOperation(getId(), getStorageName(), null, source);
- }
-
- final String raw = sink.toString();
-
- log.trace("{} Size of data before encryption is {}", getLogPrefix(), raw.length());
- log.trace("{} Data before encryption is {}", getLogPrefix(), raw);
- try {
- final String wrapped = dataSealer.wrap(raw,
- exp > 0 ? Instant.ofEpochMilli(exp) : Instant.now().plus(Duration.ofDays(1)));
- log.trace("{} Size of data after encryption is {}", getLogPrefix(), wrapped.length());
- setDirty(false);
- return new ClientStorageServiceOperation(getId(), getStorageName(), wrapped, source);
- } catch (final DataSealerException e) {
- throw new IOException(e);
- }
- } catch (final JsonException e) {
- throw new IOException(e);
- }
- }
- }
-// Checkstyle: CyclomaticComplexity ON
-
}
\ No newline at end of file
diff --git a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageServiceStore.java b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageServiceStore.java
new file mode 100644
index 000000000..6494519cb
--- /dev/null
+++ b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageServiceStore.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements. See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.opensaml.storage.impl.client;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.opensaml.storage.MutableStorageRecord;
+import org.opensaml.storage.impl.client.ClientStorageService.ClientStorageSource;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.Live;
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+
+/**
+ * Abstraction for the storage and reconstitution of data for a {@link ClientStorageService}.
+ *
+ * @since 4.1.0
+ */
+public interface ClientStorageServiceStore {
+
+ /**
+ * Get the data source.
+ *
+ * @return data source
+ */
+ @Nonnull ClientStorageSource getSource();
+
+ /**
+ * Get the dirty bit for the current data.
+ *
+ * @return status of dirty bit
+ */
+ boolean isDirty();
+
+ /**
+ * Set the dirty bit for the current data.
+ *
+ * @param flag dirty bit to set
+ */
+ void setDirty(final boolean flag);
+
+ /**
+ * Get the map of contexts to manipulate during operations.
+ *
+ * @return map of contexts to manipulate
+ */
+ @Nonnull @NonnullElements @Live Map<String,Map<String,MutableStorageRecord<?>>> getContextMap();
+
+ /**
+ * Reconstitute stored data.
+ *
+ * <p>The dirty bit is set based on the result. If successful, the bit is cleared,
+ * but if an error occurs, it will be set.</p>
+ *
+ * <p>By design this method should not throw under any non-catastrophic conditions.</p>
+ *
+ * @param raw serialized data to load
+ * @param src storage source
+ */
+ void load(@Nullable @NotEmpty final String raw, @Nonnull final ClientStorageSource src);
+
+ /**
+ * Serialize current state of stored data into a storage operation.
+ *
+ * @param storageService storage service
+ *
+ * @return the operation, or a null if the data has not been modified since loading or saving
+ *
+ * @throws IOException if an error occurs
+ */
+ @Nullable ClientStorageServiceOperation save(@Nonnull final ClientStorageService storageService) throws IOException;
+
+ /**
+ * A factory for producing new {@link ClientStorageServiceStore} instances.
+ */
+ interface Factory {
+
+ /**
+ * Load raw data into a new {@link ClientStorageServiceStore} instance.
+ *
+ * @param raw data to load
+ * @param src data source
+ *
+ * @return new store instance
+ */
+ @Nonnull ClientStorageServiceStore load(@Nullable @NotEmpty final String raw,
+ @Nonnull final ClientStorageSource src);
+ }
+
+}
\ No newline at end of file
diff --git a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/JSONClientStorageServiceStore.java b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/JSONClientStorageServiceStore.java
new file mode 100644
index 000000000..17c828219
--- /dev/null
+++ b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/JSONClientStorageServiceStore.java
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements. See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.opensaml.storage.impl.client;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.json.Json;
+import javax.json.JsonException;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
+import javax.json.JsonStructure;
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerator;
+
+import org.opensaml.storage.MutableStorageRecord;
+import org.opensaml.storage.impl.client.ClientStorageService.ClientStorageSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.security.DataSealerException;
+
+/**
+ * JSON-based storage for {@link ClientStorageService}.
+ */
+public class JSONClientStorageServiceStore extends AbstractClientStorageServiceStore {
+
+ /** Class logger. */
+ @Nonnull private final Logger log = LoggerFactory.getLogger(JSONClientStorageServiceStore.class);
+
+ /** {@inheritDoc} */
+ public void doLoad(@Nullable @NotEmpty final String raw) throws IOException {
+ try {
+ final JsonReader reader = Json.createReader(new StringReader(raw));
+ final JsonStructure st = reader.read();
+ if (!(st instanceof JsonObject)) {
+ throw new JsonException("Found invalid data structure while parsing context map");
+ }
+ final JsonObject obj = (JsonObject) st;
+
+ for (final Map.Entry<String,JsonValue> context : obj.entrySet()) {
+ if (context.getValue().getValueType() != JsonValue.ValueType.OBJECT) {
+ throw new JsonException("Found invalid data structure while parsing context map");
+ }
+
+ // Create new context if necessary.
+ Map<String,MutableStorageRecord<?>> dataMap = getContextMap().get(context.getKey());
+ if (dataMap == null) {
+ dataMap = new HashMap<>();
+ getContextMap().put(context.getKey(), dataMap);
+ }
+
+ final JsonObject contextRecords = (JsonObject) context.getValue();
+ for (final Map.Entry<String,JsonValue> record : contextRecords.entrySet()) {
+
+ final JsonObject fields = (JsonObject) record.getValue();
+ Long exp = null;
+ if (fields.containsKey("x")) {
+ exp = fields.getJsonNumber("x").longValueExact();
+ }
+
+ dataMap.put(record.getKey(), new MutableStorageRecord<>(fields.getString("v"), exp));
+ }
+ }
+ setDirty(false);
+ } catch (final NullPointerException | ClassCastException | ArithmeticException | JsonException e) {
+ log.error("Found invalid data structure while parsing context map", e);
+ throw new IOException(e);
+ }
+ }
+
+//Checkstyle: CyclomaticComplexity OFF
+ /** {@inheritDoc} */
+ @Nullable public ClientStorageServiceOperation save(@Nonnull final ClientStorageService storageService)
+ throws IOException {
+
+ if (!isDirty()) {
+ log.trace("{} Storage state has not been modified, save operation skipped", storageService.getLogPrefix());
+ return null;
+ }
+
+ if (getContextMap().isEmpty()) {
+ log.trace("{} Data is empty", storageService.getLogPrefix());
+ return new ClientStorageServiceOperation(storageService.getId(), storageService.getStorageName(), null,
+ getSource());
+ }
+
+ long exp = 0L;
+ final long now = System.currentTimeMillis();
+ boolean empty = true;
+
+ try {
+ final StringWriter sink = new StringWriter(128);
+ final JsonGenerator gen = Json.createGenerator(sink);
+
+ gen.writeStartObject();
+ for (final Map.Entry<String,Map<String, MutableStorageRecord<?>>> context
+ : getContextMap().entrySet()) {
+ if (!context.getValue().isEmpty()) {
+ gen.writeStartObject(context.getKey());
+ for (final Map.Entry<String,MutableStorageRecord<?>> entry : context.getValue().entrySet()) {
+ final MutableStorageRecord<?> record = entry.getValue();
+ final Long recexp = record.getExpiration();
+ if (recexp == null || recexp > now) {
+ empty = false;
+ gen.writeStartObject(entry.getKey())
+ .write("v", record.getValue());
+ if (recexp != null) {
+ gen.write("x", record.getExpiration());
+ exp = Math.max(exp, recexp);
+ }
+ gen.writeEnd();
+ }
+ }
+ gen.writeEnd();
+ }
+ }
+ gen.writeEnd().close();
+
+ if (empty) {
+ log.trace("{} Data is empty", storageService.getLogPrefix());
+ return new ClientStorageServiceOperation(storageService.getId(), storageService.getStorageName(), null,
+ getSource());
+ }
+
+ final String raw = sink.toString();
+
+ log.trace("{} Size of data before encryption is {}", storageService.getLogPrefix(), raw.length());
+ log.trace("{} Data before encryption is {}", storageService.getLogPrefix(), raw);
+ try {
+ final String wrapped = storageService.getDataSealer().wrap(raw,
+ exp > 0 ? Instant.ofEpochMilli(exp) : Instant.now().plus(Duration.ofDays(1)));
+ log.trace("{} Size of data after encryption is {}", storageService.getLogPrefix(), wrapped.length());
+ setDirty(false);
+ return new ClientStorageServiceOperation(storageService.getId(), storageService.getStorageName(),
+ wrapped, getSource());
+ } catch (final DataSealerException e) {
+ throw new IOException(e);
+ }
+ } catch (final JsonException e) {
+ throw new IOException(e);
+ }
+ }
+//Checkstyle: CyclomaticComplexity ON
+
+ /** Factory for JSON-backed store. */
+ public static class JSONClientStorageServiceStoreFactory implements Factory {
+
+ /** {@inheritDoc} */
+ @Nonnull public ClientStorageServiceStore load(@Nullable @NotEmpty final String raw,
+ @Nonnull final ClientStorageSource src) {
+ final ClientStorageServiceStore store = new JSONClientStorageServiceStore();
+ store.load(raw, src);
+ return store;
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/XMLClientStorageServiceStore.java b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/XMLClientStorageServiceStore.java
new file mode 100644
index 000000000..1eedaf2ae
--- /dev/null
+++ b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/XMLClientStorageServiceStore.java
@@ -0,0 +1,231 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements. See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.opensaml.storage.impl.client;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.opensaml.storage.MutableStorageRecord;
+import org.opensaml.storage.impl.client.ClientStorageService.ClientStorageSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import com.google.common.base.Strings;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.component.AbstractInitializableComponent;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+import net.shibboleth.utilities.java.support.security.DataSealerException;
+import net.shibboleth.utilities.java.support.xml.BasicParserPool;
+import net.shibboleth.utilities.java.support.xml.ElementSupport;
+import net.shibboleth.utilities.java.support.xml.ParserPool;
+import net.shibboleth.utilities.java.support.xml.SerializeSupport;
+import net.shibboleth.utilities.java.support.xml.XMLParserException;
+
+/**
+ * XML-based storage for {@link ClientStorageService}.
+ */
+public class XMLClientStorageServiceStore extends AbstractClientStorageServiceStore {
+
+ /** Class logger. */
+ @Nonnull private final Logger log = LoggerFactory.getLogger(XMLClientStorageServiceStore.class);
+
+ /** Parser machinery. */
+ @Nonnull private final ParserPool parserPool;
+
+ /**
+ * Constructor.
+ *
+ * @param pool {@link ParserPool} to use
+ */
+ public XMLClientStorageServiceStore(@Nonnull final ParserPool pool) {
+ parserPool = Constraint.isNotNull(pool, "ParserPool cannot be null");
+ }
+
+ //Checkstyle: CyclomaticComplexity|MethodLength OFF
+ /** {@inheritDoc} */
+ public void doLoad(@Nullable @NotEmpty final String raw) throws IOException {
+ try {
+ final Document doc = parserPool.parse(new StringReader(raw));
+ final Element rootElement = doc != null ? doc.getDocumentElement() : null;
+
+ if (rootElement == null || !"map".equals(rootElement.getNodeName())) {
+ throw new IOException("Found invalid data structure while parsing context map");
+ }
+
+ Element contextElement = ElementSupport.getFirstChildElement(rootElement);
+ while (contextElement != null && "c".equals(contextElement.getNodeName())) {
+
+ final String contextId = contextElement.getAttribute("id");
+ if (!Strings.isNullOrEmpty(contextId)) {
+ // Create new context if necessary.
+ Map<String,MutableStorageRecord<?>> dataMap = getContextMap().get(contextId);
+ if (dataMap == null) {
+ dataMap = new HashMap<>();
+ getContextMap().put(contextId, dataMap);
+ }
+
+ Element keyElement = ElementSupport.getFirstChildElement(contextElement);
+ while (keyElement != null && "k".equals(keyElement.getNodeName())) {
+ final String keyId = keyElement.getAttribute("id");
+ if (!Strings.isNullOrEmpty(keyId)) {
+
+ Long exp = null;
+ if (keyElement.hasAttribute("x")) {
+ exp = Long.valueOf(keyElement.getAttribute("x"));
+ }
+
+ dataMap.put(keyId, new MutableStorageRecord<>(keyElement.getTextContent(), exp));
+ }
+
+ keyElement = ElementSupport.getNextSiblingElement(keyElement);
+ }
+ }
+
+ contextElement = ElementSupport.getNextSiblingElement(contextElement);
+ }
+ setDirty(false);
+ } catch (final XMLParserException e) {
+ log.error("Found invalid data structure while parsing context map", e);
+ throw new IOException(e);
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Nullable public ClientStorageServiceOperation save(@Nonnull final ClientStorageService storageService)
+ throws IOException {
+
+ if (!isDirty()) {
+ log.trace("{} Storage state has not been modified, save operation skipped", storageService.getLogPrefix());
+ return null;
+ }
+
+ if (getContextMap().isEmpty()) {
+ log.trace("{} Data is empty", storageService.getLogPrefix());
+ return new ClientStorageServiceOperation(storageService.getId(), storageService.getStorageName(), null,
+ getSource());
+ }
+
+ long exp = 0L;
+ final long now = System.currentTimeMillis();
+ boolean empty = true;
+
+ try {
+ final Document doc = parserPool.newDocument();
+ final Element rootElement = doc.createElement("map");
+
+ for (final Map.Entry<String,Map<String, MutableStorageRecord<?>>> context
+ : getContextMap().entrySet()) {
+ if (!context.getValue().isEmpty()) {
+ final Element contextElement = doc.createElement("c");
+ contextElement.setAttribute("id", context.getKey());
+
+ for (final Map.Entry<String,MutableStorageRecord<?>> entry : context.getValue().entrySet()) {
+ final MutableStorageRecord<?> record = entry.getValue();
+ final Long recexp = record.getExpiration();
+ if (recexp == null || recexp > now) {
+ empty = false;
+ final Element keyElement = doc.createElement("k");
+ keyElement.setAttribute("id", entry.getKey());
+ keyElement.setTextContent(record.getValue());
+
+ if (recexp != null) {
+ keyElement.setAttribute("x", recexp.toString());
+ exp = Math.max(exp, recexp);
+ }
+ contextElement.appendChild(keyElement);
+ }
+ }
+
+ rootElement.appendChild(contextElement);
+ }
+ }
+
+ if (empty) {
+ log.trace("{} Data is empty", storageService.getLogPrefix());
+ return new ClientStorageServiceOperation(storageService.getId(), storageService.getStorageName(), null,
+ getSource());
+ }
+
+ final String raw = SerializeSupport.nodeToString(rootElement);
+
+ log.trace("{} Size of data before encryption is {}", storageService.getLogPrefix(), raw.length());
+ log.trace("{} Data before encryption is {}", storageService.getLogPrefix(), raw);
+ try {
+ final String wrapped = storageService.getDataSealer().wrap(raw,
+ exp > 0 ? Instant.ofEpochMilli(exp) : Instant.now().plus(Duration.ofDays(1)));
+ log.trace("{} Size of data after encryption is {}", storageService.getLogPrefix(), wrapped.length());
+ setDirty(false);
+ return new ClientStorageServiceOperation(storageService.getId(), storageService.getStorageName(),
+ wrapped, getSource());
+ } catch (final DataSealerException e) {
+ throw new IOException(e);
+ }
+ } catch (final XMLParserException e) {
+ throw new IOException(e);
+ }
+ }
+//Checkstyle: CyclomaticComplexity|MethodLength ON
+
+ /** Factory for XML-backed store. */
+ public static class XMLClientStorageServiceStoreFactory extends AbstractInitializableComponent implements Factory {
+
+ /** ParserPool to pass into stores. */
+ @Nonnull private final ParserPool parserPool;
+
+ /** Constructor. */
+ public XMLClientStorageServiceStoreFactory() {
+ parserPool = new BasicParserPool();
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ protected void doInitialize() throws ComponentInitializationException {
+ // TODO Auto-generated method stub
+ super.doInitialize();
+
+ ((BasicParserPool) parserPool).setNamespaceAware(false);
+ ((BasicParserPool) parserPool).initialize();
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ protected void doDestroy() {
+ ((BasicParserPool) parserPool).destroy();
+ }
+
+ /** {@inheritDoc} */
+ @Nonnull public ClientStorageServiceStore load(@Nullable @NotEmpty final String raw,
+ @Nonnull final ClientStorageSource src) {
+ final ClientStorageServiceStore store = new XMLClientStorageServiceStore(parserPool);
+ store.load(raw, src);
+ return store;
+ }
+ }
+
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list