ImpersonatedConnectionManager.java
8.89 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.service;
import com.adobe.service.ConnectionResource;
import com.adobe.service.ServiceAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class ImpersonatedConnectionManager {
private static Map<ServiceAPI, List<Credential>> credentialMap = new HashMap<ServiceAPI, List<Credential>>();
private static Map<ServiceAPI, Boolean> connectedServiceMap = Collections.synchronizedMap(new HashMap());
private static Map<ServiceAPI, Map<ConnectionResource, Credential>> serviceConnectionResourceMap = Collections.synchronizedMap(new HashMap());
private static final Logger logger = LoggerFactory.getLogger(ImpersonatedConnectionManager.class);
private ImpersonatedConnectionManager() {
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static Credential acquireCredential(ServiceAPI svc, ConnectionResource cr) {
if (svc == null || cr == null) {
return null;
}
Map<ServiceAPI, List<Credential>> map = credentialMap;
synchronized (map) {
List<Credential> credentialWallet = credentialMap.get(svc);
if (credentialWallet == null) {
return null;
}
Credential cred = ImpersonatedConnectionManager.getNextFreeCredential(credentialWallet);
if (cred == null) {
return null;
}
Map<ConnectionResource, Credential> connectionResourceMap = serviceConnectionResourceMap.get(svc);
if (connectionResourceMap == null) {
connectionResourceMap = new HashMap<ConnectionResource, Credential>();
serviceConnectionResourceMap.put(svc, connectionResourceMap);
}
connectionResourceMap.put(cr, cred);
logger.info("Service " + svc + ": Resource " + cr + " acquired credential " + cred.user);
return cred;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static void releaseCredential(ServiceAPI svc, ConnectionResource cr) {
if (svc == null || cr == null) {
return;
}
Map<ServiceAPI, List<Credential>> map = credentialMap;
synchronized (map) {
List<Credential> credentialWallet = credentialMap.get(svc);
if (credentialWallet == null) {
return;
}
Map<ConnectionResource, Credential> connectionResourceMap = serviceConnectionResourceMap.get(svc);
if (connectionResourceMap == null) {
return;
}
Credential cred = connectionResourceMap.remove(cr);
if (cred == null) {
return;
}
logger.info("Service " + svc + ": Resource " + cr + " returned credential " + cred.user);
ImpersonatedConnectionManager.returnFreeCredential(credentialWallet, cred);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static void addCredentials(ServiceAPI svc, Map<String, String> newCredentials) {
if (svc == null || newCredentials == null) {
return;
}
boolean bModifyServicePool = false;
Map<ServiceAPI, List<Credential>> map = credentialMap;
synchronized (map) {
List<Credential> credentialWallet = credentialMap.get(svc);
if (credentialWallet == null) {
if (connectedServiceMap.containsKey(svc)) {
logger.warn("Service {0}: Cannot accept impersonation credentials because there may already be running instance(s).", (Object)svc.getName());
return;
}
bModifyServicePool = true;
credentialWallet = new ArrayList<Credential>();
credentialMap.put(svc, credentialWallet);
}
for (Map.Entry<String, String> entry : newCredentials.entrySet()) {
boolean isInUse;
if (entry.getKey() == null || entry.getValue() == null) continue;
Credential cred = new Credential(entry.getKey(), entry.getValue());
Map<ConnectionResource, Credential> connectionResourceMap = serviceConnectionResourceMap.get(svc);
boolean bl = isInUse = connectionResourceMap == null ? false : connectionResourceMap.containsValue(cred);
if (!credentialWallet.contains(cred) && !isInUse) {
if (bModifyServicePool) {
credentialWallet.add(cred);
continue;
}
logger.warn("Service {0}: Rejecting additional impersonation credential '{1}' as pool size has already been fixed.", (Object)svc.getName(), (Object)cred.user);
continue;
}
if (credentialWallet.contains(cred)) {
int index = credentialWallet.indexOf(cred);
Credential savedCredential = credentialWallet.get(index);
if (savedCredential.password.equals(cred.password)) continue;
savedCredential.password = cred.password;
logger.info("Service {0}: Accepting new password for user {1}", (Object)svc.getName(), (Object)cred.user);
continue;
}
ImpersonatedConnectionManager.updateInUseCredential(svc, cred);
}
if (bModifyServicePool && svc.getPoolMax() > credentialWallet.size()) {
logger.warn("Service {0}: Reducing maximum pool size from {1} to {2} to match number of impersonation credentials.", new Object[]{svc.getName(), svc.getPoolMax(), credentialWallet.size()});
svc.setPoolMax(credentialWallet.size());
}
}
}
private static void updateInUseCredential(ServiceAPI svc, Credential cred) {
Map<ConnectionResource, Credential> connectionResourceMap = serviceConnectionResourceMap.get(svc);
if (connectionResourceMap != null) {
for (Map.Entry<ConnectionResource, Credential> entry : connectionResourceMap.entrySet()) {
Credential inUseCred = entry.getValue();
if (!inUseCred.equals(cred)) continue;
if (inUseCred.password.equals(cred.password)) break;
inUseCred.password = cred.password;
entry.setValue(inUseCred);
logger.info("Service {0}: Accepting new password for user {1}", (Object)svc.getName(), (Object)cred.user);
break;
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static void removeCredentials(ServiceAPI svc) {
if (svc == null) {
return;
}
Map<ServiceAPI, List<Credential>> map = credentialMap;
synchronized (map) {
credentialMap.remove(svc);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static boolean isImpersonatedConnection(ServiceAPI svc) {
if (svc == null) {
return false;
}
Map<ServiceAPI, List<Credential>> map = credentialMap;
synchronized (map) {
if (!connectedServiceMap.containsKey(svc)) {
connectedServiceMap.put(svc, Boolean.TRUE);
}
return credentialMap.containsKey(svc);
}
}
private static Credential getNextFreeCredential(List<Credential> credentialWallet) {
Credential cred = null;
if (credentialWallet != null && credentialWallet.size() > 0) {
cred = credentialWallet.remove(0);
}
return cred;
}
private static void returnFreeCredential(List<Credential> credentialWallet, Credential cred) {
if (credentialWallet != null && cred != null) {
credentialWallet.add(cred);
}
}
public static class Credential {
String user;
String password;
Credential(String str1, String str2) {
this.user = str1;
this.password = str2;
}
public boolean equals(Object cred) {
return cred instanceof Credential && this.isEquivalent(this.user, ((Credential)cred).user, false);
}
public int hashCode() {
return this.user.hashCode();
}
private boolean isEquivalent(String s1, String s2, boolean bIgnoreCase) {
if (s1 == null && s2 == null) {
return true;
}
if (s1 == null || s2 == null) {
return false;
}
return bIgnoreCase ? s1.equalsIgnoreCase(s2) : s1.equals(s2);
}
}
}