Implementing a component to invoke APIM publisher REST API (Scope mapping) #104

Merged
charithag merged 17 commits from pasindu/device-mgt-core:scopeMapAPILayer into master 1 year ago
pasindu commented 1 year ago

Purpose

The purpose of this PR is to fix http://roadmap.entgra.net/issues/10031

Goals

Implement a component to invoke APIM publisher REST API

Approach

  • Implement a new component for the APIM REST API services.
  • Change the updateScopeRoleMapping method to update the scopes by the API layer.
### Purpose The purpose of this PR is to fix http://roadmap.entgra.net/issues/10031 ### Goals Implement a component to invoke APIM publisher REST API ### Approach - Implement a new component for the APIM REST API services. - Change the updateScopeRoleMapping method to update the scopes by the API layer.
pasindu changed title from Implementing a component to invoke APIM publisher REST API (Scope maping) to WIP: Implementing a component to invoke APIM publisher REST API (Scope maping) 1 year ago
pasindu changed title from WIP: Implementing a component to invoke APIM publisher REST API (Scope maping) to WIP: Implementing a component to invoke APIM publisher REST API (Scope mapping) 1 year ago
pasindu force-pushed scopeMapAPILayer from 9a2fea8b48 to ee6d667345 1 year ago
amalka.subasinghe requested changes 1 year ago
<modelVersion>4.0.0</modelVersion>
<artifactId>io.entgra.devicemgt.apimgt.extension.rest.api</artifactId>
<packaging>bundle</packaging>
<name>Entgra - Device API Management Extension Publisher API</name>

Device Mgt extension for APIM REST API

Device Mgt extension for APIM REST API
<!-- </plugin>-->
</plugins>
</build>
<properties>

do not add this property

do not add this property
jsonObject.put("saasApp", true);
RequestBody requestBody = RequestBody.Companion.create(jsonObject.toString(), JSON);
String applicationEndpoint = "https://localhost:9443/client-registration/v0.17/register";

get url from api-manager xml

get url from api-manager xml
throws APIApplicationServicesException {
RequestBody requestBody = RequestBody.Companion.create(nameValuePairs.toString(), JSON);
String tokenEndPoint = "https://localhost:9443/oauth2/token";

need to fix url generation

need to fix url generation
public JSONObject getScopes(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo)
throws APIApplicationServicesException, BadRequestException {
String getScopesUrl = "https://localhost:9443/api/am/publisher/v2/scopes?limit=1000";

need to fix

need to fix
try {
Response response = client.newCall(request).execute();
if (response.code() == HttpStatus.SC_OK) {

should swap the condition, other can throw null pointer exception

should swap the condition, other can throw null pointer exception
//TODO: max attempt count
return updateSharedScope(apiApplicationKey, refreshedAccessToken, scope);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
log.info(response);

give proper log message

give proper log message
}
} catch (IOException e) {
String msg = "Error occurred while processing the response";
throw new APIApplicationServicesException(msg);

pass e, when throwing otherwise real exception will not be able to find

pass e, when throwing otherwise real exception will not be able to find
package io.entgra.devicemgt.apimgt.extension.rest.api.constants;

no licence header

no licence header
*/
package org.wso2.carbon.apimgt.webapp.publisher;

this fix not required for this PR , pls remove

this fix not required for this PR , pls remove
throw new APIManagerPublisherException(e);
}
catch (APIApplicationServicesException e) {
log.error(e);

handle exceptions with proper messages

handle exceptions with proper messages
pom.xml Outdated
<version>${carbon.device.mgt.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
change the artificat id and device id according to this https://docs.google.com/document/d/179pfBB2uglDSjCHMuKMPgCJ9AQlrCe0IFuRO3wOMqd0/edit#heading=h.2p4l6vczsdts
pasindu force-pushed scopeMapAPILayer from b020a82135 to 3ef3310ca5 1 year ago
pasindu force-pushed scopeMapAPILayer from 3ef3310ca5 to 2ccf1dd022 1 year ago
pasindu force-pushed scopeMapAPILayer from 6dc02dbc06 to 3be9dd6bdc 1 year ago
pasindu force-pushed scopeMapAPILayer from 3be9dd6bdc to 6008976e20 1 year ago
pasindu force-pushed scopeMapAPILayer from 8a61dc802b to 7a79030421 1 year ago
pasindu changed title from WIP: Implementing a component to invoke APIM publisher REST API (Scope mapping) to Implementing a component to invoke APIM publisher REST API (Scope mapping) 1 year ago
pasindu requested review from amalka.subasinghe 1 year ago
pasindu force-pushed scopeMapAPILayer from f856d0f1d6 to b7b3b1f203 1 year ago
Gimhan-minion reviewed 1 year ago
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
return sc.getSocketFactory();
} catch (KeyManagementException | NoSuchAlgorithmException e) {

We can use an error msg to handle exceptions here

We can use an error msg to handle exceptions here
public JSONObject getScopes(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo)
throws APIServicesException, BadRequestException {
String getAllScopesUrl = "https://" + host + ":" + port + Constants.GET_ALL_SCOPES;

Here we can store https:// and : in constants as well.

Here we can store `https://` and `:` in constants as well.
throws APIServicesException, BadRequestException {
String keyValue = new String(Base64.encodeBase64((key).getBytes())).replace("=", "");
String getScopeUrl = "https://" + host + ":" + port + Constants.GET_SCOPE + keyValue;

Here we can store https:// and : in constants as well.

Here we can store `https://` and `:` in constants as well.
public boolean updateSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope)
throws APIServicesException, BadRequestException {
String updateScopeUrl = "https://" + host + ":" + port + Constants.GET_SCOPE + scope.getId();

Here we can store https:// and : in constants as well.

Here we can store `https://` and `:` in constants as well.
JSONArray scopeList = (JSONArray) scopeObject.get("list");
for (int i = 0; i < scopeList.length(); i++) {
JSONObject scopeObj = null;
scopeObj = scopeList.getJSONObject(i);

We can use an inline variable here JSONObject scopeObj = scopeList.getJSONObject(i);

We can use an inline variable here `JSONObject scopeObj = scopeList.getJSONObject(i);`
pasindu force-pushed scopeMapAPILayer from f2c429778d to 2ca425819e 1 year ago
pasindu force-pushed scopeMapAPILayer from 751c2cae72 to 1ee66a860f 1 year ago
charithag requested changes 1 year ago
}
protected static OkHttpClient getOkHttpClient() {
X509TrustManager trustAllCerts = new X509TrustManager() {
Owner

Why trusting all certs? This a security risk

Why trusting all certs? This a security risk
private static SSLSocketFactory getSimpleTrustedSSLSocketFactory() {
try {
TrustManager[] trustAllCerts = new TrustManager[]{
Owner

Why trusting all certs? This a security risk

Why trusting all certs? This a security risk
log.error(msg);
throw new BadRequestException(msg);
} else {
return null;
Owner

If we get some other http status like bad gateway or internal server error, this can lead to incorrect state. Better to throw an exception (Ex: UnexpectedResponseException) and handle it in the other end.

If we get some other http status like bad gateway or internal server error, this can lead to incorrect state. Better to throw an exception (Ex: UnexpectedResponseException) and handle it in the other end.
log.error(msg);
throw new BadRequestException(msg);
} else {
return false;
Owner

If we got some other http status like bad gateway or internal server error, this will lead to incorrect state. Better to throw an exception (Ex: UnexpectedResponseException) and handle it in the other end.

If we got some other http status like bad gateway or internal server error, this will lead to incorrect state. Better to throw an exception (Ex: UnexpectedResponseException) and handle it in the other end.
log.error(msg);
throw new BadRequestException(msg);
} else {
return false;
Owner

If we get some other http status like bad gateway or internal server error, this will lead to incorrect state. Better to throw an exception (Ex: UnexpectedResponseException) and handle it in the other end.

If we get some other http status like bad gateway or internal server error, this will lead to incorrect state. Better to throw an exception (Ex: UnexpectedResponseException) and handle it in the other end.
pasindu force-pushed scopeMapAPILayer from 1ee66a860f to 53798af576 1 year ago
pasindu force-pushed scopeMapAPILayer from 53798af576 to 455917f21e 1 year ago
pasindu added 1 commit 1 year ago
pasindu requested review from charithag 1 year ago
pasindu added 1 commit 1 year ago
amalka.subasinghe approved these changes 1 year ago
charithag approved these changes 1 year ago
charithag merged commit 99df875fb3 into master 1 year ago

Reviewers

amalka.subasinghe approved these changes 1 year ago
charithag approved these changes 1 year ago
The pull request has been merged as 99df875fb3.
Sign in to join this conversation.
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date

No due date set.

Dependencies

No dependencies set.

Reference: community/device-mgt-core#104
Loading…
There is no content yet.