Skip to content

Commit aee84f0

Browse files
committed
F2F fix
1 parent 506da65 commit aee84f0

File tree

10 files changed

+218
-6
lines changed

10 files changed

+218
-6
lines changed

buildtokenproperties.sh

100755100644
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ M2MAUTHCONFIG_USERPROFILES_UPDATE=$(eval "echo \$${ENV}_M2MAUTHCONFIG_USERPROFIL
4444
M2MAUTHCONFIG_USERPROFILES_READ=$(eval "echo \$${ENV}_M2MAUTHCONFIG_USERPROFILES_READ")
4545
M2MAUTHCONFIG_USERPROFILES_DELETE=$(eval "echo \$${ENV}_M2MAUTHCONFIG_USERPROFILES_DELETE")
4646

47+
MEMBER_API_ENDPOINT=$(eval "echo \$${ENV}_MEMBER_API_ENDPOINT")
48+
4749
DOMAIN=$(eval "echo \$${ENV}_DOMAIN")
4850
SMTP=$(eval "echo \$${ENV}_SMTP")
4951
DB_HOST_IP=$(eval "echo \$${ENV}_DB_HOST_IP")
@@ -118,6 +120,7 @@ perl -pi -e "s/\{\{AUTH_DB_USER\}\}/$AUTH_DB_USER/g" $CONFFILENAME
118120
perl -pi -e "s/\{\{AUTH_DB_PASSWORD\}\}/$AUTH_DB_PASSWORD/g" $CONFFILENAME
119121
#perl -pi -e "s/\{\{EVENTBUSSERVICE_ENDPOINT\}\}/$EVENTBUSSERVICE_ENDPOINT/g" $CONFFILENAME
120122
perl -pi -e "s|\{\{EVENTBUSSERVICE_ENDPOINT\}\}|$EVENTBUSSERVICE_ENDPOINT|g" $CONFFILENAME
123+
perl -pi -e "s|\{\{MEMBER_API_ENDPOINT\}\}|$MEMBER_API_ENDPOINT|g" $CONFFILENAME
121124
perl -pi -e "s/\{\{EVENTBUSSERVICE_TOPIC\}\}/$EVENTBUSSERVICE_TOPIC/g" $CONFFILENAME
122125
perl -pi -e "s/\{\{EVENTBUSSERVICE_ORIGINATOR\}\}/$EVENTBUSSERVICE_ORIGINATOR/g" $CONFFILENAME
123126
perl -pi -e "s/\{\{M2MAUTHCONFIG_CID\}\}/$M2MAUTHCONFIG_CID/g" $CONFFILENAME

src/main/java/com/appirio/tech/core/service/identity/IdentityApplication.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.appirio.tech.core.api.v3.dropwizard.APIApplication;
3636
import com.appirio.tech.core.api.v3.util.jdbi.TCIDArgumentFactory;
3737
import com.appirio.tech.core.service.identity.clients.EventBusServiceClient;
38+
import com.appirio.tech.core.service.identity.clients.MemberApiClient;
3839
import com.appirio.tech.core.service.identity.dao.ClientDAO;
3940
import com.appirio.tech.core.service.identity.dao.ExternalAccountDAO;
4041
import com.appirio.tech.core.service.identity.dao.GroupDAO;
@@ -192,6 +193,11 @@ public void run(IdentityConfiguration configuration, Environment environment) th
192193
IdentityProviderResource identityProviderResource = new IdentityProviderResource(identityProviderDAO);
193194
environment.jersey().register(identityProviderResource);
194195

196+
final Client apiClient = new JerseyClientBuilder(environment).using(new JerseyClientConfiguration())
197+
.build(getName());
198+
final MemberApiClient memberApiClient = new MemberApiClient(apiClient,
199+
configuration.getMemberApiClientConfig(), configuration.getM2mAuthConfiguration());
200+
195201
// RDS
196202
final DBIFactory authDBIFactory = new DBIFactory();
197203
final DBI authjdbi = authDBIFactory.build(environment, configuration.getAuthorizationDatabase(), "Authorization");
@@ -212,7 +218,7 @@ public void run(IdentityConfiguration configuration, Environment environment) th
212218
roleDAO.setShiroSettings(shiroSettings);
213219

214220
// creating new resource for every request
215-
RoleResource roleResource = new RoleResource(roleDAO);
221+
RoleResource roleResource = new RoleResource(roleDAO, memberApiClient);
216222
environment.jersey().register(roleResource);
217223

218224
final PermissionPolicyDAO policyDAO = authjdbi.onDemand(PermissionPolicyDAO.class);
@@ -221,8 +227,6 @@ public void run(IdentityConfiguration configuration, Environment environment) th
221227
environment.jersey().register(polResource);
222228
}
223229

224-
final Client apiClient = new JerseyClientBuilder(environment).using(new JerseyClientConfiguration())
225-
.build(getName());
226230
final EventBusServiceClient eventBusServiceClient = new EventBusServiceClient(apiClient,
227231
configuration.getEventBusServiceClientConfig(), configuration.getM2mAuthConfiguration());
228232
// Resources::users

src/main/java/com/appirio/tech/core/service/identity/IdentityConfiguration.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,14 @@ public class IdentityConfiguration extends APIBaseConfiguration {
108108
@NotNull
109109
@JsonProperty("eventBusServiceClient")
110110
private final BaseClientConfiguration eventBusServiceClientConfig = new BaseClientConfiguration();
111-
111+
112+
/**
113+
* The Member API URL
114+
*/
115+
@Valid
116+
@NotNull
117+
@JsonProperty("memberApiClient")
118+
private final BaseClientConfiguration memberApiClientConfig = new BaseClientConfiguration();
112119

113120

114121
public DataSourceFactory getDataSourceFactory() {
@@ -181,4 +188,12 @@ public M2mAuthConfiguration getM2mAuthConfiguration() {
181188
public BaseClientConfiguration getEventBusServiceClientConfig() {
182189
return this.eventBusServiceClientConfig;
183190
}
191+
192+
/**
193+
* Get memberApiClientConfig
194+
* @return memberApiClientConfig
195+
*/
196+
public BaseClientConfiguration getMemberApiClientConfig() {
197+
return memberApiClientConfig;
198+
}
184199
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.appirio.tech.core.service.identity.clients;
2+
3+
import com.appirio.clients.BaseClient;
4+
import com.appirio.clients.BaseClientConfiguration;
5+
import com.appirio.tech.core.api.v3.TCID;
6+
import com.appirio.tech.core.service.identity.M2mAuthConfiguration;
7+
import com.appirio.tech.core.service.identity.representation.MemberInfo;
8+
import com.appirio.tech.core.service.identity.util.Utils;
9+
import org.eclipse.jetty.http.HttpStatus;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import javax.ws.rs.client.Client;
14+
import javax.ws.rs.client.Invocation;
15+
import javax.ws.rs.client.WebTarget;
16+
import javax.ws.rs.core.GenericType;
17+
import javax.ws.rs.core.MediaType;
18+
import javax.ws.rs.core.Response;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.Set;
22+
23+
public class MemberApiClient extends BaseClient {
24+
/**
25+
* The logger for this class
26+
*/
27+
private final static Logger LOGGER = LoggerFactory.getLogger(MemberApiClient.class);
28+
29+
private M2mAuthConfiguration m2mAuthConfiguration;
30+
31+
/**
32+
* Constructor.
33+
*
34+
* @param client the Jersey client
35+
* @param config the configuration
36+
* @param m2mAuthConfiguration m2m config
37+
*/
38+
public MemberApiClient(Client client, BaseClientConfiguration config,
39+
M2mAuthConfiguration m2mAuthConfiguration) {
40+
super(client, config);
41+
this.m2mAuthConfiguration = m2mAuthConfiguration;
42+
}
43+
44+
public List<MemberInfo> getUserInfoList(Set<TCID> userIds) {
45+
List<MemberInfo> res = new ArrayList<>();
46+
try {
47+
StringBuilder strBuffer = new StringBuilder(this.config.getEndpoint());
48+
strBuffer.append("?fields=handle,email,userId");
49+
for (TCID userId: userIds) {
50+
strBuffer.append("&userIds=");
51+
strBuffer.append(userId.getId());
52+
}
53+
WebTarget target = this.client.target(strBuffer.toString());
54+
final Invocation.Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
55+
String authToken = Utils.generateAuthToken(m2mAuthConfiguration);
56+
57+
Response response = request.header("Authorization", "Bearer " + authToken).get();
58+
if (response.getStatusInfo().getStatusCode() != HttpStatus.OK_200) {
59+
LOGGER.error("Unable to fire the event: {}", response);
60+
} else {
61+
res = response.readEntity(new GenericType<List<MemberInfo>>() {});
62+
}
63+
} catch (Exception e) {
64+
LOGGER.error("Error occurs while getting member info: {}", e);
65+
}
66+
return res;
67+
}
68+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.appirio.tech.core.service.identity.representation;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
/**
6+
* Member info containing userId, email and handle
7+
*/
8+
public class MemberInfo {
9+
10+
@JsonProperty("email")
11+
private String email;
12+
13+
@JsonProperty("handle")
14+
private String handle;
15+
16+
@JsonProperty("userId")
17+
private Long userId;
18+
19+
public String getEmail() {
20+
return email;
21+
}
22+
23+
public void setEmail(String email) {
24+
this.email = email;
25+
}
26+
27+
public String getHandle() {
28+
return handle;
29+
}
30+
31+
public void setHandle(String handle) {
32+
this.handle = handle;
33+
}
34+
35+
public Long getUserId() {
36+
return userId;
37+
}
38+
39+
public void setUserId(Long userId) {
40+
this.userId = userId;
41+
}
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.appirio.tech.core.service.identity.representation;
2+
3+
import com.appirio.tech.core.api.v3.model.AbstractIdResource;
4+
import com.appirio.tech.core.api.v3.model.annotation.ApiMapping;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Role and subject info list
10+
*/
11+
public class RoleSubjects extends AbstractIdResource {
12+
private String roleName;
13+
private List<MemberInfo> subjects;
14+
15+
public String getRoleName() {
16+
return roleName;
17+
}
18+
19+
public void setRoleName(String roleName) {
20+
this.roleName = roleName;
21+
}
22+
23+
public List<MemberInfo> getSubjects() {
24+
return subjects;
25+
}
26+
27+
@ApiMapping(queryDefault=false)
28+
public void setSubjects(List<MemberInfo> subjects) {
29+
this.subjects = subjects;
30+
}
31+
}

src/main/java/com/appirio/tech/core/service/identity/resource/RoleResource.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
import com.appirio.tech.core.api.v3.response.ApiResponse;
1717
import com.appirio.tech.core.api.v3.response.ApiResponseFactory;
1818
import com.appirio.tech.core.auth.AuthUser;
19+
import com.appirio.tech.core.service.identity.clients.MemberApiClient;
1920
import com.appirio.tech.core.service.identity.dao.RoleDAO;
21+
import com.appirio.tech.core.service.identity.representation.MemberInfo;
2022
import com.appirio.tech.core.service.identity.representation.Role;
23+
import com.appirio.tech.core.service.identity.representation.RoleSubjects;
2124
import com.appirio.tech.core.service.identity.util.Utils;
2225
import com.codahale.metrics.annotation.Timed;
2326
import io.dropwizard.auth.Auth;
@@ -38,8 +41,11 @@
3841
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
3942
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
4043

44+
import java.util.ArrayList;
4145
import java.util.Iterator;
4246
import java.util.List;
47+
import java.util.Map;
48+
import java.util.stream.Collectors;
4349

4450
/**
4551
* API resource to manage roles.
@@ -63,12 +69,15 @@ public class RoleResource implements GetResource<Role>, DDLResource<Role>{
6369
private static final Logger logger = Logger.getLogger(RoleResource.class);
6470

6571
protected RoleDAO dao;
72+
73+
protected MemberApiClient memberApiClient;
6674

6775
public RoleResource() {
6876
}
6977

70-
public RoleResource(RoleDAO roleDao) {
78+
public RoleResource(RoleDAO roleDao, MemberApiClient memberApiClient) {
7179
this.dao = roleDao;
80+
this.memberApiClient = memberApiClient;
7281
}
7382
public RoleDAO getDao() {
7483
return dao;
@@ -78,7 +87,15 @@ public void setDao(RoleDAO dao) {
7887
this.dao = dao;
7988
}
8089

81-
/**
90+
public MemberApiClient getMemberApiClient() {
91+
return memberApiClient;
92+
}
93+
94+
public void setMemberApiClient(MemberApiClient memberApiClient) {
95+
this.memberApiClient = memberApiClient;
96+
}
97+
98+
/**
8299
* Get roles.
83100
*
84101
* @param authUser the authenticated user
@@ -258,10 +275,31 @@ public ApiResponse getObject(
258275
}
259276

260277
Role role = null;
278+
RoleSubjects roleSubjects = null;
261279
try {
262280
if (hasField(selector, "subjects")) {
263281
logger.info("Found subjects");
264282
role = dao.getSubjects(roleId);
283+
if (role != null) {
284+
roleSubjects = new RoleSubjects();
285+
roleSubjects.setRoleName(role.getRoleName());
286+
roleSubjects.setSubjects(new ArrayList<>());
287+
if (!role.getSubjects().isEmpty()) {
288+
List<MemberInfo> memberList = memberApiClient.getUserInfoList(role.getSubjects());
289+
Map<String, MemberInfo> infoMap = memberList.stream().collect(
290+
Collectors.toMap(m -> m.getUserId().toString(), model -> model));
291+
for (TCID userId: role.getSubjects()) {
292+
MemberInfo info = new MemberInfo();
293+
info.setUserId(Long.parseLong(userId.getId()));
294+
if (infoMap.containsKey(userId.getId())) {
295+
MemberInfo realInfo = infoMap.get(userId.getId());
296+
info.setEmail(realInfo.getEmail());
297+
info.setHandle(realInfo.getHandle());
298+
}
299+
roleSubjects.getSubjects().add(info);
300+
}
301+
}
302+
}
265303
} else {
266304
role = dao.populateById(selector, roleId);
267305
}
@@ -273,6 +311,9 @@ public ApiResponse getObject(
273311
if (role == null)
274312
throw new APIRuntimeException(HttpServletResponse.SC_NOT_FOUND);
275313

314+
if (roleSubjects != null) {
315+
return ApiResponseFactory.createFieldSelectorResponse(roleSubjects, selector);
316+
}
276317
return ApiResponseFactory.createFieldSelectorResponse(role, selector);
277318
}
278319

src/main/resources/config.yml.localdev

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,7 @@ logging:
190190
archivedLogFilenamePattern: /var/log/ap-identity-%d.log
191191
archivedFileCount: 50
192192
timeZone: UTC
193+
194+
#for Member API, is for the real dev environment
195+
memberApiClient:
196+
endpoint: end-point-url

token.properties.localdev

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
@eventBusServiceClient.configuration.topic@=dummy
6161
@eventBusServiceClient.configuration.originator@=dummy-service
6262

63+
@memberApiClient.endpoint@=dummy
64+
6365
@m2mAuthConfig.clientId@=dummy
6466
@m2mAuthConfig.clientSecret@=dummy
6567
@m2mAuthConfig.audience@=dummy

token.properties.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
@eventBusServiceClient.configuration.topic@={{EVENTBUSSERVICE_TOPIC}}
8787
@eventBusServiceClient.configuration.originator@={{EVENTBUSSERVICE_ORIGINATOR}}
8888

89+
@memberApiClient.endpoint@={{MEMBERSERVICE_ENDPOINT}}
90+
8991
@m2mAuthConfig.clientId@={{M2MAUTHCONFIG_CID}}
9092
@m2mAuthConfig.clientSecret@={{M2MAUTHCONFIG_SECRET}}
9193
@m2mAuthConfig.audience@={{M2MAUTHCONFIG_AUDIENCE}}

0 commit comments

Comments
 (0)