Skip to content

Commit

Permalink
organization dns operation crud mofifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Maria Farooq committed Sep 22, 2017
1 parent 1156de4 commit d992dec
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@
<gmlc-password></gmlc-password>
</gmlc>

<!--
dns-provisioning: configuration is used to proform CRUD operations for organizations
<!-- dns-provisioning: configuration is used to proform CRUD operations for organizations, i.e creating new domains under restcomm
class: represents the DNS provisioning manager class
e.g org.restcomm.connect.telscale.dns.route53.Route53DnsProvisioningManager
Expand All @@ -340,9 +339,13 @@
<restcomm-address></restcomm-address>
<!-- DNS Resource Record (RR) Types -->
<rr-type>A</rr-type>

<!-- aws-route53 specific config for dns provisioning
ttl: time to live in seconds
hosted-zone-id: The ID of the hosted zone that contains the resource record sets that you want to change.
for example hosted zone id of domain restcomm.com, this can be get from aws management console
-->
<aws-route53>
<!-- ttl: time to live in seconds -->
<!-- hosted-zone-id: The ID of the hosted zone that contains the resource record sets that you want to change. -->
<ttl>3600</ttl>
<hosted-zone-id></hosted-zone-id>
</aws-route53>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.restcomm.connect.dns;

import org.apache.commons.configuration.Configuration;
import org.restcomm.connect.commons.patterns.StandardResponse;

public interface DnsProvisioningManager {

Expand All @@ -31,8 +30,43 @@ public interface DnsProvisioningManager {
*/
void init(Configuration dnsConfiguration);

StandardResponse<?> createResourceRecord(final String name);
StandardResponse<?> readResourceRecord();
StandardResponse<?> updateResourceRecord();
StandardResponse<?> deleteResourceRecord();
/**
* @param name The name of the domain you want to perform the action on.
* Enter a sub domain name only. For example to add 'company1.restcomm.com',
* provide only 'company1' and provide hosted zone for 'restcomm.com'
* @param hostedZoneId hostedZoneId The ID of the hosted zone that contains the resource record sets that you want to change.
* If none provided, then default will be used as per configuration
* @return true if operation successful, false otherwise.
*/
boolean createResourceRecord(final String name, final String hostedZoneId);

/**
* @param name The name of the domain you want to perform the action on.
* Enter a sub domain name only. For example to add 'company1.restcomm.com',
* provide only 'company1' and provide hosted zone for 'restcomm.com'
* @param hostedZoneId hostedZoneId The ID of the hosted zone that contains the resource record sets that you want to change.
* If none provided, then default will be used as per configuration
* @return true if operation successful, false otherwise.
*/
boolean readResourceRecord(final String name, final String hostedZoneId);

/**
* @param name The name of the domain you want to perform the action on.
* Enter a sub domain name only. For example to add 'company1.restcomm.com',
* provide only 'company1' and provide hosted zone for 'restcomm.com'
* @param hostedZoneId hostedZoneId The ID of the hosted zone that contains the resource record sets that you want to change.
* If none provided, then default will be used as per configuration
* @return true if operation successful, false otherwise.
*/
boolean updateResourceRecord(final String name, final String hostedZoneId);

/**
* @param name The name of the domain you want to perform the action on.
* Enter a sub domain name only. For example to add 'company1.restcomm.com',
* provide only 'company1' and provide hosted zone for 'restcomm.com'
* @param hostedZoneId hostedZoneId The ID of the hosted zone that contains the resource record sets that you want to change.
* If none provided, then default will be used as per configuration
* @return true if operation successful, false otherwise.
*/
boolean deleteResourceRecord(final String name, final String hostedZoneId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.servlet.ServletContext;

import org.apache.commons.configuration.Configuration;
import org.apache.log4j.Logger;
import org.restcomm.connect.commons.loader.ObjectFactory;
import org.restcomm.connect.commons.loader.ObjectInstantiationException;

Expand All @@ -32,6 +33,8 @@
* @author maria farooq
*/
public class DnsProvisioningManagerProvider {
protected Logger logger = Logger.getLogger(DnsProvisioningManagerProvider.class);

Configuration configuration;
ServletContext context;

Expand All @@ -40,9 +43,15 @@ public DnsProvisioningManagerProvider(Configuration configuration, ServletContex
this.context = context;
}

public DnsProvisioningManager create() {
/**
* @return initialized instance of DnsProvisioningManager
*/
private DnsProvisioningManager create() {
final String dnsProvisioningManagerClass = configuration.getString("dns-provisioning[@class]");
Configuration dnsProvisioningConfiguration = configuration.subset("dns-provisioning");
if(dnsProvisioningManagerClass == null || dnsProvisioningManagerClass.trim().equals("")){
return null;
}
DnsProvisioningManager dnsProvisioningManager;
try {
dnsProvisioningManager = (DnsProvisioningManager) new ObjectFactory(getClass().getClassLoader())
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@
import javax.ws.rs.core.UriInfo;

import org.apache.commons.configuration.Configuration;
import org.joda.time.DateTime;
import org.restcomm.connect.commons.dao.Sid;
import org.restcomm.connect.dao.entities.Account;
import org.restcomm.connect.dao.entities.Organization;
import org.restcomm.connect.dao.entities.OrganizationList;
import org.restcomm.connect.dao.entities.RestCommResponse;
import org.restcomm.connect.dns.DnsProvisioningManager;
import org.restcomm.connect.dns.DnsProvisioningManagerProvider;
import org.restcomm.connect.http.converter.AccountConverter;
import org.restcomm.connect.http.converter.AccountListConverter;
import org.restcomm.connect.http.converter.OrganizationConverter;
import org.restcomm.connect.http.converter.OrganizationListConverter;
import org.restcomm.connect.http.converter.RestCommResponseConverter;

import com.google.gson.Gson;
Expand All @@ -65,43 +65,56 @@ public class OrganizationsEndpoint extends SecuredEndpoint {
@Context
protected ServletContext context;
protected DnsProvisioningManager dnsProvisioningManager;
protected Configuration runtimeConfiguration;
protected Configuration rootConfiguration; // top-level configuration element
protected Gson gson;
protected XStream xstream;
private final String MSG_EMPTY_DOMAIN_NAME = "domain name can not be empty. Please, choose a valid name and try again.";
private final String MSG_INVALID_DOMAIN_NAME_PATTERN= "Total Length of domain_name can be upto 255 Characters. It can contain only letters, number and hyphen - sign.. Please, choose a valid name and try again.";
private final String MSG_DOMAIN_NAME_NOT_AVAILABLE = "This domain name is not available. Please, choose a different name and try again.";
private String DOMAIN_NAME_VALIDATION_PATTERN="[A-Za-z0-9\\-\\.]{1,255}";
private String SUB_DOMAIN_NAME_VALIDATION_PATTERN="[A-Za-z0-9\\-]{1,255}";
private OrganizationListConverter listConverter;

public OrganizationsEndpoint() {
super();
}

// used for testing
public OrganizationsEndpoint(ServletContext context, HttpServletRequest request) {
super(context,request);
super(context, request);
}

@PostConstruct
void init() {
rootConfiguration = (Configuration) context.getAttribute(Configuration.class.getName());
runtimeConfiguration = rootConfiguration.subset("runtime-settings");
super.init(runtimeConfiguration);
final AccountConverter converter = new AccountConverter(runtimeConfiguration);
configuration = (Configuration) context.getAttribute(Configuration.class.getName());
super.init(configuration.subset("runtime-settings"));

registerConverters();

// Make sure there is an authenticated account present when this endpoint is used
// get manager from context or create it if it does not exist
try {
dnsProvisioningManager = new DnsProvisioningManagerProvider(configuration, context).get();
} catch(Exception e) {
logger.error("Unable to get dnsProvisioningManager", e);
}
}

private void registerConverters(){
final OrganizationConverter converter = new OrganizationConverter(configuration);
listConverter = new OrganizationListConverter(configuration);
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Account.class, converter);
builder.serializeNulls();
builder.registerTypeAdapter(Organization.class, converter);
builder.registerTypeAdapter(OrganizationList.class, listConverter);
builder.setPrettyPrinting();
gson = builder.create();
xstream = new XStream();
xstream.alias("RestcommResponse", RestCommResponse.class);
xstream.registerConverter(converter);
xstream.registerConverter(new AccountListConverter(runtimeConfiguration));
xstream.registerConverter(new RestCommResponseConverter(runtimeConfiguration));
// Make sure there is an authenticated account present when this endpoint is used
// get manager from context or create it if it does not exist
dnsProvisioningManager = new DnsProvisioningManagerProvider(configuration, context).get();
xstream.registerConverter(listConverter);
xstream.registerConverter(new RestCommResponseConverter(configuration));
}

/**
* @param organizationSid
* @param responseType
Expand All @@ -115,16 +128,16 @@ protected Response getOrganization(final String organizationSid, final MediaType

if (!Sid.pattern.matcher(organizationSid).matches()) {
return status(BAD_REQUEST).build();
}else{
} else {
try {
//if account is not super admin then allow to read only affiliated organization
if(!isSuperAdmin()){
if(userIdentityContext.getEffectiveAccount().getOrganizationSid().equals(new Sid(organizationSid))){
if (!isSuperAdmin()) {
if (userIdentityContext.getEffectiveAccount().getOrganizationSid().equals(new Sid(organizationSid))) {
organization = organizationsDao.getOrganization(new Sid(organizationSid));
}else{
} else {
return status(FORBIDDEN).build();
}
}else{
} else {
organization = organizationsDao.getOrganization(new Sid(organizationSid));
}
} catch (Exception e) {
Expand Down Expand Up @@ -178,7 +191,7 @@ protected Response getOrganizations(UriInfo info, final MediaType responseType)
}

/**
* putOrganization
* putOrganization create new organization
* @param domainName
* @param data
* @param applicationJsonType
Expand All @@ -193,7 +206,7 @@ protected Response putOrganization(String domainName, MultivaluedMap<String, Str
allowOnlySuperAdmin();

//Character verification
final Pattern pattern = Pattern.compile("[A-Za-z0-9\\-]{1,255}");
final Pattern pattern = Pattern.compile(SUB_DOMAIN_NAME_VALIDATION_PATTERN);
if(!pattern.matcher(domainName).matches()){
return status(BAD_REQUEST).entity(MSG_INVALID_DOMAIN_NAME_PATTERN).build();
}
Expand All @@ -205,9 +218,15 @@ protected Response putOrganization(String domainName, MultivaluedMap<String, Str
.entity(MSG_DOMAIN_NAME_NOT_AVAILABLE)
.build();
}
//restcomm.com
data.get("HostedZone");
//TODO: dns get, check if its not already taken
if(dnsProvisioningManager == null) {
logger.warn("No DNS provisioning Manager is configured, restcomm will not make any queries to DNS server.");
organization = new Organization(Sid.generate(Sid.Type.ORGANIZATION), domainName, DateTime.now(), DateTime.now(), Organization.Status.ACTIVE);
}else {
//for example hosted zon id of domain restcomm.com or others. if not provided then default will be used as per configuration
String hostedZoneId = data.getFirst("HostedZoneId");
//TODO: dns get, check if its not already taken
dnsProvisioningManager.createResourceRecord(domainName, hostedZoneId);
}
}
}
}

0 comments on commit d992dec

Please sign in to comment.