These are chat archives for spring-cloud/spring-cloud

10th
Feb 2016
Marcos Barbero
@marcosbarbero
Feb 10 2016 12:48

Hi enverybody... I'm playing with spring cloud config server and just got stuck in a security issue about oauth2, at the docs it says:

You are free to secure your Config Server in any way that makes sense to you (from physical network security to OAuth2 bearer tokens), and Spring Security and Spring Boot make it easy to do pretty much anything.

But I couldn't find anyway to do that, is there any sample securing configserver with oauth2 and a config client application consuming the configuration files?

Dave Syer
@dsyer
Feb 10 2016 13:04
I don't have any samples for the client, but it's how the Spring Config client work in PCF so I know someone implemented it. The server is trivial though surely?
Marcos Barbero
@marcosbarbero
Feb 10 2016 13:05
The server is not a problem at all
Dave Syer
@dsyer
Feb 10 2016 13:05
Good
Essentially what they are doing is manipulating the ConfigServicePropertySourceLocator and injecting a RestTemplate with the right configuration.
Marcos Barbero
@marcosbarbero
Feb 10 2016 13:14
Thank you @dsyer
Niklas Herder
@herder
Feb 10 2016 14:17
Hi guys, I'm trying to wrap my head around how to handle Hystrix together with a DeferredResult. My Controller method looks like this:
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public DeferredResult<ResponseEntity<ResourceSupport>> getArticle(@PathVariable("id") String id, final HttpServletRequest request) {
        request.setAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE, request.getContextPath());
        final RequestAttributes requestAttributes = new ServletRequestAttributes(request);

        final DeferredResult<ResponseEntity<ResourceSupport>> result = new DeferredResult<>();

        log.debug("Request started: {}", requestAttributes);

        repository.findOneObs(id, Article.class).subscribe(article -> {
            if (!article.isPresent()) {
                result.setResult(new ResponseEntity<>(HttpStatus.NOT_FOUND));
            }
            article.ifPresent(article1 -> {
                RequestContextHolder.setRequestAttributes(requestAttributes);
                result.setResult(new ResponseEntity<>(articleResourceResourceAssembler.toResource(article1), HttpStatus.OK));
            });
        }, ex -> result.setErrorResult(ex), () -> log.debug("Request complete: {}", requestAttributes));
        return result;
    }
...and I can't seem to find a good method to use this together with Hystrix.
@HystrixCommand obviously won't work, so I guess I have to create a HystrixCommand manually which somehow waits for result.setResult(...)
but the only thing I've come up with is trying to use a CountDownLatch which the Command waits for in its execute() method, which seems very ugly and hackish.
Ideas greatly appreciated! :)
Dave Syer
@dsyer
Feb 10 2016 14:30
It's not really clear where you want the hystrix command
Do you want this exact flow, but put a circuit breaker around the call to repository.findOneObs() (whose signature we cannot see)?
Niklas Herder
@herder
Feb 10 2016 14:40
Ah, sorry: it returns an Observable<Option<Article>>
Shortly after writing this, I found this page which seems useful: https://github.com/Netflix/Hystrix/wiki/How-To-Use#Asynchronous-Execution
Maybe I cleared things up a bit for myself just by articulating the problem! So you can ignore me for now, thanks :)
Seems I can construct a HystrixObservableCommand which executes the async code inside.
Dave Syer
@dsyer
Feb 10 2016 14:49
It would be nice if you could do that with @HystrixCommand but I'm not sure you can. It purports to support observables but I'm not sure it really does.
Try it if you like though
Would be good to know if it does actaully work
Niklas Herder
@herder
Feb 10 2016 15:19

This works:

@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Observable<ResponseEntity<ResourceSupport>> getArticle(@PathVariable("id") String id, final HttpServletRequest request) {
        request.setAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE, request.getContextPath());
        final RequestAttributes requestAttributes = new ServletRequestAttributes(request);

        log.debug("Request started: {}", requestAttributes);

        return new HystrixObservableCommand<ResponseEntity<ResourceSupport>>(HystrixCommandGroupKey.Factory.asKey("ArticleSpringService")) {

            @Override
            protected Observable<ResponseEntity<ResourceSupport>> construct() {
                return Observable.create(subscriber -> {
                    if (!subscriber.isUnsubscribed()) {
                        repository.findOneObs(id, Article.class).subscribe(
                            article -> {
                                if (!article.isPresent()) {
                                    subscriber.onNext(new ResponseEntity<ResourceSupport>(HttpStatus.NOT_FOUND));
                                }
                                article.ifPresent(article1 -> {
                                    RequestContextHolder.setRequestAttributes(requestAttributes);
                                    subscriber.onNext(new ResponseEntity<>(articleResourceResourceAssembler.toResource(article1), HttpStatus.OK));
                                });
                            },
                            subscriber::onError,
                            () -> {
                                subscriber.onCompleted();
                                log.debug("Request complete: {}", requestAttributes);
                            });

                    }
                });
            }
        }.toObservable();
    }

, but annotating with @HystrixCommand and returning the Observable which is created in construct(), I get an error that a method which returns an Observable must return an ObservableResult. And ObservableResult only has an invoke() method which I'm unsure how to use when I'm getting an Observable back from the repository

Dave Syer
@dsyer
Feb 10 2016 15:20
Yeah that's why I think it's broken
Niklas Herder
@herder
Feb 10 2016 15:21
agreed
Spencer Gibb
@spencergibb
Feb 10 2016 17:50
@herder what version fo spring-cloud and hystrix are you using? Hystrix supports normal Observables since https://github.com/Netflix/Hystrix/releases/tag/v1.4.22. Brixton currently support 1.4.23
Dave Syer
@dsyer
Feb 10 2016 17:52
Interesting. I didn't know that.
Niklas Herder
@herder
Feb 10 2016 18:03
Strange, I'm using Brixton.M4. I'll double-check when I get time, probably tomorrow
Try M5 (or a snapshot)
Spencer Gibb
@spencergibb
Feb 10 2016 18:34
thanks for the investigation @dsyer
Niklas Herder
@herder
Feb 10 2016 20:00
Great, thanks! Didn't realize that M5 was out. Upgrading!