CreateSecurityGroup implements CreateServiceInstanceBindingPostFilter, ServiceBrokerPostFilter<CreateServiceInstanceBindingRequest, CreateServiceInstanceAppBindingResponse> { @Override public void run(CreateServiceInstanceBindingRequest request, CreateServiceInstanceAppBindingResponse response) { Assert.notNull(response, "expecting a non-null response"); Assert.notNull(response.getCredentials(), "expecting a non-null response credentials"); Assert.notNull(request.getContext(), "expecting a non-null OSB context"); Assert.isInstanceOf(CloudFoundryContext.class, request.getContext(), "only supporting CloudFoundry " + "clients, but received unsupported context in OSB request"); final Destination destination = ConnectionInfoFactory.fromCredentials(response.getCredentials()); if (!trustedDestinationSpecification.isSatisfiedBy(destination)) { log.warn("Cannot open security group for destination {}. Destination is out of allowed range [{}].", destination, trustedDestinationSpecification); throw new NotAllowedDestination(destination); } log.debug("creating security group for credentials {}.", response.getCredentials()); try { CloudFoundryContext cloudFoundryContext = (CloudFoundryContext) request.getContext(); final SecurityGroupEntity securityGroup = Mono.zip( getRuleDescription(cloudFoundryClient, request.getBindingId(), request.getServiceInstanceId()), Mono.just(cloudFoundryContext.getSpaceGuid()) ).flatMap(function((description, spaceId) -> create(getSecurityGroupName(request), destination, description , spaceId))) .doOnError(t -> log.error("Fail to create security group. Error details {}", t.toString(), t)) .block(); assert securityGroup != null: "mono should not return empty"; log.debug("Security Group {} created", securityGroup.getName()); } catch (Exception e) { log.error("Fail to create Security Group. Error details {}", e.toString(), e); ReflectionUtils.rethrowRuntimeException(e); } } @Autowired  CreateSecurityGroup(CloudFoundryClient cloudFoundryClient, TrustedDestinationSpecification trustedDestinationSpecification); @Override void run(CreateServiceInstanceBindingRequest request, CreateServiceInstanceAppBindingResponse response);  }
@Test(expected = ClientV2Exception.class) public void fail_to_create_create_security_group_should_raise_exception_so_that_CC_requests_unbinding_action_to_clean_up_target_broker_related_resources() { givenServicePlan(this.cloudFoundryClient, "plan-id", "service-id"); givenService(this.cloudFoundryClient, "service-id", "service-broker-id"); givenServiceBroker(this.cloudFoundryClient, "service-broker-id", "service-broker-name"); givenServiceInstance(this.cloudFoundryClient, "service-instance-id", "service-instance-name", "plan-id"); givenCreateSecurityGroupsFails(this.cloudFoundryClient, "test-securitygroup-name"); Map<String, Object> credentials = new HashMap<>(); credentials.put("uri", TEST_URI_WITH_IP_ADDRESS); createSecurityGroup .run( CreateServiceInstanceBindingRequest.builder() .serviceDefinitionId("service-id") .planId("plan-id") .bindResource(BindResource.builder() .appGuid("app_guid") .build()) .bindingId("test-securitygroup-name") .serviceInstanceId("service-instance-id") .context(CloudFoundryContext.builder() .spaceGuid("space_id") .build()) .build() , CreateServiceInstanceAppBindingResponse.builder() .credentials(credentials).build() ); }
@Test(expected = IllegalArgumentException.class) public void noHostname() { CreateServiceInstanceBindingRequest request = CreateServiceInstanceBindingRequest.builder() .bindResource(BindResource.builder() .appGuid("app_guid") .build()) .build(); Map<String, Object> credentials = new HashMap<>(); credentials.put("uri", NO_HOST_URI); CreateServiceInstanceAppBindingResponse response = CreateServiceInstanceAppBindingResponse.builder() .credentials(credentials) .build(); createSecurityGroup.run(request, response); }
@Test(expected = IllegalArgumentException.class) public void noPort() { CreateServiceInstanceBindingRequest request = CreateServiceInstanceBindingRequest.builder() .bindResource(BindResource.builder() .appGuid("app_guid") .build()) .build(); Map<String, Object> credentials = new HashMap<>(); credentials.put("uri", NO_PORT_URI); CreateServiceInstanceAppBindingResponse response = CreateServiceInstanceAppBindingResponse.builder() .credentials(credentials) .build(); createSecurityGroup.run(request, response); }