Skip to content

Commit a54fb20

Browse files
Adds gracehful handling of revoke when no sharewith exists
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
1 parent c00ae56 commit a54fb20

File tree

3 files changed

+53
-33
lines changed

3 files changed

+53
-33
lines changed

spi/src/main/java/org/opensearch/security/spi/resources/sharing/ShareWith.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public ShareWith add(ShareWith other) {
155155
* Returns a new ShareWith by revoking recipients based on another ShareWith.
156156
*/
157157
public ShareWith revoke(ShareWith other) {
158-
if (other == null || other.isPrivate()) {
158+
if (this.sharingInfo.isEmpty() || other == null || other.isPrivate()) {
159159
return this;
160160
}
161161
Map<String, Recipients> updated = new HashMap<>(this.sharingInfo);

spi/src/test/java/org/opensearch/security/spi/resources/ShareWithTests.java

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.Map;
1515
import java.util.Set;
1616

17-
import org.hamcrest.MatcherAssert;
1817
import org.junit.Test;
1918

2019
import org.opensearch.common.xcontent.XContentFactory;
@@ -31,11 +30,13 @@
3130

3231
import org.mockito.Mockito;
3332

33+
import static org.hamcrest.MatcherAssert.assertThat;
3434
import static org.hamcrest.Matchers.contains;
3535
import static org.hamcrest.Matchers.equalTo;
3636
import static org.hamcrest.Matchers.is;
3737
import static org.hamcrest.Matchers.notNullValue;
3838
import static org.hamcrest.Matchers.nullValue;
39+
import static org.hamcrest.Matchers.sameInstance;
3940
import static org.junit.Assert.assertThrows;
4041
import static org.mockito.ArgumentMatchers.any;
4142
import static org.mockito.ArgumentMatchers.eq;
@@ -62,16 +63,16 @@ public void testFromXContentWhenCurrentTokenIsNotStartObject() throws IOExceptio
6263

6364
ShareWith shareWith = ShareWith.fromXContent(parser);
6465

65-
MatcherAssert.assertThat(shareWith, notNullValue());
66+
assertThat(shareWith, notNullValue());
6667
Recipients readOnly = shareWith.atAccessLevel("read_only");
67-
MatcherAssert.assertThat(readOnly, notNullValue());
68+
assertThat(readOnly, notNullValue());
6869

6970
Map<Recipient, Set<String>> recipients = readOnly.getRecipients();
70-
MatcherAssert.assertThat(recipients, notNullValue());
71-
MatcherAssert.assertThat(recipients.get(Recipient.USERS).size(), is(1));
72-
MatcherAssert.assertThat(recipients.get(Recipient.USERS), contains("user1"));
73-
MatcherAssert.assertThat(recipients.get(Recipient.ROLES).size(), is(0));
74-
MatcherAssert.assertThat(recipients.get(Recipient.BACKEND_ROLES).size(), is(0));
71+
assertThat(recipients, notNullValue());
72+
assertThat(recipients.get(Recipient.USERS).size(), is(1));
73+
assertThat(recipients.get(Recipient.USERS), contains("user1"));
74+
assertThat(recipients.get(Recipient.ROLES).size(), is(0));
75+
assertThat(recipients.get(Recipient.BACKEND_ROLES).size(), is(0));
7576
}
7677

7778
@Test
@@ -81,9 +82,9 @@ public void testFromXContentWithEmptyInput() throws IOException {
8182

8283
ShareWith result = ShareWith.fromXContent(parser);
8384

84-
MatcherAssert.assertThat(result, notNullValue());
85-
MatcherAssert.assertThat(result.isPrivate(), is(true));
86-
MatcherAssert.assertThat(result.isPublic(), is(false));
85+
assertThat(result, notNullValue());
86+
assertThat(result.isPrivate(), is(true));
87+
assertThat(result.isPublic(), is(false));
8788
}
8889

8990
@Test
@@ -110,22 +111,22 @@ public void testFromXContentWithStartObject() throws IOException {
110111

111112
ShareWith shareWith = ShareWith.fromXContent(parser);
112113

113-
MatcherAssert.assertThat(shareWith, notNullValue());
114+
assertThat(shareWith, notNullValue());
114115

115116
Recipients defaultAccessLevel = shareWith.atAccessLevel("default");
116117

117118
Recipients readOnly = shareWith.atAccessLevel("read-only");
118119

119-
MatcherAssert.assertThat(defaultAccessLevel, notNullValue());
120-
MatcherAssert.assertThat(readOnly, notNullValue());
120+
assertThat(defaultAccessLevel, notNullValue());
121+
assertThat(readOnly, notNullValue());
121122

122-
MatcherAssert.assertThat(defaultAccessLevel.getRecipientsByType(Recipient.USERS).size(), is(2));
123-
MatcherAssert.assertThat(defaultAccessLevel.getRecipientsByType(Recipient.ROLES).size(), is(1));
124-
MatcherAssert.assertThat(defaultAccessLevel.getRecipientsByType(Recipient.BACKEND_ROLES).size(), is(1));
123+
assertThat(defaultAccessLevel.getRecipientsByType(Recipient.USERS).size(), is(2));
124+
assertThat(defaultAccessLevel.getRecipientsByType(Recipient.ROLES).size(), is(1));
125+
assertThat(defaultAccessLevel.getRecipientsByType(Recipient.BACKEND_ROLES).size(), is(1));
125126

126-
MatcherAssert.assertThat(readOnly.getRecipientsByType(Recipient.USERS).size(), is(1));
127-
MatcherAssert.assertThat(readOnly.getRecipientsByType(Recipient.ROLES).size(), is(1));
128-
MatcherAssert.assertThat(readOnly.getRecipientsByType(Recipient.BACKEND_ROLES).size(), is(1));
127+
assertThat(readOnly.getRecipientsByType(Recipient.USERS).size(), is(1));
128+
assertThat(readOnly.getRecipientsByType(Recipient.ROLES).size(), is(1));
129+
assertThat(readOnly.getRecipientsByType(Recipient.BACKEND_ROLES).size(), is(1));
129130
}
130131

131132
@Test
@@ -136,9 +137,9 @@ public void testFromXContentWithUnexpectedEndOfInput() throws IOException {
136137

137138
ShareWith result = ShareWith.fromXContent(mockParser);
138139

139-
MatcherAssert.assertThat(result, notNullValue());
140-
MatcherAssert.assertThat(result.isPrivate(), is(true));
141-
MatcherAssert.assertThat(result.isPublic(), is(false));
140+
assertThat(result, notNullValue());
141+
assertThat(result.isPrivate(), is(true));
142+
assertThat(result.isPublic(), is(false));
142143
}
143144

144145
@Test
@@ -155,8 +156,8 @@ public void testToXContentBuildsCorrectly() throws IOException {
155156

156157
String expected = "{\"actionGroup1\":{\"users\":[\"bleh\"]}}";
157158

158-
MatcherAssert.assertThat(expected.length(), equalTo(result.length()));
159-
MatcherAssert.assertThat(expected, equalTo(result));
159+
assertThat(expected.length(), equalTo(result.length()));
160+
assertThat(expected, equalTo(result));
160161
}
161162

162163
@Test
@@ -206,8 +207,8 @@ public void test_fromXContent_emptyObject() throws IOException {
206207

207208
ShareWith shareWith = ShareWith.fromXContent(parser);
208209

209-
MatcherAssert.assertThat(shareWith.isPrivate(), is(true));
210-
MatcherAssert.assertThat(shareWith.isPublic(), is(false));
210+
assertThat(shareWith.isPrivate(), is(true));
211+
assertThat(shareWith.isPublic(), is(false));
211212
}
212213

213214
@Test
@@ -241,7 +242,7 @@ public void testAdd_NewAndMerge() {
241242
// existing level merged
242243
verify(orig, times(1)).share(patchRec);
243244
// new level added
244-
MatcherAssert.assertThat(result.atAccessLevel("write"), equalTo(patchRec));
245+
assertThat(result.atAccessLevel("write"), equalTo(patchRec));
245246
}
246247

247248
@Test
@@ -260,7 +261,25 @@ public void testRevoke_ExistingAndNoop() {
260261
// revoke called on existing
261262
verify(orig, times(1)).revoke(revokeRec);
262263
// non-existing level noop
263-
MatcherAssert.assertThat(result.atAccessLevel("write"), nullValue());
264+
assertThat(result.atAccessLevel("write"), nullValue());
265+
}
266+
267+
@Test
268+
public void testRevoke_NonExisting() {
269+
ShareWith base = new ShareWith(new HashMap<>());
270+
Recipients revokeRec = mock(Recipients.class);
271+
Map<String, Recipients> revokeMap = new HashMap<>();
272+
revokeMap.put("read", revokeRec);
273+
revokeMap.put("write", revokeRec);
274+
ShareWith revokeSw = new ShareWith(revokeMap);
275+
276+
assertThat(base.getSharingInfo().size(), is(0));
277+
278+
ShareWith result = base.revoke(revokeSw);
279+
// revoke called on existing
280+
assertThat("Revoke on empty base should return the same object", result, sameInstance(base));
281+
// assert no levels were added or removed
282+
assertThat(result.getSharingInfo().size(), is(0));
264283
}
265284

266285
@Test

src/main/java/org/opensearch/security/resources/ResourceSharingIndexHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.opensearch.security.resources;
1111

1212
import java.io.IOException;
13+
import java.util.HashMap;
1314
import java.util.HashSet;
1415
import java.util.Map;
1516
import java.util.Set;
@@ -598,11 +599,11 @@ public void patchSharingInfo(
598599
sharingInfoListener.whenComplete(resourceSharing -> {
599600
ShareWith updatedShareWith = resourceSharing.getShareWith();
600601
if (updatedShareWith == null) {
601-
updatedShareWith = add;
602-
} else if (add != null) {
602+
updatedShareWith = new ShareWith(new HashMap<>());
603+
}
604+
if (add != null) {
603605
updatedShareWith = updatedShareWith.add(add);
604606
}
605-
606607
if (revoke != null) {
607608
updatedShareWith = updatedShareWith.revoke(revoke);
608609
}

0 commit comments

Comments
 (0)