-
Notifications
You must be signed in to change notification settings - Fork 160
/
KapuaConnectionContext.java
206 lines (171 loc) · 6.45 KB
/
KapuaConnectionContext.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
/*******************************************************************************
* Copyright (c) 2017 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.broker.core.plugin;
import org.apache.activemq.command.ConnectionInfo;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.service.authentication.KapuaPrincipal;
import org.eclipse.kapua.service.authentication.token.AccessToken;
import org.eclipse.kapua.service.device.registry.connection.DeviceConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.cert.Certificate;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
/**
* Connection information container
*
* @since 1.0
*/
public class KapuaConnectionContext {
protected static final Logger logger = LoggerFactory.getLogger(KapuaConnectionContext.class);
private String brokerId;
private KapuaPrincipal principal;
private String userName;
private KapuaId scopeId;
private KapuaId userId;
private String accountName;
private String clientId;
private String fullClientId;
private String clientIp;
private String connectionId;
private String oldConnectionId;
private KapuaId kapuaConnectionId;
private ConnectorDescriptor connectorDescriptor;
private boolean[] hasPermissions;
private String brokerIpOrHostName;
private Certificate[] clientCertificates;
// use to track the allowed destinations for debug purpose
private List<String> authDestinations;
public KapuaConnectionContext(Long scopeId, String clientId, String fullClientIdPattern) {
authDestinations = new ArrayList<>();
this.clientId = clientId;
updateFullClientId(scopeId, fullClientIdPattern);
}
public KapuaConnectionContext(String brokerId, ConnectionInfo info) {
authDestinations = new ArrayList<>();
this.brokerId = brokerId;
userName = info.getUserName();
clientId = info.getClientId();
clientIp = info.getClientIp();
connectionId = info.getConnectionId().getValue();
if(info.getTransportContext() instanceof Certificate[]) {
clientCertificates = (Certificate[]) info.getTransportContext();
}
}
public KapuaConnectionContext(String brokerId, String brokerIpOrHostName, KapuaPrincipal kapuaPrincipal, String accountName, ConnectionInfo info, String fullClientIdPattern) {
authDestinations = new ArrayList<>();
this.brokerId = brokerId;
this.brokerIpOrHostName = brokerIpOrHostName;
this.accountName = accountName;
userName = info.getUserName();
clientId = kapuaPrincipal.getClientId();
scopeId = kapuaPrincipal.getAccountId();
clientIp = info.getClientIp();
connectionId = info.getConnectionId().getValue();
updateFullClientId(fullClientIdPattern);
}
public void update(AccessToken accessToken, String accountName, KapuaId scopeId, KapuaId userId, String connectorName, String brokerIpOrHostName, String fullClientIdPattern) {
this.accountName = accountName;
this.scopeId = scopeId;
this.userId = userId;
this.brokerIpOrHostName = brokerIpOrHostName;
connectorDescriptor = ConnectorDescriptorProviders.getDescriptor(connectorName);
if (connectorDescriptor == null) {
throw new IllegalStateException(String.format("Unable to find connector descriptor for connector '%s'", connectorName));
}
updateFullClientId(fullClientIdPattern);
principal = new KapuaPrincipalImpl(accessToken,
userName,
clientId,
clientIp);
}
public void updatePermissions(boolean[] hasPermissions) {
this.hasPermissions = hasPermissions;
}
public void updateKapuaConnectionId(DeviceConnection deviceConnection) {
kapuaConnectionId = deviceConnection != null ? deviceConnection.getId() : null;
}
public void updateOldConnectionId(String oldConnectionId) {
this.oldConnectionId = oldConnectionId;
}
private void updateFullClientId(String fullClientIdPattern) {
fullClientId = MessageFormat.format(fullClientIdPattern, scopeId.getId().longValue(), clientId);
}
private void updateFullClientId(Long scopeId, String fullClientIdPattern) {
fullClientId = MessageFormat.format(fullClientIdPattern, scopeId, clientId);
}
public String getFullClientId() {
return fullClientId;
}
public String getAccountName() {
return accountName;
}
public String getBrokerId() {
return brokerId;
}
public Certificate[] getClientCertificates() {
return clientCertificates;
}
public String getUserName() {
return userName;
}
public KapuaId getScopeId() {
return scopeId;
}
public long getScopeIdAsLong() {
return scopeId != null ? scopeId.getId().longValue() : 0;
}
public String getClientId() {
return clientId;
}
public String getClientIp() {
return clientIp;
}
public KapuaPrincipal getPrincipal() {
return principal;
}
public String getConnectionId() {
return connectionId;
}
public String getOldConnectionId() {
return oldConnectionId;
}
public ConnectorDescriptor getConnectorDescriptor() {
return connectorDescriptor;
}
public KapuaId getKapuaConnectionId() {
return kapuaConnectionId;
}
public KapuaId getUserId() {
return userId;
}
public boolean[] getHasPermissions() {
return hasPermissions;
}
public String getBrokerIpOrHostName() {
return brokerIpOrHostName;
}
public void addAuthDestinationToLog(String message) {
if (logger.isDebugEnabled()) {
authDestinations.add(message);
}
}
public void logAuthDestinationToLog() {
if (!authDestinations.isEmpty()) {
logger.debug("Authorization map:");
for (String str : authDestinations) {
logger.debug(str);
}
}
}
}