private <R extends IPResource> void assertLinkFromAllNames(Class<R> resourceClass, String linkType, IPResource resourceTo, String... expectedNames) { IPResourceService resourceService = getCommonServicesContext().getResourceService(); List<String> actualNames = resourceService.linkFindAllByFromResourceClassAndLinkTypeAndToResource(resourceClass, linkType, resourceTo).stream() // .map(it -> it.getResourceName()) // .sorted() // .collect(Collectors.toList()); List<String> eNames = Arrays.asList(expectedNames).stream() // .sorted() // .collect(Collectors.toList()); Assert.assertEquals(eNames, actualNames); }
static protected void validate(CommonServicesContext services, DnsEntry dnsEntry) { if (!Strings.isNullOrEmpty(dnsEntry.getName())) { if (typesWithDomain.contains(dnsEntry.getType()) && !DomainValidator.getInstance().isValid(dnsEntry.getName())) { throw new IllegalUpdateException(services.getTranslationService().translate("error.notADomainName")); } if (strictTypesWithDomain.contains(dnsEntry.getType()) && !startWithLetterValidationRegex.matcher(dnsEntry.getName()).matches()) { throw new IllegalUpdateException(services.getTranslationService().translate("error.notStartingWithLetter")); } if (dnsEntry.getType() == DnsEntryType.A && !validIp(dnsEntry.getDetails())) { throw new IllegalUpdateException(services.getTranslationService().translate("error.notAnIp")); } if (dnsEntry.getName().contains(" ")) { throw new IllegalUpdateException(services.getTranslationService().translate("error.notADomainName")); } } }
@Test public void testBrokenTimerEvent() { AtomicInteger okCounter = new AtomicInteger(); getCommonServicesContext().getTimerService().timerAdd(new TimerEventContext(new CrashingTimerEventHandler(), "Crashing", Calendar.MILLISECOND, 500, false, true)); getCommonServicesContext().getTimerService().timerAdd(new TimerEventContext( // (services, changes, event) -> okCounter.incrementAndGet(), // "OK", // Calendar.MILLISECOND, // 700, // false, // true)); ThreadTools.sleep(3000); Assert.assertTrue(okCounter.get() >= 2); }
public ChangeExecutionLogic(CommonServicesContext commonServicesContext, InternalServicesContext internalServicesContext) { this.commonServicesContext = commonServicesContext; this.ipPluginService = commonServicesContext.getPluginService(); this.ipResourceService = commonServicesContext.getResourceService(); this.internalChangeService = internalServicesContext.getInternalChangeService(); }
updateResourcesColumnSearch(commonServicesContext.getResourceService().getResourceDefinitions()); commonServicesContext.getTimerService().timerAdd(timerEventContext); commonServicesContext.getTranslationService().translationAdd(basename);
IPResourceService resourceService = services.getResourceService(); services.getMessagingService().alertingWarn("Let's Encrypt - Domains Without Challenge", Joiner.on('\n').join(domainsWithoutChallenge)); services.getTimerService().timerAdd(new TimerEventContext(new LetsEncryptRefreshOldCertsWaitDnsTimer(acmeService, dnsWaitDomain, challengeByDomain), // "Let Encrypt - Complete - Wait DNS", //
@Override public void timerHandler(CommonServicesContext services, ChangesContext changes, TimerEventContext event) { IPResourceService resourceService = services.getResourceService(); // Check the certs that will expire in 1 week logger.info("Getting self-signed certificates that expire in 1 week"); List<WebsiteCertificate> certificatesToUpdate = resourceService.resourceFindAll( // resourceService.createResourceQuery(WebsiteCertificate.class) // .addEditorEquals(SelfSignedWebsiteCertificateEditor.EDITOR_NAME) // .propertyLesserAndEquals(WebsiteCertificate.PROPERTY_END, DateTools.addDate(Calendar.WEEK_OF_YEAR, 1) // )); // Update them logger.info("Got {} certificates to update", certificatesToUpdate.size()); for (WebsiteCertificate certificate : certificatesToUpdate) { logger.info("Updating certificate {}", certificate.getDomainNames()); try { RSACertificate currentRsaCertificate = CertificateHelper.toRSACertificate(certificate); AsymmetricKeys keys = currentRsaCertificate.getKeysForSigning(); RSACertificate rsaCertificate = new RSACertificate(keys).selfSign( // new CertificateDetails().setCommonName(currentRsaCertificate.getCommonName()) // .addSanDns(currentRsaCertificate.getCommonName()) // .setEndDate(DateTools.addDate(Calendar.MONTH, 1))); CertificateHelper.toWebsiteCertificate(null, rsaCertificate, certificate); changes.resourceUpdate(certificate.getInternalId(), certificate); } catch (Exception e) { logger.error("Problem updating self-signed certificate {}", certificate.getDomainNames(), e); services.getMessagingService().alertingError("Problem updating self-signed certificate " + certificate.getDomainNames(), e.getMessage()); } } }
public static void init(CommonServicesContext commonServicesContext, InternalServicesContext internalServicesContext) { // Load all the plugins commonServicesContext.getPluginService().loadPlugins(commonServicesContext, internalServicesContext); }
updateResourcesColumnSearch(commonServicesContext.getResourceService().getResourceDefinitions()); commonServicesContext.getTimerService().timerAdd(timerEventContext); commonServicesContext.getTranslationService().translationAdd(basename);
services.getTimerService().timerAdd(new TimerEventContext(this, // "Let Encrypt - Complete - Wait DNS", // services.getTimerService().timerAdd(new TimerEventContext(this, // "Let Encrypt - Complete - Wait last", // IPResourceService resourceService = services.getResourceService(); Iterator<Entry<String, Tuple2<Order, Dns01Challenge>>> it = challengeByDomain.entrySet().iterator(); List<String> failures = new ArrayList<>(); services.getMessagingService().alertingWarn("Let's Encrypt - Domains Couldn't get certificate", Joiner.on('\n').join(failures)); services.getMessagingService().alertingInfo("Let's Encrypt - Domains that got a new certificate", Joiner.on('\n').join(successes)); services.getMessagingService().alertingError("Problem while managing Lets Encrypt", e.getMessage()); } finally { logger.info("Timer completed");
@Override public void timerHandler(CommonServicesContext services, ChangesContext changes, TimerEventContext event) { IPResourceService resourceService = services.getResourceService(); // Check the certs that will expire in 1 week logger.info("Getting self-signed certificates that expire in 1 week"); List<WebsiteCertificate> certificatesToUpdate = resourceService.resourceFindAll( // resourceService.createResourceQuery(WebsiteCertificate.class) // .addEditorEquals(SelfSignedWebsiteCertificateEditor.EDITOR_NAME) // .propertyLesserAndEquals(WebsiteCertificate.PROPERTY_END, DateTools.addDate(Calendar.WEEK_OF_YEAR, 1) // )); // Update them logger.info("Got {} certificates to update", certificatesToUpdate.size()); for (WebsiteCertificate certificate : certificatesToUpdate) { logger.info("Updating certificate {}", certificate.getDomainNames()); try { RSACertificate currentRsaCertificate = CertificateHelper.toRSACertificate(certificate); AsymmetricKeys keys = currentRsaCertificate.getKeysForSigning(); RSACertificate rsaCertificate = new RSACertificate(keys).selfSign( // new CertificateDetails().setCommonName(currentRsaCertificate.getCommonName()) // .addSanDns(currentRsaCertificate.getCommonName()) // .setEndDate(DateTools.addDate(Calendar.MONTH, 1))); CertificateHelper.toWebsiteCertificate(null, rsaCertificate, certificate); changes.resourceUpdate(certificate.getInternalId(), certificate); } catch (Exception e) { logger.error("Problem updating self-signed certificate {}", certificate.getDomainNames(), e); services.getMessagingService().alertingError("Problem updating self-signed certificate " + certificate.getDomainNames(), e.getMessage()); } } }
@Test public void testBrokenPlugin() { IPPluginService ipPluginService = getCommonServicesContext().getPluginService(); List<Tuple3<Class<? extends IPPluginDefinitionProvider>, IPPluginDefinitionV1, String>> broken = ipPluginService.getBrokenPlugins(); Assert.assertEquals(1, broken.size()); Assert.assertEquals("com.foilen.infra.resource.example.failing.FoilenExampleFailingPluginDefinitionProvider", broken.get(0).getA().getName()); }
public <T extends IPResource> T assertResourceExists(boolean expectedExists, T resourcePk, Class<T> resourceClass) { IPResourceService resourceService = getCommonServicesContext().getResourceService(); Optional<T> resourceFindOne = resourceService.resourceFindByPk(resourcePk); Assert.assertEquals(expectedExists, resourceFindOne.isPresent()); if (resourceFindOne.isPresent()) { return resourceFindOne.get(); } else { return null; } }
static protected void validate(CommonServicesContext services, DnsEntry dnsEntry) { if (!Strings.isNullOrEmpty(dnsEntry.getName())) { if (typesWithDomain.contains(dnsEntry.getType()) && !DomainValidator.getInstance().isValid(dnsEntry.getName())) { throw new IllegalUpdateException(services.getTranslationService().translate("error.notADomainName")); } if (strictTypesWithDomain.contains(dnsEntry.getType()) && !startWithLetterValidationRegex.matcher(dnsEntry.getName()).matches()) { throw new IllegalUpdateException(services.getTranslationService().translate("error.notStartingWithLetter")); } if (dnsEntry.getType() == DnsEntryType.A && !validIp(dnsEntry.getDetails())) { throw new IllegalUpdateException(services.getTranslationService().translate("error.notAnIp")); } if (dnsEntry.getName().contains(" ")) { throw new IllegalUpdateException(services.getTranslationService().translate("error.notADomainName")); } } }
@Test public void testBrokenTimerEvent() { AtomicInteger okCounter = new AtomicInteger(); getCommonServicesContext().getTimerService().timerAdd(new TimerEventContext(new CrashingTimerEventHandler(), "Crashing", Calendar.MILLISECOND, 500, false, true)); getCommonServicesContext().getTimerService().timerAdd(new TimerEventContext( // (services, changes, event) -> okCounter.incrementAndGet(), // "OK", // Calendar.MILLISECOND, // 700, // false, // true)); ThreadTools.sleep(3000); Assert.assertTrue(okCounter.get() >= 2); }
@Test public void testBrokenPlugin() { IPPluginService ipPluginService = getCommonServicesContext().getPluginService(); List<Tuple3<Class<? extends IPPluginDefinitionProvider>, IPPluginDefinitionV1, String>> broken = ipPluginService.getBrokenPlugins(); Assert.assertEquals(1, broken.size()); Assert.assertEquals("com.foilen.infra.plugin.v1.failingexample.ExampleFailingPluginDefinitionProvider", broken.get(0).getA().getName()); }
protected void detachManagedResources(CommonServicesContext services, ChangesContext changes, IPResource resource, List<Tuple3<IPResource, String, IPResource>> previousLinks) { IPResourceService resourceService = services.getResourceService(); // Get the currently managed resources List<IPResource> currentlyManagedResources = linkFindAllByFromResourceAndLinkType(previousLinks, resource, LinkTypeConstants.MANAGES); // Detach them for (IPResource currentlyManagedResource : currentlyManagedResources) { removeManagedLinkAndDeleteIfNotManagedByAnyoneElse(resourceService, changes, currentlyManagedResource, resource); } }
@Override public PageDefinition providePageDefinition(CommonServicesContext servicesCtx, SystemUnixUser resource) { TranslationService translationService = servicesCtx.getTranslationService(); PageDefinition pageDefinition = new PageDefinition(translationService.translate("SystemUnixUserEditor.title")); InputTextFieldPageItem idPageItem = CommonPageItem.createInputTextField(servicesCtx, pageDefinition, "SystemUnixUserEditor.id", UnixUser.PROPERTY_ID); InputTextFieldPageItem namePageItem = CommonPageItem.createInputTextField(servicesCtx, pageDefinition, "SystemUnixUserEditor.name", UnixUser.PROPERTY_NAME); if (resource != null) { idPageItem.setFieldValue(String.valueOf(resource.getId())); namePageItem.setFieldValue(resource.getName()); } return pageDefinition; }
@Test(timeout = 20000) public void testTimer_recurrent_at_start() { AtomicInteger count = new AtomicInteger(); getCommonServicesContext().getTimerService().timerAdd( // new TimerEventContext(new CounterTimerEventHandler(count), "testTimer_recurrent_at_start", Calendar.SECOND, 2, false, true) // ); // At start ThreadTools.sleep(500); Assert.assertEquals(1, count.get()); // Wait for at least 4 while (count.get() < 4) { ThreadTools.sleep(1000); } }
public void assertResourceCount(int expectedCount, Class<? extends IPResource> resourceType) { IPResourceService resourceService = getCommonServicesContext().getResourceService(); List<? extends IPResource> actualResources = resourceService.resourceFindAll( // resourceService.createResourceQuery(resourceType) // ); logger.info("Found {} resources of type {}; expecting {}", actualResources.size(), resourceType, expectedCount); for (IPResource actualResource : actualResources) { logger.debug("Actual: {}", actualResource); } Assert.assertEquals(expectedCount, actualResources.size()); }