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

7th
Mar 2018
Abhishek Dujari
@abshkd
Mar 07 2018 05:20
i am trying to test calls to a microservice by stubbing it with MockRestServiceServer as shown here https://objectpartners.com/2013/01/09/rest-client-testing-with-mockrestserviceserver/
to check if the request headers and body is correct for outgoing requests
but I can't use RestTemplate in my tests
i get the Bean not found exception could not autowire
only when I use SpringRunner
but when i change to TestRestTemplate and do this
mockServer = MockRestServiceServer.createServer(restTemplate);
i get could not resolve method ofcourse
Abhishek Dujari
@abshkd
Mar 07 2018 06:36
how are you guys testing forward requests from gateway to downstream service?
Arne Vandamme
@arne-vandamme
Mar 07 2018 07:51
hi, i notice when we use the /restart endpoint it appears that there is an ApplicationContext leak after restart, the (presumably) top-most bootstrap context does not get fully destroyed, but a new one gets created. I googled around but did not find any issues reported about this, can anyone tell me if that's normal behaviour? just creating a project with spring-boot-admin-server, enabling restart endpoint and calling it will reproduce it, no special configuration of any kind
Dave Syer
@dsyer
Mar 07 2018 08:28
How do you know the top parent is not closed?
We have had issues in github about it
AFAIK they were fixed. What version are you using?
Maybe try to make sure you have the latest Edgware or Finchley (depending on your Boot version)?
Arne Vandamme
@arne-vandamme
Mar 07 2018 08:29
i started an app with spring boot 1.5.10 and boot admin 1.5.7, cloud-context is 1.3.1 then
Dave Syer
@dsyer
Mar 07 2018 08:31
1.3.2 is the latest
Arne Vandamme
@arne-vandamme
Mar 07 2018 08:31
just tried with Edgeware.SR2, still see the same thing
which is 1.3.2
Dave Syer
@dsyer
Mar 07 2018 08:31
How do you know there is a leak?
Arne Vandamme
@arne-vandamme
Mar 07 2018 08:32
i think it doesn't get removed from the LiveBeansView, i see it because in IntelliJ run dashboard i can inspect the actuator /beansendpoint, and there i see contexts added
Dave Syer
@dsyer
Mar 07 2018 08:33
That might be a bug in live beans view. Don't know.
Arne Vandamme
@arne-vandamme
Mar 07 2018 08:34
is the parent bootstrap context supposed to be closed as well on a /restart? from my quick glance at the code i think so?
because i never get in the doClose() of that ApplicationContext (which does the unregister from LiveBeansView)
Dave Syer
@dsyer
Mar 07 2018 08:35
I would guess so
Arne Vandamme
@arne-vandamme
Mar 07 2018 08:40
i'll add a bug in cloud-commons project then, unless someone still has a better clue
Dave Syer
@dsyer
Mar 07 2018 08:40
I only see 2 contexts in /beans.
Abhishek Dujari
@abshkd
Mar 07 2018 08:40
@dsyer i am using edgware sprign-cloud-netflix for a gateway. is there a way to test calls to downstream services in my integration tests? something like this https://objectpartners.com/2013/01/09/rest-client-testing-with-mockrestserviceserver/
Dave Syer
@dsyer
Mar 07 2018 08:40
But I agree that the doClose() appears not to be called.
Abhishek Dujari
@abshkd
Mar 07 2018 08:40
maybe there is something new and easier
sorry let @arne-vandamme finish
Dave Syer
@dsyer
Mar 07 2018 08:42
@abshkd I don't really know what you need. The gateway might well not be using rest template so mock rest service isn't going to help.
Maybe look a Wiremock and Spring Cloud Contract?
Abhishek Dujari
@abshkd
Mar 07 2018 08:42
i am definitely using resttemplate
Dave Syer
@dsyer
Mar 07 2018 08:42
What is the netflix connection then?
Arne Vandamme
@arne-vandamme
Mar 07 2018 08:42
@dsyer i'll add an issue that sums it all up?
Abhishek Dujari
@abshkd
Mar 07 2018 08:42
I am using Zuul filter to add headers for downstream services
Dave Syer
@dsyer
Mar 07 2018 08:43
@arne-vandamme yes please that would help. Just a one liner is fine.
Abhishek Dujari
@abshkd
Mar 07 2018 08:43
and Discovery Client so I just to check that when I call http://service-2/ in gateway it is being sent the headers
Dave Syer
@dsyer
Mar 07 2018 08:43
I don't think I know what you mean yet.
Maybe a sample app would help?
i am using a small variation of this https://github.com/jhipster/jhipster-sample-app-gateway
Dave Syer
@dsyer
Mar 07 2018 08:45
It really would help to have some code to run
Abhishek Dujari
@abshkd
Mar 07 2018 08:46
i think the downstream microservice may not be receiving some additional headers. so I want to test and make sure the headers are there in zuulRequestHeaders when doing an integrationt ests
since its not important what other servcie might be I was looking into this MockServer example https://objectpartners.com/2013/01/09/rest-client-testing-with-mockrestserviceserver/
Arne Vandamme
@arne-vandamme
Mar 07 2018 08:49
@dsyer spring-cloud/spring-cloud-commons#333
Dave Syer
@dsyer
Mar 07 2018 08:50
:thumbsup:
Maybe a unit test would be better?
We often end up writing integration tests that run against a local server started by the test.
I'm still not sure how a mock rest service will help with Zuul
Still building your code.
Abhishek Dujari
@abshkd
Mar 07 2018 08:53
thank you!
Dave Syer
@dsyer
Mar 07 2018 08:53
(A smaller sample would have been lovely)
Abhishek Dujari
@abshkd
Mar 07 2018 08:53
sorry about that. i used this template and i am actually removing parts of it
Dave Syer
@dsyer
Mar 07 2018 08:58
OK, it runs. Lots of error logs because Eureka is not there.
Abhishek Dujari
@abshkd
Mar 07 2018 08:59
yes i disable it in logback.xml
please can you pull a change again. i just pushed a missed edit
Dave Syer
@dsyer
Mar 07 2018 09:00
Sure
The TokenRelayFilter is pretty small
A unit test seems like the best option to me
Abhishek Dujari
@abshkd
Mar 07 2018 09:00
yes so all i do is check if a header is there and forward it
when you see the new TokenRelayFilter you will see I am using SpringSecurity. can I still do unit test?
Dave Syer
@dsyer
Mar 07 2018 09:01
Your latest code doesn't compile (missing imports)
Abhishek Dujari
@abshkd
Mar 07 2018 09:02
oh sorry again
ill fix it
Dave Syer
@dsyer
Mar 07 2018 09:02
I see. So you'd have to write a test that sets up the SecurityContext.
That's not too hard, probaby.
Depends what this SecurityUtils is doing
Looks pretty simple to me
Abhishek Dujari
@abshkd
Mar 07 2018 09:03
to you yes :)
lets see if I can share what i did so far.
private TokenRelayFilter filter = new TokenRelayFilter();    
@Test
    public void testTokenRelayFilter() {
        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/management/info");
        RequestContext.getCurrentContext().setRequest(request);

        assertTrue(filter.shouldFilter());
    }
I was working on something like this
    @Test
    public void shouldForwardJWT_on_valid_JWT() {
        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service2");
        request.addHeader(JWTConfigurer.AUTHORIZATION_HEADER,"Bearer a.valid.token");
        RequestContext context = RequestContext.getCurrentContext();
        context.setRequest(request);
        filter.run();
        context.getZuulRequestHeaders().get(JWTConfigurer.AUTHORIZATION_HEADER).equals("Bearer a.valid.token");
    }
ofcouse its wrong because I dont have SecurityContext
Dave Syer
@dsyer
Mar 07 2018 09:09
That looks like a start.
Zuul is a nightmare to test
It uses thread locals to store state, so be sure to reset it at the end
Abhishek Dujari
@abshkd
Mar 07 2018 09:10
it is T_T and trying to test iner-service is still unknown to me
Dave Syer
@dsyer
Mar 07 2018 09:10
same with the security context I guess
Abhishek Dujari
@abshkd
Mar 07 2018 09:11
how would i initialize securityContext here in unit test so the Spring Security filters run and then zuul filter finds the header
actually all SecurityUtils does is grab JWT
maybe i can mock that
Dave Syer
@dsyer
Mar 07 2018 09:12
But the security context is easy to set up and tear down
You just call SecurityContextHolder.getContext() and set the Authentication.
You already do a similar thing with Zuul RequestContext (remember to clear that afterwards)
Abhishek Dujari
@abshkd
Mar 07 2018 09:13
oh so that would work even without running the whole ApplicationContext?
ill try it nwo
thank you sir! much appreciate it
i am going to look into the spring gateway as I hear its released
Dave Syer
@dsyer
Mar 07 2018 09:15
Only milestones in 2.0.0 so far
Abhishek Dujari
@abshkd
Mar 07 2018 09:19
How do I clear the request contenxt?
RequestContext.getCurrentContext().clear();
Dave Syer
@dsyer
Mar 07 2018 09:21
I think it's unset().
Or maybe not
Abhishek Dujari
@abshkd
Mar 07 2018 09:22
yeah there is an unset method
Dave Syer
@dsyer
Mar 07 2018 09:22
I think that's just for attributes
Spring Cloud Netflix has a few of these in the zuul tests
Abhishek Dujari
@abshkd
Mar 07 2018 09:23
i will find it. thanks.
Dave Syer
@dsyer
Mar 07 2018 09:23
unset() is the right one
Abhishek Dujari
@abshkd
Mar 07 2018 09:23
i am getting NPE when i add SecurityContext()
Dave Syer
@dsyer
Mar 07 2018 09:23
It nulls out the thread local
How do you mean "add"?
Abhishek Dujari
@abshkd
Mar 07 2018 09:24
    public void shouldForwardJWT_on_valid_JWT() {
        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
            "some user",
            "test-password",
            Collections.singletonList(new SimpleGrantedAuthority(AuthoritiesConstants.USER))
        );

        SecurityContext securityContext = Mockito.mock(SecurityContext.class);
        Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
        SecurityContextHolder.setContext(securityContext);
        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service2");
        request.addHeader(JWTConfigurer.AUTHORIZATION_HEADER,"Bearer a.valid.token");
        RequestContext context = RequestContext.getCurrentContext();
        context.setRequest(request);
        filter.run();
        context.getZuulRequestHeaders().get(JWTConfigurer.AUTHORIZATION_HEADER).equals("Bearer a.valid.token");
    }
Dave Syer
@dsyer
Mar 07 2018 09:24
You shouldn't really need to mock the SecurityContext.
The NPE is probably just an artifact of the mock?
Abhishek Dujari
@abshkd
Mar 07 2018 09:25
possibly. I will set auth without mock and debug
Dave Syer
@dsyer
Mar 07 2018 09:25
Just SecurityContextHolder.getContext() and set the auth
Abhishek Dujari
@abshkd
Mar 07 2018 09:29
i keep getting NPE for tokenRelayFilter on this line
headers.remove("authorization");
when i do filter.run in my test
Dave Syer
@dsyer
Mar 07 2018 09:30
I guess that was set up by another filter?
If it's a precondition for your filter I guess you have to set it up in the test right?
Abhishek Dujari
@abshkd
Mar 07 2018 09:31
it shouldn't have occured
nvm
i'll check that
it seems running zuulfilter always results in NPE
ill have to check their test examples if i can find them
Dave Syer
@dsyer
Mar 07 2018 09:32
You need to set up the context (obviously)
Abhishek Dujari
@abshkd
Mar 07 2018 09:32
i did this in my test request.addHeader("ignoredHeader","uselessHeader");
Dave Syer
@dsyer
Mar 07 2018 09:32
Spring Cloud adds that attribute in the zuul filters it adds
It's not a request header
It's a zuul context map entry
Abhishek Dujari
@abshkd
Mar 07 2018 09:33
ooooh yea
Dave Syer
@dsyer
Mar 07 2018 09:33
And it's "ignoredHeaders"
It gets added in the ProxyRequestHelper in Spring Cloud
Abhishek Dujari
@abshkd
Mar 07 2018 09:35
so if just context.set("ignoredHeaders","jjj") i would satify this condition
that whole Set<String> cast is caught me off guard
nopes i dont know how to do that proxy helper precondition so i wont NPE
ProxyRequestHelper helper = new ProxyRequestHelper();
        helper.addIgnoredHeaders("ignoredHeaders");
Abhishek Dujari
@abshkd
Mar 07 2018 09:42
how can i set this up :(
so close
dont see an obvious way to set zuul context
obvious as in "IDe recommended autocomplete for noobs"
        ProxyRequestHelper helper = new ProxyRequestHelper();
        helper.addIgnoredHeaders("some header");
        SecurityContextHolder.getContext().setAuthentication(authentication);
        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service2");
        RequestContext context = RequestContext.getCurrentContext();
        context.setRequest(request);
        context.set("ignoredHeaders",helper);
        filter.run();
java.lang.ClassCastException: org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper cannot be cast to java.util.Set
Abhishek Dujari
@abshkd
Mar 07 2018 09:47
brb
Abhishek Dujari
@abshkd
Mar 07 2018 09:57
@dsyer u still there?
Context.set is wrong there but I don't see like a zuulContext
I cant figure out how to give zuulContext a proxyHelper for the unit test :/
Carlos Juan Gómez Peñalver
@carlosjgp
Mar 07 2018 12:34
Question about refreshing credentials using Spring Cloud + Vault + Vault rabbit credentials backend (same question for MySql)
https://stackoverflow.com/questions/49140600/refresh-credentials-for-spring-amqp-connectionfactory
Patrik Mihalcin
@pmihalcin
Mar 07 2018 17:53
Hello.. I asked in Spring boot channel whether there are any end to end tests and spring cloud seems to have some.. could you point me where?
that's one of the apps we're using for e2e
Salar Rahmanian
@softinio
Mar 07 2018 23:44
so I am trying to use spring.cloud.consul.config in my spring boot app to get KV from Consul
I keep getting this error:
Could not resolve placeholder 'apiauth.test' in value "${apiauth.test}"
whats the format of the placeholders
this is what I have:
my bootstrap.properties is:
any tips appreciated