Skip to content

Commit

Permalink
Add endpointType to client certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
RusJaI committed May 26, 2024
1 parent 01f3d5a commit fbc7d30
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ String addBlockCondition(String conditionType, String conditionValue, boolean co
* @throws APIManagementException API Management Exception.
*/
int addClientCertificate(String userName, ApiTypeWrapper apiTypeWrapper, String certificate, String alias,
String tierName, String organization) throws APIManagementException;
String tierName, String endpointType, String organization) throws APIManagementException;

/**
* Method to remove the certificate which mapped to the given alias, endpoint from publisher and gateway nodes.
Expand Down Expand Up @@ -965,7 +965,7 @@ ClientCertificateDTO getClientCertificate(String alias, ApiTypeWrapper apiTypeWr
* @throws APIManagementException API Management Exception.
*/
int updateClientCertificate(String certificate, String alias, ApiTypeWrapper apiTypeWrapper, String tier,
int tenantId, String organization) throws APIManagementException;
String endpointType, int tenantId, String organization) throws APIManagementException;

/**
* Retrieve the certificate which matches the given alias.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ public class ClientCertificateDTO {
private String certificate;
private String uniqueId;
private String tierName;
private String endpointType;
private APIIdentifier apiIdentifier;

public ClientCertificateDTO() {
}

/**
* To get the identifier of the API related with client certificate.
*
Expand Down Expand Up @@ -65,6 +69,23 @@ public void setTierName(String tierName) {
this.tierName = tierName;
}

/**
* To get the endpoint type of the certificate.
* @return endpoint type.
*/
public String getEndpointType() {
return endpointType;
}

/**
* To set the endpoint type for the current certificate.
*
* @param endpointType endpoint type (whether PRODUCTION or SANDBOX).
*/
public void setEndpointType(String endpointType) {
this.endpointType = endpointType;
}

/**
* To get the alias of the certificate.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3907,14 +3907,15 @@ public int addCertificate(String userName, String certificate, String alias, Str

@Override
public int addClientCertificate(String userName, ApiTypeWrapper apiTypeWrapper, String certificate, String alias,
String tierName, String organization) throws APIManagementException {
String tierName, String endpointType, String organization)
throws APIManagementException {
checkAccessControlPermission(userNameWithoutChange, apiTypeWrapper.getAccessControl(),
apiTypeWrapper.getAccessControlRoles());
ResponseCode responseCode = ResponseCode.INTERNAL_SERVER_ERROR;

int tenantId = APIUtil.getInternalOrganizationId(organization);
responseCode = certificateManager
.addClientCertificate(apiTypeWrapper.getId(), certificate, alias, tierName, tenantId, organization);
responseCode = certificateManager.addClientCertificate(apiTypeWrapper.getId(), certificate,
alias, tierName, endpointType, tenantId, organization);
return responseCode.getResponseCode();
}

Expand Down Expand Up @@ -4058,11 +4059,12 @@ public int updateCertificate(String certificateString, String alias) throws APIM

@Override
public int updateClientCertificate(String certificate, String alias, ApiTypeWrapper apiTypeWrapper,
String tier, int tenantId, String organization) throws APIManagementException {
String tier, String endpointType, int tenantId, String organization)
throws APIManagementException {
checkAccessControlPermission(userNameWithoutChange, apiTypeWrapper.getAccessControl(),
apiTypeWrapper.getAccessControlRoles());
ResponseCode responseCode = certificateManager
.updateClientCertificate(certificate, alias, tier, tenantId, organization);
.updateClientCertificate(certificate, alias, tier, endpointType, tenantId, organization);
return responseCode != null ?
responseCode.getResponseCode() :
ResponseCode.INTERNAL_SERVER_ERROR.getResponseCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ public SubscribedAPI getSubscriptionByUUID(String uuid) throws APIManagementExce

@Override
public int addClientCertificate(String userName, ApiTypeWrapper apiTypeWrapper, String certificate, String alias,
String tierName, String organization) throws APIManagementException {
return super.addClientCertificate(userName, apiTypeWrapper, certificate, alias, tierName, organization);
String tierName, String endpointType, String organization) throws APIManagementException {
return super.addClientCertificate(userName, apiTypeWrapper, certificate, alias, tierName, endpointType,
organization);
}

@Override
Expand Down Expand Up @@ -156,8 +157,10 @@ public int updateCertificate(String certificateString, String alias) throws APIM

@Override
public int updateClientCertificate(String certificate, String alias, ApiTypeWrapper apiIdentifier,
String tier, int tenantId, String organization) throws APIManagementException {
return super.updateClientCertificate(certificate, alias, apiIdentifier, tier, tenantId, organization);
String tier, String endpointType, int tenantId, String organization)
throws APIManagementException {
return super.updateClientCertificate(certificate, alias, apiIdentifier, tier, endpointType,
tenantId, organization);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public interface CertificateManager {
* certificate is expired.
*/
ResponseCode addClientCertificate(Identifier apiIdentifier, String certificate, String alias, String tierName,
int tenantId, String organization);
String endpointType, int tenantId, String organization);

/**
* Method to delete the client certificate from publisher node.
Expand Down Expand Up @@ -236,8 +236,8 @@ List<ClientCertificateDTO> searchClientCertificates(int tenantId, String alias,
* @param organization : Organization
* @return : true if update succeeds, false if fails
*/
ResponseCode updateClientCertificate(String certificate, String alias, String tier, int tenantId,
String organization) throws APIManagementException;
ResponseCode updateClientCertificate(String certificate, String alias, String tier, String endpointType,
int tenantId, String organization) throws APIManagementException;

/**
* To get the count of the client certificates updated for the particular tenant.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public ResponseCode addCertificateToParentNode(String certificate, String alias,

@Override
public ResponseCode addClientCertificate(Identifier apiIdentifier, String certificate, String alias,
String tierName, int tenantId, String organization) {
String tierName, String endpointType, int tenantId, String organization) {

ResponseCode responseCode;
try {
Expand All @@ -124,7 +124,8 @@ public ResponseCode addClientCertificate(Identifier apiIdentifier, String certif
responseCode = ResponseCode.ALIAS_EXISTS_IN_TRUST_STORE;
} else {
certificateMgtDAO
.addClientCertificate(certificate, apiIdentifier, alias, tierName, tenantId, organization);
.addClientCertificate(certificate, apiIdentifier, alias, tierName, endpointType,
tenantId, organization);
}
}
} catch (CertificateManagementException e) {
Expand Down Expand Up @@ -454,8 +455,8 @@ public ResponseCode updateCertificate(String certificate, String alias) throws A
}

@Override
public ResponseCode updateClientCertificate(String certificate, String alias, String tier, int tenantId,
String organization) throws APIManagementException {
public ResponseCode updateClientCertificate(String certificate, String alias, String tier, String endpointType,
int tenantId, String organization) throws APIManagementException {

ResponseCode responseCode = ResponseCode.SUCCESS;
if (StringUtils.isNotEmpty(certificate)) {
Expand All @@ -464,7 +465,7 @@ public ResponseCode updateClientCertificate(String certificate, String alias, St
try {
if (responseCode.getResponseCode() == ResponseCode.SUCCESS.getResponseCode()) {
boolean isSuccess = certificateMgtDAO
.updateClientCertificate(certificate, alias, tier, tenantId, organization);
.updateClientCertificate(certificate, alias, tier, endpointType, tenantId, organization);
if (isSuccess) {
responseCode = ResponseCode.SUCCESS;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16990,6 +16990,7 @@ public void addAPIRevision(APIRevision apiRevision) throws APIManagementExceptio
clientCertificateDTO.setAlias(rs.getString(1));
clientCertificateDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(rs.getBinaryStream(2)));
clientCertificateDTO.setTierName(rs.getString(3));
clientCertificateDTO.setEndpointType(rs.getString(4));
clientCertificateDTOS.add(clientCertificateDTO);
}
}
Expand All @@ -17004,6 +17005,7 @@ public void addAPIRevision(APIRevision apiRevision) throws APIManagementExceptio
insertClientCertificateStatement.setBoolean(5, false);
insertClientCertificateStatement.setString(6, clientCertificateDTO.getTierName());
insertClientCertificateStatement.setString(7, apiRevision.getRevisionUUID());
insertClientCertificateStatement.setString(8, clientCertificateDTO.getEndpointType());
insertClientCertificateStatement.addBatch();
}
insertClientCertificateStatement.executeBatch();
Expand Down Expand Up @@ -17908,6 +17910,7 @@ public void restoreAPIRevision(APIRevision apiRevision) throws APIManagementExce
clientCertificateDTO.setAlias(rs.getString(1));
clientCertificateDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(rs.getBinaryStream(2)));
clientCertificateDTO.setTierName(rs.getString(3));
clientCertificateDTO.setEndpointType(rs.getString(4));
clientCertificateDTOS.add(clientCertificateDTO);
}
}
Expand All @@ -17922,6 +17925,7 @@ public void restoreAPIRevision(APIRevision apiRevision) throws APIManagementExce
insertClientCertificateStatement.setBoolean(5, false);
insertClientCertificateStatement.setString(6, clientCertificateDTO.getTierName());
insertClientCertificateStatement.setString(7, "Current API");
insertClientCertificateStatement.setString(8, clientCertificateDTO.getEndpointType());
insertClientCertificateStatement.addBatch();
}
insertClientCertificateStatement.executeBatch();
Expand Down Expand Up @@ -18222,6 +18226,7 @@ public void addAPIProductRevision(APIRevision apiRevision) throws APIManagementE
clientCertificateDTO.setAlias(rs.getString(1));
clientCertificateDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(rs.getBinaryStream(2)));
clientCertificateDTO.setTierName(rs.getString(3));
clientCertificateDTO.setEndpointType(rs.getString(4));
clientCertificateDTOS.add(clientCertificateDTO);
}
}
Expand All @@ -18236,6 +18241,7 @@ public void addAPIProductRevision(APIRevision apiRevision) throws APIManagementE
insertClientCertificateStatement.setBoolean(5, false);
insertClientCertificateStatement.setString(6, clientCertificateDTO.getTierName());
insertClientCertificateStatement.setString(7, apiRevision.getRevisionUUID());
insertClientCertificateStatement.setString(8, clientCertificateDTO.getEndpointType());
insertClientCertificateStatement.addBatch();
}
insertClientCertificateStatement.executeBatch();
Expand Down Expand Up @@ -18458,6 +18464,7 @@ public void restoreAPIProductRevision(APIRevision apiRevision) throws APIManagem
clientCertificateDTO.setAlias(rs.getString(1));
clientCertificateDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(rs.getBinaryStream(2)));
clientCertificateDTO.setTierName(rs.getString(3));
clientCertificateDTO.setEndpointType(rs.getString(4));
clientCertificateDTOS.add(clientCertificateDTO);
}
}
Expand All @@ -18472,6 +18479,8 @@ public void restoreAPIProductRevision(APIRevision apiRevision) throws APIManagem
insertClientCertificateStatement.setBoolean(5, false);
insertClientCertificateStatement.setString(6, clientCertificateDTO.getTierName());
insertClientCertificateStatement.setString(7, "Current API");
insertClientCertificateStatement.setString(8,
clientCertificateDTO.getEndpointType());
insertClientCertificateStatement.addBatch();
}
insertClientCertificateStatement.executeBatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static CertificateMgtDAO getInstance() {


private boolean addClientCertificate(Connection connection, String certificate, Identifier apiIdentifier,
String alias, String tierName,
String alias, String tierName, String endpointType,
int tenantId, String organization) throws SQLException {

boolean result;
Expand All @@ -87,6 +87,7 @@ private boolean addClientCertificate(Connection connection, String certificate,
preparedStatement.setString(6, apiIdentifier.getVersion());
preparedStatement.setString(7, organization);
preparedStatement.setString(8, tierName);
preparedStatement.setString(9, endpointType);
result = preparedStatement.executeUpdate() >= 1;
}
return result;
Expand All @@ -103,8 +104,8 @@ private boolean addClientCertificate(Connection connection, String certificate,
* @return true if the update succeeds, unless false.
* @throws CertificateManagementException Certificate Management Exception.
*/
public boolean updateClientCertificate(String certificate, String alias, String tier, int tenantId,
String organization) throws CertificateManagementException {
public boolean updateClientCertificate(String certificate, String alias, String tier, String endpointType,
int tenantId, String organization) throws CertificateManagementException {

List<ClientCertificateDTO> clientCertificateDTOList = getClientCertificates(tenantId, alias, null,
organization);
Expand All @@ -124,13 +125,16 @@ public boolean updateClientCertificate(String certificate, String alias, String
if (StringUtils.isNotEmpty(tier)) {
clientCertificateDTO.setTierName(tier);
}
if (StringUtils.isNotEmpty(tier)) {
clientCertificateDTO.setEndpointType(endpointType);
}
try (Connection connection = APIMgtDBUtil.getConnection()) {
try {
connection.setAutoCommit(false);
deleteClientCertificate(connection, null, alias, tenantId);
addClientCertificate(connection, clientCertificateDTO.getCertificate(),
clientCertificateDTO.getApiIdentifier(), alias, clientCertificateDTO.getTierName(),
tenantId, organization);
clientCertificateDTO.getEndpointType(), tenantId, organization);
connection.commit();
} catch (SQLException e) {
handleConnectionRollBack(connection);
Expand Down Expand Up @@ -296,6 +300,7 @@ public List<ClientCertificateDTO> getClientCertificates(int tenantId, String ali
alias = resultSet.getString("ALIAS");
ClientCertificateDTO clientCertificateDTO = new ClientCertificateDTO();
clientCertificateDTO.setTierName(resultSet.getString("TIER_NAME"));
clientCertificateDTO.setEndpointType(resultSet.getString("ENDPOINT_TYPE"));
clientCertificateDTO.setAlias(alias);
clientCertificateDTO.setCertificate(
APIMgtDBUtil.getStringFromInputStream(resultSet.getBinaryStream("CERTIFICATE")));
Expand Down Expand Up @@ -742,19 +747,21 @@ private void handleException(String message, Throwable e) throws CertificateMana
* @param certificate : Client certificate that need to be added.
* @param apiIdentifier : API which the client certificate is uploaded against.
* @param alias : Alias for the new certificate.
* @param endpointType : Type of endpoint of the certificate
* @param tenantId : The Id of the tenant who uploaded the certificate.
* @param organization : Organization
* @return : True if the information is added successfully, false otherwise.
* @throws CertificateManagementException if existing entry is found for the given endpoint or alias.
*/
public boolean addClientCertificate(String certificate, Identifier apiIdentifier, String alias, String tierName,
int tenantId, String organization) throws CertificateManagementException {
String endpointType, int tenantId, String organization)
throws CertificateManagementException {

try (Connection connection = APIMgtDBUtil.getConnection()) {
try {
connection.setAutoCommit(false);
boolean status = addClientCertificate(connection, certificate, apiIdentifier, alias, tierName,
tenantId, organization);
endpointType, tenantId, organization);
connection.commit();
return status;
} catch (SQLException e) {
Expand Down
Loading

0 comments on commit fbc7d30

Please sign in to comment.