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: Craig Perkins <[email protected]>
- Loading branch information
Showing
12 changed files
with
225 additions
and
65 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
10 changes: 0 additions & 10 deletions
10
...src/main/java/org/opensearch/security/sampleextension/resource/SampleResourceFactory.java
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
.../src/main/java/org/opensearch/security/sampleextension/resource/SampleResourceParser.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,29 @@ | ||
package org.opensearch.security.sampleextension.resource; | ||
|
||
import java.io.IOException; | ||
|
||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.core.xcontent.XContentParserUtils; | ||
import org.opensearch.security.spi.ResourceParser; | ||
|
||
public class SampleResourceParser implements ResourceParser<SampleResource> { | ||
|
||
@Override | ||
public SampleResource parse(XContentParser parser, String id) throws IOException { | ||
SampleResource resource = new SampleResource(); | ||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); | ||
|
||
while (!parser.nextToken().equals(XContentParser.Token.END_OBJECT)) { | ||
String fieldName = parser.currentName(); | ||
parser.nextToken(); | ||
switch (fieldName) { | ||
case "name": | ||
resource.setName(parser.text()); | ||
break; | ||
default: | ||
XContentParserUtils.throwUnknownToken(parser.currentToken(), parser.getTokenLocation()); | ||
} | ||
} | ||
return resource; | ||
} | ||
} |
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
16 changes: 2 additions & 14 deletions
16
spi/src/main/java/org/opensearch/security/spi/Resource.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 |
---|---|---|
@@ -1,18 +1,6 @@ | ||
package org.opensearch.security.spi; | ||
|
||
import java.util.Map; | ||
|
||
import org.opensearch.core.common.io.stream.NamedWriteable; | ||
import org.opensearch.core.xcontent.ToXContentFragment; | ||
|
||
public abstract class Resource implements NamedWriteable, ToXContentFragment { | ||
protected String resourceId; | ||
|
||
public String getResourceId() { | ||
return resourceId; | ||
} | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
|
||
public void fromSource(String resourceId, Map<String, Object> sourceAsMap) { | ||
this.resourceId = resourceId; | ||
} | ||
} | ||
public interface Resource extends NamedWriteable, ToXContentObject {} |
94 changes: 94 additions & 0 deletions
94
spi/src/main/java/org/opensearch/security/spi/ResourceDocVersion.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,94 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* 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. | ||
*/ | ||
|
||
package org.opensearch.security.spi; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
|
||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.common.io.stream.Writeable; | ||
|
||
/** | ||
* Structure to represent resource document version. | ||
*/ | ||
public class ResourceDocVersion implements Comparable<ResourceDocVersion>, Writeable { | ||
private final long primaryTerm; | ||
private final long seqNo; | ||
private final long version; | ||
|
||
public ResourceDocVersion(long primaryTerm, long seqNo, long version) { | ||
this.primaryTerm = primaryTerm; | ||
this.seqNo = seqNo; | ||
this.version = version; | ||
} | ||
|
||
public ResourceDocVersion(StreamInput in) throws IOException { | ||
this.primaryTerm = in.readLong(); | ||
this.seqNo = in.readLong(); | ||
this.version = in.readLong(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeLong(this.primaryTerm); | ||
out.writeLong(this.seqNo); | ||
out.writeLong(this.version); | ||
} | ||
|
||
public long getPrimaryTerm() { | ||
return primaryTerm; | ||
} | ||
|
||
public long getSeqNo() { | ||
return seqNo; | ||
} | ||
|
||
public long getVersion() { | ||
return version; | ||
} | ||
|
||
/** | ||
* Compare two doc versions. Refer to https://github.com/elastic/elasticsearch/issues/10708 | ||
* | ||
* @param v the doc version to compare. | ||
* @return -1 if this < v, 0 if this == v, otherwise 1; | ||
*/ | ||
@Override | ||
public int compareTo(ResourceDocVersion v) { | ||
if (v == null) { | ||
return 1; | ||
} | ||
if (this.seqNo < v.seqNo) { | ||
return -1; | ||
} | ||
if (this.seqNo > v.seqNo) { | ||
return 1; | ||
} | ||
if (this.primaryTerm < v.primaryTerm) { | ||
return -1; | ||
} | ||
if (this.primaryTerm > v.primaryTerm) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format( | ||
Locale.getDefault(), | ||
"{_version: %s, _primary_term: %s, _seq_no: %s}", | ||
this.version, | ||
this.primaryTerm, | ||
this.seqNo | ||
); | ||
} | ||
} |
5 changes: 0 additions & 5 deletions
5
spi/src/main/java/org/opensearch/security/spi/ResourceFactory.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
spi/src/main/java/org/opensearch/security/spi/ResourceParser.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,9 @@ | ||
package org.opensearch.security.spi; | ||
|
||
import java.io.IOException; | ||
|
||
import org.opensearch.core.xcontent.XContentParser; | ||
|
||
public interface ResourceParser<T extends Resource> { | ||
T parse(XContentParser xContentParser, String id) throws IOException; | ||
} |
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
Oops, something went wrong.