@Override public String marshall(MarshallStruct<ListHostedZonesResult> input, HttpServletResponse resp) throws Exception { logger.debug("Marshalling the result into xml."); ListHostedZonesResult result = input.getMainObject(); XMLNode response = new XMLNode(DNS53Constants.LISTHOSTEDZONESRESPONSE); response.addAttr(DNS53Constants.XMLNS, DNS53Constants.XMLNS_VALUE); if(result.getHostedZones() != null && result.getHostedZones().size() > 0){ XMLNode hzs = QueryUtil.addNode(response, DNS53Constants.HOSTEDZONES); for(HostedZone hostedZone : result.getHostedZones()){ DNS53QueryUtil.marshallHostedZone(hostedZone, hzs); } } QueryUtil.addNode(response, DNS53Constants.MARKER, result.getMarker()); QueryUtil.addNode(response, DNS53Constants.ISTRUNCATED, result.getIsTruncated()); QueryUtil.addNode(response, DNS53Constants.NEXTMARKER, result.getNextMarker()); QueryUtil.addNode(response, DNS53Constants.MAXITEMS, result.getMaxItems()); logger.debug("Returning the response xml to AbstractHeaderAction."); return response.toString(); }
public static String findHostedZoneForDomain(AmazonRoute53 route53, String domain) { ListHostedZonesResult listHostedZonesResult = route53.listHostedZones(); List<HostedZone> hostedZones = listHostedZonesResult.getHostedZones(); String hostedZoneId = null; for (HostedZone hostedZone : hostedZones) { if (isRootDomain(domain, hostedZone)) { hostedZoneId = hostedZone.getId(); break; } } return hostedZoneId; }
public static boolean isRootDomain(String subDomain, HostedZone hostedZone) { if (subDomain == null || hostedZone == null) { return false; } String rootDomain = hostedZone.getName(); if (rootDomain == null) { return false; } String subDomainEndWithDot = subDomain + "."; if (subDomainEndWithDot.endsWith(rootDomain)) { return true; } else { return false; } }
private GetHostedZoneResult getHostedZone(Session sess, GetHostedZoneRequest request) throws ErrorResponse{ String zoneId = request.getId(); logger.debug("GetHosteZone target: " + zoneId); GetHostedZoneResult result = new GetHostedZoneResult(); AccessMySQL sqlaccess = AccessMySQL.getInstance(); String[] responseCont = sqlaccess.getHostedZone(zoneId); if(responseCont[1] == null){ throw DNS53Faults.NoSuchHostedZone(zoneId); } HostedZone hz = new HostedZone(); hz.setId(responseCont[0]); hz.setName(responseCont[1]); hz.setCallerReference(responseCont[2]); HostedZoneConfig config = new HostedZoneConfig(); config.setComment(responseCont[3]); hz.setConfig(config); result.setHostedZone(hz); DelegationSet delegationSet = new DelegationSet(); Collection<String> nameServers = new LinkedList<String>(); List<DNS53ResourceRecord> nsRecords = sqlaccess.listResourceRecords(sess, request.getId(), null, null, "NS", null, -1, -1); for(DNS53ResourceRecord ns : nsRecords){ String nameserver = ns.getRdata(); nameserver = nameserver.substring(0, nameserver.length() - 1); nameServers.add(nameserver); } delegationSet.setNameServers(nameServers); result.setDelegationSet(delegationSet); logger.debug("Returning the result: " + result.toString()); return result; }
public static void marshallHostedZone(HostedZone hostedZone, XMLNode response) { XMLNode hz = QueryUtil.addNode(response, DNS53Constants.HOSTEDZONE); QueryUtil.addNode(hz, DNS53Constants.ID, hostedZone.getId()); QueryUtil.addNode(hz, DNS53Constants.NAME, hostedZone.getName()); QueryUtil.addNode(hz, DNS53Constants.CALLERREFERENCE, hostedZone.getCallerReference()); if(hostedZone.getConfig() != null){ XMLNode config = QueryUtil.addNode(hz, DNS53Constants.CONFIG); QueryUtil.addNode(config, DNS53Constants.COMMENT, hostedZone.getConfig().getComment()); } QueryUtil.addNode(hz, DNS53Constants.RESOURCERECORDSETCOUNT, hostedZone.getResourceRecordSetCount()); }
public void deploy() { /* * check for existence because once created, we aren't going to delete * it. Amazon could give the name to someone else. This won't matter * when we move CDN. */ final Optional<Bucket> maybeBucket = this.s3.listBuckets() .stream() .filter(b -> b.getName().equals(BucketName)) .findAny(); if (!maybeBucket.isPresent()) { this.s3.createBucket(new CreateBucketRequest(BucketName)); } this.s3.setBucketWebsiteConfiguration(BucketName, new BucketWebsiteConfiguration("index.html")); /* * Zone must exist */ final HostedZone zone = this.route53.listHostedZonesByName(new ListHostedZonesByNameRequest().withDNSName(Tld)) .getHostedZones() .stream() .findAny() .get(); final String zoneId = zone.getId().replaceAll("/.*/", ""); final ResourceRecord record = new ResourceRecord().withValue(Domain + ".s3.amazonaws.com"); final ResourceRecordSet records = new ResourceRecordSet().withName(Domain + ".") .withType(RRType.CNAME) .withTTL(60L) .withResourceRecords(record); final Change change = new Change().withAction(ChangeAction.UPSERT).withResourceRecordSet(records); final List<Change> changes = Collections.singletonList(change); final ChangeBatch changeBatch = new ChangeBatch().withChanges(changes); final ChangeResourceRecordSetsRequest changeRecordsRequest = new ChangeResourceRecordSetsRequest() .withHostedZoneId(zoneId).withChangeBatch(changeBatch); this.route53.changeResourceRecordSets(changeRecordsRequest); upload(SignupObjectName); upload(ThankYouObjectName); }
public void populateServiceMetadata( final ServletConfig config, String serviceName) { logger.debug("init(): TXT record will be created for this service regarding its port and context path."); String contextPath = config.getServletContext().getContextPath(); String port = Appctx.getBean("TOMCAT_PORT"); String master_passwd = Appctx.getBean("DB_PASSWORD"); final String fqdn = (String) ConfigurationUtil .getConfiguration(Arrays.asList(new String[] { "FQDN" })); final String domain = (String) ConfigurationUtil .getConfiguration(Arrays.asList(new String[] { "FQDN_DOMAIN" })); String txtRecordValue = ":" + port + contextPath; String baseDNSServerURL = "http://localhost:" + port + "/DNS53Server/2012-02-29/"; logger.debug("Tomcat port = " + port + "; FQDN = " + fqdn + "; domain = " + domain + "; TXT Record Value = " + txtRecordValue + "; BaseDNSServerUrl = " + baseDNSServerURL); DNS53Client client = new DNS53Client(baseDNSServerURL + "hostedzone", baseDNSServerURL + "change", "admin", master_passwd); logger.debug("Service name = " + serviceName); String recordName = serviceName + "-" + fqdn; logger.debug("TXT Record Name: " + recordName); logger.debug("init(): Calling ListHostedZones to find the target zone!"); ListHostedZonesRequest lhzReq = new ListHostedZonesRequest(); lhzReq.setMaxItems("1"); ListHostedZonesResult lhzResult = client.listHostedZones(lhzReq); HostedZone zone = null; List<HostedZone> zones = lhzResult.getHostedZones(); if(zones != null && zones.size() > 0){ for(HostedZone hz : zones){ if(hz.getName().equals(domain + ".") || hz.getName().equals(domain)){ zone = hz; } } } else{ logger.error("BaseAsyncServlet encountered an error while it was trying to find the target hosted zone."); throw ErrorResponse.InternalFailure(); } if(zone == null){ logger.error("BaseAsyncServlet could not find any zone for this TopStackWeb instance."); throw ErrorResponse.InternalFailure(); } // TODO (optional) check for the CNAME record for this service before proceeding logger.debug("init(): Creating a new TXT record for " + recordName + " with \"" + txtRecordValue + "\" as its value!"); String zoneId = zone.getId(); ChangeResourceRecordSetsRequest crrsReq = new ChangeResourceRecordSetsRequest(); crrsReq.setHostedZoneId(zoneId); ChangeBatch cb = new ChangeBatch(); cb.setComment("BaseAsyncServlet => init(): Registering " + serviceName + " service for Transcend TopStack."); Collection<Change> changes = new LinkedList<Change>(); Change change = new Change(); change.setAction(ChangeAction.CREATE); ResourceRecordSet rrSet = new ResourceRecordSet(); rrSet.setName(recordName); rrSet.setTTL(900L); rrSet.setType(RRType.TXT); Collection<ResourceRecord> rr = new LinkedList<ResourceRecord>(); ResourceRecord record = new ResourceRecord(); record.setValue(txtRecordValue); rr.add(record); rrSet.setResourceRecords(rr); change.setResourceRecordSet(rrSet); changes.add(change); cb.setChanges(changes); crrsReq.setChangeBatch(cb); ChangeResourceRecordSetsResult result = client.changeResourceRecordSets(crrsReq); logger.debug("Result for the last ChangeResourceRecordSets request: " + result.toString()); }
public CreateHostedZoneResult createHostedZone(CreateHostedZoneRequest req) throws AmazonServiceException, AmazonClientException{ Client c = Client.create(); WebResource r = c.resource(this.serverURL); String entity = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<CreateHostedZoneRequest xmlns=\"https://route53.amazonaws.com/doc/2012-02-29/\">" + "<Name>" + req.getName() + "</Name>" + "<CallerReference>" + req.getCallerReference() + "</CallerReference>"; if(req.getHostedZoneConfig() != null && req.getHostedZoneConfig().getComment() != null){ entity += "<HostedZoneConfig>" + "<Comment>" + req.getHostedZoneConfig().getComment() + "</Comment>" + "</HostedZoneConfig>"; } entity += "</CreateHostedZoneRequest>"; ClientResponse response = r .header("X-Amzn-Authorization", "AWS3 AWSAccessKeyId=" + this.accessKey + "," + "Algorithm=HmacSHA256," + "SignedHeaders=Host;X-Amz-Date," + "Signature=THISISANEXAMPLESIGNATURE=") .type(MediaType.APPLICATION_XML_TYPE) .accept(MediaType.TEXT_XML) .entity(entity) .post(ClientResponse.class); String resultXml = response.getEntity(String.class); if(response.getStatus() > 299 || response.getStatus() < 200){ exceptionMapper(response, resultXml); } CreateHostedZoneResponsePOJO interResult = null; try { StringReader reader = new StringReader(resultXml); JAXBContext context = JAXBContext.newInstance(CreateHostedZoneResponsePOJO.class); Unmarshaller unmarshaller = context.createUnmarshaller(); interResult = (CreateHostedZoneResponsePOJO) unmarshaller.unmarshal(reader); } catch (JAXBException e) { e.printStackTrace(); return null; } if(interResult == null){ return null; } CreateHostedZoneResult result = new CreateHostedZoneResult(); if(interResult.getHostedZone() != null){ HostedZone hostedZone = new HostedZone(); hostedZone.setId(interResult.getHostedZone().getId()); hostedZone.setName(interResult.getHostedZone().getName()); hostedZone.setCallerReference(interResult.getHostedZone().getCallerReference()); if(interResult.getHostedZone().getConfig() != null){ HostedZoneConfig config = new HostedZoneConfig(); config.setComment(interResult.getHostedZone().getConfig().getComment()); hostedZone.setConfig(config); } result.setHostedZone(hostedZone); } if(interResult.getChangeInfo() != null){ ChangeInfo changeInfo = new ChangeInfo(); changeInfo.setId(interResult.getChangeInfo().getId()); changeInfo.setStatus(interResult.getChangeInfo().getStatus()); changeInfo.setSubmittedAt(interResult.getChangeInfo().getSubmittedAt()); changeInfo.setComment(interResult.getChangeInfo().getComment()); result.setChangeInfo(changeInfo); } if(interResult.getDelegationSet() != null){ DelegationSet dSet = new DelegationSet(); dSet.setNameServers(interResult.getDelegationSet().getNameServers()); result.setDelegationSet(dSet); } return result; }
public GetHostedZoneResult getHostedZone(GetHostedZoneRequest req) throws AmazonServiceException, AmazonClientException{ Client c = Client.create(); WebResource r = c.resource(this.serverURL); ClientResponse response = r .path(req.getId()) .type(MediaType.APPLICATION_XML_TYPE) .accept(MediaType.TEXT_XML) .header("X-Amzn-Authorization", "AWS3 AWSAccessKeyId=" + this.accessKey + "," + "Algorithm=HmacSHA256," + "SignedHeaders=Host;X-Amz-Date," + "Signature=THISISANEXAMPLESIGNATURE=") .get(ClientResponse.class); String resultXml = response.getEntity(String.class); if(response.getStatus() != 200){ exceptionMapper(response, resultXml); } GetHostedZoneResponsePOJO interResult = null; try { StringReader reader = new StringReader(resultXml); JAXBContext context = JAXBContext.newInstance(GetHostedZoneResponsePOJO.class); Unmarshaller unmarshaller = context.createUnmarshaller(); interResult = (GetHostedZoneResponsePOJO) unmarshaller.unmarshal(reader); } catch (JAXBException e) { e.printStackTrace(); return null; } if(interResult == null){ return null; } GetHostedZoneResult result = new GetHostedZoneResult(); if(interResult.getHostedZone() != null){ HostedZone hz = new HostedZone(); hz.setCallerReference(interResult.getHostedZone().getCallerReference()); hz.setId(interResult.getHostedZone().getId()); hz.setName(interResult.getHostedZone().getName()); hz.setResourceRecordSetCount(interResult.getHostedZone().getResourceRecordSetCount()); if(interResult.getHostedZone().getConfig() != null){ HostedZoneConfig config = new HostedZoneConfig(); config.setComment(interResult.getHostedZone().getConfig().getComment()); hz.setConfig(config); } result.setHostedZone(hz); } if(interResult.getDelegationSet() != null){ DelegationSetPOJO ds = interResult.getDelegationSet(); DelegationSet ds_ = new DelegationSet(); if(ds.getNameServers() != null){ ds_.setNameServers(ds.getNameServers()); } result.setDelegationSet(ds_); } return result; }
public ListHostedZonesResult listHostedZones(ListHostedZonesRequest req) throws AmazonServiceException, AmazonClientException{ Client c = Client.create(); WebResource r = c.resource(this.serverURL); MultivaluedMap<String, String> paramMap = new MultivaluedMapImpl(); if(req.getMarker() != null){ paramMap.add("marker", req.getMarker()); } if(req.getMaxItems() != null){ paramMap.add("maxitems", req.getMaxItems()); } ClientResponse response = r .queryParams(paramMap) .type(MediaType.APPLICATION_XML_TYPE) .accept(MediaType.TEXT_XML) .header("X-Amzn-Authorization", "AWS3 AWSAccessKeyId=" + this.accessKey + "," + "Algorithm=HmacSHA256," + "SignedHeaders=Host;X-Amz-Date," + "Signature=THISISANEXAMPLESIGNATURE=") .get(ClientResponse.class); String resultXml = response.getEntity(String.class); if(response.getStatus() != 200){ exceptionMapper(response, resultXml); } ListHostedZonesResponsePOJO interResult = null; try { StringReader reader = new StringReader(resultXml); JAXBContext context = JAXBContext.newInstance(ListHostedZonesResponsePOJO.class); Unmarshaller unmarshaller = context.createUnmarshaller(); interResult = (ListHostedZonesResponsePOJO) unmarshaller.unmarshal(reader); } catch (JAXBException e) { e.printStackTrace(); return null; } if(interResult == null){ return null; } ListHostedZonesResult result = new ListHostedZonesResult(); if(interResult.getHostedZones() != null){ Collection<HostedZone> hzs = new LinkedList<HostedZone>(); for(HostedZonePOJO hz : interResult.getHostedZones()){ HostedZone temp = new HostedZone(); temp.setCallerReference(hz.getCallerReference()); temp.setId(hz.getId()); temp.setName(hz.getName()); temp.setResourceRecordSetCount(hz.getResourceRecordSetCount()); if(hz.getConfig() != null){ HostedZoneConfig config = new HostedZoneConfig(); if(hz.getConfig().getComment() != null){ config.setComment(hz.getConfig().getComment()); } temp.setConfig(config); } hzs.add(temp); } result.setHostedZones(hzs); } if(interResult.getMarker() != null){ result.setMarker(interResult.getMarker()); } if(interResult.getMaxItems() != null){ result.setMaxItems(interResult.getMaxItems()); } if(interResult.getNextMarker() != null){ result.setNextMarker(interResult.getNextMarker()); } return result; }
/** * Returns hashmap of <KEY: zoneID, VALUE: String[] of ID, name, caller * reference, and comment> * * @param marker_tableName * table name of the marker * @param maxItems * number of items returned when the actual number of list * exceeds maxItems * @return Hashmap of <KEY: zoneID, VALUE: String[] of ID, name, caller * reference, and comment> * @throws InternalErrorException */ @Override public ListHostedZonesResult listHostedZones(String marker, int maxItems, long accId) throws ErrorResponse { ListHostedZonesResult result = new ListHostedZonesResult(); Collection<HostedZone> hostedZones = new LinkedList<HostedZone>(); int lim = maxItems; try { ResultSet rs = null; String query = null; Statement stmt = this.sqlConnection.createStatement(); if (marker == null) { logger.debug("No marker is given."); query = "SELECT * FROM msi.zones WHERE accountId = " + accId + ";"; } else { logger.debug("Marker is assigned."); query = "SELECT * FROM msi.zones WHERE accountId = " + accId + " AND ID >= \'" + marker + "\';"; } rs = stmt.executeQuery(query); while (lim != 0 && rs.next()) { HostedZone hz = new HostedZone(rs.getString("ID"), rs.getString("name"), rs.getString("callerReference")); HostedZoneConfig config = new HostedZoneConfig(); config.setComment(rs.getString("comment")); hz.setConfig(config); --lim; hostedZones.add(hz); } if (marker != null && hostedZones.size() == 0) { // TODO throw an exception for marker not existing (test against // AWS to see which exception is being returned) } boolean truncated = rs.next(); logger.debug("Relative Limit = " + lim + "; MaxItems = " + maxItems); logger.debug("Truncated = " + truncated); if (lim == 0 && truncated) { truncated = true; } result.setHostedZones(hostedZones); result.setMaxItems(String.valueOf(maxItems)); result.setIsTruncated(truncated); if (truncated) { result.setNextMarker(rs.getString("ID")); } } catch (SQLException e) { System.err .println("Failed to get zone informations for listHostedZone request."); e.printStackTrace(); throw DNS53Faults.InternalError(); } logger.debug("Returning " + hostedZones.size() + " hosted zones information."); return result; }
private CreateHostedZoneResult createHostedZone(Session session, CreateHostedZoneRequest request) throws ErrorResponse{ CreateHostedZoneResult result = new CreateHostedZoneResult(); Date submittedAt = new Date(); AccessMySQL sqlaccess = AccessMySQL.getInstance(); String name = request.getName(); try { new java.net.URI("http://" + name); } catch(URISyntaxException e) { throw DNS53Faults.InvalidDomainName(); } String comment = null; if(request.getHostedZoneConfig() != null){ comment = request.getHostedZoneConfig().getComment(); } List<String> zoneInfo = sqlaccess.createHostedZone(session, name, request.getCallerReference(), comment, this.getAccountId()); String zoneId = zoneInfo.get(0); if(zoneId.equals("DUPLICATE_REFERENCE")){ throw DNS53Faults.HostedZoneAlreadyExists(); } else if(zoneId.equals("DUPLICATE_NAME")){ throw DNS53Faults.DelegationSetNotAvailable(); } else{ String status = DNS53Constants.PENDING; String tableName = zoneInfo.get(1); String changeID = RequestHandler.writeChange(sqlaccess, status, submittedAt.toString(), tableName, "CREATE"); HostedZone hz = new HostedZone(zoneId, request.getName(), request.getCallerReference()); hz.setConfig(request.getHostedZoneConfig()); result.setHostedZone(hz); ChangeInfo ci = new ChangeInfo(changeID, status, submittedAt); result.setChangeInfo(ci); List<String> nameServers = new LinkedList<String>(); for(int i = 2; i < zoneInfo.size(); ++i){ nameServers.add(zoneInfo.get(i)); } DelegationSet ds = new DelegationSet(nameServers); result.setDelegationSet(ds); } return result; }
public HostedZone findZone(final String source) { final ListHostedZonesResult zoneResult = amazonClient.listHostedZones(); final List<HostedZone> zoneList = zoneResult.getHostedZones(); for (final HostedZone zone : zoneList) { final String name = zone.getName(); if (source.endsWith(name)) { return zone; } } return null; }
public void ensureCNAME(final String source, final String target) throws Exception { final HostedZone zone = findZone(source); Util.assertNotNull(zone, "missing zone for " + source); final String zoneId = zone.getId(); final boolean isPresent; final ResourceRecordSet recordOld; { final ResourceRecordSet recordFound = findRecord(zoneId, source); if (recordFound == null) { isPresent = false; recordOld = makeRecordCNAME(source, target); } else { isPresent = true; recordOld = recordFound; } } final ResourceRecordSet recordNew = makeRecordCNAME(source, target); recordNew.setTTL(recordOld.getTTL()); // final Collection<Change> changeList = new LinkedList<Change>(); if (isPresent) { changeList.add(new Change(ChangeAction.DELETE, recordOld)); changeList.add(new Change(ChangeAction.CREATE, recordNew)); } else { changeList.add(new Change(ChangeAction.CREATE, recordNew)); } final ChangeBatch changeRequest = new ChangeBatch(); changeRequest.setComment("updated : " + new Date()); changeRequest.setChanges(changeList); final ChangeResourceRecordSetsRequest request = new ChangeResourceRecordSetsRequest(); request.setHostedZoneId(zone.getId()); request.setChangeBatch(changeRequest); final ChangeResourceRecordSetsResult result = amazonClient .changeResourceRecordSets(request); final ChangeInfo changeResult = result.getChangeInfo(); logger.info("changeResult : \n{}", changeResult); }
@Override public void execute() throws MojoExecutionException, MojoFailureException { try { getLog().info("dns find init [" + dnsHostName + "]"); final CarrotRoute53 route53 = newRoute53(); final HostedZone zone = route53.findZone( // route53.canonical(dnsHostName)); final Properties props = project().getProperties(); final String zoneName; if (zone == null) { zoneName = null; props.remove(dnsResultProperty); } else { zoneName = zone.getName(); props.put(dnsResultProperty, zoneName); } getLog().info("dns zone name : " + zoneName); getLog().info("dns find done [" + dnsHostName + "]"); } catch (final Exception e) { throw new MojoFailureException("bada-boom", e); } }
public List<String> listZone(final String source) { final List<String> nameList = new LinkedList<String>(); final HostedZone zone = findZone(source); if (zone == null) { return nameList; } final ListResourceRecordSetsRequest request = new ListResourceRecordSetsRequest(); request.setHostedZoneId(zone.getId()); while (true) { final ListResourceRecordSetsResult result = amazonClient .listResourceRecordSets(request); final List<ResourceRecordSet> recordList = result .getResourceRecordSets(); for (final ResourceRecordSet record : recordList) { nameList.add(record.getName()); } if (!result.isTruncated()) { break; } request.setStartRecordName(result.getNextRecordName()); } return nameList; }