ServiceStack breaks when hosted in AWS API Gateway - aws-api-gateway

Currently experiencing an issue with ServiceStack where it will not run in AWS api gateway past version 5.8 with request logging turned on. If I turn request logging off everything is fine.
Fixed as of version 5.10.3, thanks mythz!

By default ServiceStack infers the BaseUrl from the incoming Request URL, but you can configure it to use a specific external facing URL with:
SetConfig(new HostConfig {
#if !DEBUG
WebHostUrl = "https://xxxx.execute-api.us-west-2.amazonaws.com/dev"
#endif
});
Alternatively you can override GetBaseUrl(IRequest) in your AppHost to change the default runtime behavior for which BaseUrl to use at runtime.

Related

Netlify deploy can't connect to Heroku backend

I've built a wee program that works fine when I run it locally. I've deployed the backend to Heroku, and I can access that either by going straight to the URL (http://gymbud-tracker.herokuapp.com/users) or when running the frontend locally. So far so good.
However, when I run npm run-script build and deploy it to Netlify, something goes wrong, and any attempt to access the server gives me the following error in the console:
auth.js:37 Error: Network Error
at e.exports (createError.js:16)
at XMLHttpRequest.p.onerror (xhr.js:99)
The action that is pushing that error is the following, if it is relevant:
export const signin = (formData, history) => async (dispatch) => {
try {
const { data } = await api.signIn(formData);
dispatch({ type: AUTH, data });
history.push("../signedin");
} catch (error) {
console.log(error);
}
};
I've been tearing my hair out trying to work out what is changing when I build and deploy, but cannot work it out.
As I say, if I run the front end locally then it access the Heroku backend no problem - no errors, and working exactly as I'd expect. The API call is correct, I believe: const API = axios.create({baseURL: 'http://gymbud-tracker.herokuapp.com/' });
I wondered if it was an issue with network access to the MongoDB database that Heroku is linked to, but it's open to "0.0.0.0/0" (I've not taken any security precautions yet, don't kill me!). The MDB database is actually in the same collection as other projects I've used, that haven't had this problem at all.
Any ideas what I need to do?
The front end is live here: https://gym-bud.netlify.app/
And the whole thing is deployed to GitHub here: https://github.com/gordonmaloney/gymbud
Your issue is CORS (Cross-Origin Resource Sharing). When I visit your site and inspect the page, I see the following error in the JavaScript console which is how I know this:
This error essentially means that your public-facing application (running live on Netlify) is trying to make an HTTP request from your JavaScript front-end to your Heroku backend deployed on a different domain.
CORS dictates which requests from a frontend application are ALLOWED to make a request to your backend API.
What you need to do to fix this is to modify your Heroku application and have it return the appropriate Access-Control-Allow-Origin header. This article on MDN explains the header and how you can use it.
Here's a simple example of the header you could set on your Heroku backend to allow this to work:
Access-Control-Allow-Origin: *
Please be sure to read the MDN documentation, however, as this example will allow any front-end application to make requests to your Heroku backend when in reality, you'll likely want to restrict it to just the front-end domains you build.
God I feel so daft, but at least I've worked it out.
I looked at the console on a different browser (Edge), and it said it was blocking it because it was mixed origin, and I realised I had just missed out the s in the https on my API call, so it wasn't actually a cors issue (I don't think?), but just a typo on my part!
So I changed:
const API = axios.create({baseURL: 'http://gymbud-tracker.herokuapp.com' });
To this:
const API = axios.create({baseURL: 'https://gymbud-tracker.herokuapp.com' });
And now it is working perfectly ☺️
Thanks for your help! Even if it wasn't the issue here, I've definitely learned a lot more about cors on the way, so that's good

Config Server in Play framework 2.7

I am implementing a web socket server application using play framework 2.7
I would like to implement a remote configuration where all the application's
configuration should reside in a github.
When i searched for documents to implement it, i found below url,
https://github.com/play-rconf
but accessing configuration from github is not listed.
Is there any better way or document do access the config server from github (like in Spring) ?
You can try play-rconf-http by specifying a URL of your config file:
remote-configuration {
## Provider - HTTP
# ~~~~~
# Retrieves configuration from a simple HTTP server
http {
# URL where is located the configuration file to fetch. You can
# use basic authentication
url = "https://raw.githubusercontent.com/<user>/<repo>/<branch>/<path-to-file>"
url = ${?REMOTECONF_HTTP_URL}
}
}
You can use basic authentication as well.
Look Download single files from GitHub for more info regarding GitHub link.

Using Spring Cloud Config not getting encrypted value?

I am experimenting with Spring Cloud Config Server for encryption/decryption of config values.
For this, under Config Server project made following changes:
In bootstrap.properties
encrypt.key=abcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh
I have also updated JCE using following command in my Ubuntu 18.04 machine:
sudo apt install oracle-java8-unlimited-jce-policy
But after issuing a POST request, I couldn't see anything in response.
Ideally, encrypted text of the request body should be coming as response.
Sample project: https://github.com/Omkar-Shetkar/pluralsight-springcloud-m2-git
What could be missing here ?
Thanks.
It is actually due CSRF, was getting 401-unauthorized response. Since Spring Boot 2.0, we can disable this in code.
Detailed answer is available here:
spring config server encrypt forbidden
Following above steps solved the issue.
Just putting out there, If someone is stuck with my silly mistake, even after doing all above things, if /encrypt endpoint is still not authorized.
Please check your security adaptor class. If you have overridden below method as well
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user")
.password(passwordEncoder().encode(userPassword))
.roles("USER")
.and()
.withUser("admin")
.password(passwordEncoder().encode(adminPassword))
.roles("USER", "ADMIN");
}
You need to provide these credentials, under Basic Auth option of Authorization tab

I cant make rest calls with react

I am learning to use React at the moment, and I have a problem: I want react to work with my Java Spring Boot backend (Tomcat). React is running on port 3000 (default) and my Tomcat server on port 8080 (default). Now, when I make a REST call, I get the following error
script.js:13 GET http://localhost:8080/api/path?p=test net::ERR_CONNECTION_REFUSED
My rest call looks like:
fetch('http://localhost:8080/api/path?p=test')
.then(res => {
console.log(res);
});
What do I make wrong? I do not really have an idea.
An net::ERR_CONNECTION_REFUSED error is typically thrown when your frontend (React application) cannot connect to your backend (your Spring boot application). There are many possible reasons for this to happen, such as:
Your back-end application is not running
The path to your back-end application is not correct
There is some kind of firewall between your front- and back-end application that is stopping the traffic
...
In your case it appeared to be the back-end application that was not running properly.
Your second problem is related to CORS, which is a mechanism that prevents JavaScript from connecting to other APIs/websites that are not on the same (sub)domain and port. In your case your frontend is running on a different port than you backend, so that means you have to deal with CORS.
To handle CORS, you have to add a header to your backend (namely the Access-Contol-Allow-Origin header the error is mentioning). With Spring, you can do that by adding the following annotation to your controller:
#CrossOrigin(origins = "http://localhost:3000")
Or you can configure CORS globally with a filter or using WebMvcConfigurer:
#Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**").allowedOrigins("http://localhost:3000");
}
};
}
As mentioned in the comments by #Lutz Horn, this is also described in a Spring guide.

Upgrading to Spring cloud 1.0.1 zuul url encoded parameters not working

We use zuul as an gateway to dispatch incoming requests to services.
When we upgraded from 1.0.0, we noticed two issues, one of which we got a workaround to.
The second issue is that in some of the incoming have encoded uris to deal with special characters in the request, e.g. ....rovi//45846 which needs to be changed to rovi%2F%2F45846 in order to pass in.
So for a rest uri like the following POST http://localhost:8902/contentservice/content/subscriptionPackages/624460160/channels/rovi%252F%252F45846
If I make this request directly to the service, it works correctly.
But if I route it through zuul as POST http://localhost:8765/contentservice/content/subscriptionPackages/624460160/channels/rovi%252F%252F45846 then it disappears.
Now if I take the % out it is passed in and treated as an error in the contentservice when I step through the content service front end controller (off course).
What has changed between spring cloud 1.0.0 to 1.0.1 in the zuul functionality to stop this from working. As it definitely was working in 1.0.0.
So the spring cloud team has fixed this in the snapshot releases and you can fixed more detail here
https://github.com/spring-cloud/spring-cloud-netflix/issues/366#issuecomment-106363315