26
26
import org .elasticsearch .common .xcontent .XContentBuilder ;
27
27
28
28
import java .io .IOException ;
29
+ import java .util .Arrays ;
30
+ import java .util .List ;
31
+ import java .util .stream .IntStream ;
29
32
30
33
/**
31
34
* Request for invalidating API key(s) so that it can no longer be used
@@ -34,38 +37,62 @@ public final class InvalidateApiKeyRequest implements Validatable, ToXContentObj
34
37
35
38
private final String realmName ;
36
39
private final String userName ;
37
- private final String id ;
40
+ private final List < String > ids ;
38
41
private final String name ;
39
42
private final boolean ownedByAuthenticatedUser ;
40
43
41
44
// pkg scope for testing
45
+ @ Deprecated
42
46
InvalidateApiKeyRequest (@ Nullable String realmName , @ Nullable String userName , @ Nullable String apiKeyId ,
43
47
@ Nullable String apiKeyName , boolean ownedByAuthenticatedUser ) {
44
- if (Strings .hasText (realmName ) == false && Strings .hasText (userName ) == false && Strings .hasText (apiKeyId ) == false
45
- && Strings .hasText (apiKeyName ) == false && ownedByAuthenticatedUser == false ) {
46
- throwValidationError ("One of [api key id, api key name, username, realm name] must be specified if [owner] flag is false" );
48
+ this (realmName , userName , apiKeyName , ownedByAuthenticatedUser , apiKeyIdToIds (apiKeyId ));
49
+ }
50
+
51
+ InvalidateApiKeyRequest (@ Nullable String realmName , @ Nullable String userName ,
52
+ @ Nullable String apiKeyName , boolean ownedByAuthenticatedUser , @ Nullable List <String > apiKeyIds ) {
53
+ validateApiKeyIds (apiKeyIds );
54
+ if (Strings .hasText (realmName ) == false && Strings .hasText (userName ) == false && apiKeyIds == null
55
+ && Strings .hasText (apiKeyName ) == false && ownedByAuthenticatedUser == false ) {
56
+ throwValidationError ("One of [api key id(s), api key name, username, realm name] must be specified if [owner] flag is false" );
47
57
}
48
- if (Strings . hasText ( apiKeyId ) || Strings .hasText (apiKeyName )) {
58
+ if (apiKeyIds != null || Strings .hasText (apiKeyName )) {
49
59
if (Strings .hasText (realmName ) || Strings .hasText (userName )) {
50
60
throwValidationError (
51
- "username or realm name must not be specified when the api key id or api key name is specified" );
61
+ "username or realm name must not be specified when the api key id(s) or api key name is specified" );
52
62
}
53
63
}
54
64
if (ownedByAuthenticatedUser ) {
55
65
if (Strings .hasText (realmName ) || Strings .hasText (userName )) {
56
66
throwValidationError ("neither username nor realm-name may be specified when invalidating owned API keys" );
57
67
}
58
68
}
59
- if (Strings . hasText ( apiKeyId ) && Strings .hasText (apiKeyName )) {
60
- throwValidationError ("only one of [api key id, api key name] can be specified" );
69
+ if (apiKeyIds != null && Strings .hasText (apiKeyName )) {
70
+ throwValidationError ("only one of [api key id(s) , api key name] can be specified" );
61
71
}
62
72
this .realmName = realmName ;
63
73
this .userName = userName ;
64
- this .id = apiKeyId ;
74
+ this .ids = apiKeyIds == null ? null : List . copyOf ( apiKeyIds ) ;
65
75
this .name = apiKeyName ;
66
76
this .ownedByAuthenticatedUser = ownedByAuthenticatedUser ;
67
77
}
68
78
79
+ private void validateApiKeyIds (@ Nullable List <String > apiKeyIds ) {
80
+ if (apiKeyIds != null ) {
81
+ if (apiKeyIds .size () == 0 ) {
82
+ throwValidationError ("Argument [apiKeyIds] cannot be an empty array" );
83
+ } else {
84
+ final int [] idxOfBlankIds = IntStream .range (0 , apiKeyIds .size ())
85
+ .filter (i -> Strings .hasText (apiKeyIds .get (i )) == false ).toArray ();
86
+ if (idxOfBlankIds .length > 0 ) {
87
+ throwValidationError ("Argument [apiKeyIds] must not contain blank id, but got blank "
88
+ + (idxOfBlankIds .length == 1 ? "id" : "ids" ) + " at index "
89
+ + (idxOfBlankIds .length == 1 ? "position" : "positions" ) + ": "
90
+ + Arrays .toString (idxOfBlankIds ));
91
+ }
92
+ }
93
+ }
94
+ }
95
+
69
96
private void throwValidationError (String message ) {
70
97
throw new IllegalArgumentException (message );
71
98
}
@@ -78,8 +105,20 @@ public String getUserName() {
78
105
return userName ;
79
106
}
80
107
108
+ @ Deprecated
81
109
public String getId () {
82
- return id ;
110
+ if (ids == null ) {
111
+ return null ;
112
+ } else if (ids .size () == 1 ) {
113
+ return ids .get (0 );
114
+ } else {
115
+ throw new IllegalArgumentException ("Cannot get a single api key id when multiple ids have been set ["
116
+ + Strings .collectionToCommaDelimitedString (ids ) + "]" );
117
+ }
118
+ }
119
+
120
+ public List <String > getIds () {
121
+ return ids ;
83
122
}
84
123
85
124
public String getName () {
@@ -96,7 +135,7 @@ public boolean ownedByAuthenticatedUser() {
96
135
* @return {@link InvalidateApiKeyRequest}
97
136
*/
98
137
public static InvalidateApiKeyRequest usingRealmName (String realmName ) {
99
- return new InvalidateApiKeyRequest (realmName , null , null , null , false );
138
+ return new InvalidateApiKeyRequest (realmName , null , null , false , null );
100
139
}
101
140
102
141
/**
@@ -105,7 +144,7 @@ public static InvalidateApiKeyRequest usingRealmName(String realmName) {
105
144
* @return {@link InvalidateApiKeyRequest}
106
145
*/
107
146
public static InvalidateApiKeyRequest usingUserName (String userName ) {
108
- return new InvalidateApiKeyRequest (null , userName , null , null , false );
147
+ return new InvalidateApiKeyRequest (null , userName , null , false , null );
109
148
}
110
149
111
150
/**
@@ -115,7 +154,7 @@ public static InvalidateApiKeyRequest usingUserName(String userName) {
115
154
* @return {@link InvalidateApiKeyRequest}
116
155
*/
117
156
public static InvalidateApiKeyRequest usingRealmAndUserName (String realmName , String userName ) {
118
- return new InvalidateApiKeyRequest (realmName , userName , null , null , false );
157
+ return new InvalidateApiKeyRequest (realmName , userName , null , false , null );
119
158
}
120
159
121
160
/**
@@ -126,7 +165,18 @@ public static InvalidateApiKeyRequest usingRealmAndUserName(String realmName, St
126
165
* @return {@link InvalidateApiKeyRequest}
127
166
*/
128
167
public static InvalidateApiKeyRequest usingApiKeyId (String apiKeyId , boolean ownedByAuthenticatedUser ) {
129
- return new InvalidateApiKeyRequest (null , null , apiKeyId , null , ownedByAuthenticatedUser );
168
+ return new InvalidateApiKeyRequest (null , null , null , ownedByAuthenticatedUser , apiKeyIdToIds (apiKeyId ));
169
+ }
170
+
171
+ /**
172
+ * Creates invalidate API key request for given api key ids
173
+ * @param apiKeyIds api key ids
174
+ * @param ownedByAuthenticatedUser set {@code true} if the request is only for the API keys owned by current authenticated user else
175
+ * {@code false}
176
+ * @return {@link InvalidateApiKeyRequest}
177
+ */
178
+ public static InvalidateApiKeyRequest usingApiKeyIds (List <String > apiKeyIds , boolean ownedByAuthenticatedUser ) {
179
+ return new InvalidateApiKeyRequest (null , null , null , ownedByAuthenticatedUser , apiKeyIds );
130
180
}
131
181
132
182
/**
@@ -137,14 +187,14 @@ public static InvalidateApiKeyRequest usingApiKeyId(String apiKeyId, boolean own
137
187
* @return {@link InvalidateApiKeyRequest}
138
188
*/
139
189
public static InvalidateApiKeyRequest usingApiKeyName (String apiKeyName , boolean ownedByAuthenticatedUser ) {
140
- return new InvalidateApiKeyRequest (null , null , null , apiKeyName , ownedByAuthenticatedUser );
190
+ return new InvalidateApiKeyRequest (null , null , apiKeyName , ownedByAuthenticatedUser , null );
141
191
}
142
192
143
193
/**
144
194
* Creates invalidate api key request to invalidate api keys owned by the current authenticated user.
145
195
*/
146
196
public static InvalidateApiKeyRequest forOwnedApiKeys () {
147
- return new InvalidateApiKeyRequest (null , null , null , null , true );
197
+ return new InvalidateApiKeyRequest (null , null , null , true , null );
148
198
}
149
199
150
200
@ Override
@@ -156,13 +206,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
156
206
if (userName != null ) {
157
207
builder .field ("username" , userName );
158
208
}
159
- if (id != null ) {
160
- builder .field ("id " , id );
209
+ if (ids != null ) {
210
+ builder .field ("ids " , ids );
161
211
}
162
212
if (name != null ) {
163
213
builder .field ("name" , name );
164
214
}
165
215
builder .field ("owner" , ownedByAuthenticatedUser );
166
216
return builder .endObject ();
167
217
}
218
+
219
+ static List <String > apiKeyIdToIds (@ Nullable String apiKeyId ) {
220
+ return Strings .hasText (apiKeyId ) ? List .of (apiKeyId ) : null ;
221
+ }
168
222
}
0 commit comments