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 23, 2024
1 parent 01f3d5a commit 7adb04e
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 40 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 Name of the tier.
*/
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 @@ -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 @@ -742,19 +746,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
Original file line number Diff line number Diff line change
Expand Up @@ -4616,6 +4616,7 @@ paths:
- alias
- certificate
- tier
- endpointType
properties:
certificate:
type: string
Expand All @@ -4629,6 +4630,12 @@ paths:
tier:
type: string
description: API tier to which the certificate should be applied.
endpointType:
type: string
description: Whether the endpoint is production or sandbox
enum:
- PRODUCTION
- SANDBOX
required: true
responses:
200:
Expand Down Expand Up @@ -4741,6 +4748,12 @@ paths:
tier:
type: string
description: The tier of the certificate
endpointType:
type: string
description: Whether the endpoint is production or sandbox
enum:
- PRODUCTION
- SANDBOX
responses:
200:
description: |
Expand Down Expand Up @@ -11328,6 +11341,12 @@ components:
tier:
type: string
example: Gold
endpointType:
type: string
example: PRODUCTION
enum:
- PRODUCTION
- SANDBOX
description: Meta data of certificate
LifecycleState:
title: Lifecycle State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ public class ClientCertMetadataDTO {
private String apiId = null;
private String tier = null;

@XmlType(name="EndpointTypeEnum")
@XmlEnum(String.class)
public enum EndpointTypeEnum {
PRODUCTION("PRODUCTION"),
SANDBOX("SANDBOX");
private String value;

EndpointTypeEnum (String v) {
value = v;
}

public String value() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static EndpointTypeEnum fromValue(String v) {
for (EndpointTypeEnum b : EndpointTypeEnum.values()) {
if (String.valueOf(b.value).equals(v)) {
return b;
}
}
return null;
}
}
private EndpointTypeEnum endpointType = null;

/**
**/
public ClientCertMetadataDTO alias(String alias) {
Expand Down Expand Up @@ -78,6 +110,23 @@ public void setTier(String tier) {
this.tier = tier;
}

/**
**/
public ClientCertMetadataDTO endpointType(EndpointTypeEnum endpointType) {
this.endpointType = endpointType;
return this;
}


@ApiModelProperty(example = "PRODUCTION", value = "")
@JsonProperty("endpointType")
public EndpointTypeEnum getEndpointType() {
return endpointType;
}
public void setEndpointType(EndpointTypeEnum endpointType) {
this.endpointType = endpointType;
}


@Override
public boolean equals(java.lang.Object o) {
Expand All @@ -90,12 +139,13 @@ public boolean equals(java.lang.Object o) {
ClientCertMetadataDTO clientCertMetadata = (ClientCertMetadataDTO) o;
return Objects.equals(alias, clientCertMetadata.alias) &&
Objects.equals(apiId, clientCertMetadata.apiId) &&
Objects.equals(tier, clientCertMetadata.tier);
Objects.equals(tier, clientCertMetadata.tier) &&
Objects.equals(endpointType, clientCertMetadata.endpointType);
}

@Override
public int hashCode() {
return Objects.hash(alias, apiId, tier);
return Objects.hash(alias, apiId, tier, endpointType);
}

@Override
Expand All @@ -106,6 +156,7 @@ public String toString() {
sb.append(" alias: ").append(toIndentedString(alias)).append("\n");
sb.append(" apiId: ").append(toIndentedString(apiId)).append("\n");
sb.append(" tier: ").append(toIndentedString(tier)).append("\n");
sb.append(" endpointType: ").append(toIndentedString(endpointType)).append("\n");
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2257,10 +2257,10 @@ private static void addClientCertificates(String pathToArchive, APIProvider apiP
for (ClientCertificateDTO certDTO : certificateMetadataDTOS) {
if (ResponseCode.ALIAS_EXISTS_IN_TRUST_STORE.getResponseCode() == (apiProvider.addClientCertificate(
APIUtil.replaceEmailDomainBack(apiIdentifier.getProviderName()), apiTypeWrapper,
certDTO.getCertificate(), certDTO.getAlias(), certDTO.getTierName(), organization))
&& isOverwrite) {
certDTO.getCertificate(), certDTO.getAlias(), certDTO.getTierName(),certDTO.getEndpointType(),
organization)) && isOverwrite) {
apiProvider.updateClientCertificate(certDTO.getCertificate(), certDTO.getAlias(), apiTypeWrapper,
certDTO.getTierName(), tenantId, organization);
certDTO.getTierName(), certDTO.getEndpointType(), tenantId, organization);
}
}
} catch (APIManagementException e) {
Expand Down
Loading

0 comments on commit 7adb04e

Please sign in to comment.