/
IgniteAuthenticationProcessor.java
1462 lines (1166 loc) · 48 KB
/
IgniteAuthenticationProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.ignite.internal.processors.authentication;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteException;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.internal.GridKernalContext;
import org.apache.ignite.internal.GridTopic;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.IgniteInterruptedCheckedException;
import org.apache.ignite.internal.managers.communication.GridIoManager;
import org.apache.ignite.internal.managers.communication.GridIoPolicy;
import org.apache.ignite.internal.managers.communication.GridMessageListener;
import org.apache.ignite.internal.managers.discovery.CustomEventListener;
import org.apache.ignite.internal.managers.discovery.GridDiscoveryManager;
import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
import org.apache.ignite.internal.processors.GridProcessorAdapter;
import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
import org.apache.ignite.internal.processors.cache.GridCacheSharedContext;
import org.apache.ignite.internal.processors.cache.GridCacheUtils;
import org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture;
import org.apache.ignite.internal.processors.cache.distributed.dht.preloader.PartitionsExchangeAware;
import org.apache.ignite.internal.processors.cache.persistence.metastorage.MetastorageLifecycleListener;
import org.apache.ignite.internal.processors.cache.persistence.metastorage.MetastorageTree;
import org.apache.ignite.internal.processors.cache.persistence.metastorage.ReadOnlyMetastorage;
import org.apache.ignite.internal.processors.cache.persistence.metastorage.ReadWriteMetastorage;
import org.apache.ignite.internal.processors.security.GridSecurityProcessor;
import org.apache.ignite.internal.processors.security.IgniteSecurityProcessor;
import org.apache.ignite.internal.processors.security.SecurityContext;
import org.apache.ignite.internal.util.future.GridFutureAdapter;
import org.apache.ignite.internal.util.tostring.GridToStringExclude;
import org.apache.ignite.internal.util.tostring.GridToStringInclude;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.internal.S;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.internal.util.worker.GridWorker;
import org.apache.ignite.lang.IgniteFuture;
import org.apache.ignite.lang.IgniteFutureCancelledException;
import org.apache.ignite.lang.IgniteUuid;
import org.apache.ignite.plugin.security.AuthenticationContext;
import org.apache.ignite.plugin.security.SecurityCredentials;
import org.apache.ignite.plugin.security.SecurityException;
import org.apache.ignite.plugin.security.SecurityPermission;
import org.apache.ignite.plugin.security.SecurityPermissionSet;
import org.apache.ignite.plugin.security.SecuritySubject;
import org.apache.ignite.plugin.security.SecuritySubjectType;
import org.apache.ignite.spi.discovery.DiscoveryDataBag;
import org.apache.ignite.spi.discovery.DiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.thread.IgniteThreadPoolExecutor;
import org.jetbrains.annotations.Nullable;
import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
import static org.apache.ignite.internal.GridComponent.DiscoveryDataExchangeType.AUTH_PROC;
import static org.apache.ignite.internal.IgniteNodeAttributes.ATTR_AUTHENTICATION_ENABLED;
import static org.apache.ignite.internal.IgniteNodeAttributes.ATTR_IGNITE_INSTANCE_NAME;
import static org.apache.ignite.internal.processors.authentication.UserManagementOperation.OperationType.ADD;
import static org.apache.ignite.internal.processors.authentication.UserManagementOperation.OperationType.REMOVE;
import static org.apache.ignite.internal.processors.authentication.UserManagementOperation.OperationType.UPDATE;
import static org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.ALLOW_ALL;
import static org.apache.ignite.plugin.security.SecuritySubjectType.REMOTE_CLIENT;
import static org.apache.ignite.plugin.security.SecuritySubjectType.REMOTE_NODE;
/**
*
*/
public class IgniteAuthenticationProcessor extends GridProcessorAdapter implements GridSecurityProcessor,
MetastorageLifecycleListener, PartitionsExchangeAware {
/** Store user prefix. */
private static final String STORE_USER_PREFIX = "user.";
/** Discovery event types. */
private static final int[] DISCO_EVT_TYPES = new int[] {EVT_NODE_LEFT, EVT_NODE_FAILED, EVT_NODE_JOINED};
/** User operation finish futures (Operation ID -> future). */
private final ConcurrentHashMap<IgniteUuid, UserOperationFinishFuture> opFinishFuts = new ConcurrentHashMap<>();
/** Futures prepared user map. Authentication message ID -> public future. */
private final ConcurrentMap<IgniteUuid, AuthenticateFuture> authFuts = new ConcurrentHashMap<>();
/** Whan the future is done the node is ready for authentication. */
private final GridFutureAdapter<Void> readyForAuthFut = new GridFutureAdapter<>();
/** Operation mutex. */
private final Object mux = new Object();
/** Active operations. Collects to send on joining node. */
private final Map<IgniteUuid, UserManagementOperation> activeOps = Collections.synchronizedMap(new LinkedHashMap<>());
/** User map. */
private ConcurrentMap<UUID, User> users;
/** Shared context. */
@GridToStringExclude
private GridCacheSharedContext<?, ?> sharedCtx;
/** Meta storage. */
private ReadWriteMetastorage metastorage;
/** Executor. */
private IgniteThreadPoolExecutor exec;
/** Coordinator node. */
private ClusterNode crdNode;
/** Disconnected flag. */
private volatile boolean disconnected;
/** Finish message of the current operation. May be resend when coordinator node leave. */
private UserManagementOperationFinishedMessage curOpFinishMsg;
/** Initial users map and operations received from coordinator on the node joined to the cluster. */
private InitialUsersData initUsrs;
/** I/O message listener. */
private GridMessageListener ioLsnr;
/** System discovery message listener. */
private DiscoveryEventListener discoLsnr;
/** Node activate future. */
private final GridFutureAdapter<Void> activateFut = new GridFutureAdapter<>();
/**
* @param ctx Kernal context.
*/
public IgniteAuthenticationProcessor(GridKernalContext ctx) {
super(ctx);
}
/** Starts processor. */
public void startProcessor() throws IgniteCheckedException {
if (!GridCacheUtils.isPersistenceEnabled(ctx.config())) {
throw new IgniteCheckedException("Authentication can be enabled only for cluster with enabled persistence."
+ " Check the DataRegionConfiguration");
}
ctx.internalSubscriptionProcessor().registerMetastorageListener(this);
ctx.addNodeAttribute(ATTR_AUTHENTICATION_ENABLED, true);
sharedCtx = ctx.cache().context();
sharedCtx.exchange().registerExchangeAwareComponent(this);
GridDiscoveryManager discoMgr = ctx.discovery();
GridIoManager ioMgr = ctx.io();
discoMgr.setCustomEventListener(UserProposedMessage.class, new UserProposedListener());
discoMgr.setCustomEventListener(UserAcceptedMessage.class, new UserAcceptedListener());
discoMgr.localJoinFuture().listen(fut -> onLocalJoin());
discoLsnr = (evt, discoCache) -> {
if (ctx.isStopping())
return;
switch (evt.type()) {
case EVT_NODE_LEFT:
case EVT_NODE_FAILED:
onNodeLeft(evt.eventNode().id());
break;
case EVT_NODE_JOINED:
onNodeJoin(evt.eventNode());
break;
}
};
ctx.event().addDiscoveryEventListener(discoLsnr, DISCO_EVT_TYPES);
ioLsnr = (nodeId, msg, plc) -> {
if (ctx.isStopping())
return;
if (msg instanceof UserManagementOperationFinishedMessage)
onFinishMessage(nodeId, (UserManagementOperationFinishedMessage)msg);
else if (msg instanceof UserAuthenticateRequestMessage)
onAuthenticateRequestMessage(nodeId, (UserAuthenticateRequestMessage)msg);
else if (msg instanceof UserAuthenticateResponseMessage)
onAuthenticateResponseMessage((UserAuthenticateResponseMessage)msg);
};
ioMgr.addMessageListener(GridTopic.TOPIC_AUTH, ioLsnr);
exec = new IgniteThreadPoolExecutor(
"auth",
ctx.config().getIgniteInstanceName(),
1,
1,
0,
new LinkedBlockingQueue<>());
}
/** {@inheritDoc} */
@Override public void stop(boolean cancel) throws IgniteCheckedException {
if (ioLsnr != null)
ctx.io().removeMessageListener(GridTopic.TOPIC_AUTH, ioLsnr);
if (discoLsnr != null)
ctx.event().removeDiscoveryEventListener(discoLsnr, DISCO_EVT_TYPES);
cancelFutures("Node stopped");
if (exec != null) {
if (!cancel)
exec.shutdown();
else
exec.shutdownNow();
}
}
/** {@inheritDoc} */
@Override public void onKernalStop(boolean cancel) {
synchronized (mux) {
cancelFutures("Kernal stopped.");
}
}
/** {@inheritDoc} */
@Override public void onDisconnected(IgniteFuture reconnectFut) {
synchronized (mux) {
assert !disconnected;
disconnected = true;
cancelFutures("Client node was disconnected from topology (operation result is unknown).");
}
}
/** {@inheritDoc} */
@Override public IgniteInternalFuture<?> onReconnected(boolean active) {
synchronized (mux) {
assert disconnected;
disconnected = false;
return null;
}
}
/** {@inheritDoc} */
@Override public SecurityContext authenticate(AuthenticationContext authCtx) throws IgniteCheckedException {
SecurityCredentials creds = authCtx.credentials();
String login = (String)creds.getLogin();
if (F.isEmpty(login))
throw new IgniteAccessControlException("The user name or password is incorrect [userName=" + login + ']');
String passwd = (String)creds.getPassword();
UUID subjId;
if (ctx.clientNode()) {
if (ctx.discovery().aliveServerNodes().isEmpty()) {
throw new IgniteAccessControlException("No alive server node was found to which the authentication" +
" operation could be delegated. It is possible that the client node has been started with the" +
" \"forceServerMode\" flag enabled and no server node had been started yet.");
}
AuthenticateFuture fut;
do {
synchronized (mux) {
ClusterNode rndNode = U.randomServerNode(ctx);
fut = new AuthenticateFuture(rndNode.id());
UserAuthenticateRequestMessage msg = new UserAuthenticateRequestMessage(login, passwd);
authFuts.put(msg.id(), fut);
ctx.io().sendToGridTopic(rndNode, GridTopic.TOPIC_AUTH, msg, GridIoPolicy.SYSTEM_POOL);
}
fut.get();
} while (fut.retry());
subjId = toSubjectId(login);
}
else
subjId = authenticateOnServer(login, passwd);
return new SecurityContextImpl(subjId, login, authCtx.subjectType(), authCtx.address());
}
/**
* @param login User's login.
* @param passwd Password.
* @throws UserManagementException On error.
*/
public static void validate(String login, char[] passwd) throws UserManagementException {
if (F.isEmpty(login))
throw new UserManagementException("User name is empty");
if (F.isEmpty(passwd))
throw new UserManagementException("Password is empty");
if ((STORE_USER_PREFIX + login).getBytes().length > MetastorageTree.MAX_KEY_LEN)
throw new UserManagementException("User name is too long. " +
"The user name length must be less then 60 bytes in UTF8");
}
/** {@inheritDoc} */
@Override public void createUser(String login, char[] passwd) throws IgniteCheckedException {
validate(login, passwd);
UserManagementOperation op = new UserManagementOperation(User.create(login, new String(passwd)), ADD);
execUserOperation(op).get();
}
/** {@inheritDoc} */
@Override public void dropUser(String login) throws IgniteCheckedException {
UserManagementOperation op = new UserManagementOperation(User.create(login), REMOVE);
execUserOperation(op).get();
}
/** {@inheritDoc} */
@Override public void alterUser(String login, char[] passwd) throws IgniteCheckedException {
UserManagementOperation op = new UserManagementOperation(User.create(login, new String(passwd)), UPDATE);
execUserOperation(op).get();
}
/** {@inheritDoc} */
@Override public void onReadyForRead(ReadOnlyMetastorage metastorage) throws IgniteCheckedException {
if (!ctx.clientNode()) {
users = new ConcurrentHashMap<>();
metastorage.iterate(STORE_USER_PREFIX, (key, val) -> {
User u = (User)val;
User cur = users.putIfAbsent(toSubjectId(u.name()), u);
if (cur != null) {
throw new IllegalStateException("Security users with conflicting IDs were found while reading from" +
" metastorage [logins=" + u.name() + ", " + cur.name() + "]. It is possible that the Ignite" +
" metastorage is corrupted or the specified users were created bypassing the Ignite Security API.");
}
}, true);
}
else
users = null;
}
/** {@inheritDoc} */
@Override public void onReadyForReadWrite(ReadWriteMetastorage metastorage) {
if (!ctx.clientNode())
this.metastorage = metastorage;
else
this.metastorage = null;
}
/** {@inheritDoc} */
@Nullable @Override public DiscoveryDataExchangeType discoveryDataType() {
return DiscoveryDataExchangeType.AUTH_PROC;
}
/** {@inheritDoc} */
@Override public void collectGridNodeData(DiscoveryDataBag dataBag) {
// 1. Collect users info only on coordinator
// 2. Doesn't collect users info to send on client node due to security reason.
if (!isLocalNodeCoordinator() || dataBag.isJoiningNodeClient())
return;
synchronized (mux) {
if (!dataBag.commonDataCollectedFor(AUTH_PROC.ordinal())) {
InitialUsersData d = new InitialUsersData(users.values(), activeOps.values());
if (log.isDebugEnabled())
log.debug("Collected initial users data: " + d);
dataBag.addGridCommonData(AUTH_PROC.ordinal(), d);
}
}
}
/**
* Checks whether local node is coordinator. Nodes that are leaving or failed
* (but are still in topology) are removed from search.
*
* @return {@code true} if local node is coordinator.
*/
private boolean isLocalNodeCoordinator() {
DiscoverySpi spi = ctx.discovery().getInjectedDiscoverySpi();
if (spi instanceof TcpDiscoverySpi)
return ((TcpDiscoverySpi)spi).isLocalNodeCoordinator();
else
return F.eq(ctx.localNodeId(), coordinator().id());
}
/** {@inheritDoc} */
@Override public void onGridDataReceived(DiscoveryDataBag.GridDiscoveryData data) {
initUsrs = (InitialUsersData)data.commonData();
}
/** {@inheritDoc} */
@Override public boolean enabled() {
return true;
}
/**
* Check cluster state.
*/
private void checkActivate() {
if (!ctx.state().publicApiActiveState(true)) {
throw new IgniteException("Can not perform the operation because the cluster is inactive. Note, that " +
"the cluster is considered inactive by default if Ignite Persistent Store is used to let all the nodes " +
"join the cluster. To activate the cluster call Ignite.active(true).");
}
}
/**
*/
private void addDefaultUser() {
assert users != null && users.isEmpty();
User dfltUser = User.defaultUser();
// Put to local map to be ready for authentication.
users.put(toSubjectId(dfltUser.name()), dfltUser);
// Put to MetaStore when it will be ready.
exec.execute(new RefreshUsersStorageWorker(new ArrayList<>(Collections.singleton(dfltUser))));
}
/**
* Authenticate user.
*
* @param login User's login.
* @param passwd Plain text password.
* @return Authenticated user security ID.
* @throws IgniteCheckedException On authentication error.
*/
private UUID authenticateOnServer(String login, String passwd) throws IgniteCheckedException {
assert !ctx.clientNode() : "Must be used on server node";
readyForAuthFut.get();
UUID subjId = toSubjectId(login);
User usr = findUser(subjId, login);
if (usr == null || !usr.authorize(passwd))
throw new IgniteAccessControlException("The user name or password is incorrect [userName=" + login + ']');
return subjId;
}
/**
* @param op User operation.
* @return Operation future.
* @throws IgniteCheckedException On error.
*/
private UserOperationFinishFuture execUserOperation(UserManagementOperation op) throws IgniteCheckedException {
checkActivate();
synchronized (mux) {
if (disconnected) {
throw new UserManagementException("Failed to initiate user management operation because "
+ "client node is disconnected.");
}
checkUserOperation(op);
UserOperationFinishFuture fut = new UserOperationFinishFuture(op.id());
opFinishFuts.put(op.id(), fut);
UserProposedMessage msg = new UserProposedMessage(op);
ctx.discovery().sendCustomEvent(msg);
return fut;
}
}
/**
* @param op The operation with users.
* @throws IgniteCheckedException On error.
*/
private void processOperationLocal(UserManagementOperation op) throws IgniteCheckedException {
assert op != null && op.user() != null : "Invalid operation: " + op;
switch (op.type()) {
case ADD:
addUserLocal(op);
break;
case REMOVE:
removeUserLocal(op);
break;
case UPDATE:
updateUserLocal(op);
break;
}
}
/**
* Adds new user locally.
*
* @param op User operation.
* @throws IgniteCheckedException On error.
*/
private void addUserLocal(final UserManagementOperation op) throws IgniteCheckedException {
User usr = op.user();
String userName = usr.name();
UUID subjId = toSubjectId(userName);
if (users.get(subjId) != null)
throw new UserManagementException("User already exists [login=" + userName + ']');
metastorage.write(STORE_USER_PREFIX + userName, usr);
synchronized (mux) {
activeOps.remove(op.id());
users.put(subjId, usr);
}
}
/**
* Remove user from MetaStorage.
*
* @param op Operation.
* @throws IgniteCheckedException On error.
*/
private void removeUserLocal(UserManagementOperation op) throws IgniteCheckedException {
User usr = op.user();
String login = usr.name();
UUID subjId = toSubjectId(login);
if (findUser(subjId, login) == null)
throw new UserManagementException("User doesn't exist [userName=" + login + ']');
metastorage.remove(STORE_USER_PREFIX + login);
synchronized (mux) {
activeOps.remove(op.id());
users.remove(subjId);
}
}
/**
* Remove user from MetaStorage.
*
* @param op Operation.
* @throws IgniteCheckedException On error.
*/
private void updateUserLocal(UserManagementOperation op) throws IgniteCheckedException {
User usr = op.user();
String login = usr.name();
UUID subjId = toSubjectId(login);
if (findUser(subjId, login) == null)
throw new UserManagementException("User doesn't exist [userName=" + login + ']');
metastorage.write(STORE_USER_PREFIX + login, usr);
synchronized (mux) {
activeOps.remove(op.id());
users.put(subjId, usr);
}
}
/**
* Get current coordinator node.
*
* @return Coordinator node.
*/
private ClusterNode coordinator() {
synchronized (mux) {
if (crdNode != null)
return crdNode;
else {
ClusterNode res = null;
for (ClusterNode node : ctx.discovery().aliveServerNodes()) {
if (res == null || res.order() > node.order())
res = node;
}
if (res == null
&& !ctx.discovery().allNodes().isEmpty()
&& ctx.discovery().aliveServerNodes().isEmpty()) {
U.warn(log, "Cannot find the server coordinator node. "
+ "Possible a client is started with forceServerMode=true.");
}
else
assert res != null;
crdNode = res;
return res;
}
}
}
/**
* @param msg Error message.
*/
private void cancelFutures(String msg) {
synchronized (mux) {
for (UserOperationFinishFuture fut : opFinishFuts.values())
fut.onDone(null, new IgniteFutureCancelledException(msg));
}
for (GridFutureAdapter<Void> fut : authFuts.values())
fut.onDone(null, new IgniteFutureCancelledException(msg));
}
/**
* @param node Joined node ID.
*/
private void onNodeJoin(ClusterNode node) {
if (isNodeHoldsUsers(ctx.discovery().localNode()) && isNodeHoldsUsers(node)) {
synchronized (mux) {
for (UserOperationFinishFuture f : opFinishFuts.values())
f.onNodeJoin(node.id());
}
}
}
/**
* @param nodeId Left node ID.
*/
private void onNodeLeft(UUID nodeId) {
synchronized (mux) {
if (!ctx.clientNode()) {
for (UserOperationFinishFuture f : opFinishFuts.values())
f.onNodeLeft(nodeId);
}
// Found all auth requests that were be sent to left node.
// Complete these futures with special exception to retry authentication.
for (Iterator<Map.Entry<IgniteUuid, AuthenticateFuture>> it = authFuts.entrySet().iterator();
it.hasNext(); ) {
AuthenticateFuture f = it.next().getValue();
if (F.eq(nodeId, f.nodeId())) {
f.retry(true);
f.onDone();
it.remove();
}
}
// Coordinator left
if (F.eq(coordinator().id(), nodeId)) {
// Refresh info about coordinator node.
crdNode = null;
if (curOpFinishMsg != null)
sendFinish(curOpFinishMsg);
}
}
}
/**
* Handle finish operation message from a node.
*
* @param nodeId Node ID.
* @param msg Message.
*/
private void onFinishMessage(UUID nodeId, UserManagementOperationFinishedMessage msg) {
if (log.isDebugEnabled())
log.debug(msg.toString());
synchronized (mux) {
UserOperationFinishFuture fut = opFinishFuts.get(msg.operationId());
if (fut == null) {
fut = new UserOperationFinishFuture(msg.operationId());
opFinishFuts.put(msg.operationId(), fut);
}
if (msg.success())
fut.onSuccessOnNode(nodeId);
else
fut.onOperationFailOnNode(nodeId, msg.errorMessage());
}
}
/**
* Called when all required finish messages are received,
* send ACK message to complete operation futures on all nodes.
*
* @param opId Operation ID.
* @param err Error.
*/
private void onFinishOperation(IgniteUuid opId, IgniteCheckedException err) {
try {
UserAcceptedMessage msg = new UserAcceptedMessage(opId, err);
ctx.discovery().sendCustomEvent(msg);
}
catch (IgniteCheckedException e) {
if (!e.hasCause(IgniteFutureCancelledException.class))
U.error(log, "Unexpected exception on send UserAcceptedMessage.", e);
}
}
/**
* @param nodeId Node ID.
* @param msg Message.
*/
private void onAuthenticateRequestMessage(UUID nodeId, UserAuthenticateRequestMessage msg) {
UserAuthenticateResponseMessage respMsg;
try {
authenticateOnServer(msg.name(), msg.password());
respMsg = new UserAuthenticateResponseMessage(msg.id(), null);
}
catch (IgniteCheckedException e) {
respMsg = new UserAuthenticateResponseMessage(msg.id(), e.toString());
e.printStackTrace();
}
try {
ctx.io().sendToGridTopic(nodeId, GridTopic.TOPIC_AUTH, respMsg, GridIoPolicy.SYSTEM_POOL);
}
catch (IgniteCheckedException e) {
U.error(log, "Unexpected exception on send UserAuthenticateResponseMessage.", e);
}
}
/**
* @param msg Message.
*/
private void onAuthenticateResponseMessage(UserAuthenticateResponseMessage msg) {
GridFutureAdapter<Void> fut = authFuts.get(msg.id());
fut.onDone(null, !msg.success() ? new IgniteAccessControlException(msg.errorMessage()) : null);
authFuts.remove(msg.id());
}
/**
* Local node joined to topology. Discovery cache is available but no discovery custom message are received.
* Initial user set and initial user operation (received on join) are processed here.
*/
private void onLocalJoin() {
if (ctx.isDaemon() || ctx.clientDisconnected() || coordinator() == null)
return;
if (F.eq(coordinator().id(), ctx.localNodeId())) {
assert initUsrs == null;
// Creates default user on coordinator if it is the first start of PDS cluster
// or start of in-memory cluster.
if (users.isEmpty())
addDefaultUser();
}
else {
if (ctx.clientNode())
return;
assert initUsrs != null;
// Can be empty on initial start of PDS cluster (default user will be created and stored after activate)
if (!F.isEmpty(initUsrs.usrs)) {
if (users == null)
users = new ConcurrentHashMap<>();
else
users.clear();
for (User u : initUsrs.usrs)
users.put(toSubjectId(u.name()), u);
exec.execute(new RefreshUsersStorageWorker(initUsrs.usrs));
}
for (UserManagementOperation op : initUsrs.activeOps)
submitOperation(op);
}
readyForAuthFut.onDone();
}
/** {@inheritDoc} */
@Override public void onDoneBeforeTopologyUnlock(GridDhtPartitionsExchangeFuture fut) {
activateFut.onDone();
}
/**
*
*/
private void waitActivate() {
try {
activateFut.get();
}
catch (IgniteCheckedException e) {
// No-op.
}
}
/**
* @param msg Finish message.
*/
private void sendFinish(UserManagementOperationFinishedMessage msg) {
try {
ctx.io().sendToGridTopic(coordinator(), GridTopic.TOPIC_AUTH, msg, GridIoPolicy.SYSTEM_POOL);
}
catch (Exception e) {
U.error(log, "Failed to send UserManagementOperationFinishedMessage [op=" + msg.operationId() +
", node=" + coordinator() + ", err=" + msg.errorMessage() + ']', e);
}
}
/**
* Register operation, future and add operation worker to execute queue.
*
* @param op User operation.
*/
private void submitOperation(UserManagementOperation op) {
synchronized (mux) {
UserOperationFinishFuture fut = opFinishFuts.get(op.id());
if (fut == null) {
fut = new UserOperationFinishFuture(op.id());
opFinishFuts.put(op.id(), fut);
}
if (!fut.workerSubmitted()) {
fut.workerSubmitted(true);
activeOps.put(op.id(), op);
exec.execute(new UserOperationWorker(op, fut));
}
}
}
/**
* @param n Node.
* @return {@code true} if node holds user information. Otherwise returns {@code false}.
*/
private static boolean isNodeHoldsUsers(ClusterNode n) {
return !n.isClient() && !n.isDaemon();
}
/**
* {@inheritDoc}
*
* The current implementation of {@link GridSecurityProcessor} allows any Ignite node to join the Ignite cluster
* without authentication check.
*/
@Override public SecurityContext authenticateNode(ClusterNode node, SecurityCredentials cred) throws IgniteCheckedException {
return new SecurityContextImpl(
node.id(),
node.attribute(ATTR_IGNITE_INSTANCE_NAME),
REMOTE_NODE,
new InetSocketAddress(F.first(node.addresses()), 0));
}
/** {@inheritDoc} */
@Override public SecuritySubject authenticatedSubject(UUID subjId) throws IgniteCheckedException {
return null;
}
/** {@inheritDoc} */
@Override public Collection<SecuritySubject> authenticatedSubjects() throws IgniteCheckedException {
return null;
}
/** {@inheritDoc} */
@Override public boolean isGlobalNodeAuthentication() {
return false;
}
/** {@inheritDoc} */
@Override public void authorize(String name, SecurityPermission perm, SecurityContext securityCtx) throws SecurityException {
// No-op.
}
/** {@inheritDoc} */
@Override public void onSessionExpired(UUID subjId) {
// No-op.
}
/**
* {@inheritDoc}
*
* This method works with the assumption that {@link SecurityContext} associated with the Ignite node is stored in
* node attributes and is obtained automatically by the Ignite using the node ID
* (see {@link IgniteSecurityProcessor#withContext(java.util.UUID)}). Since we use the node ID as the subject ID
* during node authentication, this method is used for obtaining security context for thin clients only.
* Note, that the returned security context does not contain the address of the security subject.
* Since the client node does not store user data, the {@link SecurityContext} returned by the client node does
* not contain any user information, address, or username.
*/
@Override public SecurityContext securityContext(UUID subjId) {
if (ctx.clientNode())
return new SecurityContextImpl(subjId, null, REMOTE_CLIENT, null);
User user = users.get(subjId);
return user == null ? null : new SecurityContextImpl(subjId, user.name(), REMOTE_CLIENT, null);
}
/**
* Gets the user with the specified ID and login. It is necessary to check the login to make sure that there was
* no collision when calculating the user ID.
*/
private User findUser(UUID subjId, String login) {
User user = users.get(subjId);
if (user == null || !user.name().equals(login))
return null;
return user;
}
/** Calculates user id based on specified login. */
private UUID toSubjectId(String login) {
return UUID.nameUUIDFromBytes(login.getBytes());
}
/**
* @param op User operation to check.
* @throws IgniteAccessControlException If operation check fails: user hasn't permissions for user management
* or try to remove default user.
*/
public void checkUserOperation(UserManagementOperation op) throws IgniteAccessControlException {
assert op != null;
SecuritySubject subj = ctx.security().securityContext().subject();
try {
if (subj.type() == REMOTE_NODE) {
throw new IgniteAccessControlException("User management operations initiated on behalf of" +
" the Ignite node are not expected.");
}
if (!User.DFAULT_USER_NAME.equals(subj.login())
&& !(UserManagementOperation.OperationType.UPDATE == op.type() && subj.login().equals(op.user().name())))
throw new IgniteAccessControlException("User management operations are not allowed for user. " +
"[curUser=" + subj.login() + ']');
if (op.type() == UserManagementOperation.OperationType.REMOVE
&& User.DFAULT_USER_NAME.equals(op.user().name()))
throw new IgniteAccessControlException("Default user cannot be removed.");
}
catch (SecurityException e) {
throw new IgniteAccessControlException("Failed to perform user management operation.", e);
}