forked from opensearch-project/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andrey Pleskach <[email protected]>
- Loading branch information
1 parent
3fbeec6
commit 5016037
Showing
8 changed files
with
299 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/org/opensearch/security/opensaml/integration/CleanerCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.opensaml.integration; | ||
|
||
import org.opensearch.common.util.concurrent.OpenSearchExecutors; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.lang.ref.Cleaner; | ||
|
||
public class CleanerCreator { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(CleanerCreator.class); | ||
|
||
/** Constructor. */ | ||
private CleanerCreator() {} | ||
|
||
public static Cleaner create(final Class<?> requester) { | ||
// Current approach here is to create a new Cleaner on each call. A given class requester/owner | ||
// is assumed to call only once and store in static storage. | ||
LOG.debug("Creating new java.lang.ref.Cleaner instance requested by class: {}", requester.getName()); | ||
return Cleaner.create(OpenSearchExecutors.daemonThreadFactory("cleaners")); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/opensearch/security/opensaml/integration/SecurityX509CRLBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.opensaml.integration; | ||
|
||
import org.opensaml.xmlsec.signature.X509CRL; | ||
import org.opensaml.xmlsec.signature.impl.X509CRLBuilder; | ||
|
||
public class SecurityX509CRLBuilder extends X509CRLBuilder { | ||
|
||
public X509CRL buildObject(final String namespaceURI, final String localName, final String namespacePrefix) { | ||
return new SecurityX509CRLImpl(namespaceURI, localName, namespacePrefix); | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
src/main/java/org/opensearch/security/opensaml/integration/SecurityX509CRLImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.opensaml.integration; | ||
|
||
import net.shibboleth.utilities.java.support.collection.IndexingObjectStore; | ||
import org.opensaml.core.xml.AbstractXMLObject; | ||
import org.opensaml.core.xml.XMLObject; | ||
import org.opensaml.xmlsec.signature.X509CRL; | ||
import org.opensaml.xmlsec.signature.impl.X509CRLImpl; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.lang.ref.Cleaner; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class SecurityX509CRLImpl extends AbstractXMLObject implements X509CRL { | ||
|
||
private static final IndexingObjectStore<String> B64_CRL_STORE = new IndexingObjectStore<>(); | ||
|
||
private static final Cleaner CLEANER = CleanerCreator.create(X509CRLImpl.class); | ||
|
||
private Cleaner.Cleanable cleanable; | ||
|
||
/** Index to a stored Base64 encoded CRL. */ | ||
private String b64CRLIndex; | ||
|
||
protected SecurityX509CRLImpl(final String namespaceURI, final String elementLocalName, final String namespacePrefix) { | ||
super(namespaceURI, elementLocalName, namespacePrefix); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public String getValue() { | ||
return B64_CRL_STORE.get(b64CRLIndex); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public void setValue(final String newValue) { | ||
// Dump our cached DOM if the new value really is new | ||
final String currentCRL = B64_CRL_STORE.get(b64CRLIndex); | ||
final String newCRL = prepareForAssignment(currentCRL, newValue); | ||
|
||
// This is a new value, remove the old one, add the new one | ||
if (!Objects.equals(currentCRL, newCRL)) { | ||
if (cleanable != null) { | ||
cleanable.clean(); | ||
cleanable = null; | ||
} | ||
b64CRLIndex = B64_CRL_STORE.put(newCRL); | ||
if (b64CRLIndex != null) { | ||
cleanable = CLEANER.register(this, new SecurityX509CRLImpl.CleanerState(b64CRLIndex)); | ||
} | ||
} | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public List<XMLObject> getOrderedChildren() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
/** | ||
* The action to be taken when the current state must be cleaned. | ||
*/ | ||
static class CleanerState implements Runnable { | ||
|
||
/** The index to remove from the store. */ | ||
private String index; | ||
|
||
public CleanerState(@Nonnull final String idx) { | ||
index = idx; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public void run() { | ||
SecurityX509CRLImpl.B64_CRL_STORE.remove(index); | ||
} | ||
|
||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ain/java/org/opensearch/security/opensaml/integration/SecurityX509CertificateBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.opensaml.integration; | ||
|
||
import org.opensaml.xmlsec.signature.X509Certificate; | ||
import org.opensaml.xmlsec.signature.impl.X509CertificateBuilder; | ||
|
||
public class SecurityX509CertificateBuilder extends X509CertificateBuilder { | ||
|
||
@Override | ||
public X509Certificate buildObject(final String namespaceURI, final String localName, final String namespacePrefix) { | ||
return new SecurityX509CertificateImpl(namespaceURI, localName, namespacePrefix); | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/org/opensearch/security/opensaml/integration/SecurityX509CertificateImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.opensaml.integration; | ||
|
||
import net.shibboleth.utilities.java.support.collection.IndexingObjectStore; | ||
import org.opensaml.core.xml.AbstractXMLObject; | ||
import org.opensaml.core.xml.XMLObject; | ||
import org.opensaml.xmlsec.signature.X509Certificate; | ||
import org.opensaml.xmlsec.signature.impl.X509CertificateImpl; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.lang.ref.Cleaner; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class SecurityX509CertificateImpl extends AbstractXMLObject implements X509Certificate { | ||
|
||
private static final IndexingObjectStore<String> B64_CERT_STORE = new IndexingObjectStore<>(); | ||
|
||
private static final Cleaner CLEANER = CleanerCreator.create(X509CertificateImpl.class); | ||
|
||
private Cleaner.Cleanable cleanable; | ||
|
||
private String b64CertIndex; | ||
|
||
protected SecurityX509CertificateImpl(final String namespaceURI, final String elementLocalName, final String namespacePrefix) { | ||
super(namespaceURI, elementLocalName, namespacePrefix); | ||
} | ||
|
||
@Override | ||
public String getValue() { | ||
return B64_CERT_STORE.get(b64CertIndex); | ||
} | ||
|
||
@Override | ||
public void setValue(final String newValue) { | ||
// Dump our cached DOM if the new value really is new | ||
final String currentCert = B64_CERT_STORE.get(b64CertIndex); | ||
final String newCert = prepareForAssignment(currentCert, newValue); | ||
|
||
// This is a new value, remove the old one, add the new one | ||
if (!Objects.equals(currentCert, newCert)) { | ||
if (cleanable != null) { | ||
cleanable.clean(); | ||
cleanable = null; | ||
} | ||
b64CertIndex = B64_CERT_STORE.put(newCert); | ||
if (b64CertIndex != null) { | ||
cleanable = CLEANER.register(this, new SecurityX509CertificateImpl.CleanerState(b64CertIndex)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public List<XMLObject> getOrderedChildren() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
/** | ||
* The action to be taken when the current state must be cleaned. | ||
*/ | ||
static class CleanerState implements Runnable { | ||
|
||
private String index; | ||
|
||
public CleanerState(@Nonnull final String idx) { | ||
index = idx; | ||
} | ||
|
||
public void run() { | ||
SecurityX509CertificateImpl.B64_CERT_STORE.remove(index); | ||
} | ||
|
||
} | ||
} |